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: new.target
with shadowed class name
#14611
Merged
nicolo-ribaudo
merged 4 commits into
babel:main
from
liuxingbaoyu:fix-shadowed-new-target
Jun 10, 2022
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,19 @@ export default declare(api => { | |
} | ||
if (!node.id) { | ||
node.id = scope.generateUidIdentifier("target"); | ||
} else { | ||
// packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow | ||
let scope = path.scope; | ||
const name = node.id.name; | ||
while (scope !== func.parentPath.scope) { | ||
if ( | ||
scope.hasOwnBinding(name) && | ||
!scope.bindingIdentifierEquals(name, node.id) | ||
) { | ||
scope.rename(name); | ||
} | ||
scope = scope.parent; | ||
} | ||
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. Nit: they can be moved after the |
||
} | ||
|
||
const constructor = t.memberExpression( | ||
|
13 changes: 13 additions & 0 deletions
13
.../babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/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,13 @@ | ||
function Foo() { | ||
function Foo() { | ||
var Foo = new.target; | ||
} | ||
|
||
Foo.prototype.test = function() { | ||
var Foo = new.target; | ||
}; | ||
} | ||
|
||
var Bar = function() { | ||
var Bar = new.target; | ||
}; |
13 changes: 13 additions & 0 deletions
13
...babel-plugin-transform-new-target/test/fixtures/general/function-duplicate-name/output.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,13 @@ | ||
function Foo() { | ||
function Foo() { | ||
var _Foo = this instanceof Foo ? this.constructor : void 0; | ||
} | ||
|
||
Foo.prototype.test = function _target() { | ||
var Foo = this instanceof _target ? this.constructor : void 0; | ||
}; | ||
} | ||
|
||
var Bar = function _target2() { | ||
var Bar = this instanceof _target2 ? this.constructor : void 0; | ||
}; |
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.
We could actually move this to a
Scope#unshadow(name, targetAncestorScope)
method, and use it here likeIt will only work when people update their
@babel/traverse
(usually by updating their@babel/core
), but it's not a breaking change because when using older versionsnew.target
will just continue to be broken.Then, we can use
scope.unshadow()
inbabel-helper-create-class-features-plugin
starting from Babel 8 (or better, now with a fallback to the inlineunshadow()
function when!process.env.BABEL_8_BREAKING
).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.
I feel a little unnecessary.
Because
unshadow
is only a few lines of code and may be different in different scenarios.Another major reason is that our current Scope is a bit messy, eg binding is not always reliable.
Maybe we can do it in the future.
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.
Oh ok fair enough, we can refactor if needed.