-
-
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
Fix issue semi-colon gets inserted unnecessarily #5749
Conversation
Codecov Report
@@ Coverage Diff @@
## 7.0 #5749 +/- ##
==========================================
- Coverage 84.63% 84.62% -0.01%
==========================================
Files 282 282
Lines 9861 9862 +1
Branches 2766 2766
==========================================
Hits 8346 8346
Misses 997 997
- Partials 518 519 +1
Continue to review full report at Codecov.
|
@@ -126,7 +126,10 @@ export function insertAfter(nodes) { | |||
if (Array.isArray(this.container)) { | |||
return this._containerInsertAfter(nodes); | |||
} else if (this.isStatementOrBlock()) { | |||
if (this.node) nodes.unshift(this.node); | |||
// Unshift current node if it's not an empty expression | |||
if (this.node && (!this.isExpressionStatement() || this.node.expression != null)) { |
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.
Is it better to do ^ or just check if it's an not an EmptyExpression
?
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 can't find EmptyExpression
or isEmptyExpression
in the codebase. Or did you mean we should create a new method isEmptyExpression
to wrap the above? Like:
if (this.node && !this.isEmptyExpression()) {...}
...
isEmptyExpression() => this.isExpressionStatement() && this.node.expression == null
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.
Or you probably meant EmptyStatement
:). Let me try that.
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.
oh sorry I thought it was "EmptyStatement"
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 just tried, it doesn't work.
In this particular case, this.node
is of type ExpressionStatement
. this.isEmptyStatement()
returns false
because ExpressionStatement
isn't EmptyStatement
and they aren't aliases of one another.
I wondered why this.node
wasn't EmptyStatement
in the first place and dug in a bit more. Turns out this line https://github.com/babel/babel/blob/7.0/packages/babel-traverse/src/path/replacement.js#L50 sets this.container['expression'] = null
and this.container
is this.node
in the piece of code in question.
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.
And do we want to make the same change in insertBefore?
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 thought so too, but couldn't come up with a test for that case. Basically we need something that results in:
{
code
code
; <-- empty statement here
}
Without a test/bug that exposes the above behavior, there's no guarantee such situation is possible. Currently, the behavior is caused by that line to this.container['expression'] = null
, which is only invoked before insertAfter
. I haven't seen anything line that before insertBefore
. That said, I could definitely make that change to insertBefore
for the extra caution. Let me know.
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.
Ah ok I see. I guess this.container['expression'] = null
is rather a weird way to do things but we can do this
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.
lgtm was just curious about how we should do the check
Thanks for looking into this and doing a through investigation! |
Original:
Transpiled version:
The problem is
insertAfter
could push an empty expression statement, resulting in the semi-colon. This change checks to make sure the expression statement is not empty before pushing.