Skip to content

Commit 7e9817d

Browse files
LongTengDaomarijnh
authored andcommitted
Allow sourceType: module even with ecmaVersion < 6
1 parent e2b8cc0 commit 7e9817d

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

acorn-loose/src/statement.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ lp.parseTopLevel = function() {
1010
while (this.tok.type !== tt.eof) node.body.push(this.parseStatement())
1111
this.toks.adaptDirectivePrologue(node.body)
1212
this.last = this.tok
13-
if (this.options.ecmaVersion >= 6) {
14-
node.sourceType = this.options.sourceType
15-
}
13+
node.sourceType = this.options.sourceType
1614
return this.finishNode(node, "Program")
1715
}
1816

acorn/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ an object containing any of these fields:
6464
either `"script"` or `"module"`. This influences global strict mode
6565
and parsing of `import` and `export` declarations.
6666

67+
**NOTE**: If set to `"module"`, then static `import` / `export` syntax
68+
will be valid, even if `ecmaVersion` is less than 6.
69+
6770
- **onInsertedSemicolon**: If given a callback, that callback will be
6871
called whenever a missing semicolon is inserted by the parser. The
6972
callback will be given the character offset of the point where the

acorn/src/identifier.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

acorn/src/state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Parser {
99
constructor(options, input, startPos) {
1010
this.options = options = getOptions(options)
1111
this.sourceFile = options.sourceFile
12-
this.keywords = wordsRegexp(keywords[options.ecmaVersion >= 6 ? 6 : 5])
12+
this.keywords = wordsRegexp(keywords[options.ecmaVersion >= 6 ? 6 : options.sourceType === "module" ? "5module" : 5])
1313
let reserved = ""
1414
if (options.allowReserved !== true) {
1515
for (let v = options.ecmaVersion;; v--)

acorn/src/statement.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ pp.parseTopLevel = function(node) {
2727
this.raiseRecoverable(this.undefinedExports[name].start, `Export '${name}' is not defined`)
2828
this.adaptDirectivePrologue(node.body)
2929
this.next()
30-
if (this.options.ecmaVersion >= 6) {
31-
node.sourceType = this.options.sourceType
32-
}
30+
node.sourceType = this.options.sourceType
3331
return this.finishNode(node, "Program")
3432
}
3533

test/tests.js

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ if (typeof exports != "undefined") {
77
var acorn = require("../acorn");
88
}
99

10+
test("import ''", {
11+
type: "Program",
12+
start: 0,
13+
end: 9,
14+
body: [
15+
{
16+
type: "ImportDeclaration",
17+
start: 0,
18+
end: 9,
19+
specifiers: [],
20+
source: {
21+
type: "Literal",
22+
start: 7,
23+
end: 9,
24+
value: "",
25+
raw: "''"
26+
}
27+
}
28+
]
29+
}, {
30+
ecmaVersion: 5,
31+
sourceType: "module"
32+
});
33+
34+
testFail("import('')", "Unexpected token (1:6)", {
35+
ecmaVersion: 5,
36+
sourceType: "module"
37+
});
38+
1039
test("new Object", {
1140
type: "Program",
1241
start: 0,

0 commit comments

Comments
 (0)