Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Fix: sourceType not correctly set in AST when provided in options. (#583
Browse files Browse the repository at this point in the history
)
  • Loading branch information
armano2 authored and JamesHenry committed Jan 13, 2019
1 parent 567e8db commit a8fee90
Show file tree
Hide file tree
Showing 12 changed files with 12,213 additions and 1,632 deletions.
6 changes: 3 additions & 3 deletions analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,17 +778,17 @@ class Referencer extends OriginalReferencer {
}
}

module.exports = function(ast, parserOptions, extraOptions) {
module.exports = function(ast, parserOptions) {
const options = {
ignoreEval: true,
optimistic: false,
directive: false,
nodejsScope:
ast.sourceType === "script" &&
parserOptions.sourceType === "script" &&
(parserOptions.ecmaFeatures &&
parserOptions.ecmaFeatures.globalReturn) === true,
impliedStrict: false,
sourceType: extraOptions.sourceType,
sourceType: parserOptions.sourceType,
ecmaVersion: parserOptions.ecmaVersion || 2018,
childVisitorKeys,
fallback
Expand Down
24 changes: 9 additions & 15 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ exports.parseForESLint = function parseForESLint(code, options) {
}
}

// https://eslint.org/docs/user-guide/configuring#specifying-parser-options
// if sourceType is not provided by default eslint expect that it will be set to "script"
options.sourceType = options.sourceType || "script";
if (options.sourceType !== "module" && options.sourceType !== "script") {
options.sourceType = "script";
}

const ast = parse(code, options);
const extraOptions = {
sourceType: ast.sourceType
};
ast.sourceType = options.sourceType;

traverser.traverse(ast, {
enter: node => {
Expand All @@ -47,23 +52,12 @@ exports.parseForESLint = function parseForESLint(code, options) {
node.type = `TSEmptyBody${node.type}`;
}
break;

// Import/Export declarations cannot appear in script.
// But if those appear only in namespace/module blocks, `ast.sourceType` was `"script"`.
// This doesn't modify `ast.sourceType` directly for backward compatibility.
case "ImportDeclaration":
case "ExportAllDeclaration":
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
extraOptions.sourceType = "module";
break;

// no default
}
}
});

const scopeManager = analyzeScope(ast, options, extraOptions);
const scopeManager = analyzeScope(ast, options);
return { ast, scopeManager, visitorKeys };
};

Expand Down
18 changes: 9 additions & 9 deletions tests/lib/__snapshots__/basics.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Object {
0,
16,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -515,7 +515,7 @@ Object {
0,
59,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -1194,7 +1194,7 @@ Object {
0,
60,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -1562,7 +1562,7 @@ Object {
0,
17,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -1731,7 +1731,7 @@ Object {
0,
15,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -1993,7 +1993,7 @@ Object {
0,
23,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2236,7 +2236,7 @@ Object {
0,
13,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2537,7 +2537,7 @@ Object {
0,
39,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2997,7 +2997,7 @@ Object {
0,
17,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down
40 changes: 20 additions & 20 deletions tests/lib/__snapshots__/comments.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Object {
0,
27,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -335,7 +335,7 @@ Object {
10,
31,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -984,7 +984,7 @@ Object {
99,
137,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -1489,7 +1489,7 @@ Object {
0,
98,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2193,7 +2193,7 @@ Object {
0,
129,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2598,7 +2598,7 @@ Object {
12,
12,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [],
"type": "Program",
}
Expand Down Expand Up @@ -2752,7 +2752,7 @@ Object {
6,
35,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -2946,7 +2946,7 @@ Object {
0,
35,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -3204,7 +3204,7 @@ Object {
0,
38,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -3504,7 +3504,7 @@ Object {
0,
61,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -3822,7 +3822,7 @@ Object {
0,
64,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -4105,7 +4105,7 @@ Object {
0,
62,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -4406,7 +4406,7 @@ Object {
0,
64,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -4799,7 +4799,7 @@ Object {
0,
69,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -5297,7 +5297,7 @@ Object {
0,
92,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -5890,7 +5890,7 @@ Object {
0,
142,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -6442,7 +6442,7 @@ Object {
0,
59,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -6911,7 +6911,7 @@ Object {
0,
134,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -7996,7 +7996,7 @@ Object {
0,
288,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down Expand Up @@ -9115,7 +9115,7 @@ Object {
0,
65,
],
"sourceType": "script",
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
Expand Down
Loading

0 comments on commit a8fee90

Please sign in to comment.