@typescript-eslint/scope-manager 8.53.2-alpha.5 → 8.53.2-alpha.6

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.
@@ -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;
@@ -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));
@@ -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
- return super.close(scopeManager);
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
- readonly through: Reference[];
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
- private shouldStaticallyCloseForGlobal;
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.
@@ -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");
@@ -187,16 +186,6 @@ class ScopeBase {
187
186
  /* eslint-enable @typescript-eslint/no-non-null-assertion */
188
187
  } while (current);
189
188
  };
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
189
  #staticCloseRef = (ref) => {
201
190
  const resolve = () => {
202
191
  const name = ref.identifier.name;
@@ -246,50 +235,18 @@ class ScopeBase {
246
235
  isVariableScope() {
247
236
  return VARIABLE_SCOPE_TYPES.has(this.type);
248
237
  }
249
- shouldStaticallyCloseForGlobal(ref, scopeManager) {
250
- // On global scope, let/const/class declarations should be resolved statically.
251
- const name = ref.identifier.name;
252
- const variable = this.set.get(name);
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
- }
238
+ close(_scopeManager) {
239
+ const closeRef = this.shouldStaticallyClose()
240
+ ? this.#staticCloseRef
241
+ : this.#dynamicCloseRef;
285
242
  // Try Resolving all references in this scope.
286
243
  (0, assert_1.assert)(this.leftToResolve);
287
- this.leftToResolve.forEach(ref => closeRef(ref, scopeManager));
244
+ this.leftToResolve.forEach(ref => closeRef(ref));
288
245
  this.leftToResolve = null;
289
246
  return this.upper;
290
247
  }
291
248
  shouldStaticallyClose() {
292
- return !this.#dynamic;
249
+ return !this.#dynamic || this.type === 'global';
293
250
  }
294
251
  /**
295
252
  * 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.5",
3
+ "version": "8.53.2-alpha.6",
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.5",
51
- "@typescript-eslint/visitor-keys": "8.53.2-alpha.5"
50
+ "@typescript-eslint/types": "8.53.2-alpha.6",
51
+ "@typescript-eslint/visitor-keys": "8.53.2-alpha.6"
52
52
  },
53
53
  "devDependencies": {
54
- "@typescript-eslint/typescript-estree": "8.53.2-alpha.5",
54
+ "@typescript-eslint/typescript-estree": "8.53.2-alpha.6",
55
55
  "@vitest/coverage-v8": "^3.2.4",
56
56
  "@vitest/pretty-format": "^3.2.4",
57
57
  "eslint": "*",