Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the support of arguments in private get/set method #16307

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -1266,11 +1266,25 @@
(isGetter || isSetter) &&
!privateFieldsAsProperties
) {
const thisArg = prop.get("body").scope.generateUidIdentifier("this");
// eslint-disable-next-line @typescript-eslint/no-use-before-define
prop.traverse(thisContextVisitor, {
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));
}

Expand Down Expand Up @@ -1311,12 +1325,18 @@
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 (state.argumentsPath && path.node.name === "arguments") {
state.argumentsPath.push(path);
}
},
UnaryExpression(path) {
// Replace `delete this` with `true`
const { node } = path;
Expand Down Expand Up @@ -1351,7 +1371,7 @@
state.needsClassRef = true;
path.node.name = state.thisRef.name;
}
},

Check failure on line 1374 in packages/babel-helper-create-class-features-plugin/src/fields.ts

View workflow job for this annotation

GitHub Actions / Publish to local Verdaccio registry

Namespace '"/home/runner/work/babel/babel/packages/babel-types/src/index"' has no exported member 'NodePath'.

Check failure on line 1374 in packages/babel-helper-create-class-features-plugin/src/fields.ts

View workflow job for this annotation

GitHub Actions / Publish to local Verdaccio registry

Namespace '"/home/runner/work/babel/babel/packages/babel-types/src/index"' has no exported member 'NodePath'.

Check failure on line 1374 in packages/babel-helper-create-class-features-plugin/src/fields.ts

View workflow job for this annotation

GitHub Actions / Build Babel 8 Artifacts

Namespace '"/home/runner/work/babel/babel/packages/babel-types/src/index"' has no exported member 'NodePath'.

Check failure on line 1374 in packages/babel-helper-create-class-features-plugin/src/fields.ts

View workflow job for this annotation

GitHub Actions / Lint

Namespace '"/home/runner/work/babel/babel/packages/babel-types/src/index"' has no exported member 'NodePath'.
};

function replaceThisContext(
Expand Down
@@ -0,0 +1,33 @@
class Cl {
#privateField = "top secret string";

constructor() {
this.publicField = "not secret string";
}

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

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

publicGetPrivateField() {
return this.#privateFieldValue;
}

publicSetPrivateField(newValue) {
this.#privateFieldValue = newValue;
}
}

const cl = new Cl();

expect(cl.publicGetPrivateField()).toEqual("top secret string");

cl.publicSetPrivateField("new secret string");
expect(cl.publicGetPrivateField()).toEqual("new secret string");

@@ -0,0 +1,25 @@
class Cl {
#privateField = "top secret string";

constructor() {
this.publicField = "not secret string";
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use arguments somewhere here maybe, to show the outputs for getters too.

}

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

publicGetPrivateField() {
return this.#privateFieldValue;
}

publicSetPrivateField(newValue) {
this.#privateFieldValue = newValue;
}
}
@@ -0,0 +1,25 @@
var _privateField = /*#__PURE__*/new WeakMap();
var _Cl_brand = /*#__PURE__*/new WeakSet();
class Cl {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _Cl_brand);
babelHelpers.classPrivateFieldInitSpec(this, _privateField, "top secret string");
this.publicField = "not secret string";
}
publicGetPrivateField() {
return babelHelpers.classPrivateGetter(_Cl_brand, this, _get_privateFieldValue);
}
publicSetPrivateField(newValue) {
babelHelpers.classPrivateSetter(_Cl_brand, _set_privateFieldValue, this, newValue);
}
}
function _get_privateFieldValue(_this) {
var _arguments = [].slice.call(arguments, 1);
expect(_arguments.length).toBe(0);
return babelHelpers.classPrivateFieldGet2(_privateField, _this);
}
function _set_privateFieldValue(_this2, newValue) {
var _arguments2 = [].slice.call(arguments, 1);
expect(_arguments2.length).toBe(1);
babelHelpers.classPrivateFieldSet2(_privateField, _this2, newValue);
}