-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Remove unused catch binding #6048
Conversation
Awesome (regarding where this goes, making the PR wherever is fine), I was almost thinking maybe each proposal should include the codemod version of itself too, but then I guess it would be hard to use since you need a cli... So maybe it would be better (later) as part of another tool like lebab/babel-update. |
c6dba28
to
7663e57
Compare
!path.scope.getOwnBinding(path.node.param.name).referenced | ||
) { | ||
const paramPath = path.get("param"); | ||
paramPath.remove(); |
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.
So there's one more test case we need to fix:
try {
} catch (e) {
e = new TypeError('explain');
}
That's awesome, thanks. I think we should also expose some sort of standalone executable. What do you think about that? |
93d2cc9
to
4902b39
Compare
f0de374
to
2b5b252
Compare
e64ebab
to
4e0ec2c
Compare
try { | ||
throw 0; | ||
} catch (e) { | ||
let e = new TypeError('New variable declaration, initialization, and assignment; the catch binding is not being referenced and can be removed.'); |
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.
This is actually a runtime error.
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.
Yeah. This is fixed in #5980 . and that let e
will throw an error now. Can you rebase your branch to 7.0
?
CatchClause(path) { | ||
const binding = path.scope.getOwnBinding(path.node.param.name); | ||
if ( | ||
binding.constantViolations.filter(el => { |
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.
Why are we filtering?
if ( | ||
path.node.param && | ||
!binding.referenced && | ||
path.scope.hasBinding(path.node.param.name) |
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 think this is not required here. binding
is already defined, and path.scope.hasBinding
is going to return true
always. Did you face any issues without this check?
|
||
visitor: { | ||
CatchClause(path) { | ||
const binding = path.scope.getOwnBinding(path.node.param.name); |
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 binding
can be null.
If the actual.js
is
try {} catch {}
There is no param, and it'll throw when using binding.something
. Can you also add a test case where the catch param is not present.
4e0ec2c
to
a431d15
Compare
a431d15
to
87a6fbb
Compare
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/4865/ |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/4692/ |
if (path.node.param === null) { | ||
return; | ||
} | ||
const binding = path.scope.getOwnBinding(path.node.param.name); |
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.
param
can be a Pattern
, too, so we an only match if it's an Identifier
.
} | ||
const binding = path.scope.getOwnBinding(path.node.param.name); | ||
if ( | ||
binding.constantViolations.filter(el => { |
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.
Do we really need to filter at all?
) { | ||
return; | ||
} | ||
if (path.node.param && !binding.referenced) { |
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.
path.node.param
has already been checked above.
4ac05c5
to
b5ea732
Compare
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.
Getting very close.
if (path.node.param === null) { | ||
return; | ||
} | ||
if (t.isObjectPattern(path.node.param)) { |
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 it's a pattern, we can just skip this as too much work.
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.
So, I had previously checked if the param was an identifier and if not exited early--is this what you mean, simply exit early if param not an identifier? (Also: thank you for your code reviews!)
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.
Yup. We could check all the binding identifiers inside a pattern, but that feels like too much work since the usual pattern is just try {} catch(e) {}
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.
OK, done. Happy to take on any more advice :-)
b5ea732
to
6d37b4a
Compare
…Error (not passing)
…variable declaration
… now passing Took out the pass in visitor when param not an Identifier, wrote case to handle when param isObjectPattern, and wrote failing tests for when param isArrayPattern
6d37b4a
to
fd5dc18
Compare
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.
code itself looks good, just was thinking about how we want to publish this/users to consume - we can always change it after merge too. Not sure it should be a part of the packages/* - maybe we should move to a codemods/ folder? Ideally, users would just use a npx command to use it, and ultimately lebab would be the tool to do so?
Agree on moving this into a separate folder. |
Not sure on the name of the folder but |
Nice work @MarckK, going to rename the folder! |
codemod to remove unused catch binding