split-if-assignments should handle else if assignments#241
Merged
GoodForOneFare merged 3 commits intomasterfrom Nov 24, 2016
Merged
split-if-assignments should handle else if assignments#241GoodForOneFare merged 3 commits intomasterfrom
else if assignments#241GoodForOneFare merged 3 commits intomasterfrom
Conversation
lemonmade
requested changes
Nov 23, 2016
Member
lemonmade
left a comment
There was a problem hiding this comment.
Minor comments, otherwise LGTM 👍
| baz(); | ||
| } else { | ||
| qux(); | ||
| } |
Member
There was a problem hiding this comment.
Can you add a test with nested if else?
if (a = 1) {
if (b = 2) {
} else if (c = 3) {
}
} else if (d = 4) {
}| const sourceAST = j(source); | ||
|
|
||
| // Handle else-ifs in reverse order (otherwise initial transforms orphan later paths). | ||
| sourceAST.find(j.IfStatement, { |
Member
There was a problem hiding this comment.
.find on its own line, aligned with .paths()
| .forEach(({node}) => { | ||
| const elseIf = node.alternate; | ||
| const test = elseIf.test; | ||
| node.alternate = j.blockStatement([ |
Member
There was a problem hiding this comment.
I am a little rusty on this, but I seem to remember preferring using the path and using replace() instead of directly mutating the node.
| }); | ||
|
|
||
| // Hoist assignments out of the first `if` and into the block body. | ||
| return sourceAST.find(j.IfStatement, { |
TylerHorth
approved these changes
Nov 23, 2016
| bar(); | ||
| } else { | ||
| c = 3; | ||
|
|
Contributor
There was a problem hiding this comment.
Why are these newlines being added? Is this intentional?
Member
Author
There was a problem hiding this comment.
recast is adding them. I don't think there's much that we can do about that.
Member
Author
|
@lemonmade please take another look. I've cleaned up indentation, and used |
lemonmade
approved these changes
Nov 24, 2016
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Previously,
else if (foo = bar)would throw an exception because the transform tried to move the assignment to before theelse. That would've created an invalid AST.This updates the logic to rewrite
else if (foo = bar) {}toPlease review @lemonmade, @TylerHorth