Skip to content

Commit

Permalink
Move ensureFunctionName to NodePath.prototype (#16658)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 26, 2024
1 parent de55414 commit d364545
Show file tree
Hide file tree
Showing 30 changed files with 257 additions and 473 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
],
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-member-expression-to-functions": "workspace:^",
"@babel/helper-optimise-call-expression": "workspace:^",
"@babel/helper-replace-supers": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { types as t, template } from "@babel/core";
import type { File, NodePath } from "@babel/core";
import ReplaceSupers from "@babel/helper-replace-supers";
import nameFunction from "@babel/helper-function-name";

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

Expand Down Expand Up @@ -91,6 +90,20 @@ function extractElementDescriptor(
);
}

if (path.isFunction()) {
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// polyfill when being run by an older Babel version
path.ensureFunctionName ??=
// eslint-disable-next-line no-restricted-globals
require("@babel/traverse").NodePath.prototype.ensureFunctionName;
}
// @ts-expect-error path is a ClassMethod, that technically
// is not supported as it does not have an .id property
// This plugin will however then transform the ClassMethod
// to a function expression, so it's fine.
path.ensureFunctionName(false);
}

const { node, scope } = path as NodePath<SupportedElement>;

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

if (t.isClassMethod(node)) {
const id = node.computed
? null
: (node.key as
| t.Identifier
| t.StringLiteral
| t.NumericLiteral
| t.BigIntLiteral);
const transformed = t.toExpression(node);
properties.push(
prop(
"value",
nameFunction({ node: transformed, id, scope }) || transformed,
),
);
properties.push(prop("value", t.toExpression(node)));
} else if (t.isClassProperty(node) && node.value) {
properties.push(
method("value", template.statements.ast`return ${node.value}`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { types as t } from "@babel/core";
import type { PluginAPI, PluginObject, NodePath } from "@babel/core";
import nameFunction from "@babel/helper-function-name";
import createDecoratorTransform from "./decorators.ts";
import type { DecoratorVersionKind } from "./decorators.ts";

Expand Down Expand Up @@ -231,7 +230,13 @@ export function createClassFeaturePlugin({
const innerBinding = path.node.id;
let ref: t.Identifier | null;
if (!innerBinding || !pathIsClassDeclaration) {
nameFunction(path as NodePath<t.ClassExpression>);
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// polyfill when being run by an older Babel version
path.ensureFunctionName ??=
// eslint-disable-next-line no-restricted-globals
require("@babel/traverse").NodePath.prototype.ensureFunctionName;
}
(path as NodePath<t.ClassExpression>).ensureFunctionName(false);
ref = path.scope.generateUidIdentifier(innerBinding?.name || "Class");
}
const classRefForDefine = ref ?? t.cloneNode(innerBinding);
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-helper-function-name/.npmignore

This file was deleted.

19 changes: 0 additions & 19 deletions packages/babel-helper-function-name/README.md

This file was deleted.

53 changes: 0 additions & 53 deletions packages/babel-helper-function-name/package.json

This file was deleted.

Loading

0 comments on commit d364545

Please sign in to comment.