Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parenthesis for nullish coalescing #10269

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-generator/src/node/index.js
Expand Up @@ -96,6 +96,7 @@ export function needsParens(node, parent, printStack) {
if (t.isNewExpression(parent) && parent.callee === node) {
if (isOrHasCallExpression(node)) return true;
}
jridgewell marked this conversation as resolved.
Show resolved Hide resolved

/* this check is for NullishCoalescing being used with LogicalOperators like && and ||
* For example when someone creates an ast programmaticaly like this
* t.logicalExpression(
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -308,6 +308,7 @@ describe("programmatic generation", function() {
const output = generate(nullishCoalesc).code;
expect(output).toBe(`(a || b) ?? c`);
});
jridgewell marked this conversation as resolved.
Show resolved Hide resolved

it("should add parenthesis when NullishCoalesing is used with &&", function() {
const nullishCoalesc = t.logicalExpression(
"??",
Expand All @@ -321,6 +322,7 @@ describe("programmatic generation", function() {
const output = generate(nullishCoalesc).code;
expect(output).toBe(`a ?? (b && c && d)`);
});
jridgewell marked this conversation as resolved.
Show resolved Hide resolved

it("numeric member expression", function() {
// Should not generate `0.foo`
const mem = t.memberExpression(
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-parser/test/unit/tokenizer/types.js
@@ -0,0 +1,13 @@
import { types } from "../../../src/tokenizer/types";

describe("token types", () => {
it("should check if the binOp for relational === in", () => {
expect(types.relational.binop).toEqual(types._in.binop);
});
it("should check if the binOp for relational === instanceOf", () => {
vivek12345 marked this conversation as resolved.
Show resolved Hide resolved
expect(types.relational.binop).toEqual(types._instanceof.binop);
});
it("should check if the binOp for in === instanceOf", () => {
expect(types._in.binop).toEqual(types._instanceof.binop);
});
});