• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* eslint-disable @typescript-eslint/no-empty-interface, @typescript-eslint/no-explicit-any */
2
3import {
4  Scope as ESLintScope,
5  GlobalScope as ESLintGlobalScope,
6  ModuleScope as ESLintModuleScope,
7  FunctionExpressionNameScope as ESLintFunctionExpressionNameScope,
8  CatchScope as ESLintCatchScope,
9  WithScope as ESLintWithScope,
10  BlockScope as ESLintBlockScope,
11  SwitchScope as ESLintSwitchScope,
12  FunctionScope as ESLintFunctionScope,
13  ForScope as ESLintForScope,
14  ClassScope as ESLintClassScope,
15} from 'eslint-scope/lib/scope';
16import { TSESTree } from '../ts-estree';
17import { Definition } from './Definition';
18import { Reference, ReferenceFlag } from './Reference';
19import { ScopeManager } from './ScopeManager';
20import { Variable } from './Variable';
21
22type ScopeType =
23  | 'block'
24  | 'catch'
25  | 'class'
26  | 'for'
27  | 'function'
28  | 'function-expression-name'
29  | 'global'
30  | 'module'
31  | 'switch'
32  | 'with'
33  | 'TDZ'
34  | 'enum'
35  | 'empty-function';
36
37interface Scope {
38  type: ScopeType;
39  isStrict: boolean;
40  upper: Scope | null;
41  childScopes: Scope[];
42  variableScope: Scope;
43  block: TSESTree.Node;
44  variables: Variable[];
45  set: Map<string, Variable>;
46  references: Reference[];
47  through: Reference[];
48  thisFound?: boolean;
49  taints: Map<string, boolean>;
50  functionExpressionScope: boolean;
51  __left: Reference[];
52
53  __shouldStaticallyClose(scopeManager: ScopeManager): boolean;
54  __shouldStaticallyCloseForGlobal(ref: any): boolean;
55  __staticCloseRef(ref: any): void;
56  __dynamicCloseRef(ref: any): void;
57  __globalCloseRef(ref: any): void;
58  __close(scopeManager: ScopeManager): Scope;
59  __isValidResolution(ref: any, variable: any): variable is Variable;
60  __resolve(ref: Reference): boolean;
61  __delegateToUpperScope(ref: any): void;
62  __addDeclaredVariablesOfNode(variable: any, node: TSESTree.Node): void;
63  __defineGeneric(
64    name: string,
65    set: Map<string, Variable>,
66    variables: Variable[],
67    node: TSESTree.Identifier,
68    def: Definition,
69  ): void;
70
71  __define(node: TSESTree.Node, def: Definition): void;
72
73  __referencing(
74    node: TSESTree.Node,
75    assign?: ReferenceFlag,
76    writeExpr?: TSESTree.Node,
77    maybeImplicitGlobal?: any,
78    partial?: any,
79    init?: any,
80  ): void;
81
82  __detectEval(): void;
83  __detectThis(): void;
84  __isClosed(): boolean;
85  /**
86   * returns resolved {Reference}
87   * @method Scope#resolve
88   * @param {Espree.Identifier} ident - identifier to be resolved.
89   * @returns {Reference} reference
90   */
91  resolve(ident: TSESTree.Node): Reference;
92
93  /**
94   * returns this scope is static
95   * @method Scope#isStatic
96   * @returns {boolean} static
97   */
98  isStatic(): boolean;
99
100  /**
101   * returns this scope has materialized arguments
102   * @method Scope#isArgumentsMaterialized
103   * @returns {boolean} arguments materialized
104   */
105  isArgumentsMaterialized(): boolean;
106
107  /**
108   * returns this scope has materialized `this` reference
109   * @method Scope#isThisMaterialized
110   * @returns {boolean} this materialized
111   */
112  isThisMaterialized(): boolean;
113
114  isUsedName(name: any): boolean;
115}
116interface ScopeConstructor {
117  new (
118    scopeManager: ScopeManager,
119    type: ScopeType,
120    upperScope: Scope | null,
121    block: TSESTree.Node | null,
122    isMethodDefinition: boolean,
123  ): Scope;
124}
125const Scope = ESLintScope as ScopeConstructor;
126
127interface ScopeChildConstructorWithUpperScope<T> {
128  new (
129    scopeManager: ScopeManager,
130    upperScope: Scope,
131    block: TSESTree.Node | null,
132  ): T;
133}
134
135interface GlobalScope extends Scope {}
136const GlobalScope = ESLintGlobalScope as ScopeConstructor & {
137  new (scopeManager: ScopeManager, block: TSESTree.Node | null): GlobalScope;
138};
139
140interface ModuleScope extends Scope {}
141const ModuleScope = ESLintModuleScope as ScopeConstructor &
142  ScopeChildConstructorWithUpperScope<ModuleScope>;
143
144interface FunctionExpressionNameScope extends Scope {}
145const FunctionExpressionNameScope = ESLintFunctionExpressionNameScope as ScopeConstructor &
146  ScopeChildConstructorWithUpperScope<FunctionExpressionNameScope>;
147
148interface CatchScope extends Scope {}
149const CatchScope = ESLintCatchScope as ScopeConstructor &
150  ScopeChildConstructorWithUpperScope<CatchScope>;
151
152interface WithScope extends Scope {}
153const WithScope = ESLintWithScope as ScopeConstructor &
154  ScopeChildConstructorWithUpperScope<WithScope>;
155
156interface BlockScope extends Scope {}
157const BlockScope = ESLintBlockScope as ScopeConstructor &
158  ScopeChildConstructorWithUpperScope<BlockScope>;
159
160interface SwitchScope extends Scope {}
161const SwitchScope = ESLintSwitchScope as ScopeConstructor &
162  ScopeChildConstructorWithUpperScope<SwitchScope>;
163
164interface FunctionScope extends Scope {}
165const FunctionScope = ESLintFunctionScope as ScopeConstructor & {
166  new (
167    scopeManager: ScopeManager,
168    upperScope: Scope,
169    block: TSESTree.Node | null,
170    isMethodDefinition: boolean,
171  ): FunctionScope;
172};
173
174interface ForScope extends Scope {}
175const ForScope = ESLintForScope as ScopeConstructor &
176  ScopeChildConstructorWithUpperScope<ForScope>;
177
178interface ClassScope extends Scope {}
179const ClassScope = ESLintClassScope as ScopeConstructor &
180  ScopeChildConstructorWithUpperScope<ClassScope>;
181
182export {
183  ScopeType,
184  Scope,
185  GlobalScope,
186  ModuleScope,
187  FunctionExpressionNameScope,
188  CatchScope,
189  WithScope,
190  BlockScope,
191  SwitchScope,
192  FunctionScope,
193  ForScope,
194  ClassScope,
195};
196