Skip to content

split-if-assignments should handle else if assignments#241

Merged
GoodForOneFare merged 3 commits intomasterfrom
split-if-else-if-assignments
Nov 24, 2016
Merged

split-if-assignments should handle else if assignments#241
GoodForOneFare merged 3 commits intomasterfrom
split-if-else-if-assignments

Conversation

@GoodForOneFare
Copy link
Member

Previously, else if (foo = bar) would throw an exception because the transform tried to move the assignment to before the else. That would've created an invalid AST.

This updates the logic to rewrite else if (foo = bar) {} to

else { 
  foo = bar; 
  if (foo) {

  }
}

Please review @lemonmade, @TylerHorth

Copy link
Member

@lemonmade lemonmade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, otherwise LGTM 👍

baz();
} else {
qux();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find on its own line, aligned with .paths()

.forEach(({node}) => {
const elseIf = node.alternate;
const test = elseIf.test;
node.alternate = j.blockStatement([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor

@TylerHorth TylerHorth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

bar();
} else {
c = 3;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these newlines being added? Is this intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recast is adding them. I don't think there's much that we can do about that.

@GoodForOneFare
Copy link
Member Author

@lemonmade please take another look. I've cleaned up indentation, and used path.replace instead of direct node assignment.

@GoodForOneFare GoodForOneFare merged commit a211d25 into master Nov 24, 2016
@GoodForOneFare GoodForOneFare deleted the split-if-else-if-assignments branch January 17, 2017 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants