diff --git a/lib/fast-path.ts b/lib/fast-path.ts index 925cbaac..6904eff3 100644 --- a/lib/fast-path.ts +++ b/lib/fast-path.ts @@ -314,6 +314,13 @@ FPp.getNextToken = function (node) { FPp.needsParens = function (assumeExpressionContext) { const node = this.getNode(); + // If the value of this path is some child of a Node and not a Node + // itself, then it doesn't need parentheses. Only Node objects (in fact, + // only Expression nodes) need parentheses. + if (this.getValue() !== node) { + return false; + } + // This needs to come before `if (!parent) { return false }` because // an object destructuring assignment requires parens for // correctness even when it's the topmost expression. @@ -328,13 +335,6 @@ FPp.needsParens = function (assumeExpressionContext) { const name = this.getName(); - // If the value of this path is some child of a Node and not a Node - // itself, then it doesn't need parentheses. Only Node objects (in fact, - // only Expression nodes) need parentheses. - if (this.getValue() !== node) { - return false; - } - // Only statements don't need parentheses. if (n.Statement.check(node)) { return false; diff --git a/test/parens.ts b/test/parens.ts index dd20af5f..63469b9e 100644 --- a/test/parens.ts +++ b/test/parens.ts @@ -360,6 +360,20 @@ describe("parens", function () { check("({ foo } = bar)"); }); + it("don't parenthesize comment (issue #575)", function () { + const source = "(/**/{foo} = 1)"; + const ast = parse(source); // not esprima + const expressionAst = ast.program.body[0].expression; + const reprint = printer.print(expressionAst).code; + if (!types.astNodesAreEquivalent(parse(reprint), ast)) { + throw new assert.AssertionError({ + message: "Expected values to parse to equivalent ASTs", + actual: reprint, + expected: source, + }); + } + }); + it("regression test for issue #327", function () { const expr = "(function(){}())"; check(expr);