@typescript-eslint/scope-manager 8.53.2-alpha.1 → 8.53.2-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ScopeManager.d.ts +6 -0
- package/dist/ScopeManager.js +8 -0
- package/dist/definition/CatchClauseDefinition.d.ts +2 -2
- package/dist/referencer/Referencer.js +2 -3
- package/dist/scope/GlobalScope.d.ts +1 -0
- package/dist/scope/GlobalScope.js +21 -1
- package/dist/scope/ScopeBase.d.ts +2 -3
- package/dist/scope/ScopeBase.js +8 -60
- package/package.json +4 -4
package/dist/ScopeManager.d.ts
CHANGED
|
@@ -48,6 +48,12 @@ export declare class ScopeManager {
|
|
|
48
48
|
* If `inner` is `true` then this returns the innermost scope.
|
|
49
49
|
*/
|
|
50
50
|
acquire(node: TSESTree.Node, inner?: boolean): Scope | null;
|
|
51
|
+
/**
|
|
52
|
+
* Adds dynamically created globals to the global scope and resolve their references.
|
|
53
|
+
* This method is called by ESLint.
|
|
54
|
+
* @param names Names of the globals to create
|
|
55
|
+
*/
|
|
56
|
+
addGlobals(names: string[]): void;
|
|
51
57
|
nestBlockScope(node: BlockScope['block']): BlockScope;
|
|
52
58
|
nestCatchScope(node: CatchScope['block']): CatchScope;
|
|
53
59
|
nestClassFieldInitializerScope(node: ClassFieldInitializerScope['block']): ClassFieldInitializerScope;
|
package/dist/ScopeManager.js
CHANGED
|
@@ -98,6 +98,14 @@ class ScopeManager {
|
|
|
98
98
|
}
|
|
99
99
|
return scopes.find(predicate) ?? null;
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Adds dynamically created globals to the global scope and resolve their references.
|
|
103
|
+
* This method is called by ESLint.
|
|
104
|
+
* @param names Names of the globals to create
|
|
105
|
+
*/
|
|
106
|
+
addGlobals(names) {
|
|
107
|
+
this.globalScope?.addVariables(names);
|
|
108
|
+
}
|
|
101
109
|
nestBlockScope(node) {
|
|
102
110
|
(0, assert_1.assert)(this.currentScope);
|
|
103
111
|
return this.nestScope(new scope_1.BlockScope(this, this.currentScope, node));
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { TSESTree } from '@typescript-eslint/types';
|
|
2
2
|
import { DefinitionBase } from './DefinitionBase';
|
|
3
3
|
import { DefinitionType } from './DefinitionType';
|
|
4
|
-
export declare class CatchClauseDefinition extends DefinitionBase<DefinitionType.CatchClause, TSESTree.CatchClause, null, TSESTree.
|
|
4
|
+
export declare class CatchClauseDefinition extends DefinitionBase<DefinitionType.CatchClause, TSESTree.CatchClause, null, TSESTree.Identifier> {
|
|
5
5
|
readonly isTypeDefinition = false;
|
|
6
6
|
readonly isVariableDefinition = true;
|
|
7
|
-
constructor(name: TSESTree.
|
|
7
|
+
constructor(name: TSESTree.Identifier, node: CatchClauseDefinition['node']);
|
|
8
8
|
}
|
|
@@ -265,9 +265,8 @@ class Referencer extends Visitor_1.Visitor {
|
|
|
265
265
|
CatchClause(node) {
|
|
266
266
|
this.scopeManager.nestCatchScope(node);
|
|
267
267
|
if (node.param) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
this.currentScope().defineIdentifier(pattern, new definition_1.CatchClauseDefinition(param, node));
|
|
268
|
+
this.visitPattern(node.param, (pattern, info) => {
|
|
269
|
+
this.currentScope().defineIdentifier(pattern, new definition_1.CatchClauseDefinition(pattern, node));
|
|
271
270
|
this.referencingDefaultValue(pattern, info.assignments, null, true);
|
|
272
271
|
}, { processRightHandNodes: true });
|
|
273
272
|
}
|
|
@@ -11,6 +11,7 @@ export declare class GlobalScope extends ScopeBase<ScopeType.global, TSESTree.Pr
|
|
|
11
11
|
null> {
|
|
12
12
|
private readonly implicit;
|
|
13
13
|
constructor(scopeManager: ScopeManager, block: GlobalScope['block']);
|
|
14
|
+
addVariables(names: string[]): void;
|
|
14
15
|
close(scopeManager: ScopeManager): Scope | null;
|
|
15
16
|
defineImplicitVariable(name: string, options: ImplicitLibVariableOptions): void;
|
|
16
17
|
}
|
|
@@ -18,6 +18,24 @@ class GlobalScope extends ScopeBase_1.ScopeBase {
|
|
|
18
18
|
variables: [],
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
+
addVariables(names) {
|
|
22
|
+
for (const name of names) {
|
|
23
|
+
this.defineVariable(name, this.set, this.variables, null, null);
|
|
24
|
+
this.implicit.set.delete(name);
|
|
25
|
+
}
|
|
26
|
+
const nameSet = new Set(names);
|
|
27
|
+
for (const reference of this.through) {
|
|
28
|
+
if (nameSet.has(reference.identifier.name)) {
|
|
29
|
+
const variable = this.set.get(reference.identifier.name);
|
|
30
|
+
(0, assert_1.assert)(variable, `Expected variable with name "${reference.identifier.name}" to be specified.`);
|
|
31
|
+
reference.resolved = variable;
|
|
32
|
+
variable.references.push(reference);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this.through = this.through.filter(reference => !nameSet.has(reference.identifier.name));
|
|
36
|
+
this.implicit.variables = this.implicit.variables.filter(variable => !nameSet.has(variable.name));
|
|
37
|
+
this.implicit.leftToBeResolved = this.implicit.leftToBeResolved.filter(reference => !nameSet.has(reference.identifier.name));
|
|
38
|
+
}
|
|
21
39
|
close(scopeManager) {
|
|
22
40
|
(0, assert_1.assert)(this.leftToResolve);
|
|
23
41
|
for (const ref of this.leftToResolve) {
|
|
@@ -31,7 +49,9 @@ class GlobalScope extends ScopeBase_1.ScopeBase {
|
|
|
31
49
|
}
|
|
32
50
|
}
|
|
33
51
|
this.implicit.leftToBeResolved = this.leftToResolve;
|
|
34
|
-
|
|
52
|
+
super.close(scopeManager);
|
|
53
|
+
this.implicit.leftToBeResolved = [...this.through];
|
|
54
|
+
return null;
|
|
35
55
|
}
|
|
36
56
|
defineImplicitVariable(name, options) {
|
|
37
57
|
this.defineVariable(new variable_1.ImplicitLibVariable(this, name, options), this.set, this.variables, null, null);
|
|
@@ -59,7 +59,7 @@ export declare abstract class ScopeBase<Type extends ScopeType, Block extends TS
|
|
|
59
59
|
* The {@link Reference}s that are not resolved with this scope.
|
|
60
60
|
* @public
|
|
61
61
|
*/
|
|
62
|
-
|
|
62
|
+
through: Reference[];
|
|
63
63
|
readonly type: Type;
|
|
64
64
|
/**
|
|
65
65
|
* Reference to the parent {@link Scope}.
|
|
@@ -77,8 +77,7 @@ export declare abstract class ScopeBase<Type extends ScopeType, Block extends TS
|
|
|
77
77
|
readonly variableScope: VariableScope;
|
|
78
78
|
constructor(scopeManager: ScopeManager, type: Type, upperScope: Upper, block: Block, isMethodDefinition: boolean);
|
|
79
79
|
private isVariableScope;
|
|
80
|
-
|
|
81
|
-
close(scopeManager: ScopeManager): Scope | null;
|
|
80
|
+
close(_scopeManager: ScopeManager): Scope | null;
|
|
82
81
|
shouldStaticallyClose(): boolean;
|
|
83
82
|
/**
|
|
84
83
|
* To override by function scopes.
|
package/dist/scope/ScopeBase.js
CHANGED
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ScopeBase = void 0;
|
|
4
4
|
const types_1 = require("@typescript-eslint/types");
|
|
5
5
|
const assert_1 = require("../assert");
|
|
6
|
-
const definition_1 = require("../definition");
|
|
7
6
|
const ID_1 = require("../ID");
|
|
8
7
|
const Reference_1 = require("../referencer/Reference");
|
|
9
8
|
const variable_1 = require("../variable");
|
|
@@ -60,22 +59,13 @@ function isStrictScope(scope, block, isMethodDefinition) {
|
|
|
60
59
|
}
|
|
61
60
|
// Search 'use strict' directive.
|
|
62
61
|
for (const stmt of body.body) {
|
|
63
|
-
if (stmt.type !== types_1.AST_NODE_TYPES.ExpressionStatement
|
|
62
|
+
if (stmt.type !== types_1.AST_NODE_TYPES.ExpressionStatement ||
|
|
63
|
+
stmt.directive == null) {
|
|
64
64
|
break;
|
|
65
65
|
}
|
|
66
66
|
if (stmt.directive === 'use strict') {
|
|
67
67
|
return true;
|
|
68
68
|
}
|
|
69
|
-
const expr = stmt.expression;
|
|
70
|
-
if (expr.type !== types_1.AST_NODE_TYPES.Literal) {
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
if (expr.raw === '"use strict"' || expr.raw === "'use strict'") {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
if (expr.value === 'use strict') {
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
69
|
}
|
|
80
70
|
return false;
|
|
81
71
|
}
|
|
@@ -187,16 +177,6 @@ class ScopeBase {
|
|
|
187
177
|
/* eslint-enable @typescript-eslint/no-non-null-assertion */
|
|
188
178
|
} while (current);
|
|
189
179
|
};
|
|
190
|
-
#globalCloseRef = (ref, scopeManager) => {
|
|
191
|
-
// let/const/class declarations should be resolved statically.
|
|
192
|
-
// others should be resolved dynamically.
|
|
193
|
-
if (this.shouldStaticallyCloseForGlobal(ref, scopeManager)) {
|
|
194
|
-
this.#staticCloseRef(ref);
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
this.#dynamicCloseRef(ref);
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
180
|
#staticCloseRef = (ref) => {
|
|
201
181
|
const resolve = () => {
|
|
202
182
|
const name = ref.identifier.name;
|
|
@@ -246,50 +226,18 @@ class ScopeBase {
|
|
|
246
226
|
isVariableScope() {
|
|
247
227
|
return VARIABLE_SCOPE_TYPES.has(this.type);
|
|
248
228
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if (!variable) {
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
// variable exists on the scope
|
|
257
|
-
// in module mode, we can statically resolve everything, regardless of its decl type
|
|
258
|
-
if (scopeManager.isModule()) {
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
// in script mode, only certain cases should be statically resolved
|
|
262
|
-
// Example:
|
|
263
|
-
// a `var` decl is ignored by the runtime if it clashes with a global name
|
|
264
|
-
// this means that we should not resolve the reference to the variable
|
|
265
|
-
const defs = variable.defs;
|
|
266
|
-
return (defs.length > 0 &&
|
|
267
|
-
defs.every(def => {
|
|
268
|
-
if (def.type === definition_1.DefinitionType.Variable && def.parent.kind === 'var') {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
return true;
|
|
272
|
-
}));
|
|
273
|
-
}
|
|
274
|
-
close(scopeManager) {
|
|
275
|
-
let closeRef;
|
|
276
|
-
if (this.shouldStaticallyClose()) {
|
|
277
|
-
closeRef = this.#staticCloseRef;
|
|
278
|
-
}
|
|
279
|
-
else if (this.type !== 'global') {
|
|
280
|
-
closeRef = this.#dynamicCloseRef;
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
closeRef = this.#globalCloseRef;
|
|
284
|
-
}
|
|
229
|
+
close(_scopeManager) {
|
|
230
|
+
const closeRef = this.shouldStaticallyClose()
|
|
231
|
+
? this.#staticCloseRef
|
|
232
|
+
: this.#dynamicCloseRef;
|
|
285
233
|
// Try Resolving all references in this scope.
|
|
286
234
|
(0, assert_1.assert)(this.leftToResolve);
|
|
287
|
-
this.leftToResolve.forEach(ref => closeRef(ref
|
|
235
|
+
this.leftToResolve.forEach(ref => closeRef(ref));
|
|
288
236
|
this.leftToResolve = null;
|
|
289
237
|
return this.upper;
|
|
290
238
|
}
|
|
291
239
|
shouldStaticallyClose() {
|
|
292
|
-
return !this.#dynamic;
|
|
240
|
+
return !this.#dynamic || this.type === 'global';
|
|
293
241
|
}
|
|
294
242
|
/**
|
|
295
243
|
* To override by function scopes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-eslint/scope-manager",
|
|
3
|
-
"version": "8.53.2-alpha.
|
|
3
|
+
"version": "8.53.2-alpha.10",
|
|
4
4
|
"description": "TypeScript scope analyser for ESLint",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"typecheck": "yarn run -BT nx typecheck"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@typescript-eslint/types": "8.53.2-alpha.
|
|
51
|
-
"@typescript-eslint/visitor-keys": "8.53.2-alpha.
|
|
50
|
+
"@typescript-eslint/types": "8.53.2-alpha.10",
|
|
51
|
+
"@typescript-eslint/visitor-keys": "8.53.2-alpha.10"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@typescript-eslint/typescript-estree": "8.53.2-alpha.
|
|
54
|
+
"@typescript-eslint/typescript-estree": "8.53.2-alpha.10",
|
|
55
55
|
"@vitest/coverage-v8": "^3.2.4",
|
|
56
56
|
"@vitest/pretty-format": "^3.2.4",
|
|
57
57
|
"eslint": "*",
|