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 scope of computed method keys #12812
Merged
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
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
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-1/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,8 @@ | ||
let a = "outside"; | ||
|
||
const obj = { | ||
[a]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-1/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": ["./plugin"] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-1/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,8 @@ | ||
let z = "outside"; | ||
const obj = { | ||
[z]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
}; |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-1/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-2/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,8 @@ | ||
let a = "outside"; | ||
|
||
class C { | ||
[a]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-2/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": ["./plugin"] | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-2/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,9 @@ | ||
let z = "outside"; | ||
|
||
class C { | ||
[z]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-2/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/babel-traverse/test/fixtures/rename/method-computed-key-3/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,7 @@ | ||
let a = "outside"; | ||
|
||
const obj = { | ||
[a](a = "inside") { | ||
return a; | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-3/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": ["./plugin"] | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/babel-traverse/test/fixtures/rename/method-computed-key-3/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,7 @@ | ||
let z = "outside"; | ||
const obj = { | ||
[z](a = "inside") { | ||
return a; | ||
} | ||
|
||
}; |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-3/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/babel-traverse/test/fixtures/rename/method-computed-key-4/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,7 @@ | ||
let a = "outside"; | ||
|
||
class C { | ||
[a](a = "inside") { | ||
return a; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-4/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": ["./plugin"] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-4/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,8 @@ | ||
let z = "outside"; | ||
|
||
class C { | ||
[z](a = "inside") { | ||
return a; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-4/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-5/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,8 @@ | ||
let a = "outside"; | ||
|
||
const obj = { | ||
[(() => a)()]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-5/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": ["./plugin"] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-5/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,8 @@ | ||
let z = "outside"; | ||
const obj = { | ||
[(() => z)()]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
}; |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-5/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/babel-traverse/test/fixtures/rename/method-computed-key-6/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,8 @@ | ||
let a = "outside"; | ||
|
||
class C { | ||
[(() => a)()]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-6/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": ["./plugin"] | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-6/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,9 @@ | ||
let z = "outside"; | ||
|
||
class C { | ||
[(() => z)()]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-6/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/babel-traverse/test/fixtures/rename/method-computed-key-7/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,15 @@ | ||
let a = "outside"; | ||
|
||
const obj = { | ||
get [ | ||
{ | ||
get [a]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
}.outside | ||
]() { | ||
let a = "middle"; | ||
return a; | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-7/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": ["./plugin"] | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/babel-traverse/test/fixtures/rename/method-computed-key-7/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,14 @@ | ||
let z = "outside"; | ||
const obj = { | ||
get [{ | ||
get [z]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
}.outside]() { | ||
let a = "middle"; | ||
return a; | ||
} | ||
|
||
}; |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-7/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/babel-traverse/test/fixtures/rename/method-computed-key-8/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,15 @@ | ||
let a = "outside"; | ||
|
||
class C { | ||
static get [ | ||
class D { | ||
static get [a]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
}.outside | ||
]() { | ||
let a = "middle"; | ||
return a; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-traverse/test/fixtures/rename/method-computed-key-8/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": ["./plugin"] | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/babel-traverse/test/fixtures/rename/method-computed-key-8/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,15 @@ | ||
let z = "outside"; | ||
|
||
class C { | ||
static get [class D { | ||
static get [z]() { | ||
let a = "inside"; | ||
return a; | ||
} | ||
|
||
}.outside]() { | ||
let a = "middle"; | ||
return a; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-traverse/test/fixtures/rename/method-computed-key-8/plugin.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,9 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
path.scope.rename("a", "z"); | ||
} | ||
} | ||
}; | ||
}; |
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.
Q: Can we call
Scope#parent
recursively?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 agree that would be clearer code. But, if I've understood correctly what you're suggesting, I don't think it can work.
Problem is that the when testing if to skip a scope, we need to know not just where we are, but where we've come from (from inside the key or not?).
The computed key may not be the initial path. e.g.:
If we start at the scope of
f()
, at that point the parent is not a method, so the check is not triggered at this point. We proceed down to the parent scope (withthis._parent()
) and then discover it's a method. But by this point we don't know if we came from inside the key or not - we don't have the previous path - so we don't know whether to skip it or not. To fix this, the method check has to move into_parent()
, at which point_parent()
becomes as complicated as the original.Here's a couple of other versions. Do you prefer either of them?
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.
Thanks for a detailed explainer! I slightly prefer the second one but no strong opinion.