Skip to content

Conversation

@nathancahill
Copy link
Collaborator

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 the ref variable to immediately before the expression. See the tests for how code is now generated.

// 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') {
Copy link
Member

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)()

Copy link
Collaborator Author

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.

Copy link
Member

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 );
      }

@adrianheine
Copy link
Member

@nathancahill Do you still want to work on this? I would like to cut a release in the next days.

@nathancahill
Copy link
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.

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.

2 participants