|
1 | | -import * as eslint from "eslint"; |
2 | 1 | import * as eslintScope from "eslint-scope"; |
| 2 | +import type { AnalyzeOptions } from "eslint-scope"; |
| 3 | +import * as espree from "espree"; |
3 | 4 | import * as estree from "estree"; |
4 | 5 |
|
5 | | -declare const program: estree.Program; |
6 | | -declare const scope: eslintScope.Scope; |
7 | | -declare const variable: eslintScope.Variable; |
8 | | -declare const reference: eslintScope.Reference; |
| 6 | +const code = ` |
| 7 | +function example() { |
| 8 | + let x = 1; |
| 9 | + console.log(x); |
| 10 | +} |
| 11 | +`; |
9 | 12 |
|
10 | | -const manager1: eslintScope.ScopeManager = eslintScope.analyze( |
11 | | - program, |
| 13 | +const ast = espree.parse(code, { ecmaVersion: 2022, sourceType: "module" }) as estree.Program; |
| 14 | + |
| 15 | +// $ExpectType ScopeManager |
| 16 | +const scopeManager = eslintScope.analyze( |
| 17 | + ast, |
12 | 18 | { |
13 | | - directive: false, |
14 | | - ecmaVersion: 2018, |
15 | | - fallback(node) { |
16 | | - return Object.keys(node); |
17 | | - }, |
18 | | - ignoreEval: true, |
19 | | - impliedStrict: true, |
20 | | - nodejsScope: true, |
21 | | - optimistic: true, |
| 19 | + ecmaVersion: 2022, |
22 | 20 | sourceType: "module", |
23 | | - }, |
24 | | -); |
25 | | -const manager2: eslintScope.ScopeManager = eslintScope.analyze( |
26 | | - program, |
27 | | - { |
28 | | - ecmaVersion: 5, |
29 | | - ignoreEval: false, |
30 | | - impliedStrict: false, |
| 21 | + ignoreEval: true, |
31 | 22 | nodejsScope: false, |
32 | | - optimistic: false, |
33 | | - sourceType: "script", |
34 | | - }, |
| 23 | + impliedStrict: false, |
| 24 | + childVisitorKeys: null, |
| 25 | + fallback: "iteration", |
| 26 | + } satisfies AnalyzeOptions, |
35 | 27 | ); |
36 | | -const manager3: eslintScope.ScopeManager = eslintScope.analyze(program); |
37 | 28 |
|
38 | | -const managerInterface = manager1; // $ExpectType ScopeManager |
39 | | -const scopeInterface = scope; // $ExpectType Scope |
40 | | -const variableInterface = variable; // $ExpectType Variable |
41 | | -const referenceInterface = reference; // $ExpectType Reference |
| 29 | +// $ExpectType GlobalScope |
| 30 | +scopeManager.globalScope; |
| 31 | +// $ExpectType Scope<Variable<Reference>, Reference>[] |
| 32 | +scopeManager.scopes; |
| 33 | + |
| 34 | +// $ExpectType Scope<Variable<Reference>, Reference> | null |
| 35 | +const scope = scopeManager.acquire(ast); |
| 36 | + |
| 37 | +// $ExpectType Scope<Variable<Reference>, Reference> | null |
| 38 | +scopeManager.release(ast); |
| 39 | + |
| 40 | +if (scope) { |
| 41 | + // $ExpectType "function" | "module" | "block" | "catch" | "class" | "for" | "function-expression-name" | "global" | "switch" | "with" | "TDZ" |
| 42 | + scope.type; |
| 43 | + // $ExpectType boolean |
| 44 | + scope.isStrict; |
| 45 | + // $ExpectType Scope<Variable<Reference>, Reference> | null |
| 46 | + scope.upper; |
| 47 | + // $ExpectType Scope<Variable<Reference>, Reference> |
| 48 | + scope.variableScope; |
| 49 | + // $ExpectType Variable<Reference>[] |
| 50 | + scope.variables; |
| 51 | + // $ExpectType Reference[] |
| 52 | + scope.references; |
| 53 | + // $ExpectType Scope<Variable<Reference>, Reference>[] |
| 54 | + scope.childScopes; |
| 55 | + // $ExpectType Node |
| 56 | + scope.block; |
| 57 | + // $ExpectType boolean |
| 58 | + scope.functionExpressionScope; |
| 59 | + // $ExpectType Reference[] |
| 60 | + scope.implicit.left; |
| 61 | + // $ExpectType Map<string, Variable> |
| 62 | + scope.set; |
| 63 | + // $ExpectType Reference[] |
| 64 | + scope.through; |
| 65 | +} |
| 66 | + |
| 67 | +const variable = scope?.variables[0]; |
| 68 | +if (variable) { |
| 69 | + // $ExpectType string |
| 70 | + variable.name; |
| 71 | + // $ExpectType Scope<Variable<Reference>, Reference> |
| 72 | + variable.scope; |
| 73 | + // $ExpectType Identifier[] |
| 74 | + variable.identifiers; |
| 75 | + // $ExpectType Reference[] |
| 76 | + variable.references; |
| 77 | + // $ExpectType Definition[] |
| 78 | + variable.defs; |
| 79 | +} |
| 80 | + |
| 81 | +const reference = scope?.references[0]; |
| 82 | +if (reference) { |
| 83 | + // $ExpectType Identifier |
| 84 | + reference.identifier; |
| 85 | + // $ExpectType Variable<Reference> | null |
| 86 | + reference.resolved; |
| 87 | + // $ExpectType () => boolean |
| 88 | + reference.isWrite; |
| 89 | + // $ExpectType () => boolean |
| 90 | + reference.isRead; |
| 91 | + // $ExpectType Scope<Variable<Reference>, Reference> |
| 92 | + reference.from; |
| 93 | +} |
| 94 | + |
| 95 | +const definition = variable?.defs[0]; |
| 96 | +if (definition) { |
| 97 | + // $ExpectType "CatchClause" | "TDZ" | "ClassName" | "FunctionName" | "ImplicitGlobalVariable" | "ImportBinding" | "Parameter" | "Variable" |
| 98 | + definition.type; |
| 99 | + // $ExpectType Identifier |
| 100 | + definition.name; |
| 101 | + // $ExpectType ImportDeclaration | VariableDeclaration | null |
| 102 | + definition.parent; |
| 103 | +} |
| 104 | + |
| 105 | +// $ExpectType GlobalScope |
| 106 | +const globalScope = scopeManager.globalScope; |
| 107 | +// $ExpectType 'global' |
| 108 | +globalScope.type; |
42 | 109 |
|
43 | | -scope.references[0].resolved?.scope; // $ExpectType Scope | undefined |
| 110 | +// $ExpectType ScopeManager |
| 111 | +eslintScope.analyze(ast); |
0 commit comments