11import type { Context } from "../context" ;
22import { debug } from "../debug" ;
3- import type { ParserOptionsContext } from "../context/parser-options" ;
3+ import type {
4+ ParserOptions ,
5+ ParserOptionsContext ,
6+ } from "../context/parser-options" ;
47import type { ESLintExtendedProgram } from "../types" ;
58import { tsPatch } from "./ts-patch" ;
69import { 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" ;
815/**
916 * Parse for script
1017 */
@@ -17,17 +24,45 @@ export function parseScript(
1724
1825 const parserOptions = parserOptionsCtx . parserOptions ;
1926 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 , {
2242 globalReturn : parserOptions . ecmaFeatures ?. globalReturn ,
2343 jsxPragma : parserOptions . jsxPragma ,
2444 jsxFragmentName : parserOptions . jsxFragmentName ,
2545 lib : parserOptions . lib ,
2646 sourceType : parserOptions . sourceType ,
2747 } ) ;
48+ } catch {
49+ // ignore
2850 }
51+ const ecmaFeatures = parserOptions . ecmaFeatures || { } ;
2952
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 ;
3166}
3267
3368/**
0 commit comments