1
1
import type { Context } from "../context" ;
2
2
import { debug } from "../debug" ;
3
- import type { ParserOptionsContext } from "../context/parser-options" ;
3
+ import type {
4
+ ParserOptions ,
5
+ ParserOptionsContext ,
6
+ } from "../context/parser-options" ;
4
7
import type { ESLintExtendedProgram } from "../types" ;
5
8
import { tsPatch } from "./ts-patch" ;
6
9
import { isEnhancedParserObject } from "../context/resolve-parser/parser-object" ;
7
- import { analyze } from "@typescript-eslint/scope-manager" ;
10
+ import type { ScopeManager } from "@typescript-eslint/scope-manager" ;
11
+ import { analyze as analyzeForTypeScript } from "@typescript-eslint/scope-manager" ;
12
+ import { analyze as analyzeForEcmaScript } from "eslint-scope" ;
13
+ import { KEYS } from "../visitor-keys" ;
14
+ import { getKeys } from "../traverse" ;
8
15
/**
9
16
* Parse for script
10
17
*/
@@ -17,17 +24,45 @@ export function parseScript(
17
24
18
25
const parserOptions = parserOptionsCtx . parserOptions ;
19
26
if ( ! result . scopeManager && parserOptions . eslintScopeManager ) {
20
- result . scopeManager = analyze ( result . ast , {
21
- ecmaVersion : 1e8 ,
27
+ result . scopeManager = analyzeScope ( result , parserOptions ) ;
28
+ }
29
+
30
+ return result ;
31
+ }
32
+
33
+ /**
34
+ * Analyze scope
35
+ */
36
+ function analyzeScope (
37
+ result : ESLintExtendedProgram ,
38
+ parserOptions : ParserOptions ,
39
+ ) : ScopeManager {
40
+ try {
41
+ return analyzeForTypeScript ( result . ast , {
22
42
globalReturn : parserOptions . ecmaFeatures ?. globalReturn ,
23
43
jsxPragma : parserOptions . jsxPragma ,
24
44
jsxFragmentName : parserOptions . jsxFragmentName ,
25
45
lib : parserOptions . lib ,
26
46
sourceType : parserOptions . sourceType ,
27
47
} ) ;
48
+ } catch {
49
+ // ignore
28
50
}
51
+ const ecmaFeatures = parserOptions . ecmaFeatures || { } ;
29
52
30
- return result ;
53
+ return analyzeForEcmaScript ( result . ast , {
54
+ ignoreEval : true ,
55
+ nodejsScope : ecmaFeatures . globalReturn ,
56
+ impliedStrict : ecmaFeatures . impliedStrict as never ,
57
+ ecmaVersion : 1e8 ,
58
+ sourceType :
59
+ parserOptions . sourceType === "commonjs"
60
+ ? "script"
61
+ : parserOptions . sourceType || "script" ,
62
+ // @ts -expect-error -- Type bug?
63
+ childVisitorKeys : result . visitorKeys || KEYS ,
64
+ fallback : getKeys ,
65
+ } ) as ScopeManager ;
31
66
}
32
67
33
68
/**
0 commit comments