Skip to content

Commit 4f74648

Browse files
committed
also try to parse the module as script
this should allow with() in a weird module
1 parent 8ca07fd commit 4f74648

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

lib/Parser.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -956,13 +956,38 @@ Parser.prototype.parseCalculatedString = function parseCalculatedString(expressi
956956
};
957957
});
958958

959+
var POSSIBLE_AST_OPTIONS = [{
960+
ranges: true,
961+
locations: true,
962+
ecmaVersion: 6,
963+
sourceType: "module"
964+
}, {
965+
ranges: true,
966+
locations: true,
967+
ecmaVersion: 6,
968+
sourceType: "script"
969+
}]
970+
959971
Parser.prototype.parse = function parse(source, initialState) {
960-
var ast = acorn.parse(source, {
961-
ranges: true,
962-
locations: true,
963-
ecmaVersion: 6,
964-
sourceType: "module"
965-
});
972+
var ast;
973+
for(var i = 0; i < POSSIBLE_AST_OPTIONS.length; i++) {
974+
if(!ast) {
975+
try {
976+
ast = acorn.parse(source, POSSIBLE_AST_OPTIONS[i]);
977+
} catch(e) {
978+
// ignore the error
979+
}
980+
}
981+
}
982+
if(!ast) {
983+
// for the error
984+
ast = acorn.parse(source, {
985+
ranges: true,
986+
locations: true,
987+
ecmaVersion: 6,
988+
sourceType: "module"
989+
});
990+
}
966991
if(!ast || typeof ast !== "object")
967992
throw new Error("Source couldn't be parsed");
968993
var oldScope = this.scope;

0 commit comments

Comments
 (0)