Skip to content

Commit 3658660

Browse files
committed
remove dependency on eslint types
1 parent c6d38b2 commit 3658660

File tree

3 files changed

+89
-47
lines changed

3 files changed

+89
-47
lines changed

packages/eslint-scope/lib/definition.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ import Variable from "./variable.js";
2626

2727
/** @import * as types from "eslint-scope" */
2828

29+
// Cannot implement `types.Definition` directly because it contains a union.
2930
/**
3031
* @constructor Definition
31-
* @implements {types.Definition}
32+
* @implements {Omit<types.Definition, never>}
3233
*/
3334
class Definition {
3435
constructor(type, name, node, parent, index, kind) {

packages/eslint-scope/lib/index.d.cts

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
* SOFTWARE
2626
*/
2727

28-
import * as eslint from "eslint";
2928
import { VisitorKeys } from "eslint-visitor-keys";
3029
import { Visitor, VisitorOptions } from "esrecurse";
3130
import * as ESTree from "estree";
@@ -101,7 +100,7 @@ export type PatternVisitorCallback = (
101100
/**
102101
* Manages the scope hierarchy of an AST.
103102
*/
104-
export class ScopeManager implements eslint.Scope.ScopeManager {
103+
export class ScopeManager {
105104
/**
106105
* Creates a new ScopeManager instance.
107106
* @param options Options for scope analysis.
@@ -172,9 +171,7 @@ export class ScopeManager implements eslint.Scope.ScopeManager {
172171
/**
173172
* Base export class for all scopes.
174173
*/
175-
export class Scope<TVariable extends Variable = Variable, TReference extends Reference = Reference>
176-
implements eslint.Scope.Scope
177-
{
174+
export class Scope<TVariable extends Variable = Variable, TReference extends Reference = Reference> {
178175
/**
179176
* Creates a new Scope instance.
180177
* @param scopeManager The scope manager this scope belongs to.
@@ -194,7 +191,19 @@ export class Scope<TVariable extends Variable = Variable, TReference extends Ref
194191
/**
195192
* The type of the scope (e.g., 'global', 'function').
196193
*/
197-
type: eslint.Scope.Scope["type"];
194+
type:
195+
| "block"
196+
| "catch"
197+
| "class"
198+
| "class-field-initializer"
199+
| "class-static-block"
200+
| "for"
201+
| "function"
202+
| "function-expression-name"
203+
| "global"
204+
| "module"
205+
| "switch"
206+
| "with";
198207

199208
/**
200209
* Whether the scope is in strict mode.
@@ -239,18 +248,18 @@ export class Scope<TVariable extends Variable = Variable, TReference extends Ref
239248
/**
240249
* Map of variable names to variables.
241250
*/
242-
set: eslint.Scope.Scope["set"];
251+
set: Map<string, TVariable>;
243252

244253
/**
245254
* The tainted variables of this scope.
246255
* @deprecated
247256
*/
248-
taints: Map<string, Variable>;
257+
taints: Map<string, TVariable>;
249258

250259
/**
251260
* References that pass through this scope to outer scopes.
252261
*/
253-
through: eslint.Scope.Scope["through"];
262+
through: TReference[];
254263

255264
/**
256265
* Dynamic flag for certain scope types.
@@ -275,7 +284,7 @@ export class Scope<TVariable extends Variable = Variable, TReference extends Ref
275284
* @param ident An AST node to get their reference object.
276285
* @deprecated
277286
*/
278-
resolve(ident: ESTree.Identifier): Reference | null;
287+
resolve(ident: ESTree.Identifier): TReference | null;
279288

280289
/**
281290
* Whether the reference is static.
@@ -348,7 +357,7 @@ export class FunctionExpressionNameScope extends Scope {
348357
* @param block The AST node that created this scope.
349358
*/
350359
constructor(scopeManager: ScopeManager, upperScope: Scope, block: ESTree.Node);
351-
360+
352361
type: "function-expression-name";
353362

354363
functionExpressionScope: true;
@@ -516,7 +525,7 @@ export class ClassStaticBlockScope extends Scope {
516525
/**
517526
* Represents a variable in a scope.
518527
*/
519-
export class Variable<TReference extends Reference = Reference> implements eslint.Scope.Variable {
528+
export class Variable<TReference extends Reference = Reference> {
520529
/**
521530
* Creates a new Variable instance.
522531
* @param name The name of the variable.
@@ -547,7 +556,7 @@ export class Variable<TReference extends Reference = Reference> implements eslin
547556
/**
548557
* Definitions of this variable.
549558
*/
550-
defs: eslint.Scope.Definition[];
559+
defs: Definition[];
551560

552561
/**
553562
* Whether the variable is tainted (e.g., potentially modified externally).
@@ -565,7 +574,7 @@ export class Variable<TReference extends Reference = Reference> implements eslin
565574
/**
566575
* Represents a reference to a variable.
567576
*/
568-
export class Reference implements eslint.Scope.Reference {
577+
export class Reference {
569578
/**
570579
* Creates a new Reference instance.
571580
* @param ident The identifier node of the reference.
@@ -605,12 +614,12 @@ export class Reference implements eslint.Scope.Reference {
605614
/**
606615
* Whether this is a write operation.
607616
*/
608-
isWrite: eslint.Scope.Reference["isWrite"];
617+
isWrite(): boolean;
609618

610619
/**
611620
* Whether this is a read operation.
612621
*/
613-
isRead: eslint.Scope.Reference["isRead"];
622+
isRead(): boolean;
614623

615624
/**
616625
* The scope where the reference occurs.
@@ -664,9 +673,8 @@ export class Reference implements eslint.Scope.Reference {
664673

665674
/**
666675
* Represents a variable definition.
667-
* @todo extends eslint.Scope.Definition for this class
668676
*/
669-
export class Definition {
677+
export const Definition: {
670678
/**
671679
* Creates a new Definition instance.
672680
* @param type The type of definition (e.g., 'Variable', 'Parameter').
@@ -676,34 +684,57 @@ export class Definition {
676684
* @param index The index of the definition in a pattern, if applicable.
677685
* @param kind The kind of variable (e.g., 'var', 'let', 'const'), if applicable.
678686
*/
679-
constructor(
680-
type: eslint.Scope.Definition["type"],
681-
name: eslint.Scope.Definition["name"],
682-
node: eslint.Scope.Definition["node"],
683-
parent: eslint.Scope.Definition["parent"],
687+
new (
688+
type: Definition["type"],
689+
name: ESTree.Identifier,
690+
node: Definition["node"],
691+
parent: Definition["parent"],
684692
index: number | null,
685693
kind: string | null,
686-
);
687-
688-
/**
689-
* The type of definition (e.g., 'Variable', 'Parameter').
690-
*/
691-
type: eslint.Scope.Definition["type"];
694+
): Definition;
695+
};
696+
697+
export type Definition = (
698+
| { type: "CatchClause"; node: ESTree.CatchClause; parent: null }
699+
| {
700+
type: "ClassName";
701+
node: ESTree.ClassDeclaration | ESTree.ClassExpression;
702+
parent: null;
703+
} | {
704+
type: "FunctionName";
705+
node: ESTree.FunctionDeclaration | ESTree.FunctionExpression;
706+
parent: null;
707+
} | {
708+
type: "ImplicitGlobalVariable";
709+
node:
710+
| ESTree.AssignmentExpression
711+
| ESTree.ForInStatement
712+
| ESTree.ForOfStatement;
713+
parent: null;
714+
} | {
715+
type: "ImportBinding";
716+
node:
717+
| ESTree.ImportSpecifier
718+
| ESTree.ImportDefaultSpecifier
719+
| ESTree.ImportNamespaceSpecifier;
720+
parent: ESTree.ImportDeclaration;
721+
} | {
722+
type: "Parameter";
723+
node:
724+
| ESTree.FunctionDeclaration
725+
| ESTree.FunctionExpression
726+
| ESTree.ArrowFunctionExpression;
727+
parent: null;
728+
} | {
729+
type: "Variable";
730+
node: ESTree.VariableDeclarator;
731+
parent: ESTree.VariableDeclaration;
732+
}) & {
692733

693734
/**
694735
* The identifier node of the definition.
695736
*/
696-
name: eslint.Scope.Definition["name"];
697-
698-
/**
699-
* The AST node where the definition occurs.
700-
*/
701-
node: eslint.Scope.Definition["node"];
702-
703-
/**
704-
* The parent node, if applicable.
705-
*/
706-
parent: eslint.Scope.Definition["parent"];
737+
name: ESTree.Identifier;
707738

708739
/**
709740
* The index of the definition in a pattern, if applicable.
@@ -716,7 +747,7 @@ export class Definition {
716747
* @deprecated
717748
*/
718749
kind: string | null;
719-
}
750+
};
720751

721752
/**
722753
* Visitor for destructuring patterns.

packages/eslint-scope/tests/types/types.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
* SOFTWARE
2626
*/
2727

28+
import * as eslint from "eslint";
2829
import * as eslintScope from "eslint-scope";
29-
import type { AnalyzeOptions } from "eslint-scope";
3030
import * as espree from "espree";
3131
import * as estree from "estree";
3232

@@ -50,7 +50,7 @@ const scopeManager = eslintScope.analyze(
5050
impliedStrict: false,
5151
childVisitorKeys: null,
5252
fallback: "iteration",
53-
} satisfies AnalyzeOptions,
53+
} satisfies eslintScope.AnalyzeOptions,
5454
);
5555

5656
// $ExpectType GlobalScope | null
@@ -96,7 +96,7 @@ if (scope) {
9696
scope.block;
9797
// $ExpectType boolean
9898
scope.functionExpressionScope;
99-
// $ExpectType Map<string, Variable>
99+
// $ExpectType Map<string, Variable<Reference>>
100100
scope.set;
101101
// $ExpectType Reference[]
102102
scope.through;
@@ -169,7 +169,7 @@ const identifier: estree.Identifier = {
169169
const definition2 = new eslintScope.Definition(
170170
"Variable",
171171
identifier,
172-
ast,
172+
{ type: "VariableDeclarator", id: identifier },
173173
null,
174174
null,
175175
"let",
@@ -335,7 +335,7 @@ scopeInstance.childScopes;
335335
scopeInstance.block;
336336
// $ExpectType boolean
337337
scopeInstance.functionExpressionScope;
338-
// $ExpectType Map<string, Variable>
338+
// $ExpectType Map<string, Variable<Reference>>
339339
scopeInstance.set;
340340
// $ExpectType Map<string, Variable<Reference>>
341341
scopeInstance.taints;
@@ -472,3 +472,13 @@ patternVisitor.AssignmentExpression;
472472

473473
// $ExpectType (pattern: CallExpression) => void
474474
patternVisitor.CallExpression;
475+
476+
(definition: eslintScope.Definition) => definition satisfies eslint.Scope.Definition;
477+
478+
(reference: eslintScope.Reference) => reference satisfies eslint.Scope.Reference;
479+
480+
(scope: eslintScope.Scope) => scope satisfies eslint.Scope.Scope;
481+
482+
(scopeManager: eslintScope.ScopeManager) => scopeManager satisfies eslint.Scope.ScopeManager;
483+
484+
(variable: eslintScope.Variable) => variable satisfies eslint.Scope.Variable;

0 commit comments

Comments
 (0)