From 6e1693c160c8e0af693aaf3dd3bf244d281bc001 Mon Sep 17 00:00:00 2001 From: tuchida Date: Fri, 14 Jan 2022 02:10:11 +0900 Subject: [PATCH 1/8] Refactor: Add isShorthand method in AST --- .../main/java/org/mozilla/javascript/IRFactory.java | 4 +--- rhino/src/main/java/org/mozilla/javascript/Node.java | 11 +++++------ .../src/main/java/org/mozilla/javascript/Parser.java | 2 +- .../org/mozilla/javascript/ast/ObjectProperty.java | 10 ++++++++++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java index 8434dbecd0..4d55956402 100644 --- a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java +++ b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java @@ -2554,10 +2554,8 @@ void decompileObjectLiteral(ObjectLiteral node) { int size = props.size(); for (int i = 0; i < size; i++) { ObjectProperty prop = props.get(i); - boolean shorthandPropertyName = - Boolean.TRUE.equals(prop.getProp(Node.SHORTHAND_PROPERTY_NAME)); decompile(prop.getLeft()); - if (!shorthandPropertyName) { + if (!prop.isShorthand()) { decompiler.addToken(Token.COLON); decompile(prop.getRight()); } diff --git a/rhino/src/main/java/org/mozilla/javascript/Node.java b/rhino/src/main/java/org/mozilla/javascript/Node.java index bbd9f5f3c8..0ece300c54 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Node.java +++ b/rhino/src/main/java/org/mozilla/javascript/Node.java @@ -60,12 +60,11 @@ public class Node implements Iterable { DESTRUCTURING_PARAMS = 23, JSDOC_PROP = 24, EXPRESSION_CLOSURE_PROP = 25, // JS 1.8 expression closure pseudo-return - SHORTHAND_PROPERTY_NAME = 26, - ARROW_FUNCTION_PROP = 27, - TEMPLATE_LITERAL_PROP = 28, - TRAILING_COMMA = 29, - OBJECT_IDS_COMPUTED_PROP = 30, - LAST_PROP = 30; + ARROW_FUNCTION_PROP = 26, + TEMPLATE_LITERAL_PROP = 27, + TRAILING_COMMA = 28, + OBJECT_IDS_COMPUTED_PROP = 39, + LAST_PROP = 39; // values of ISNUMBER_PROP to specify // which of the children are Number types diff --git a/rhino/src/main/java/org/mozilla/javascript/Parser.java b/rhino/src/main/java/org/mozilla/javascript/Parser.java index 04c9b38256..b87791b363 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Parser.java +++ b/rhino/src/main/java/org/mozilla/javascript/Parser.java @@ -3690,7 +3690,7 @@ private ObjectProperty plainProperty(AstNode property, int ptt) throws IOExcepti } AstNode nn = new Name(property.getPosition(), property.getString()); ObjectProperty pn = new ObjectProperty(); - pn.putProp(Node.SHORTHAND_PROPERTY_NAME, Boolean.TRUE); + pn.setIsShorthand(true); pn.setLeftAndRight(property, nn); return pn; } diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java index 4d06059808..3f19d92245 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java @@ -89,6 +89,14 @@ public boolean isMethod() { return isGetterMethod() || isSetterMethod() || isNormalMethod(); } + public void setIsShorthand(boolean shorthand) { + this.shorthand = shorthand; + } + + public boolean isShorthand() { + return shorthand; + } + @Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); @@ -106,4 +114,6 @@ public String toSource(int depth) { sb.append(right.toSource(getType() == Token.COLON ? 0 : depth + 1)); return sb.toString(); } + + private boolean shorthand; } From d0e4813c0d5c0edb44a6be803614f92d516234b7 Mon Sep 17 00:00:00 2001 From: tuchida Date: Fri, 12 Jul 2024 00:09:51 +0900 Subject: [PATCH 2/8] fix functionSourceStart of Generator --- .../java/org/mozilla/javascript/Parser.java | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/Parser.java b/rhino/src/main/java/org/mozilla/javascript/Parser.java index b87791b363..51731911e6 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Parser.java +++ b/rhino/src/main/java/org/mozilla/javascript/Parser.java @@ -873,47 +873,48 @@ private void parseFunctionParams(FunctionNode fnNode) throws IOException { } private FunctionNode function(int type) throws IOException { - return function(type, false); - } - - private FunctionNode function(int type, boolean isGenerator) throws IOException { + boolean isGenerator = false; int syntheticType = type; int baseLineno = ts.lineno; // line number where source starts int functionSourceStart = ts.tokenBeg; // start of "function" kwd Name name = null; AstNode memberExprNode = null; - if (matchToken(Token.NAME, true)) { - name = createNameNode(true, Token.NAME); - if (inUseStrictDirective) { - String id = name.getIdentifier(); - if ("eval".equals(id) || "arguments".equals(id)) { - reportError("msg.bad.id.strict", id); + do { + if (matchToken(Token.NAME, true)) { + name = createNameNode(true, Token.NAME); + if (inUseStrictDirective) { + String id = name.getIdentifier(); + if ("eval".equals(id) || "arguments".equals(id)) { + reportError("msg.bad.id.strict", id); + } } - } - if (!matchToken(Token.LP, true)) { + if (!matchToken(Token.LP, true)) { + if (compilerEnv.isAllowMemberExprAsFunctionName()) { + AstNode memberExprHead = name; + name = null; + memberExprNode = memberExprTail(false, memberExprHead); + } + mustMatchToken(Token.LP, "msg.no.paren.parms", true); + } + } else if (matchToken(Token.LP, true)) { + // Anonymous function: leave name as null + } else if (matchToken(Token.MUL, true) + && (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6)) { + // ES6 generator function + isGenerator = true; + continue; + } else { if (compilerEnv.isAllowMemberExprAsFunctionName()) { - AstNode memberExprHead = name; - name = null; - memberExprNode = memberExprTail(false, memberExprHead); + // Note that memberExpr can not start with '(' like + // in function (1+2).toString(), because 'function (' already + // processed as anonymous function + memberExprNode = memberExpr(false); } mustMatchToken(Token.LP, "msg.no.paren.parms", true); } - } else if (matchToken(Token.LP, true)) { - // Anonymous function: leave name as null - } else if (matchToken(Token.MUL, true) - && (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6)) { - // ES6 generator function - return function(type, true); - } else { - if (compilerEnv.isAllowMemberExprAsFunctionName()) { - // Note that memberExpr can not start with '(' like - // in function (1+2).toString(), because 'function (' already - // processed as anonymous function - memberExprNode = memberExpr(false); - } - mustMatchToken(Token.LP, "msg.no.paren.parms", true); - } + break; + } while (isGenerator); int lpPos = currentToken == Token.LP ? ts.tokenBeg : -1; if (memberExprNode != null) { From f85e4661ebdf3c1f92d8bf791e235aa0ad737d69 Mon Sep 17 00:00:00 2001 From: tuchida Date: Fri, 14 Jan 2022 01:06:33 +0900 Subject: [PATCH 3/8] Remove destructuring assignment-only decompile function --- .../org/mozilla/javascript/IRFactory.java | 86 +------------------ .../mozilla/javascript/ast/ObjectLiteral.java | 14 ++- .../javascript/ast/ObjectProperty.java | 10 ++- 3 files changed, 20 insertions(+), 90 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java index 4d55956402..cd5300a67c 100644 --- a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java +++ b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java @@ -2494,90 +2494,6 @@ Node decompileFunctionHeader(FunctionNode fn) { } void decompile(AstNode node) { - switch (node.getType()) { - case Token.ARRAYLIT: - decompileArrayLiteral((ArrayLiteral) node); - break; - case Token.OBJECTLIT: - decompileObjectLiteral((ObjectLiteral) node); - break; - case Token.STRING: - decompiler.addString(((StringLiteral) node).getValue()); - break; - case Token.NAME: - decompiler.addName(((Name) node).getIdentifier()); - break; - case Token.NUMBER: - decompiler.addNumber(((NumberLiteral) node).getNumber()); - break; - case Token.BIGINT: - decompiler.addBigInt(node.getBigInt()); - break; - case Token.GETPROP: - decompilePropertyGet((PropertyGet) node); - break; - case Token.EMPTY: - break; - case Token.GETELEM: - decompileElementGet((ElementGet) node); - break; - case Token.THIS: - decompiler.addToken(node.getType()); - break; - case Token.COMPUTED_PROPERTY: - parser.reportError("msg.bad.computed.property.in.destruct"); - break; - default: - Kit.codeBug("unexpected token: " + Token.typeToName(node.getType())); - } - } - - // used for destructuring forms, since we don't transform() them - void decompileArrayLiteral(ArrayLiteral node) { - decompiler.addToken(Token.LB); - List elems = node.getElements(); - int size = elems.size(); - for (int i = 0; i < size; i++) { - AstNode elem = elems.get(i); - decompile(elem); - if (i < size - 1) { - decompiler.addToken(Token.COMMA); - } - } - decompiler.addToken(Token.RB); - } - - // only used for destructuring forms - void decompileObjectLiteral(ObjectLiteral node) { - decompiler.addToken(Token.LC); - List props = node.getElements(); - int size = props.size(); - for (int i = 0; i < size; i++) { - ObjectProperty prop = props.get(i); - decompile(prop.getLeft()); - if (!prop.isShorthand()) { - decompiler.addToken(Token.COLON); - decompile(prop.getRight()); - } - if (i < size - 1) { - decompiler.addToken(Token.COMMA); - } - } - decompiler.addToken(Token.RC); - } - - // only used for destructuring forms - void decompilePropertyGet(PropertyGet node) { - decompile(node.getTarget()); - decompiler.addToken(Token.DOT); - decompile(node.getProperty()); - } - - // only used for destructuring forms - void decompileElementGet(ElementGet node) { - decompile(node.getTarget()); - decompiler.addToken(Token.LB); - decompile(node.getElement()); - decompiler.addToken(Token.RB); + decompiler.addName(node.toSource()); } } diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectLiteral.java b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectLiteral.java index ab42268093..d761d974b8 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectLiteral.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectLiteral.java @@ -110,7 +110,19 @@ public String toSource(int depth) { sb.append(makeIndent(depth)); sb.append("{"); if (elements != null) { - printList(elements, sb); + int i = 0; + sb.append("\n"); + for (AstNode element : elements) { + sb.append(element.toSource(depth)); + if (sb.charAt(sb.length() - 1) == '\n') { + sb.deleteCharAt(sb.length() - 1); + } + if (i < elements.size() - 1) { + sb.append(","); + } + sb.append("\n"); + i++; + } } sb.append("}"); return sb.toString(); diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java index 3f19d92245..ab8e4020a2 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/ObjectProperty.java @@ -100,7 +100,6 @@ public boolean isShorthand() { @Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); - sb.append("\n"); sb.append(makeIndent(depth + 1)); if (isGetterMethod()) { sb.append("get "); @@ -108,10 +107,13 @@ public String toSource(int depth) { sb.append("set "); } sb.append(left.toSource(getType() == Token.COLON ? 0 : depth)); - if (type == Token.COLON) { - sb.append(": "); + + if (!shorthand) { + if (type == Token.COLON) { + sb.append(": "); + } + sb.append(right.toSource(getType() == Token.COLON ? 0 : depth + 1)); } - sb.append(right.toSource(getType() == Token.COLON ? 0 : depth + 1)); return sb.toString(); } From b639fa0f5f63d2430a99b749888e5413ad0f74a0 Mon Sep 17 00:00:00 2001 From: tuchida Date: Sat, 15 Jan 2022 20:18:29 +0900 Subject: [PATCH 4/8] Remove Decompiler in IRFactory --- .../org/mozilla/javascript/IRFactory.java | 366 +----------------- 1 file changed, 6 insertions(+), 360 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java index cd5300a67c..06fc65f738 100644 --- a/rhino/src/main/java/org/mozilla/javascript/IRFactory.java +++ b/rhino/src/main/java/org/mozilla/javascript/IRFactory.java @@ -39,7 +39,6 @@ import org.mozilla.javascript.ast.Label; import org.mozilla.javascript.ast.LabeledStatement; import org.mozilla.javascript.ast.LetNode; -import org.mozilla.javascript.ast.Loop; import org.mozilla.javascript.ast.Name; import org.mozilla.javascript.ast.NewExpression; import org.mozilla.javascript.ast.NumberLiteral; @@ -66,7 +65,6 @@ import org.mozilla.javascript.ast.VariableInitializer; import org.mozilla.javascript.ast.WhileLoop; import org.mozilla.javascript.ast.WithStatement; -import org.mozilla.javascript.ast.XmlDotQuery; import org.mozilla.javascript.ast.XmlElemRef; import org.mozilla.javascript.ast.XmlExpression; import org.mozilla.javascript.ast.XmlFragment; @@ -93,7 +91,6 @@ public final class IRFactory { private static final int ALWAYS_FALSE_BOOLEAN = -1; private Parser parser; - private Decompiler decompiler = new Decompiler(); public IRFactory(CompilerEnvirons env) { this(env, env.getErrorReporter()); @@ -103,37 +100,24 @@ public IRFactory(CompilerEnvirons env, ErrorReporter errorReporter) { parser = new Parser(env, errorReporter); } - /** - * Transforms the tree into a lower-level IR suitable for codegen. Optionally generates the - * encoded source. - */ + /** Transforms the tree into a lower-level IR suitable for codegen. */ public ScriptNode transformTree(AstRoot root) { parser.currentScriptOrFn = root; parser.inUseStrictDirective = root.isInStrictMode(); - int sourceStartOffset = decompiler.getCurrentOffset(); if (Token.printTrees) { System.out.println("IRFactory.transformTree"); System.out.println(root.debugPrint()); } - ScriptNode script = (ScriptNode) transform(root); - - int sourceEndOffset = decompiler.getCurrentOffset(); - script.setEncodedSourceBounds(sourceStartOffset, sourceEndOffset); - - if (parser.compilerEnv.isGeneratingSource()) { - script.setEncodedSource(decompiler.getEncodedSource()); - } - decompiler = null; - return script; + return (ScriptNode) transform(root); } // Might want to convert this to polymorphism - move transform* // functions into the AstNode subclasses. OTOH that would make // IR transformation part of the public AST API - desirable? // Another possibility: create AstTransformer interface and adapter. - public Node transform(AstNode node) { + private Node transform(AstNode node) { switch (node.getType()) { case Token.ARRAYCOMP: return transformArrayComp((ArrayComprehension) node); @@ -301,7 +285,6 @@ private Node transformArrayComp(ArrayComprehension node) { } private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) { - decompiler.addToken(Token.LB); int lineno = node.getLineno(); Node expr = transform(node.getResult()); @@ -314,21 +297,12 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) for (int i = 0; i < numLoops; i++) { ArrayComprehensionLoop acl = loops.get(i); - decompiler.addName(" "); - decompiler.addToken(Token.FOR); - if (acl.isForEach()) { - decompiler.addName("each "); - } - decompiler.addToken(Token.LP); - AstNode iter = acl.getIterator(); String name = null; if (iter.getType() == Token.NAME) { name = iter.getString(); - decompiler.addName(name); } else { // destructuring assignment - decompile(iter); name = parser.currentScriptOrFn.getNextTempName(); parser.defineSymbol(Token.LP, name, false); expr = @@ -343,13 +317,7 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) parser.defineSymbol(Token.LET, name, false); iterators[i] = init; - if (acl.isForOf()) { - decompiler.addName("of "); - } else { - decompiler.addToken(Token.IN); - } iteratedObjs[i] = transform(acl.getIteratedObject()); - decompiler.addToken(Token.RP); } // generate code for tmpArray.push(body) @@ -361,11 +329,7 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) Node body = new Node(Token.EXPR_VOID, call, lineno); if (node.getFilter() != null) { - decompiler.addName(" "); - decompiler.addToken(Token.IF); - decompiler.addToken(Token.LP); body = createIf(transform(node.getFilter()), body, null, lineno); - decompiler.addToken(Token.RP); } // Now walk loops in reverse to build up the body statement. @@ -395,8 +359,6 @@ private Node arrayCompTransformHelper(ArrayComprehension node, String arrayName) } } - decompiler.addToken(Token.RB); - // Now that we've accumulated any destructuring forms, // add expr to the call node; it's pushed on each iteration. call.addChildToBack(expr); @@ -407,7 +369,6 @@ private Node transformArrayLiteral(ArrayLiteral node) { if (node.isDestructuring()) { return node; } - decompiler.addToken(Token.LB); List elems = node.getElements(); Node array = new Node(Token.ARRAYLIT); List skipIndexes = null; @@ -421,9 +382,7 @@ private Node transformArrayLiteral(ArrayLiteral node) { } skipIndexes.add(Integer.valueOf(i)); } - if (i < elems.size() - 1) decompiler.addToken(Token.COMMA); } - decompiler.addToken(Token.RB); array.putIntProp(Node.DESTRUCTURING_ARRAY_LENGTH, node.getDestructuringLength()); if (skipIndexes != null) { int[] skips = new int[skipIndexes.size()]; @@ -440,13 +399,11 @@ private Node transformAssignment(Assignment node) { Node target = null; if (isDestructuring(left)) { - decompile(left); target = left; } else { target = transform(left); } - decompiler.addToken(node.getType()); return createAssignment(node.getType(), target, transform(right)); } @@ -476,7 +433,6 @@ private AstNode transformAssignmentLeft(Assignment node, AstNode left, AstNode r } private Node transformBigInt(BigIntLiteral node) { - decompiler.addBigInt(node.getBigInt()); return node; } @@ -502,29 +458,17 @@ private Node transformBlock(AstNode node) { } private Node transformBreak(BreakStatement node) { - decompiler.addToken(Token.BREAK); - if (node.getBreakLabel() != null) { - decompiler.addName(node.getBreakLabel().getIdentifier()); - } - decompiler.addEOL(Token.SEMI); return node; } private Node transformCondExpr(ConditionalExpression node) { Node test = transform(node.getTestExpression()); - decompiler.addToken(Token.HOOK); Node ifTrue = transform(node.getTrueExpression()); - decompiler.addToken(Token.COLON); Node ifFalse = transform(node.getFalseExpression()); return createCondExpr(test, ifTrue, ifFalse); } private Node transformContinue(ContinueStatement node) { - decompiler.addToken(Token.CONTINUE); - if (node.getLabel() != null) { - decompiler.addName(node.getLabel().getIdentifier()); - } - decompiler.addEOL(Token.SEMI); return node; } @@ -532,15 +476,8 @@ private Node transformDoLoop(DoLoop loop) { loop.setType(Token.LOOP); parser.pushScope(loop); try { - decompiler.addToken(Token.DO); - decompiler.addEOL(Token.LC); Node body = transform(loop.getBody()); - decompiler.addToken(Token.RC); - decompiler.addToken(Token.WHILE); - decompiler.addToken(Token.LP); Node cond = transform(loop.getCondition()); - decompiler.addToken(Token.RP); - decompiler.addEOL(Token.SEMI); return createLoop(loop, LOOP_DO_WHILE, body, cond, null, null); } finally { parser.popScope(); @@ -551,23 +488,16 @@ private Node transformElementGet(ElementGet node) { // OPT: could optimize to createPropertyGet // iff elem is string that can not be number Node target = transform(node.getTarget()); - decompiler.addToken(Token.LB); Node element = transform(node.getElement()); - decompiler.addToken(Token.RB); return new Node(Token.GETELEM, target, element); } private Node transformExprStmt(ExpressionStatement node) { Node expr = transform(node.getExpression()); - decompiler.addEOL(Token.SEMI); return new Node(node.getType(), expr, node.getLineno()); } private Node transformForInLoop(ForInLoop loop) { - decompiler.addToken(Token.FOR); - if (loop.isForEach()) decompiler.addName("each "); - decompiler.addToken(Token.LP); - loop.setType(Token.LOOP); parser.pushScope(loop); try { @@ -577,16 +507,8 @@ private Node transformForInLoop(ForInLoop loop) { declType = iter.getType(); } Node lhs = transform(iter); - if (loop.isForOf()) { - decompiler.addName("of "); - } else { - decompiler.addToken(Token.IN); - } Node obj = transform(loop.getIteratedObject()); - decompiler.addToken(Token.RP); - decompiler.addEOL(Token.LC); Node body = transform(loop.getBody()); - decompiler.addEOL(Token.RC); return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf()); } finally { parser.popScope(); @@ -594,8 +516,6 @@ private Node transformForInLoop(ForInLoop loop) { } private Node transformForLoop(ForLoop loop) { - decompiler.addToken(Token.FOR); - decompiler.addToken(Token.LP); loop.setType(Token.LOOP); // XXX: Can't use pushScope/popScope here since 'createFor' may split // the scope @@ -603,14 +523,9 @@ private Node transformForLoop(ForLoop loop) { parser.currentScope = loop; try { Node init = transform(loop.getInitializer()); - decompiler.addToken(Token.SEMI); Node test = transform(loop.getCondition()); - decompiler.addToken(Token.SEMI); Node incr = transform(loop.getIncrement()); - decompiler.addToken(Token.RP); - decompiler.addEOL(Token.LC); Node body = transform(loop.getBody()); - decompiler.addEOL(Token.RC); return createFor(loop, init, test, incr, body); } finally { parser.currentScope = savedScope; @@ -618,8 +533,6 @@ private Node transformForLoop(ForLoop loop) { } private Node transformFunction(FunctionNode fn) { - int functionType = fn.getFunctionType(); - int start = decompiler.markFunctionStart(functionType, fn.isES6Generator()); Node mexpr = decompileFunctionHeader(fn); int index = parser.currentScriptOrFn.addFunction(fn); @@ -634,17 +547,6 @@ private Node transformFunction(FunctionNode fn) { ++parser.nestingOfFunction; // only for body, not params Node body = transform(fn.getBody()); - if (!fn.isExpressionClosure()) { - decompiler.addToken(Token.RC); - } - fn.setEncodedSourceBounds(start, decompiler.markFunctionEnd(start)); - - if (functionType != FunctionNode.FUNCTION_EXPRESSION && !fn.isExpressionClosure()) { - // Add EOL only if function is not part of expression - // since it gets SEMI + EOL from Statement in that case - decompiler.addToken(Token.EOL); - } - if (destructuring != null) { body.addChildToFront(new Node(Token.EXPR_VOID, destructuring, lineno)); } @@ -668,16 +570,11 @@ private Node transformFunction(FunctionNode fn) { private Node transformFunctionCall(FunctionCall node) { Node call = createCallOrNew(Token.CALL, transform(node.getTarget())); call.setLineno(node.getLineno()); - decompiler.addToken(Token.LP); List args = node.getArguments(); for (int i = 0; i < args.size(); i++) { AstNode arg = args.get(i); call.addChildToBack(transform(arg)); - if (i < args.size() - 1) { - decompiler.addToken(Token.COMMA); - } } - decompiler.addToken(Token.RP); return call; } @@ -690,8 +587,6 @@ private Node transformGenExpr(GeneratorExpression node) { fn.setFunctionType(FunctionNode.FUNCTION_EXPRESSION); fn.setRequiresActivation(); - int functionType = fn.getFunctionType(); - int start = decompiler.markFunctionStart(functionType, false); Node mexpr = decompileFunctionHeader(fn); int index = parser.currentScriptOrFn.addFunction(fn); @@ -706,17 +601,6 @@ private Node transformGenExpr(GeneratorExpression node) { ++parser.nestingOfFunction; // only for body, not params Node body = genExprTransformHelper(node); - if (!fn.isExpressionClosure()) { - decompiler.addToken(Token.RC); - } - fn.setEncodedSourceBounds(start, decompiler.markFunctionEnd(start)); - - if (functionType != FunctionNode.FUNCTION_EXPRESSION && !fn.isExpressionClosure()) { - // Add EOL only if function is not part of expression - // since it gets SEMI + EOL from Statement in that case - decompiler.addToken(Token.EOL); - } - if (destructuring != null) { body.addChildToFront(new Node(Token.EXPR_VOID, destructuring, lineno)); } @@ -736,13 +620,10 @@ private Node transformGenExpr(GeneratorExpression node) { Node call = createCallOrNew(Token.CALL, pn); call.setLineno(node.getLineno()); - decompiler.addToken(Token.LP); - decompiler.addToken(Token.RP); return call; } private Node genExprTransformHelper(GeneratorExpression node) { - decompiler.addToken(Token.LP); int lineno = node.getLineno(); Node expr = transform(node.getResult()); @@ -755,18 +636,13 @@ private Node genExprTransformHelper(GeneratorExpression node) { for (int i = 0; i < numLoops; i++) { GeneratorExpressionLoop acl = loops.get(i); - decompiler.addName(" "); - decompiler.addToken(Token.FOR); - decompiler.addToken(Token.LP); AstNode iter = acl.getIterator(); String name = null; if (iter.getType() == Token.NAME) { name = iter.getString(); - decompiler.addName(name); } else { // destructuring assignment - decompile(iter); name = parser.currentScriptOrFn.getNextTempName(); parser.defineSymbol(Token.LP, name, false); expr = @@ -781,13 +657,7 @@ private Node genExprTransformHelper(GeneratorExpression node) { parser.defineSymbol(Token.LET, name, false); iterators[i] = init; - if (acl.isForOf()) { - decompiler.addName("of "); - } else { - decompiler.addToken(Token.IN); - } iteratedObjs[i] = transform(acl.getIteratedObject()); - decompiler.addToken(Token.RP); } // generate code for tmpArray.push(body) @@ -796,11 +666,7 @@ private Node genExprTransformHelper(GeneratorExpression node) { Node body = new Node(Token.EXPR_VOID, yield, lineno); if (node.getFilter() != null) { - decompiler.addName(" "); - decompiler.addToken(Token.IF); - decompiler.addToken(Token.LP); body = createIf(transform(node.getFilter()), body, null, lineno); - decompiler.addToken(Token.RP); } // Now walk loops in reverse to build up the body statement. @@ -830,61 +696,28 @@ private Node genExprTransformHelper(GeneratorExpression node) { } } - decompiler.addToken(Token.RP); - return body; } private Node transformIf(IfStatement n) { - decompiler.addToken(Token.IF); - decompiler.addToken(Token.LP); Node cond = transform(n.getCondition()); - decompiler.addToken(Token.RP); - decompiler.addEOL(Token.LC); Node ifTrue = transform(n.getThenPart()); Node ifFalse = null; if (n.getElsePart() != null) { - decompiler.addToken(Token.RC); - decompiler.addToken(Token.ELSE); - decompiler.addEOL(Token.LC); ifFalse = transform(n.getElsePart()); } - decompiler.addEOL(Token.RC); return createIf(cond, ifTrue, ifFalse, n.getLineno()); } private Node transformInfix(InfixExpression node) { Node left = transform(node.getLeft()); - decompiler.addToken(node.getType()); Node right = transform(node.getRight()); - if (node instanceof XmlDotQuery) { - decompiler.addToken(Token.RP); - } return createBinary(node.getType(), left, right); } private Node transformLabeledStatement(LabeledStatement ls) { Label label = ls.getFirstLabel(); - List