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

Unify parens printing for postfix-like expressions #11382

Merged
merged 4 commits into from Apr 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 16 additions & 44 deletions packages/babel-generator/src/node/parentheses.js
Expand Up @@ -32,6 +32,16 @@ const isClassExtendsClause = (node: Object, parent: Object): boolean =>
(t.isClassDeclaration(parent) || t.isClassExpression(parent)) &&
parent.superClass === node;

const hasPostfixPart = (node: Object, parent: Object) =>
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
(t.isTaggedTemplateExpression(parent) && parent.tag === node) ||
t.isTSNonNullExpression(parent);

export function NullableTypeAnnotation(node: Object, parent: Object): boolean {
return t.isArrayTypeAnnotation(parent);
}
Expand All @@ -56,19 +66,7 @@ export function FunctionTypeAnnotation(
}

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)
);
return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
}

export function ObjectExpression(
Expand Down Expand Up @@ -100,13 +98,8 @@ export function Binary(node: Object, parent: Object): boolean {
}

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

export function UnaryLike(node: Object, parent: Object): boolean {
return (
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
hasPostfixPart(node, parent) ||
t.isBinaryExpression(parent, { operator: "**", left: node }) ||
isClassExtendsClause(node, parent)
);
Expand All @@ -254,9 +238,6 @@ export function ConditionalExpression(node: Object, parent: Object): boolean {
t.isBinary(parent) ||
t.isConditionalExpression(parent, { test: node }) ||
t.isAwaitExpression(parent) ||
t.isOptionalMemberExpression(parent, { object: node }) ||
t.isOptionalCallExpression(parent, { callee: node }) ||
t.isTaggedTemplateExpression(parent) ||
t.isTSTypeAssertion(parent) ||
t.isTSAsExpression(parent)
) {
Expand All @@ -276,12 +257,7 @@ export function OptionalMemberExpression(
);
}

export function OptionalCallExpression(node: Object, parent: Object): boolean {
return (
t.isCallExpression(parent, { callee: node }) ||
t.isMemberExpression(parent, { object: node })
);
}
export { OptionalMemberExpression as OptionalCallExpression };

export function AssignmentExpression(
node: Object,
Expand Down Expand Up @@ -320,7 +296,6 @@ function isFirstInStatement(
while (i > 0) {
if (
t.isExpressionStatement(parent, { expression: node }) ||
t.isTaggedTemplateExpression(parent) ||
(considerDefaultExports &&
t.isExportDefaultDeclaration(parent, { declaration: node })) ||
(considerArrow && t.isArrowFunctionExpression(parent, { body: node }))
Expand All @@ -329,11 +304,8 @@ function isFirstInStatement(
}

if (
((t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) &&
parent.callee === node) ||
(hasPostfixPart(node, parent) && !t.isNewExpression(parent)) ||
(t.isSequenceExpression(parent) && parent.expressions[0] === 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
@@ -1,3 +1,8 @@
(() => {})``;
(function(){}``);
(a ? b : c)``;
(a ? b : c)``;

function* fn() {
(yield)`foo`;
(yield f)`foo`;
}
@@ -1,3 +1,8 @@
(() => {})``;
(function () {})``;
(a ? b : c)``;
(a ? b : c)``;

function* fn() {
(yield)`foo`;
(yield f)`foo`;
}
@@ -0,0 +1,16 @@
(a ? b : c)!;
(a ? b : c)!.d;
(a ? b : c!);
(a ? b : c!).d;

foo!();
foo()!;

async function* f() {
(yield x)!;
yield x!;
(yield)!;

(await x)!;
(await x!);
}
@@ -0,0 +1,14 @@
(a ? b : c)!;
(a ? b : c)!.d;
a ? b : c!;
(a ? b : c!).d;
foo!();
foo()!;

async function* f() {
(yield x)!;
yield x!;
(yield)!;
(await x)!;
await x!;
}
Expand Up @@ -15,5 +15,5 @@ async function g() {
F: A,
d: []
};
}, (await B));
}, await B);
}
Expand Up @@ -15,5 +15,5 @@ function* g() {
F: A,
d: []
};
}, (yield B));
}, yield B);
}
Expand Up @@ -2,7 +2,7 @@ var _ref, _;

function then(fn) {
return async value => {
return fn((await value));
return fn(await value);
};
}

Expand Down
@@ -1,5 +1,5 @@
async function test() {
(function (e) {
throw e;
})(new Error((await 'test')));
})(new Error(await 'test'));
}
@@ -1,5 +1,5 @@
function* test() {
(function (e) {
throw e;
})(new Error((yield 'test')));
})(new Error(yield 'test'));
}
Expand Up @@ -4,7 +4,7 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar

(function () {
var _poll = _asyncToGenerator(function* () {
console.log((yield Promise.resolve('Hello')));
console.log(yield Promise.resolve('Hello'));
setTimeout(poll, 1000);
});

Expand Down