Skip to content

Commit

Permalink
Merge pull request #1095 from benjamn/pr/xyy94813/896
Browse files Browse the repository at this point in the history
Fix parenthesis missing
  • Loading branch information
eventualbuddha authored Apr 27, 2022
2 parents 482afbe + 33cc570 commit 91bc665
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fast-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const isNumber = types.builtInTypes.number;

const PRECEDENCE: any = {};
[
["??"],
["||"],
["&&"],
["|"],
Expand Down
25 changes: 25 additions & 0 deletions test/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,31 @@ describe("printer", function () {
assert.strictEqual(pretty, code);
});

it("adds parenthesis around nullish expression, when nullish expression is BinaryExpression's child", function () {
const code = "let a = (c?.b ?? 1) + 1;";

const b = recast.types.builders;

const ast = b.variableDeclaration("let", [
b.variableDeclarator(
b.identifier("a"),
b.binaryExpression(
"+",
b.logicalExpression(
"??",
b.optionalMemberExpression(b.identifier("c"), b.identifier("b")),
b.numericLiteral(1),
),
b.numericLiteral(1),
),
),
]);

const printer = new Printer();
const pretty = printer.printGenerically(ast).code;
assert.strictEqual(pretty, code);
});

it("prints class property initializers with type annotations correctly", function () {
const code = ["class A {", " foo = (a: b): void => {};", "}"].join(eol);

Expand Down

0 comments on commit 91bc665

Please sign in to comment.