Skip to content

Conversation

mysticatea
Copy link
Contributor

@mysticatea mysticatea commented Jul 13, 2019

This PR changes the AST of dynamic imports as following estree/estree#198. Please don't merge this PR til estree/estree#198 was merged. (estree/estree#198 is waiting for @adrianheine's response.)

This PR adds the second parameter to parser.parseExprAtom() method to make the following behavior:

  • new import(s) is a syntax error.
  • new (import(s)) is not a syntax error. (though it's a runtime type error.)
  • new import.meta(s) is not a syntax error in the future. (though it will be a runtime type error.)

Also, this PR adds some test cases:

  • valid:
    • import(a = 'dynamicImport.js')
    • new (import(s))
    • import((s,t))
  • invalid
    • (import)(s)

fixes #833, fixes #832

@mysticatea
Copy link
Contributor Author

@marijnh estree/estree#198 has been merged, so this PR is ready. Would you review this PR?

Copy link
Member

@marijnh marijnh left a comment

Choose a reason for hiding this comment

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

Thanks. I've marked some things where I'd like further explanation.

this.next() // skip `(`

// Parse node.source.
if (this.type === tt.ellipsis) {
Copy link
Member

Choose a reason for hiding this comment

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

Why are the explicit checks (the ellipsis and also the comma below) for things that would raise an error anyway? I'd prefer the terse variant that just allows the callee methods to raise the errors.

Copy link
Contributor Author

@mysticatea mysticatea Aug 12, 2019

Choose a reason for hiding this comment

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

This is the same behavior as before this PR. I will remove it.

this.raise(node.callee.start, "Cannot use new with import(...)")
}
if (this.eat(tt.parenL)) node.arguments = this.parseExprList(tt.parenR, this.options.ecmaVersion >= 8 && node.callee.type !== "Import", false)
node.callee = this.parseSubscripts(this.parseExprAtom(null, true), startPos, startLoc, true)
Copy link
Member

Choose a reason for hiding this comment

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

What is the advantage of adding a new argument to parseExprAtom over the way the check worked before?

Copy link
Contributor Author

@mysticatea mysticatea Aug 12, 2019

Choose a reason for hiding this comment

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

Because

  1. new (import(s)) is the valid syntax.
  2. new import.meta is the valid syntax, in the future. If acorn throws the parse error there, we make import.meta plugin relatively hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm. I will replace the new argument by another way.

@mysticatea
Copy link
Contributor Author

I have updated this PR.

// Parse node.source.
node.source = this.parseMaybeAssign()

// Verify ending.
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adjusting the ellipses thing. Any reason the 8 lines below can't simply be this.expect(tt.parenR)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to show a detailed message for trailing commas because import(s) is similar to call expressions, but doesn't support trailing commas. I.e., it handles import(s,) as special, but not import(a,b) and import(s!.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have moved the logic to the errored code path.

@marijnh marijnh merged commit 091933c into acornjs:master Aug 12, 2019
@mikesherov
Copy link

👍

@marijnh
Copy link
Member

marijnh commented Aug 12, 2019

Great, merged!

@mysticatea mysticatea deleted the follow-estree-update branch August 12, 2019 16:49
@papandreou
Copy link

Thanks a lot for the fast action! 🐎

Any chance of a new release with this change?

@marijnh
Copy link
Member

marijnh commented Aug 13, 2019

I've tagged 7.0.0 with this AST format change and ecmaVersion defaulting to 10.

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.

Please support dynamic imports and BigInt (Stage 4) Dynamic imports in mainling Acorn?
4 participants