Skip to content
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

Add dynamic import #834

Closed
wants to merge 3 commits into from
Closed

Conversation

kesne
Copy link

@kesne kesne commented Jun 4, 2019

This adds dynamic import support to Acorn. I mostly just copied this out of my existing acorn plugin: https://github.com/kesne/acorn-dynamic-import.

Fixes #833, #832.

@@ -178,6 +178,12 @@ lp.parseStatement = function() {
return this.parseClass(true)

case tt._import:
if (this.options.ecmaVersion > 10 && this.lookAhead(1).type === tt.parenL) {
Copy link
Member

Choose a reason for hiding this comment

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

Any chance we could move this into parseExport and do away with the lookAhead (by simply consuming the import token and looking at the next token)?

Copy link
Author

Choose a reason for hiding this comment

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

That seems to make cases like var dynImport = import; work in acorn loose mode, which probably isn't desirable?

Copy link
Member

Choose a reason for hiding this comment

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

No, that's not what I mean—inside parseImport, after calling .next() for the import token, you'd first look for (, and parse a dynamic import only if you see it (which would change the signature of parseDynamicImport to require a start position and expect the first token to already be consumed).

@@ -0,0 +1,95 @@
// Tests for ECMAScript 2020 dynaimic import

Choose a reason for hiding this comment

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

Suggested change
// Tests for ECMAScript 2020 dynaimic import
// Tests for ECMAScript 2020 dynamic import


testFail("var dynImport = import; dynImport('http');", 'Unexpected token (1:22)', {
ecmaVersion: 11
});
Copy link
Contributor

@mysticatea mysticatea Jun 4, 2019

Choose a reason for hiding this comment

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

Would you add more fail tests?

  • import(); ... unexpected token ')'.
  • import(a, b); ... unexpected token ','.
  • import(source,); ... unexpected token ','.
  • var a = import(source); ... unexpected token 'import' if ecmaVersion < 11.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to consider these as errors and added test cases as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you!

@adrianheine
Copy link
Member

Thanks for the PR! Please also remove dynamic-import from unsupportedFeatures in bin/run_test262.js.

@kesne
Copy link
Author

kesne commented Jun 4, 2019

Updated to remove dynamic import from test 262 unsupported features.

@mysticatea
Copy link
Contributor

By the way, do you think about estree/estree#197?

@mercmobily
Copy link

Is this PR good to go, other than the conflict on test/run.js?

@marijnh
Copy link
Member

marijnh commented Jul 4, 2019

Yes, I think so. I've merged it as e7c7102

@marijnh marijnh closed this Jul 4, 2019
@mysticatea
Copy link
Contributor

mysticatea commented Jul 4, 2019

Wait.

ESTree may be going to change the AST of dynamic imports: estree/estree#198

@marijnh
Copy link
Member

marijnh commented Jul 4, 2019

Ugh, I just tagged 6.2.0 with this AST format. Oh well, we'll see what comes out of that discussion.

@TimothyGu
Copy link
Contributor

@marijnh I think it'd be a wise idea to in fact publish a 6.2.1 that removes the support in the mean time, seeing that no one is probably depending on that at the moment. If we let it sit for any more time, people could start using the current AST format, and that'd be basically impossible to correct.

@marijnh
Copy link
Member

marijnh commented Jul 4, 2019

That would still violate semver, I think—once a release is out there, releasing a patch version that removes features would be weird. We'll need a major version bump when we make ecmaVersion: 10 the default at some point, so I'm fine with doing the AST change at that point.

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