Skip to content

Commit

Permalink
Fix excessive parenthesization of ternary ?: expressions.
Browse files Browse the repository at this point in the history
Helps with #366.
  • Loading branch information
benjamn committed Sep 10, 2018
1 parent 3d45d4a commit 0475616
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,9 @@ function genericPrintNoParens(path, options, print) {

case "ConditionalExpression":
return concat([
"(", path.call(print, "test"),
path.call(print, "test"),
" ? ", path.call(print, "consequent"),
" : ", path.call(print, "alternate"), ")"
" : ", path.call(print, "alternate")
]);

case "NewExpression":
Expand Down
15 changes: 15 additions & 0 deletions test/parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,19 @@ describe("parens", function () {
"(() => {})();"
);
});

it("regression test for issue #366", function () {
const code = "typeof a ? b : c";
check(code);

const ast = parse(code);
const exprStmt = ast.program.body[0];
const callee = exprStmt.expression;
exprStmt.expression = b.callExpression(callee, []);

assert.strictEqual(
printer.print(ast).code,
"(typeof a ? b : c)()"
);
});
});

0 comments on commit 0475616

Please sign in to comment.