Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/fast-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions test/parens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down