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: fix static private field shadowed by local variable #13656

Merged
merged 5 commits into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -58,7 +58,7 @@ export function buildPrivateNamesMap(props: PropPath[]) {
export function buildPrivateNamesNodes(
privateNamesMap: PrivateNamesMap,
privateFieldsAsProperties: boolean,
state,
state: File,
) {
const initNodes: t.Statement[] = [];

Expand Down Expand Up @@ -165,6 +165,7 @@ interface PrivateNameState {
classRef: t.Identifier;
file: File;
noDocumentAll: boolean;
innerBinding?: t.Identifier;
}

const privateNameVisitor = privateNameVisitorFactory<
Expand Down Expand Up @@ -255,7 +256,7 @@ const privateNameHandlerSpec: Handler<PrivateNameState & Receiver> & Receiver =
},

get(member) {
const { classRef, privateNamesMap, file } = this;
const { classRef, privateNamesMap, file, innerBinding } = this;
const { name } = (member.node.property as t.PrivateName).id;
const {
id,
Expand All @@ -273,6 +274,13 @@ const privateNameHandlerSpec: Handler<PrivateNameState & Receiver> & Receiver =
? "classStaticPrivateMethodGet"
: "classStaticPrivateFieldSpecGet";

const binding = member.scope.getBinding(classRef.name);
if (innerBinding && binding && !(binding.identifier === innerBinding)) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
if (innerBinding && binding && !(binding.identifier === innerBinding)) {
if (innerBinding && binding && binding.identifier !== innerBinding) {

Also, binding should always be defined (because if there isn't a conflicting variable, it's innerBinding). It would be better to assert it, rather than checking it:

if (innerBinding) {
  if (!binding) throw new Error("Internal Babel error: binding should be defined");
  if (binding.identifier !== innerBinding) {
    // ...
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Infect, binding could be undefined.

e.g: for a ClassExpression, classRef is generated by path.scope.generateUidIdentifier("class"); and would be inserted after our transformation.

const cls = class Test {
  static #x = 1
  method() {
    const Test = 1;
    return this.#x;
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

Oh thanks, I didn't realize it

// the classRef has been shadowed
throw binding.path.buildCodeFrameError(
`Shadowing class ${classRef.name} with private property`,
);
Copy link
Member

Choose a reason for hiding this comment

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

Q: any idea how could we generate correct code?
Should we rename the local variable?

Yeah, I'd rename the local variable:

path.scope.rename(classRef.name);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

I renamed all the local variables that shadowing the classRef including those in parent scopes.

}
return t.callExpression(file.addHelper(helperName), [
this.receiver(member),
t.cloneNode(classRef),
Expand Down Expand Up @@ -463,8 +471,8 @@ export function transformPrivateNamesUsage(
ref: t.Identifier,
path: NodePath<t.Class>,
privateNamesMap: PrivateNamesMap,
{ privateFieldsAsProperties, noDocumentAll },
state,
{ privateFieldsAsProperties, noDocumentAll, innerBinding },
state: File,
) {
if (!privateNamesMap.size) return;

Expand All @@ -479,6 +487,7 @@ export function transformPrivateNamesUsage(
file: state,
...handler,
noDocumentAll,
innerBinding,
});
body.traverse(privateInVisitor, {
privateNamesMap,
Expand Down Expand Up @@ -770,7 +779,7 @@ const thisContextVisitor = traverse.visitors.merge([
]);

const innerReferencesVisitor = {
ReferencedIdentifier(path, state) {
ReferencedIdentifier(path: NodePath<t.Identifier>, state) {
if (
path.scope.bindingIdentifierEquals(path.node.name, state.innerBinding)
) {
Expand Down Expand Up @@ -831,7 +840,7 @@ export function buildFieldsInitNodes(
superRef: t.Expression | undefined,
props: PropPath[],
privateNamesMap: PrivateNamesMap,
state,
state: File,
setPublicClassFields: boolean,
privateFieldsAsProperties: boolean,
constantSuper: boolean,
Expand Down
@@ -1,4 +1,5 @@
import { types as t } from "@babel/core";
import type { File } from "@babel/core";
import type { NodePath } from "@babel/traverse";
import nameFunction from "@babel/helper-function-name";
import splitExportDeclaration from "@babel/helper-split-export-declaration";
Expand Down Expand Up @@ -92,7 +93,7 @@ export function createClassFeaturePlugin({
},

visitor: {
Class(path: NodePath<t.Class>, state) {
Class(path: NodePath<t.Class>, state: File) {
if (this.file.get(versionKey) !== version) return;

verifyUsedFeatures(path, this.file);
Expand Down Expand Up @@ -198,6 +199,7 @@ export function createClassFeaturePlugin({
{
privateFieldsAsProperties: privateFieldsAsProperties ?? loose,
noDocumentAll,
innerBinding,
},
state,
);
Expand Down
@@ -0,0 +1,9 @@
class Test {

static #x = 1

method() {
const Test = 1;
return this.#x;
}
}
@@ -0,0 +1,4 @@
{
"plugins": ["proposal-class-properties"],
"throws": "Shadowing class Test with private property"
}