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
nicolo-ribaudo
merged 5 commits into
babel:main
from
colinaaa:fix/shadowed-private-static-field
Aug 30, 2021
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80b4d84
fix: fix static private field shadowed by local variable
colinaaa d01e651
feat: rename local variable and add test cases
colinaaa 8de2f46
feat: add unshadow to privateIn visitor
colinaaa 5c366b3
test: add reference to shadowed variable
colinaaa 68decd2
refactor: apply suggested changes
colinaaa 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
15 changes: 15 additions & 0 deletions
15
.../babel-plugin-proposal-class-properties/test/fixtures/private-loose/static-shadow/exec.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,15 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
static method() { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return this.#x; | ||
} | ||
return func(); | ||
nicolo-ribaudo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
expect(Test.method()).toBe(1) |
13 changes: 13 additions & 0 deletions
13
...babel-plugin-proposal-class-properties/test/fixtures/private-loose/static-shadow/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 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
static method() { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return this.#x; | ||
} | ||
return func(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...l-plugin-proposal-class-properties/test/fixtures/private-loose/static-shadow/options.json
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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"] | ||
} |
18 changes: 18 additions & 0 deletions
18
...abel-plugin-proposal-class-properties/test/fixtures/private-loose/static-shadow/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,18 @@ | ||
class Test { | ||
static method() { | ||
const _Test2 = 2; | ||
|
||
const func = () => { | ||
const _Test = 3; | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Test, _x); | ||
}; | ||
|
||
return func(); | ||
} | ||
|
||
} | ||
|
||
var _x = { | ||
writable: true, | ||
value: 1 | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/babel-plugin-proposal-class-properties/test/fixtures/private/static-shadow/exec.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,15 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
static method() { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return this.#x; | ||
} | ||
return func(); | ||
} | ||
} | ||
|
||
expect(Test.method()).toBe(1) |
13 changes: 13 additions & 0 deletions
13
packages/babel-plugin-proposal-class-properties/test/fixtures/private/static-shadow/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 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
static method() { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return this.#x; | ||
} | ||
return func(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...s/babel-plugin-proposal-class-properties/test/fixtures/private/static-shadow/options.json
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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"] | ||
} |
18 changes: 18 additions & 0 deletions
18
...ages/babel-plugin-proposal-class-properties/test/fixtures/private/static-shadow/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,18 @@ | ||
class Test { | ||
static method() { | ||
const _Test2 = 2; | ||
|
||
const func = () => { | ||
const _Test = 3; | ||
return babelHelpers.classStaticPrivateFieldSpecGet(this, Test, _x); | ||
}; | ||
|
||
return func(); | ||
} | ||
|
||
} | ||
|
||
var _x = { | ||
writable: true, | ||
value: 1 | ||
}; |
17 changes: 17 additions & 0 deletions
17
...gin-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/exec.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,17 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} | ||
|
||
const t = new Test(); | ||
const t2 = new Test(); | ||
expect(t.method(Test)).toBe(true) |
13 changes: 13 additions & 0 deletions
13
...in-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/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 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...roposal-private-property-in-object/test/fixtures/private-loose/static-shadow/options.json
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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"] | ||
} |
18 changes: 18 additions & 0 deletions
18
...n-proposal-private-property-in-object/test/fixtures/private-loose/static-shadow/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,18 @@ | ||
class Test { | ||
method(other) { | ||
const _Test2 = 2; | ||
|
||
const func = () => { | ||
const _Test = 3; | ||
return other === Test; | ||
}; | ||
|
||
return func(); | ||
} | ||
|
||
} | ||
|
||
var _x = { | ||
writable: true, | ||
value: 1 | ||
}; |
17 changes: 17 additions & 0 deletions
17
...el-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/exec.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,17 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} | ||
|
||
const t = new Test(); | ||
const t2 = new Test(); | ||
expect(t.method(Test)).toBe(true) |
13 changes: 13 additions & 0 deletions
13
...l-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/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 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/options.json
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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"] | ||
} |
18 changes: 18 additions & 0 deletions
18
...-plugin-proposal-private-property-in-object/test/fixtures/private/static-shadow/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,18 @@ | ||
class Test { | ||
method(other) { | ||
const _Test2 = 2; | ||
|
||
const func = () => { | ||
const _Test = 3; | ||
return other === Test; | ||
}; | ||
|
||
return func(); | ||
} | ||
|
||
} | ||
|
||
var _x = { | ||
writable: true, | ||
value: 1 | ||
}; |
17 changes: 17 additions & 0 deletions
17
...-proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/exec.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,17 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} | ||
|
||
const t = new Test(); | ||
const t2 = new Test(); | ||
expect(t.method(Test)).toBe(true) |
13 changes: 13 additions & 0 deletions
13
...proposal-private-property-in-object/test/fixtures/to-native-fields/static-shadow/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 @@ | ||
class Test { | ||
|
||
static #x = 1 | ||
|
||
method(other) { | ||
const Test = 2; | ||
const func = () => { | ||
const Test = 3; | ||
return #x in other; | ||
} | ||
return func(); | ||
} | ||
} |
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.
The
if
branch is an inlined version ofScope#bindingIdentifierEquals
, guess we can just puthere?
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.
Sounds great! And all tests passed.
But I got a question: there are some cases that
scope.bindingIdentifier(name) === undefined
andinnerBinding !== undefined
which meansscope.bindingIdentifierEquals(name, innerBinding)
will always returnsfalse
. This will cause the while loop runs all the way alone to the top level scope and renaming all the variables.The renaming and looping here are useless, and maybe cause performance problem.
So maybe we change it to:
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.
If the
scope
is the parent scope of the class whereinnerBinding
is defined, thenscope.bindingIdentifier(classRef.name)
is surelyundefined
, unless defined otherwise. We could exit the loop after scope becomes the class scope.