Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Feb 27, 2024
1 parent 0167694 commit 0584b20
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 93 deletions.
81 changes: 33 additions & 48 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -1261,6 +1261,33 @@ function buildPrivateMethodDeclaration(
const isGetter = getId && !getterDeclared && params.length === 0;
const isSetter = setId && !setterDeclared && params.length > 0;

if (
(process.env.BABEL_8_BREAKING || newHelpers(file)) &&
(isGetter || isSetter) &&
!privateFieldsAsProperties
) {
const scope = prop.get("body").scope;
const thisArg = scope.generateUidIdentifier("this");
const state: ReplaceThisState = {
thisRef: thisArg,
argumentsPath: [],
};
// eslint-disable-next-line @typescript-eslint/no-use-before-define
prop.traverse(thisContextVisitor, state);
if (state.argumentsPath.length) {
const argumentsId = scope.generateUidIdentifier("arguments");
scope.push({
id: argumentsId,
init: template.expression.ast`[].slice.call(arguments, 1)`,
});
for (const path of state.argumentsPath) {
path.replaceWith(t.cloneNode(argumentsId));
}
}

params.unshift(t.cloneNode(thisArg));
}

let declId = methodId;

if (isGetter) {
Expand All @@ -1281,43 +1308,6 @@ function buildPrivateMethodDeclaration(
declId = id;
}

if (
(process.env.BABEL_8_BREAKING || newHelpers(file)) &&
(isGetter || isSetter) &&
!privateFieldsAsProperties
) {
const thisArg = prop.get("body").scope.generateUidIdentifier("this");
const state: ReplaceThisState = {
thisPaths: [],
};
// eslint-disable-next-line @typescript-eslint/no-use-before-define
prop.traverse(thisContextVisitor, state);
if (state.hasArguments) {
return t.variableDeclaration("var", [
t.variableDeclarator(
t.cloneNode(declId),
t.callExpression(file.addHelper("curryThis"), [
inheritPropComments(
t.functionExpression(
t.cloneNode(declId),
// @ts-expect-error params for ClassMethod has TSParameterProperty
params,
body,
generator,
async,
),
prop,
),
]),
),
]);
} else {
for (const path of state.thisPaths) {
path.replaceWith(t.cloneNode(thisArg));
}
params.unshift(t.cloneNode(thisArg));
}
}
return inheritPropComments(
t.functionDeclaration(
t.cloneNode(declId),
Expand All @@ -1332,20 +1322,19 @@ function buildPrivateMethodDeclaration(
}

type ReplaceThisState = {
thisRef?: t.Identifier;
thisPaths?: NodePath<t.ThisExpression>[];
hasArguments?: boolean;
thisRef: t.Identifier;
needsClassRef?: boolean;
innerBinding?: t.Identifier | null;
argumentsPath?: t.NodePath<t.Identifier>[];
};

type ReplaceInnerBindingReferenceState = ReplaceThisState;

const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
{
Identifier(path, state) {
if (path.node.name === "arguments") {
state.hasArguments = true;
if (state.argumentsPath && path.node.name === "arguments") {
state.argumentsPath.push(path);
}
},
UnaryExpression(path) {
Expand All @@ -1360,11 +1349,7 @@ const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
},
ThisExpression(path, state) {
state.needsClassRef = true;
if (state.thisPaths) {
state.thisPaths.push(path);
} else {
path.replaceWith(t.cloneNode(state.thisRef));
}
path.replaceWith(t.cloneNode(state.thisRef));
},
MetaProperty(path) {
const { node, scope } = path;
Expand Down Expand Up @@ -1491,7 +1476,7 @@ export function buildFieldsInitNodes(
const instanceNodes: t.ExpressionStatement[] = [];
let lastInstanceNodeReturnsThis = false;
// These nodes are pure and can be moved to the closest statement position
const pureStaticNodes: (t.FunctionDeclaration | t.VariableDeclaration)[] = [];
const pureStaticNodes: t.FunctionDeclaration[] = [];
let classBindingNode: t.ExpressionStatement | null = null;

const getSuperRef = t.isIdentifier(superRef)
Expand Down
Expand Up @@ -269,7 +269,7 @@ export function createClassFeaturePlugin({
staticNodes: t.Statement[],
instanceNodes: t.ExpressionStatement[],
lastInstanceNodeReturnsThis: boolean,
pureStaticNodes: (t.FunctionDeclaration | t.VariableDeclaration)[],
pureStaticNodes: t.FunctionDeclaration[],
classBindingNode: t.Statement | null,
wrapClass: (path: NodePath<t.Class>) => NodePath;

Expand Down
5 changes: 0 additions & 5 deletions packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -108,11 +108,6 @@ export default Object.freeze({
"7.0.0-beta.0",
'import setPrototypeOf from"setPrototypeOf";import isNativeReflectConstruct from"isNativeReflectConstruct";export default function _construct(t,e,r){if(isNativeReflectConstruct())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,e);var p=new(t.bind.apply(t,o));return r&&setPrototypeOf(p,r.prototype),p}',
),
// size: 103, gzip size: 103
curryThis: helper(
"7.24.0",
"export default function _curryThis(r){return function(n){return r.apply(n,[].slice.call(arguments,1))}}",
),
// size: 130, gzip size: 130
defineAccessor: helper(
"7.20.7",
Expand Down
7 changes: 0 additions & 7 deletions packages/babel-helpers/src/helpers/curryThis.ts

This file was deleted.

Expand Up @@ -11,7 +11,8 @@ class Cl {
}

set #privateFieldValue(newValue) {
this.#privateField = arguments[0];
expect(arguments.length).toBe(1);
this.#privateField = newValue;
}

publicGetPrivateField() {
Expand Down
Expand Up @@ -6,11 +6,13 @@ class Cl {
}

get #privateFieldValue() {
expect(arguments.length).toBe(0);
return this.#privateField;
}

set #privateFieldValue(newValue) {
this.#privateField = arguments[0];
expect(arguments.length).toBe(1);
this.#privateField = newValue;
}

publicGetPrivateField() {
Expand Down
Expand Up @@ -14,8 +14,12 @@ class Cl {
}
}
function _get_privateFieldValue(_this) {
var _arguments = [].slice.call(arguments, 1);
expect(_arguments.length).toBe(0);
return babelHelpers.classPrivateFieldGet2(_privateField, _this);
}
var _set_privateFieldValue = babelHelpers.curryThis(function _set_privateFieldValue(newValue) {
babelHelpers.classPrivateFieldSet2(_privateField, this, arguments[0]);
});
function _set_privateFieldValue(_this2, newValue) {
var _arguments2 = [].slice.call(arguments, 1);
expect(_arguments2.length).toBe(1);
babelHelpers.classPrivateFieldSet2(_privateField, _this2, newValue);
}
9 changes: 0 additions & 9 deletions packages/babel-runtime-corejs2/package.json
Expand Up @@ -189,15 +189,6 @@
"./helpers/construct.js"
],
"./helpers/esm/construct": "./helpers/esm/construct.js",
"./helpers/curryThis": [
{
"node": "./helpers/curryThis.js",
"import": "./helpers/esm/curryThis.js",
"default": "./helpers/curryThis.js"
},
"./helpers/curryThis.js"
],
"./helpers/esm/curryThis": "./helpers/esm/curryThis.js",
"./helpers/defineAccessor": [
{
"node": "./helpers/defineAccessor.js",
Expand Down
9 changes: 0 additions & 9 deletions packages/babel-runtime-corejs3/package.json
Expand Up @@ -188,15 +188,6 @@
"./helpers/construct.js"
],
"./helpers/esm/construct": "./helpers/esm/construct.js",
"./helpers/curryThis": [
{
"node": "./helpers/curryThis.js",
"import": "./helpers/esm/curryThis.js",
"default": "./helpers/curryThis.js"
},
"./helpers/curryThis.js"
],
"./helpers/esm/curryThis": "./helpers/esm/curryThis.js",
"./helpers/defineAccessor": [
{
"node": "./helpers/defineAccessor.js",
Expand Down
9 changes: 0 additions & 9 deletions packages/babel-runtime/package.json
Expand Up @@ -188,15 +188,6 @@
"./helpers/construct.js"
],
"./helpers/esm/construct": "./helpers/esm/construct.js",
"./helpers/curryThis": [
{
"node": "./helpers/curryThis.js",
"import": "./helpers/esm/curryThis.js",
"default": "./helpers/curryThis.js"
},
"./helpers/curryThis.js"
],
"./helpers/esm/curryThis": "./helpers/esm/curryThis.js",
"./helpers/defineAccessor": [
{
"node": "./helpers/defineAccessor.js",
Expand Down

0 comments on commit 0584b20

Please sign in to comment.