Unify parens printing for postfix-like expressions (#11382)
* Unify parens printing for postfix exprs: (), [...], ! * Also move template tags handling * Add tagged template test * isPostfixExpression -> hasPostfixPart
This commit is contained in:
parent
b04ddff853
commit
ce6cc4eb55
@ -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);
|
||||
}
|
||||
@ -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(
|
||||
@ -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;
|
||||
@ -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)
|
||||
@ -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)
|
||||
);
|
||||
@ -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)
|
||||
) {
|
||||
@ -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,
|
||||
@ -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 }))
|
||||
@ -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 })
|
||||
|
||||
@ -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`;
|
||||
}
|
||||
16
packages/babel-generator/test/fixtures/typescript/non-null-parentheses/input.js
vendored
Normal file
16
packages/babel-generator/test/fixtures/typescript/non-null-parentheses/input.js
vendored
Normal file
@ -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!);
|
||||
}
|
||||
14
packages/babel-generator/test/fixtures/typescript/non-null-parentheses/output.js
vendored
Normal file
14
packages/babel-generator/test/fixtures/typescript/non-null-parentheses/output.js
vendored
Normal file
@ -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!;
|
||||
}
|
||||
@ -15,5 +15,5 @@ async function g() {
|
||||
F: A,
|
||||
d: []
|
||||
};
|
||||
}, (await B));
|
||||
}, await B);
|
||||
}
|
||||
|
||||
@ -15,5 +15,5 @@ function* g() {
|
||||
F: A,
|
||||
d: []
|
||||
};
|
||||
}, (yield B));
|
||||
}, yield B);
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ var _ref, _;
|
||||
|
||||
function then(fn) {
|
||||
return async value => {
|
||||
return fn((await value));
|
||||
return fn(await value);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -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'));
|
||||
}
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user