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 remove hooks for Assignment/Conditional #4896
Conversation
const body = path.get("body"); | ||
body.remove(); | ||
|
||
assert.equal(generateCode(rootPath), "x = () => void 0;", "body should be replaced with undefined"); |
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.
In that case we could also replace void 0
with a simple blockStatement I think?
x = () => {};
Which would also be shorter.
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.
yes 👍
Current coverage is 89.38% (diff: 100%)@@ master #4896 diff @@
==========================================
Files 196 196
Lines 14022 14022
Methods 1460 1460
Messages 0 0
Branches 3263 3263
==========================================
+ Hits 12529 12534 +5
+ Misses 1493 1488 -5
Partials 0 0
|
I see few options here. When we remove a node and that makes for an invalid AST, we could either:
Since we're probably going with the 2nd one, the further options are to either:
The API such as The issues with replacing expression with Another example I can think of is removing an argument from the Thoughts? |
Throwing in places where a required Field went missing in parentNode is a better idea - tells the plugin author that you're not allowed to remove this - you can only replace it with something else, or remove the entire parent. I think babel shouldn't decide that it can remove the parent node when the plugin intended to remove the node. |
The more I think about the more it makes sense what boopathi said. Though we should still have a helper for supporting plugin authors, so we can check if it is safe to remove, and some way that the plugin knows what are valid replacement options. Not sure though how this should look like. I think this would also not be a breaking change as the plugin already throws in this cases. |
I close this for now, as I cannot come up with a solution right now. |
This ensures that if any subnode of
ConditionalExpression
s andright
ofAssignmentExpression
are removed they get replaced byundefined
(orvoid 0
).I'm not sure what should happen if the left-hand-side of an assignment gets removed.
If now someone removes the left-hand-side we probably need to turn it into
But I'm not sure. In certain cases, we can probably also remove the whole thing alltogether if there can be no side-effects:
//cc @boopathi @kangax