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: check parentheses between optional chain and other types #11325

Merged
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
28 changes: 21 additions & 7 deletions packages/babel-generator/src/node/parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ export function UpdateExpression(node: Object, parent: Object): boolean {
return (
// (foo++).test(), (foo++)[0]
t.isMemberExpression(parent, { object: node }) ||
// (foo++)?.test(), (foo++)?.[0]
t.isOptionalMemberExpression(parent, { object: node }) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should be adding an alias for these, so we don't need to duplicate everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree.

Copy link
Contributor Author

@JLHwung JLHwung Mar 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really difficult to come up with a proper name other than CallExpression/MemberExpression itself. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MemberExpressionish 😛

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, I was thinking isMemberish and isCallish.

// (foo++)()
t.isCallExpression(parent, { callee: node }) ||
// (foo++)?.()
t.isOptionalCallExpression(parent, { callee: node }) ||
// new (foo++)()
t.isNewExpression(parent, { callee: node }) ||
isClassExtendsClause(node, parent)
Expand Down Expand Up @@ -96,10 +100,13 @@ export function Binary(node: Object, parent: Object): boolean {
}

if (
((t.isCallExpression(parent) || t.isNewExpression(parent)) &&
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
t.isUnaryLike(parent) ||
(t.isMemberExpression(parent) && parent.object === node) ||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
t.isAwaitExpression(parent)
) {
return true;
Expand Down Expand Up @@ -196,7 +203,9 @@ export function YieldExpression(node: Object, parent: Object): boolean {
t.isBinary(parent) ||
t.isUnaryLike(parent) ||
t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isMemberExpression(parent) ||
t.isOptionalMemberExpression(parent) ||
t.isNewExpression(parent) ||
(t.isAwaitExpression(parent) && t.isYieldExpression(node)) ||
(t.isConditionalExpression(parent) && node === parent.test) ||
Expand All @@ -216,9 +225,12 @@ export function ClassExpression(

export function UnaryLike(node: Object, parent: Object): boolean {
return (
t.isMemberExpression(parent, { object: node }) ||
t.isCallExpression(parent, { callee: node }) ||
t.isNewExpression(parent, { callee: node }) ||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
t.isBinaryExpression(parent, { operator: "**", left: node }) ||
isClassExtendsClause(node, parent)
);
Expand Down Expand Up @@ -317,9 +329,11 @@ function isFirstInStatement(
}

if (
t.isCallExpression(parent, { callee: node }) ||
((t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) &&
parent.callee === node) ||
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
t.isMemberExpression(parent, { object: node }) ||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
t.isConditional(parent, { test: node }) ||
t.isBinary(parent, { left: node }) ||
t.isAssignmentExpression(parent, { left: node })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
async function asdf() {
async function foo() {
(await 1) || (await 2);
(await b)();
(await b)?.();
new (await b)();
true ? (await 1) : (await 2);
await (1 ? 2 : 3);
Expand All @@ -10,4 +11,5 @@ async function asdf() {

async function a(b) {
(await xhr({ url: "views/test.html" })).data;
(await replay())?.data;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
async function asdf() {
async function foo() {
(await 1) || (await 2);
(await b)();
(await b)?.();
new (await b)();
true ? await 1 : await 2;
await (1 ? 2 : 3);
Expand All @@ -12,4 +13,5 @@ async function a(b) {
(await xhr({
url: "views/test.html"
})).data;
(await replay())?.data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(a + b)[0];
(a + b)?.[0];
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(a + b)[0];
(a + b)?.[0];
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
({}) === foo;
({}) && foo;
({})();
({})?.();
({}).foo;
({})?.foo;
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
({}) === foo;
({}) && foo;
({}) && foo;
({})();
({})?.();
({}).foo;
({})?.foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(void 0)();
(void 0)?.();
(void 0)[0];
(void 0)?.[0];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(void 0)();
(void 0)?.();
(void 0)[0];
(void 0)?.[0];
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ new (a++)();

new (++a);
new (a++);

(++a)?.();
(a++)?.();

(++a)?.[0];
(a++)?.[0];
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
new (++a)();
new (a++)();
new (++a)();
new (a++)();
new (a++)();
(++a)?.();
(a++)?.();
(++a)?.[0];
(a++)?.[0];
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function* asdf() {
function* foo() {
(yield 1) || (yield 2);
(yield b)();
(yield b)?.();
new (yield b)();
(yield 1) ? (yield 2) : (yield 3);
yield (1 ? 2 : 3);
Expand All @@ -9,6 +10,7 @@ function* asdf() {

function* a(b) {
(yield xhr({ url: "views/test.html" })).data;
(yield replay())?.data;
}

(async function* () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function* asdf() {
function* foo() {
(yield 1) || (yield 2);
(yield b)();
(yield b)?.();
new (yield b)();
(yield 1) ? yield 2 : yield 3;
yield 1 ? 2 : 3;
Expand All @@ -11,8 +12,9 @@ function* a(b) {
(yield xhr({
url: "views/test.html"
})).data;
(yield replay())?.data;
}

(async function* () {
await (yield 1);
});
});