-
Notifications
You must be signed in to change notification settings - Fork 849
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
Add dynamic import #834
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Tests for ECMAScript 2020 dynamic import | ||
|
||
if (typeof exports != 'undefined') { | ||
var test = require('./driver.js').test; | ||
var testFail = require('./driver.js').testFail; | ||
} | ||
|
||
test( | ||
"import('dynamicImport.js')", | ||
{ | ||
type: 'Program', | ||
start: 0, | ||
end: 26, | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
start: 0, | ||
end: 26, | ||
expression: { | ||
type: 'CallExpression', | ||
start: 0, | ||
end: 26, | ||
callee: { type: 'Import', start: 0, end: 6 }, | ||
arguments: [ | ||
{ | ||
type: 'Literal', | ||
start: 7, | ||
end: 25, | ||
value: 'dynamicImport.js', | ||
raw: "'dynamicImport.js'" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
sourceType: 'script' | ||
}, | ||
{ ecmaVersion: 11 } | ||
); | ||
|
||
test( | ||
"function* a() { yield import('http'); }", | ||
{ | ||
type: 'Program', | ||
start: 0, | ||
end: 39, | ||
body: [ | ||
{ | ||
type: 'FunctionDeclaration', | ||
start: 0, | ||
end: 39, | ||
id: { type: 'Identifier', start: 10, end: 11, name: 'a' }, | ||
expression: false, | ||
generator: true, | ||
async: false, | ||
params: [], | ||
body: { | ||
type: 'BlockStatement', | ||
start: 14, | ||
end: 39, | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
start: 16, | ||
end: 37, | ||
expression: { | ||
type: 'YieldExpression', | ||
start: 16, | ||
end: 36, | ||
delegate: false, | ||
argument: { | ||
type: 'CallExpression', | ||
start: 22, | ||
end: 36, | ||
callee: { type: 'Import', start: 22, end: 28 }, | ||
arguments: [{ type: 'Literal', start: 29, end: 35, value: 'http', raw: "'http'" }] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
sourceType: 'script' | ||
}, | ||
{ ecmaVersion: 11 } | ||
); | ||
|
||
testFail('function failsParse() { return import.then(); }', 'Unexpected token (1:37)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("var dynImport = import; dynImport('http');", 'Unexpected token (1:22)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("import('test.js')", 'Unexpected token (1:6)', { | ||
ecmaVersion: 10, | ||
loose: false, | ||
sourceType: 'module' | ||
}); | ||
|
||
testFail("import()", 'import() requires exactly one argument (1:0)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("import(a, b)", 'import() requires exactly one argument (1:0)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("import(...[a])", '... is not allowed in import() (1:7)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("import(source,)", 'Unexpected token (1:14)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
|
||
testFail("new import(source)", 'Cannot use new with import(...) (1:4)', { | ||
ecmaVersion: 11, | ||
loose: false | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you add more fail tests?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to consider these as errors and added test cases as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
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.
Any chance we could move this into
parseExport
and do away with thelookAhead
(by simply consuming theimport
token and looking at the next token)?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.
That seems to make cases like
var dynImport = import;
work in acorn loose mode, which probably isn't desirable?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.
No, that's not what I mean—inside
parseImport
, after calling.next()
for theimport
token, you'd first look for(
, and parse a dynamic import only if you see it (which would change the signature ofparseDynamicImport
to require a start position and expect the first token to already be consumed).