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 nested classes reference private fields #11405
Merged
nicolo-ribaudo
merged 5 commits into
babel:master
from
jridgewell:nested-private-redeclare
Apr 14, 2020
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
808445b
Fix nested classes reference private fields
jridgewell 995a6be
Process only visible private fields when redeclaring
jridgewell d450792
Comments
jridgewell 27a09f1
Skip class traversal if there are no private fields
jridgewell 3a673dc
Handle redeclared private field in computed key
jridgewell File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ export { FEATURES, injectInitialization }; | |
const version = pkg.version.split(".").reduce((v, x) => v * 1e5 + +x, 0); | ||
const versionKey = "@babel/plugin-class-features/version"; | ||
|
||
const privateNamesStack = [new Map()]; | ||
|
||
export function createClassFeaturePlugin({ | ||
name, | ||
feature, | ||
|
@@ -49,141 +51,158 @@ export function createClassFeaturePlugin({ | |
}, | ||
|
||
visitor: { | ||
Class(path, state) { | ||
if (this.file.get(versionKey) !== version) return; | ||
Class: { | ||
enter(path, state) { | ||
if (this.file.get(versionKey) !== version) return; | ||
|
||
verifyUsedFeatures(path, this.file); | ||
|
||
const loose = isLoose(this.file, feature); | ||
verifyUsedFeatures(path, this.file); | ||
|
||
let constructor; | ||
let isDecorated = hasOwnDecorators(path.node); | ||
const props = []; | ||
const elements = []; | ||
const computedPaths = []; | ||
const privateNames = new Set(); | ||
const body = path.get("body"); | ||
const loose = isLoose(this.file, feature); | ||
const privateNamesMap = new Map( | ||
privateNamesStack[privateNamesStack.length - 1], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since nodes can be re-queued and skipped, it's not guaranteed that they are visited in-order. Instead of a stack, I think we could use a |
||
); | ||
privateNamesStack.push(privateNamesMap); | ||
|
||
for (const path of body.get("body")) { | ||
verifyUsedFeatures(path, this.file); | ||
let constructor; | ||
let isDecorated = hasOwnDecorators(path.node); | ||
const props = []; | ||
const elements = []; | ||
const computedPaths = []; | ||
const privateNames = new Set(); | ||
const body = path.get("body"); | ||
|
||
if (path.node.computed) { | ||
computedPaths.push(path); | ||
} | ||
for (const path of body.get("body")) { | ||
verifyUsedFeatures(path, this.file); | ||
|
||
if (path.isPrivate()) { | ||
const { name } = path.node.key.id; | ||
const getName = `get ${name}`; | ||
const setName = `set ${name}`; | ||
|
||
if (path.node.kind === "get") { | ||
if ( | ||
privateNames.has(getName) || | ||
(privateNames.has(name) && !privateNames.has(setName)) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
} | ||
if (path.node.computed) { | ||
computedPaths.push(path); | ||
} | ||
|
||
privateNames.add(getName).add(name); | ||
} else if (path.node.kind === "set") { | ||
if ( | ||
privateNames.has(setName) || | ||
(privateNames.has(name) && !privateNames.has(getName)) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
if (path.isPrivate()) { | ||
const { name } = path.node.key.id; | ||
const getName = `get ${name}`; | ||
const setName = `set ${name}`; | ||
|
||
if (path.node.kind === "get") { | ||
if ( | ||
privateNames.has(getName) || | ||
(privateNames.has(name) && !privateNames.has(setName)) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
} | ||
|
||
privateNames.add(getName).add(name); | ||
} else if (path.node.kind === "set") { | ||
if ( | ||
privateNames.has(setName) || | ||
(privateNames.has(name) && !privateNames.has(getName)) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
} | ||
|
||
privateNames.add(setName).add(name); | ||
} else { | ||
if ( | ||
(privateNames.has(name) && | ||
!privateNames.has(getName) && | ||
!privateNames.has(setName)) || | ||
(privateNames.has(name) && | ||
(privateNames.has(getName) || privateNames.has(setName))) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
} | ||
|
||
privateNames.add(name); | ||
} | ||
} | ||
|
||
privateNames.add(setName).add(name); | ||
if (path.isClassMethod({ kind: "constructor" })) { | ||
constructor = path; | ||
} else { | ||
if ( | ||
(privateNames.has(name) && | ||
!privateNames.has(getName) && | ||
!privateNames.has(setName)) || | ||
(privateNames.has(name) && | ||
(privateNames.has(getName) || privateNames.has(setName))) | ||
) { | ||
throw path.buildCodeFrameError("Duplicate private field"); | ||
elements.push(path); | ||
if (path.isProperty() || path.isPrivate()) { | ||
props.push(path); | ||
} | ||
|
||
privateNames.add(name); | ||
} | ||
|
||
if (!isDecorated) isDecorated = hasOwnDecorators(path.node); | ||
} | ||
|
||
if (path.isClassMethod({ kind: "constructor" })) { | ||
constructor = path; | ||
if (!props.length && !isDecorated) return; | ||
|
||
let ref; | ||
|
||
if (path.isClassExpression() || !path.node.id) { | ||
nameFunction(path); | ||
ref = path.scope.generateUidIdentifier("class"); | ||
} else { | ||
elements.push(path); | ||
if (path.isProperty() || path.isPrivate()) { | ||
props.push(path); | ||
} | ||
ref = path.node.id; | ||
} | ||
|
||
if (!isDecorated) isDecorated = hasOwnDecorators(path.node); | ||
} | ||
// NODE: These three functions don't support decorators yet, | ||
// but verifyUsedFeatures throws if there are both | ||
// decorators and private fields. | ||
const depth = privateNamesStack.length; | ||
buildPrivateNamesMap(privateNamesMap, props, depth); | ||
const privateNamesNodes = buildPrivateNamesNodes( | ||
privateNamesMap, | ||
loose, | ||
state, | ||
depth, | ||
); | ||
|
||
if (!props.length && !isDecorated) return; | ||
transformPrivateNamesUsage(ref, path, privateNamesMap, loose, state); | ||
|
||
let ref; | ||
let keysNodes, staticNodes, instanceNodes, wrapClass; | ||
|
||
if (path.isClassExpression() || !path.node.id) { | ||
nameFunction(path); | ||
ref = path.scope.generateUidIdentifier("class"); | ||
} else { | ||
ref = path.node.id; | ||
} | ||
if (isDecorated) { | ||
staticNodes = keysNodes = []; | ||
({ instanceNodes, wrapClass } = buildDecoratedClass( | ||
ref, | ||
path, | ||
elements, | ||
this.file, | ||
)); | ||
} else { | ||
keysNodes = extractComputedKeys( | ||
ref, | ||
path, | ||
computedPaths, | ||
this.file, | ||
); | ||
({ staticNodes, instanceNodes, wrapClass } = buildFieldsInitNodes( | ||
ref, | ||
path.node.superClass, | ||
props, | ||
privateNamesMap, | ||
state, | ||
loose, | ||
)); | ||
} | ||
|
||
// NODE: These three functions don't support decorators yet, | ||
// but verifyUsedFeatures throws if there are both | ||
// decorators and private fields. | ||
const privateNamesMap = buildPrivateNamesMap(props); | ||
const privateNamesNodes = buildPrivateNamesNodes( | ||
privateNamesMap, | ||
loose, | ||
state, | ||
); | ||
|
||
transformPrivateNamesUsage(ref, path, privateNamesMap, loose, state); | ||
|
||
let keysNodes, staticNodes, instanceNodes, wrapClass; | ||
|
||
if (isDecorated) { | ||
staticNodes = keysNodes = []; | ||
({ instanceNodes, wrapClass } = buildDecoratedClass( | ||
ref, | ||
path, | ||
elements, | ||
this.file, | ||
)); | ||
} else { | ||
keysNodes = extractComputedKeys(ref, path, computedPaths, this.file); | ||
({ staticNodes, instanceNodes, wrapClass } = buildFieldsInitNodes( | ||
ref, | ||
path.node.superClass, | ||
props, | ||
privateNamesMap, | ||
state, | ||
loose, | ||
)); | ||
} | ||
if (instanceNodes.length > 0) { | ||
injectInitialization( | ||
path, | ||
constructor, | ||
instanceNodes, | ||
(referenceVisitor, state) => { | ||
if (isDecorated) return; | ||
for (const prop of props) { | ||
if (prop.node.static) continue; | ||
prop.traverse(referenceVisitor, state); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
if (instanceNodes.length > 0) { | ||
injectInitialization( | ||
path, | ||
constructor, | ||
instanceNodes, | ||
(referenceVisitor, state) => { | ||
if (isDecorated) return; | ||
for (const prop of props) { | ||
if (prop.node.static) continue; | ||
prop.traverse(referenceVisitor, state); | ||
} | ||
}, | ||
); | ||
} | ||
path = wrapClass(path); | ||
path.insertBefore(keysNodes); | ||
path.insertAfter([...privateNamesNodes, ...staticNodes]); | ||
}, | ||
|
||
path = wrapClass(path); | ||
path.insertBefore(keysNodes); | ||
path.insertAfter([...privateNamesNodes, ...staticNodes]); | ||
exit() { | ||
privateNamesStack.pop(); | ||
}, | ||
}, | ||
|
||
PrivateName(path) { | ||
|
18 changes: 18 additions & 0 deletions
18
...posal-class-properties/test/fixtures/private-loose/nested-class-other-redeclared/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Foo { | ||
#foo = 1; | ||
#bar = 1; | ||
|
||
test() { | ||
class Nested { | ||
#bar = 2; | ||
|
||
test() { | ||
this.#foo; | ||
this.#bar; | ||
} | ||
} | ||
|
||
this.#foo; | ||
this.#bar; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using a
WeakSet
, please put this in thecreateClassFeaturePluign
function so that when the plugin finishes running it can be garbage-collected, even if the AST was still retained.