Visitar URL original
fix: invalid `minified: true` output for new expression (#18461) · babel/babel@599a2dd · GitHub
Skip to content

Commit 599a2dd

Browse files
babel-botJLHwunggithub-actions[bot]
authored
fix: invalid minified: true output for new expression (#18461)
* fix: invalid `minified: true` output for new expression (#18324) backport of fd417e2 * Fix Babel 7 TS type parameters handling after backporting #18324 --------- Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7d2d2b6 commit 599a2dd

15 files changed

Lines changed: 162 additions & 20 deletions

File tree

‎packages/babel-generator/src/generators/expressions.ts‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import type Printer from "../printer.ts";
2-
import {
3-
isCallExpression,
4-
isLiteral,
5-
isMemberExpression,
6-
isNewExpression,
7-
isPattern,
8-
} from "@babel/types";
2+
import { isLiteral, isMemberExpression, isPattern } from "@babel/types";
93
// We inline this package
104
// eslint-disable-next-line import/no-extraneous-dependencies
115
import * as charCodes from "charcodes";
126
import type * as t from "@babel/types";
13-
import { TokenContext } from "../node/index.ts";
7+
import { TokenContext, hasPostfixPart } from "../node/index.ts";
148

159
export function UnaryExpression(this: Printer, node: t.UnaryExpression) {
1610
const { operator } = node;
@@ -94,18 +88,20 @@ export function NewExpression(
9488
node: t.NewExpression,
9589
parent: t.Node,
9690
) {
91+
const omitArguments =
92+
this.format.minified &&
93+
node.arguments.length === 0 &&
94+
!node.typeArguments &&
95+
(process.env.BABEL_8_BREAKING ||
96+
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
97+
(!node.typeParameters && !node.optional));
98+
const wrapInParens = omitArguments && hasPostfixPart(node, parent);
99+
if (wrapInParens) this.token("(");
97100
this.word("new");
98101
this.space();
99102
this.print(node.callee);
100-
if (
101-
this.format.minified &&
102-
node.arguments.length === 0 &&
103-
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
104-
!node.optional &&
105-
!isCallExpression(parent, { callee: node }) &&
106-
!isMemberExpression(parent) &&
107-
!isNewExpression(parent)
108-
) {
103+
if (omitArguments) {
104+
if (wrapInParens) this.token(")");
109105
return;
110106
}
111107

@@ -122,7 +118,8 @@ export function NewExpression(
122118
if (
123119
node.arguments.length === 0 &&
124120
this.tokenMap &&
125-
!this.tokenMap.endMatches(node, ")")
121+
!this.tokenMap.endMatches(node, ")") &&
122+
(!hasPostfixPart(node, parent) || node.extra?.parenthesized)
126123
) {
127124
return;
128125
}

‎packages/babel-generator/src/node/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ for (const type of Object.keys(parens) as (keyof typeof parens)[]) {
3838
}
3939
}
4040

41+
// Whether the parent prints something right after the node, such as `()` in
42+
// `node()`, `.x` in `node.x` or `!` in `node!`.
43+
export function hasPostfixPart(node: t.Node, parent: t.Node | null): boolean {
44+
return (
45+
parent != null &&
46+
parens.hasPostfixPart(node, parent, generatorInfosMap.get(parent.type)![1])
47+
);
48+
}
49+
4150
function newCalleeNeedsParens(node: t.Node): boolean {
4251
let current: t.Node = node;
4352
while (true) {

‎packages/babel-generator/src/node/parentheses.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const isClassExtendsClause = (
5656
);
5757
};
5858

59-
const hasPostfixPart = (node: t.Node, parent: any, parentId: number) => {
59+
export const hasPostfixPart = (node: t.Node, parent: any, parentId: number) => {
6060
switch (parentId) {
6161
case __node("MemberExpression"):
6262
case __node("OptionalMemberExpression"):
@@ -354,6 +354,13 @@ export function TSInstantiationExpression(
354354
: // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
355355
parent.typeParameters) != null
356356
);
357+
case __node("MemberExpression"):
358+
case __node("OptionalMemberExpression"):
359+
// `a<b>.c` is invalid, and `a<b>[0]` is parsed as a comparison
360+
return parent.object === node;
361+
case __node("TSNonNullExpression"):
362+
// `a<b>!` is invalid
363+
return true;
357364
}
358365

359366
return false;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(new F)!;
2+
(new F)!.b;
3+
(new F)<T>;
4+
new F<T>();
5+
new (F<T>).b;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["typescript"]
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(new F)!;(new F)!.b;(new F)<T>;new F<T>();new(F<T>).b;

‎packages/babel-generator/test/fixtures/minified/new-expression/input.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ new new x()();
66
new new x()(a);
77
new new x(a)();
88
new new x(a)(a);
9+
10+
(new F)?.();
11+
(new F)?.b;
12+
(new F)?.[b];
13+
(new F)`x`;
14+
15+
a[new F];
16+
new G(new F);
17+
18+
new F().x.y;
19+
new F()[0];
20+
new (new F)().x;

‎packages/babel-generator/test/fixtures/minified/new-expression/output.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(a<T>).b;
2+
(a<T>)?.b;
3+
(a<T>)[0];
4+
(a<T>)?.[0];
5+
(a<T>)!;
6+
new (F<T>).b();
7+
new (F<T>)!();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["typescript"]
3+
}

0 commit comments

Comments
 (0)