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)) &&
|
(t.isClassDeclaration(parent) || t.isClassExpression(parent)) &&
|
||||||
parent.superClass === node;
|
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 {
|
export function NullableTypeAnnotation(node: Object, parent: Object): boolean {
|
||||||
return t.isArrayTypeAnnotation(parent);
|
return t.isArrayTypeAnnotation(parent);
|
||||||
}
|
}
|
||||||
@ -56,19 +66,7 @@ export function FunctionTypeAnnotation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function UpdateExpression(node: Object, parent: Object): boolean {
|
export function UpdateExpression(node: Object, parent: Object): boolean {
|
||||||
return (
|
return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
|
||||||
// (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)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ObjectExpression(
|
export function ObjectExpression(
|
||||||
@ -100,13 +98,8 @@ export function Binary(node: Object, parent: Object): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
((t.isCallExpression(parent) ||
|
hasPostfixPart(node, parent) ||
|
||||||
t.isOptionalCallExpression(parent) ||
|
|
||||||
t.isNewExpression(parent)) &&
|
|
||||||
parent.callee === node) ||
|
|
||||||
t.isUnaryLike(parent) ||
|
t.isUnaryLike(parent) ||
|
||||||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
|
|
||||||
parent.object === node) ||
|
|
||||||
t.isAwaitExpression(parent)
|
t.isAwaitExpression(parent)
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
@ -202,11 +195,7 @@ export function YieldExpression(node: Object, parent: Object): boolean {
|
|||||||
return (
|
return (
|
||||||
t.isBinary(parent) ||
|
t.isBinary(parent) ||
|
||||||
t.isUnaryLike(parent) ||
|
t.isUnaryLike(parent) ||
|
||||||
t.isCallExpression(parent) ||
|
hasPostfixPart(node, parent) ||
|
||||||
t.isOptionalCallExpression(parent) ||
|
|
||||||
t.isMemberExpression(parent) ||
|
|
||||||
t.isOptionalMemberExpression(parent) ||
|
|
||||||
t.isNewExpression(parent) ||
|
|
||||||
(t.isAwaitExpression(parent) && t.isYieldExpression(node)) ||
|
(t.isAwaitExpression(parent) && t.isYieldExpression(node)) ||
|
||||||
(t.isConditionalExpression(parent) && node === parent.test) ||
|
(t.isConditionalExpression(parent) && node === parent.test) ||
|
||||||
isClassExtendsClause(node, parent)
|
isClassExtendsClause(node, parent)
|
||||||
@ -225,12 +214,7 @@ export function ClassExpression(
|
|||||||
|
|
||||||
export function UnaryLike(node: Object, parent: Object): boolean {
|
export function UnaryLike(node: Object, parent: Object): boolean {
|
||||||
return (
|
return (
|
||||||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
|
hasPostfixPart(node, parent) ||
|
||||||
parent.object === node) ||
|
|
||||||
((t.isCallExpression(parent) ||
|
|
||||||
t.isOptionalCallExpression(parent) ||
|
|
||||||
t.isNewExpression(parent)) &&
|
|
||||||
parent.callee === node) ||
|
|
||||||
t.isBinaryExpression(parent, { operator: "**", left: node }) ||
|
t.isBinaryExpression(parent, { operator: "**", left: node }) ||
|
||||||
isClassExtendsClause(node, parent)
|
isClassExtendsClause(node, parent)
|
||||||
);
|
);
|
||||||
@ -254,9 +238,6 @@ export function ConditionalExpression(node: Object, parent: Object): boolean {
|
|||||||
t.isBinary(parent) ||
|
t.isBinary(parent) ||
|
||||||
t.isConditionalExpression(parent, { test: node }) ||
|
t.isConditionalExpression(parent, { test: node }) ||
|
||||||
t.isAwaitExpression(parent) ||
|
t.isAwaitExpression(parent) ||
|
||||||
t.isOptionalMemberExpression(parent, { object: node }) ||
|
|
||||||
t.isOptionalCallExpression(parent, { callee: node }) ||
|
|
||||||
t.isTaggedTemplateExpression(parent) ||
|
|
||||||
t.isTSTypeAssertion(parent) ||
|
t.isTSTypeAssertion(parent) ||
|
||||||
t.isTSAsExpression(parent)
|
t.isTSAsExpression(parent)
|
||||||
) {
|
) {
|
||||||
@ -276,12 +257,7 @@ export function OptionalMemberExpression(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function OptionalCallExpression(node: Object, parent: Object): boolean {
|
export { OptionalMemberExpression as OptionalCallExpression };
|
||||||
return (
|
|
||||||
t.isCallExpression(parent, { callee: node }) ||
|
|
||||||
t.isMemberExpression(parent, { object: node })
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AssignmentExpression(
|
export function AssignmentExpression(
|
||||||
node: Object,
|
node: Object,
|
||||||
@ -320,7 +296,6 @@ function isFirstInStatement(
|
|||||||
while (i > 0) {
|
while (i > 0) {
|
||||||
if (
|
if (
|
||||||
t.isExpressionStatement(parent, { expression: node }) ||
|
t.isExpressionStatement(parent, { expression: node }) ||
|
||||||
t.isTaggedTemplateExpression(parent) ||
|
|
||||||
(considerDefaultExports &&
|
(considerDefaultExports &&
|
||||||
t.isExportDefaultDeclaration(parent, { declaration: node })) ||
|
t.isExportDefaultDeclaration(parent, { declaration: node })) ||
|
||||||
(considerArrow && t.isArrowFunctionExpression(parent, { body: node }))
|
(considerArrow && t.isArrowFunctionExpression(parent, { body: node }))
|
||||||
@ -329,11 +304,8 @@ function isFirstInStatement(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
((t.isCallExpression(parent) || t.isOptionalCallExpression(parent)) &&
|
(hasPostfixPart(node, parent) && !t.isNewExpression(parent)) ||
|
||||||
parent.callee === node) ||
|
|
||||||
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
|
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
|
||||||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
|
|
||||||
parent.object === node) ||
|
|
||||||
t.isConditional(parent, { test: node }) ||
|
t.isConditional(parent, { test: node }) ||
|
||||||
t.isBinary(parent, { left: node }) ||
|
t.isBinary(parent, { left: node }) ||
|
||||||
t.isAssignmentExpression(parent, { left: node })
|
t.isAssignmentExpression(parent, { left: node })
|
||||||
|
|||||||
@ -1,3 +1,8 @@
|
|||||||
(() => {})``;
|
(() => {})``;
|
||||||
(function(){}``);
|
(function(){}``);
|
||||||
(a ? b : c)``;
|
(a ? b : c)``;
|
||||||
|
|
||||||
|
function* fn() {
|
||||||
|
(yield)`foo`;
|
||||||
|
(yield f)`foo`;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,3 +1,8 @@
|
|||||||
(() => {})``;
|
(() => {})``;
|
||||||
(function () {})``;
|
(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,
|
F: A,
|
||||||
d: []
|
d: []
|
||||||
};
|
};
|
||||||
}, (await B));
|
}, await B);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,5 +15,5 @@ function* g() {
|
|||||||
F: A,
|
F: A,
|
||||||
d: []
|
d: []
|
||||||
};
|
};
|
||||||
}, (yield B));
|
}, yield B);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ var _ref, _;
|
|||||||
|
|
||||||
function then(fn) {
|
function then(fn) {
|
||||||
return async value => {
|
return async value => {
|
||||||
return fn((await value));
|
return fn(await value);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
async function test() {
|
async function test() {
|
||||||
(function (e) {
|
(function (e) {
|
||||||
throw e;
|
throw e;
|
||||||
})(new Error((await 'test')));
|
})(new Error(await 'test'));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
function* test() {
|
function* test() {
|
||||||
(function (e) {
|
(function (e) {
|
||||||
throw 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 () {
|
(function () {
|
||||||
var _poll = _asyncToGenerator(function* () {
|
var _poll = _asyncToGenerator(function* () {
|
||||||
console.log((yield Promise.resolve('Hello')));
|
console.log(yield Promise.resolve('Hello'));
|
||||||
setTimeout(poll, 1000);
|
setTimeout(poll, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user