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 1 commit
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
55 changes: 15 additions & 40 deletions packages/babel-generator/src/node/parentheses.js
Expand Up @@ -32,6 +32,15 @@ const isClassExtendsClause = (node: Object, parent: Object): boolean =>
(t.isClassDeclaration(parent) || t.isClassExpression(parent)) &&
parent.superClass === node;

const isPostfixExpression = (node: Object, parent: Object) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK the postfix expression is used to describe a way of how the operator and its operands are arranged, so I am confused about the term here.

The Spec provides a term LeftHandSideExpression and I think we can stick with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

LeftHandSideExpression doesn't work, because it also covers PrimaryExpression.

I initially called this "postfix" because TS's ! is a postfix operator, and because in foo() and foo.bar, () and .bar are "postfixes" of the main foo expression 😅

Maybe hasPostfixPart?

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with hasPostfixPart.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good for me 🌝

((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
parent.object === node) ||
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
t.isTSNonNullExpression(parent);

export function NullableTypeAnnotation(node: Object, parent: Object): boolean {
return t.isArrayTypeAnnotation(parent);
}
Expand All @@ -57,17 +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)
isPostfixExpression(node, parent) || isClassExtendsClause(node, parent)
);
}

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

if (
((t.isCallExpression(parent) ||
t.isOptionalCallExpression(parent) ||
t.isNewExpression(parent)) &&
parent.callee === node) ||
isPostfixExpression(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 +196,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) ||
isPostfixExpression(node, parent) ||
(t.isAwaitExpression(parent) && t.isYieldExpression(node)) ||
(t.isConditionalExpression(parent) && node === parent.test) ||
isClassExtendsClause(node, parent)
Expand All @@ -225,12 +215,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) ||
isPostfixExpression(node, parent) ||
t.isBinaryExpression(parent, { operator: "**", left: node }) ||
isClassExtendsClause(node, parent)
);
Expand All @@ -254,8 +239,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 +259,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 @@ -329,11 +307,8 @@ function isFirstInStatement(
}

if (
((t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) &&
parent.callee === node) ||
(isPostfixExpression(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
@@ -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