Skip to content

Commit

Permalink
fix: support non-BMP method name in class transform
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 31, 2022
1 parent 0f4efae commit b27a859
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helper-wrap-function/src/index.ts
Expand Up @@ -148,7 +148,7 @@ function plainFunction(
const returnFn = container.callee.body.body[1].argument;
nameFunction({
node: returnFn,
parent: path.parent,
parent: (path as NodePath<t.FunctionExpression>).parent,
scope: path.scope,
});
functionId = returnFn.id;
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-classes/package.json
Expand Up @@ -15,6 +15,7 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-compilation-targets": "workspace:^",
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-optimise-call-expression": "workspace:^",
Expand Down
26 changes: 19 additions & 7 deletions packages/babel-plugin-transform-classes/src/index.ts
@@ -1,4 +1,5 @@
import { declare } from "@babel/helper-plugin-utils";
import { isRequired } from "@babel/helper-compilation-targets";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
Expand Down Expand Up @@ -30,6 +31,10 @@ export default declare((api, options: Options) => {
"superIsCallableConstructor",
) ?? loose) as boolean;
const noClassCalls = (api.assumption("noClassCalls") ?? loose) as boolean;
const supportUnicodeId = !isRequired(
"transform-unicode-escapes",
api.targets(),
);

// todo: investigate traversal requeueing
const VISITED = new WeakSet();
Expand Down Expand Up @@ -59,7 +64,7 @@ export default declare((api, options: Options) => {
const { node } = path;
if (VISITED.has(node)) return;

const inferred = nameFunction(path);
const inferred = nameFunction(path, undefined, supportUnicodeId);
if (inferred && inferred !== node) {
path.replaceWith(inferred);
return;
Expand All @@ -68,12 +73,19 @@ export default declare((api, options: Options) => {
VISITED.add(node);

const [replacedPath] = path.replaceWith(
transformClass(path, state.file, builtinClasses, loose, {
setClassMethods,
constantSuper,
superIsCallableConstructor,
noClassCalls,
}),
transformClass(
path,
state.file,
builtinClasses,
loose,
{
setClassMethods,
constantSuper,
superIsCallableConstructor,
noClassCalls,
},
supportUnicodeId,
),
);

if (replacedPath.isCallExpression()) {
Expand Down
25 changes: 19 additions & 6 deletions packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -98,6 +98,7 @@ export default function transformClass(
builtinClasses: ReadonlySet<string>,
isLoose: boolean,
assumptions: ClassAssumptions,
supportUnicodeId: boolean,
) {
const classState: State = {
parent: undefined,
Expand Down Expand Up @@ -508,7 +509,14 @@ export default function transformClass(
if (node.kind === "method") {
// @ts-expect-error Fixme: we are passing a ClassMethod to nameFunction, but nameFunction
// does not seem to support it
fn = nameFunction({ id: key, node: node, scope });
fn =
nameFunction(
// @ts-expect-error Fixme: we are passing a ClassMethod to nameFunction, but nameFunction
// does not seem to support it
{ id: key, node: node, scope },
undefined,
supportUnicodeId,
) || fn;
}
} else {
// todo(flow->ts) find a way to avoid "key as t.StringLiteral" below which relies on this assignment
Expand Down Expand Up @@ -570,11 +578,16 @@ export default function transformClass(

const key = t.toComputedKey(node, node.key);
if (t.isStringLiteral(key)) {
func = nameFunction({
node: func,
id: key,
scope,
});
func =
nameFunction(
{
node: func,
id: key,
scope,
},
undefined,
supportUnicodeId,
) || func;
}

const expr = t.expressionStatement(
Expand Down
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 43",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

var _proto = o.prototype;

_proto.馉閲庡 = function () {};

return babelHelpers.createClass(o);
}();
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 44",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

var _proto = o.prototype;

_proto.馉閲庡 = function 馉閲庡() {};

return babelHelpers.createClass(o);
}();
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 43",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

babelHelpers.createClass(o, [{
key: "\uD842\uDFB7\u91CE\u5BB6",
value: function () {}
}]);
return o;
}();
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 44",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

babelHelpers.createClass(o, [{
key: "\uD842\uDFB7\u91CE\u5BB6",
value: function 馉閲庡() {}
}]);
return o;
}();
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 43",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

babelHelpers.createClass(o, [{
key: "\uD842\uDFB7\u91CE\u5BB6",
value: function () {}
}]);
return o;
}();
@@ -0,0 +1 @@
var o = class { 馉閲庡() {} };
@@ -0,0 +1,4 @@
{
"targets": "chrome 44",
"plugins": ["transform-classes"]
}
@@ -0,0 +1,13 @@
var o = /*#__PURE__*/function () {
"use strict";

function o() {
babelHelpers.classCallCheck(this, o);
}

babelHelpers.createClass(o, [{
key: "\uD842\uDFB7\u91CE\u5BB6",
value: function 馉閲庡() {}
}]);
return o;
}();
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -2315,6 +2315,7 @@ __metadata:
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-annotate-as-pure": "workspace:^"
"@babel/helper-compilation-targets": "workspace:^"
"@babel/helper-environment-visitor": "workspace:^"
"@babel/helper-function-name": "workspace:^"
"@babel/helper-optimise-call-expression": "workspace:^"
Expand Down

0 comments on commit b27a859

Please sign in to comment.