fix: check parenthesis between optional chain and other types (#11325)

This commit is contained in:
Huáng Jùnliàng 2020-03-24 03:28:47 -04:00 committed by GitHub
parent dc7c5640e9
commit 146ea4176e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 66 additions and 14 deletions

View File

@ -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 }) ||
// (foo++)()
t.isCallExpression(parent, { callee: node }) ||
// (foo++)?.()
t.isOptionalCallExpression(parent, { callee: node }) ||
// new (foo++)()
t.isNewExpression(parent, { callee: node }) ||
isClassExtendsClause(node, parent)
@ -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;
@ -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) ||
@ -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)
);
@ -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 })

View File

@ -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);
@ -10,4 +11,5 @@ async function asdf() {
async function a(b) {
(await xhr({ url: "views/test.html" })).data;
(await replay())?.data;
}

View File

@ -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);
@ -12,4 +13,5 @@ async function a(b) {
(await xhr({
url: "views/test.html"
})).data;
(await replay())?.data;
}

View File

@ -0,0 +1,2 @@
(a + b)[0];
(a + b)?.[0];

View File

@ -0,0 +1,2 @@
(a + b)[0];
(a + b)?.[0];

View File

@ -1,2 +1,6 @@
({}) === foo;
({}) && foo;
({})();
({})?.();
({}).foo;
({})?.foo;

View File

@ -1,2 +1,6 @@
({}) === foo;
({}) && foo;
({}) && foo;
({})();
({})?.();
({}).foo;
({})?.foo;

View File

@ -0,0 +1,4 @@
(void 0)();
(void 0)?.();
(void 0)[0];
(void 0)?.[0];

View File

@ -0,0 +1,4 @@
(void 0)();
(void 0)?.();
(void 0)[0];
(void 0)?.[0];

View File

@ -6,3 +6,9 @@ new (a++)();
new (++a);
new (a++);
(++a)?.();
(a++)?.();
(++a)?.[0];
(a++)?.[0];

View File

@ -3,4 +3,8 @@
new (++a)();
new (a++)();
new (++a)();
new (a++)();
new (a++)();
(++a)?.();
(a++)?.();
(++a)?.[0];
(a++)?.[0];

View File

@ -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);
@ -9,6 +10,7 @@ function* asdf() {
function* a(b) {
(yield xhr({ url: "views/test.html" })).data;
(yield replay())?.data;
}
(async function* () {

View File

@ -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;
@ -11,8 +12,9 @@ function* a(b) {
(yield xhr({
url: "views/test.html"
})).data;
(yield replay())?.data;
}
(async function* () {
await (yield 1);
});
});