Move var declaration to in-place for spread in expressions#95
Open
nathancahill wants to merge 1 commit intobublejs:masterfrom
Open
Move var declaration to in-place for spread in expressions#95nathancahill wants to merge 1 commit intobublejs:masterfrom
nathancahill wants to merge 1 commit intobublejs:masterfrom
Conversation
adrianheine
requested changes
Jan 10, 2018
| // begining with a parenthesis. Other types are guarded by | ||
| // an expression before the parenthesis so the declaration | ||
| // is created at the top of the scope. | ||
| if (this.parent.type === 'ExpressionStatement') { |
Member
There was a problem hiding this comment.
This check is not enough if the CallExpression is not the expression of the ExpressionStatement, like this:
const foo = { bar: [] }
const baz = true ? [1,2,3] : []
foo.bar.push(...baz)()
Collaborator
Author
There was a problem hiding this comment.
Good point. A better check here would be if the start of parent is also the start of the statement.
Member
There was a problem hiding this comment.
I tried the following a few days ago:
} else {
context = this.findScope(true).createDeclaration('ref');
const callExpression = this.callee.object;
- code.prependRight(callExpression.start, `(${context} = `);
+ const statement = this.findNearest(/Statement/);
+ code.prependRight(callExpression.start, (statement.start === callExpression.start ? ';' : '') + `(${context} = `);
code.appendLeft(callExpression.end, `)`);
}That broke the following test, though:
function foo (x) {
if ( x )
return ref => (bar || baz).Test( ref, ...arguments );
}
Member
|
@nathancahill Do you still want to work on this? I would like to cut a release in the next days. |
Collaborator
Author
|
I'll take another swing at it this afternoon. I think your approach with a special case for statements without a body (like arrow functions) will work. |
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.
When a spread is in a "naked" expression, the added
(refwill become a function call of the previous statement, unless a;is added before it. This fix moves the declaration of therefvariable to immediately before the expression. See the tests for how code is now generated.