From 38bbcd133ad9a4b18988ac6f7646b1ae9355e5fd Mon Sep 17 00:00:00 2001 From: Hari Kumar G Date: Mon, 25 Mar 2013 16:03:12 +0530 Subject: [PATCH] Fix syntax error in output of for statement with some expressions The semi-colon in the initialiser part of the following for statements were missing in the .to_ecma() output : for(Q||(Q=[]);d> (foo ? 32 : 43) && 54;21;){ a = c;} for(/^.+/g;cond();++z){ev();} for((new Number("32"));f;g){} Fixed by looking for the various AST classes in whether to add a semi-colon or not. --- src/slimit/tests/test_ecmavisitor.py | 24 ++++++++++++++++++++++++ src/slimit/visitors/ecmavisitor.py | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/slimit/tests/test_ecmavisitor.py b/src/slimit/tests/test_ecmavisitor.py index d949058..2776571 100644 --- a/src/slimit/tests/test_ecmavisitor.py +++ b/src/slimit/tests/test_ecmavisitor.py @@ -119,6 +119,30 @@ def setUp(self): } """, + # retain the semicolon in the initialiser part of a 'for' statement + """ + for (Q || (Q = []); d < b; ) { + d = 1; + } + """, + + """ + for (new Foo(); d < b; ) { + d = 1; + } + """, + + """ + for (2 >> (foo ? 32 : 43) && 54; 21; ) { + a = c; + } + """, + + """ + for (/^.+/g; cond(); ++z) { + ev(); + } + """, # test 12 """ diff --git a/src/slimit/visitors/ecmavisitor.py b/src/slimit/visitors/ecmavisitor.py index ed57ff6..48d5ab2 100644 --- a/src/slimit/visitors/ecmavisitor.py +++ b/src/slimit/visitors/ecmavisitor.py @@ -138,7 +138,8 @@ def visit_For(self, node): if node.init is None: s += ' ; ' elif isinstance(node.init, (ast.Assign, ast.Comma, ast.FunctionCall, - ast.UnaryOp, ast.Identifier)): + ast.UnaryOp, ast.Identifier, ast.BinOp, + ast.Conditional, ast.Regex, ast.NewExpr)): s += '; ' else: s += ' '