Skip to content

Commit 927272d

Browse files
authored
fix: correct Scope typings (#20198)
1 parent c7d3229 commit 927272d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/types/index.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export namespace Scope {
148148
references: Reference[];
149149
through: Reference[];
150150
functionExpressionScope: boolean;
151+
implicit?: {
152+
variables: Variable[];
153+
set: Map<string, Variable>;
154+
};
151155
}
152156

153157
interface Variable {
@@ -188,7 +192,14 @@ export namespace Scope {
188192
node: ESTree.FunctionDeclaration | ESTree.FunctionExpression;
189193
parent: null;
190194
}
191-
| { type: "ImplicitGlobalVariable"; node: ESTree.Program; parent: null }
195+
| {
196+
type: "ImplicitGlobalVariable";
197+
node:
198+
| ESTree.AssignmentExpression
199+
| ESTree.ForInStatement
200+
| ESTree.ForOfStatement;
201+
parent: null;
202+
}
192203
| {
193204
type: "ImportBinding";
194205
node:

tests/lib/types/types.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ const scopeManager: Scope.ScopeManager = {
499499

500500
const scope = scopeManager.scopes[0];
501501

502+
scope.implicit;
503+
scope.implicit?.variables;
504+
scope.implicit?.set;
505+
502506
const variable = scope.variables[0];
503507

504508
variable.name = "foo";
@@ -524,6 +528,37 @@ reference.isWrite();
524528
reference.isWriteOnly();
525529
reference.isReadWrite();
526530

531+
let catchDef!: Extract<Scope.DefinitionType, { type: "CatchClause" }>;
532+
catchDef.node; // $ExpectType CatchClause
533+
catchDef.parent; // $ExpectType null
534+
535+
let classNameDef!: Extract<Scope.DefinitionType, { type: "ClassName" }>;
536+
classNameDef.node; // $ExpectType ClassDeclaration | ClassExpression
537+
classNameDef.parent; // $ExpectType null
538+
539+
let functionNameDef!: Extract<Scope.DefinitionType, { type: "FunctionName" }>;
540+
functionNameDef.node; // $ExpectType FunctionDeclaration | FunctionExpression
541+
functionNameDef.parent; // $ExpectType null
542+
543+
let implicitGlobalVarDef!: Extract<
544+
Scope.DefinitionType,
545+
{ type: "ImplicitGlobalVariable" }
546+
>;
547+
implicitGlobalVarDef.node; // $ExpectType AssignmentExpression | ForInStatement | ForOfStatement
548+
implicitGlobalVarDef.parent; // $ExpectType null
549+
550+
let importBindingDef!: Extract<Scope.DefinitionType, { type: "ImportBinding" }>;
551+
importBindingDef.node; // $ExpectType ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
552+
importBindingDef.parent; // $ExpectType ImportDeclaration
553+
554+
let parameterDef!: Extract<Scope.DefinitionType, { type: "Parameter" }>;
555+
parameterDef.node; // $ExpectType FunctionDeclaration | FunctionExpression | ArrowFunctionExpression
556+
parameterDef.parent; // $ExpectType null
557+
558+
let variableDef!: Extract<Scope.DefinitionType, { type: "Variable" }>;
559+
variableDef.node; // $ExpectType VariableDeclarator
560+
variableDef.parent; // $ExpectType VariableDeclaration
561+
527562
// #endregion
528563

529564
// #region Rule

0 commit comments

Comments
 (0)