Skip to content

Commit d364545

Browse files
Move ensureFunctionName to NodePath.prototype (#16658)
1 parent de55414 commit d364545

File tree

30 files changed

+257
-473
lines changed

30 files changed

+257
-473
lines changed

packages/babel-helper-create-class-features-plugin/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
],
2020
"dependencies": {
2121
"@babel/helper-annotate-as-pure": "workspace:^",
22-
"@babel/helper-function-name": "workspace:^",
2322
"@babel/helper-member-expression-to-functions": "workspace:^",
2423
"@babel/helper-optimise-call-expression": "workspace:^",
2524
"@babel/helper-replace-supers": "workspace:^",

packages/babel-helper-create-class-features-plugin/src/decorators-2018-09.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { types as t, template } from "@babel/core";
44
import type { File, NodePath } from "@babel/core";
55
import ReplaceSupers from "@babel/helper-replace-supers";
6-
import nameFunction from "@babel/helper-function-name";
76

87
type Decoratable = Extract<t.Node, { decorators?: t.Decorator[] | null }>;
98

@@ -91,6 +90,20 @@ function extractElementDescriptor(
9190
);
9291
}
9392

93+
if (path.isFunction()) {
94+
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
95+
// polyfill when being run by an older Babel version
96+
path.ensureFunctionName ??=
97+
// eslint-disable-next-line no-restricted-globals
98+
require("@babel/traverse").NodePath.prototype.ensureFunctionName;
99+
}
100+
// @ts-expect-error path is a ClassMethod, that technically
101+
// is not supported as it does not have an .id property
102+
// This plugin will however then transform the ClassMethod
103+
// to a function expression, so it's fine.
104+
path.ensureFunctionName(false);
105+
}
106+
94107
const { node, scope } = path as NodePath<SupportedElement>;
95108

96109
if (!path.isTSDeclareMethod()) {
@@ -113,20 +126,7 @@ function extractElementDescriptor(
113126
].filter(Boolean);
114127

115128
if (t.isClassMethod(node)) {
116-
const id = node.computed
117-
? null
118-
: (node.key as
119-
| t.Identifier
120-
| t.StringLiteral
121-
| t.NumericLiteral
122-
| t.BigIntLiteral);
123-
const transformed = t.toExpression(node);
124-
properties.push(
125-
prop(
126-
"value",
127-
nameFunction({ node: transformed, id, scope }) || transformed,
128-
),
129-
);
129+
properties.push(prop("value", t.toExpression(node)));
130130
} else if (t.isClassProperty(node) && node.value) {
131131
properties.push(
132132
method("value", template.statements.ast`return ${node.value}`),

packages/babel-helper-create-class-features-plugin/src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { types as t } from "@babel/core";
22
import type { PluginAPI, PluginObject, NodePath } from "@babel/core";
3-
import nameFunction from "@babel/helper-function-name";
43
import createDecoratorTransform from "./decorators.ts";
54
import type { DecoratorVersionKind } from "./decorators.ts";
65

@@ -231,7 +230,13 @@ export function createClassFeaturePlugin({
231230
const innerBinding = path.node.id;
232231
let ref: t.Identifier | null;
233232
if (!innerBinding || !pathIsClassDeclaration) {
234-
nameFunction(path as NodePath<t.ClassExpression>);
233+
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
234+
// polyfill when being run by an older Babel version
235+
path.ensureFunctionName ??=
236+
// eslint-disable-next-line no-restricted-globals
237+
require("@babel/traverse").NodePath.prototype.ensureFunctionName;
238+
}
239+
(path as NodePath<t.ClassExpression>).ensureFunctionName(false);
235240
ref = path.scope.generateUidIdentifier(innerBinding?.name || "Class");
236241
}
237242
const classRefForDefine = ref ?? t.cloneNode(innerBinding);

packages/babel-helper-function-name/.npmignore

-3
This file was deleted.

packages/babel-helper-function-name/README.md

-19
This file was deleted.

packages/babel-helper-function-name/package.json

-53
This file was deleted.

0 commit comments

Comments
 (0)