Skip to content

Commit

Permalink
Allow sourceType: module even with ecmaVersion < 6
Browse files Browse the repository at this point in the history
  • Loading branch information
LongTengDao authored and marijnh committed Aug 12, 2019
1 parent e2b8cc0 commit 7e9817d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
4 changes: 1 addition & 3 deletions acorn-loose/src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ lp.parseTopLevel = function() {
while (this.tok.type !== tt.eof) node.body.push(this.parseStatement())
this.toks.adaptDirectivePrologue(node.body)
this.last = this.tok
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
}
node.sourceType = this.options.sourceType
return this.finishNode(node, "Program")
}

Expand Down
3 changes: 3 additions & 0 deletions acorn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ an object containing any of these fields:
either `"script"` or `"module"`. This influences global strict mode
and parsing of `import` and `export` declarations.

**NOTE**: If set to `"module"`, then static `import` / `export` syntax
will be valid, even if `ecmaVersion` is less than 6.

- **onInsertedSemicolon**: If given a callback, that callback will be
called whenever a missing semicolon is inserted by the parser. The
callback will be given the character offset of the point where the
Expand Down
1 change: 1 addition & 0 deletions acorn/src/identifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion acorn/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Parser {
constructor(options, input, startPos) {
this.options = options = getOptions(options)
this.sourceFile = options.sourceFile
this.keywords = wordsRegexp(keywords[options.ecmaVersion >= 6 ? 6 : 5])
this.keywords = wordsRegexp(keywords[options.ecmaVersion >= 6 ? 6 : options.sourceType === "module" ? "5module" : 5])
let reserved = ""
if (options.allowReserved !== true) {
for (let v = options.ecmaVersion;; v--)
Expand Down
4 changes: 1 addition & 3 deletions acorn/src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pp.parseTopLevel = function(node) {
this.raiseRecoverable(this.undefinedExports[name].start, `Export '${name}' is not defined`)
this.adaptDirectivePrologue(node.body)
this.next()
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
}
node.sourceType = this.options.sourceType
return this.finishNode(node, "Program")
}

Expand Down
29 changes: 29 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ if (typeof exports != "undefined") {
var acorn = require("../acorn");
}

test("import ''", {
type: "Program",
start: 0,
end: 9,
body: [
{
type: "ImportDeclaration",
start: 0,
end: 9,
specifiers: [],
source: {
type: "Literal",
start: 7,
end: 9,
value: "",
raw: "''"
}
}
]
}, {
ecmaVersion: 5,
sourceType: "module"
});

testFail("import('')", "Unexpected token (1:6)", {
ecmaVersion: 5,
sourceType: "module"
});

test("new Object", {
type: "Program",
start: 0,
Expand Down

0 comments on commit 7e9817d

Please sign in to comment.