From 0097595387668a12cf2f747945edb4d088201338 Mon Sep 17 00:00:00 2001 From: Tommy Yu Date: Fri, 8 Oct 2021 20:04:12 +1300 Subject: [PATCH] Special case string handling for operator - First implemented for BinOp such that the original strings are preserved with quotes intact if they were part of a bianry operator expression, to present a more natural appearance for the final string. --- .../parse/tests/test_unparsers_extractor.py | 2 +- src/calmjs/parse/unparsers/extractor.py | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/calmjs/parse/tests/test_unparsers_extractor.py b/src/calmjs/parse/tests/test_unparsers_extractor.py index 54ffa49..489a6fc 100644 --- a/src/calmjs/parse/tests/test_unparsers_extractor.py +++ b/src/calmjs/parse/tests/test_unparsers_extractor.py @@ -601,7 +601,7 @@ def test_basic_operator_str(self): a = 'hello' + ' ' + 'world'; """) self.assertEqual(dict(unparser(ast)), { - 'a': 'hello + + world', + 'a': "'hello' + ' ' + 'world'", }) def test_ternary_assignment(self): diff --git a/src/calmjs/parse/unparsers/extractor.py b/src/calmjs/parse/unparsers/extractor.py index cdf67b7..af3dfc4 100644 --- a/src/calmjs/parse/unparsers/extractor.py +++ b/src/calmjs/parse/unparsers/extractor.py @@ -727,6 +727,23 @@ def __call__(self, walk, dispatcher, node): yield next(dispatcher.token(None, node, self.value, None)) +class OpString(Token): + """ + Special case for the BinOp/UnaryExpr, to ensure that the raw value + that was parsed into the ast node is returned instead of being + treated, such that the original expression have their quotes + preserved. + """ + + def __call__(self, walk, dispatcher, node): + value = getattr(node, self.attr) + if isinstance(value, String): + yield next(dispatcher.token(None, node, value.value, None)) + else: + for chunk in walk(dispatcher, value, token=self): + yield chunk + + class RawBoolean(Attr): """ Simple yield the raw boolean value from the attribute. @@ -890,9 +907,9 @@ def __call__(self, walk, dispatcher, node): ),), ), 'BinOp': (GroupAsStr(( - Attr('left'), Text(value=' '), + OpString('left'), Text(value=' '), Operator(attr='op'), - Text(value=' '), Attr('right'), + Text(value=' '), OpString('right'), ),),), 'UnaryExpr': ( # the default grouping required to ensure positive/negative