-
Notifications
You must be signed in to change notification settings - Fork 67
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
Move var declaration to in-place for spread in expressions #95
base: master
Are you sure you want to change the base?
Conversation
// 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') { |
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.
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)()
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.
Good point. A better check here would be if the start of parent is also the start of the statement.
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 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 );
}
@nathancahill Do you still want to work on this? I would like to cut a release in the next days. |
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. |
When a spread is in a "naked" expression, the added
(ref
will become a function call of the previous statement, unless a;
is added before it. This fix moves the declaration of theref
variable to immediately before the expression. See the tests for how code is now generated.