Skip to content

Commit 6bfcaed

Browse files
jugglinmikerwaldron
authored andcommittedApr 5, 2021
[[FEAT]] Add support for dynamic import
1 parent b125dbe commit 6bfcaed

File tree

4 files changed

+82
-803
lines changed

4 files changed

+82
-803
lines changed
 

‎src/jshint.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -5576,10 +5576,22 @@ var JSHINT = (function() {
55765576
}
55775577
});
55785578

5579-
if (!mp) {
5579+
if (mp) {
5580+
return mp;
5581+
}
5582+
5583+
if (!checkPunctuator(state.tokens.next, "(")) {
55805584
return state.syntax["(identifier)"].nud.call(this, context);
55815585
}
5582-
return mp;
5586+
5587+
if (!state.inES11()) {
5588+
warning("W119", state.tokens.curr, "dynamic import", "11");
5589+
}
5590+
5591+
advance("(");
5592+
expression(context, 10);
5593+
advance(")");
5594+
return this;
55835595
});
55845596

55855597
var importSymbol = stmt("import", function(context) {
@@ -5686,8 +5698,9 @@ var JSHINT = (function() {
56865698
importSymbol.reserved = true;
56875699
importSymbol.meta = { isFutureReservedWord: true, es5: true };
56885700
importSymbol.useFud = function() {
5689-
return !(checkPunctuator(state.tokens.next, ".") && peek().identifier);
5701+
return !(checkPunctuators(state.tokens.next, [".", "("]));
56905702
};
5703+
importSymbol.rbp = 161;
56915704

56925705
stmt("export", function(context) {
56935706
var ok = true;

‎src/options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ exports.val = {
10531053
* Notable additions: optional catch bindings.
10541054
* - `11` - To enable language features introduced by ECMAScript 11. Notable
10551055
* additions: "export * as ns from 'module'", `import.meta`, the nullish
1056-
* coalescing operator, and optional chaining.
1056+
* coalescing operator, and optional chaining, and dynamic import.
10571057
*/
10581058
esversion: 5
10591059
};

‎tests/test262/expectations.txt

+3-799
Large diffs are not rendered by default.

‎tests/unit/dynamic-import.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Tests for the parser/tokenizer
3+
*/
4+
5+
"use strict";
6+
7+
var JSHINT = require("../..").JSHINT;
8+
var fs = require('fs');
9+
var TestRun = require("../helpers/testhelper").setup.testRun;
10+
var path = require("path");
11+
12+
exports.dynamicImport = {};
13+
14+
exports.dynamicImport.valid = function (test) {
15+
TestRun(test)
16+
.test([
17+
"import(0);",
18+
"import(0 ? 0 : 0);",
19+
"(function * () { ",
20+
" import(yield);",
21+
"})();",
22+
"import(() => {});",
23+
"import(async () => {});",
24+
"import(x = 0);",
25+
"new (import(0))();",
26+
"import(import(0));",
27+
], {esversion: 11});
28+
29+
test.done();
30+
};
31+
32+
exports.dynamicImport.invalidvalid = function (test) {
33+
TestRun(test, "empty")
34+
.addError(1, 8, "Expected an identifier and instead saw ')'.")
35+
.addError(1, 9, "Expected ')' and instead saw ';'.")
36+
.addError(1, 10, "Missing semicolon.")
37+
.test("import();", {esversion: 11});
38+
39+
TestRun(test, "expression")
40+
.addError(1, 11, "Expected ')' and instead saw ','.")
41+
.addError(1, 12, "Missing semicolon.")
42+
.addError(1, 13, "Expected an assignment or function call and instead saw an expression.")
43+
.addError(1, 16, "Missing semicolon.")
44+
.addError(1, 16, "Expected an identifier and instead saw ')'.")
45+
.addError(1, 16, "Expected an assignment or function call and instead saw an expression.")
46+
.test("import('a', 'b');", {esversion: 11});
47+
48+
TestRun(test, "NewExpression")
49+
.addError(1, 5, "Unexpected 'import'.")
50+
.addError(1, 13, "Missing '()' invoking a constructor.")
51+
.test("new import(0);", {esversion: 11});
52+
53+
test.done();
54+
};
55+
56+
exports.dynamicImport.esversion = function (test) {
57+
TestRun(test)
58+
.addError(1, 1, "'dynamic import' is only available in ES11 (use 'esversion: 11').")
59+
.test("import(0);", {esversion: 10});
60+
61+
test.done();
62+
};

0 commit comments

Comments
 (0)
Please sign in to comment.