Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions types/eslint-scope/eslint-scope-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,21 @@ const scope = scopeManager.acquire(ast);
scopeManager.release(ast);

if (scope) {
// $ExpectType "function" | "module" | "block" | "catch" | "class" | "for" | "function-expression-name" | "global" | "switch" | "with" | "TDZ"
scope.type;
((
type:
| "function"
| "module"
| "block"
| "catch"
| "class"
| "class-field-initializer"
| "class-static-block"
| "for"
| "function-expression-name"
| "global"
| "switch"
| "with",
) => type satisfies typeof scope.type);
// $ExpectType boolean
scope.isStrict;
// $ExpectType Scope<Variable<Reference>, Reference> | null
Expand All @@ -58,6 +71,10 @@ if (scope) {
scope.functionExpressionScope;
// $ExpectType Reference[]
scope.implicit.left;
// $ExpectType Map<string, Variable<Reference>>
scope.implicit.set;
// $ExpectType Variable<Reference>[]
scope.implicit.variables;
// $ExpectType Map<string, Variable>
scope.set;
// $ExpectType Reference[]
Expand Down Expand Up @@ -94,8 +111,16 @@ if (reference) {

const definition = variable?.defs[0];
if (definition) {
// $ExpectType "CatchClause" | "TDZ" | "ClassName" | "FunctionName" | "ImplicitGlobalVariable" | "ImportBinding" | "Parameter" | "Variable"
definition.type;
((
type:
| "CatchClause"
| "ClassName"
| "FunctionName"
| "ImplicitGlobalVariable"
| "ImportBinding"
| "Parameter"
| "Variable",
) => type satisfies typeof definition.type);
// $ExpectType Identifier
definition.name;
// $ExpectType ImportDeclaration | VariableDeclaration | null
Expand Down Expand Up @@ -126,8 +151,16 @@ const definition2 = new eslintScope.Definition(
null,
"let",
);
// $ExpectType "CatchClause" | "TDZ" | "ClassName" | "FunctionName" | "ImplicitGlobalVariable" | "ImportBinding" | "Parameter" | "Variable"
definition2.type;
((
type:
| "CatchClause"
| "ClassName"
| "FunctionName"
| "ImplicitGlobalVariable"
| "ImportBinding"
| "Parameter"
| "Variable",
) => type satisfies typeof definition2.type);
// $ExpectType Identifier
definition2.name;

Expand Down Expand Up @@ -178,8 +211,21 @@ const scopeInstance = new eslintScope.Scope(
ast,
false,
);
// $ExpectType "function" | "module" | "block" | "catch" | "class" | "for" | "function-expression-name" | "global" | "switch" | "with" | "TDZ"
scopeInstance.type;
((
type:
| "function"
| "module"
| "block"
| "catch"
| "class"
| "class-field-initializer"
| "class-static-block"
| "for"
| "function-expression-name"
| "global"
| "switch"
| "with",
) => type satisfies typeof scopeInstance.type);
// $ExpectType boolean
scopeInstance.isStrict;
// $ExpectType Scope<Variable<Reference>, Reference> | null
Expand All @@ -196,7 +242,7 @@ scopeInstance.childScopes;
scopeInstance.block;
// $ExpectType boolean
scopeInstance.functionExpressionScope;
// $ExpectType { left: Reference[]; set: Map<string, Variable<Reference>>; }
// $ExpectType { left: Reference[]; set: Map<string, Variable<Reference>>; variables: Variable<Reference>[]; }
scopeInstance.implicit;
// $ExpectType Map<string, Variable>
scopeInstance.set;
Expand Down Expand Up @@ -229,6 +275,8 @@ const scopeManagerInstance = new eslintScope.ScopeManager({
scopeManagerInstance.globalScope;
// $ExpectType Scope<Variable<Reference>, Reference>[]
scopeManagerInstance.scopes;
// $ExpectType void
scopeManagerInstance.addGlobals(["window", "self"]);
// $ExpectType Scope<Variable<Reference>, Reference> | null
scopeManagerInstance.acquire(ast);
// $ExpectType Scope<Variable<Reference>, Reference>[] | null
Expand Down
9 changes: 8 additions & 1 deletion types/eslint-scope/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export class ScopeManager implements eslint.Scope.ScopeManager {
*/
scopes: Scope[];

/**
* Adds variables to the global scope and resolves references to them.
* @param names An array of strings, the names of variables to add to the global scope.
* @returns void
*/
addGlobals(names: ReadonlyArray<string>): void;

/**
* Acquires the scope for a given node.
* @param node The AST node to get the scope for.
Expand Down Expand Up @@ -201,7 +208,7 @@ export class Scope<TVariable extends Variable = Variable, TReference extends Ref
/**
* Implicit references (e.g., 'arguments' in functions).
*/
implicit: { left: TReference[]; set: Map<string, Variable> };
implicit: { left: TReference[]; set: Map<string, Variable>; variables: Variable[] };

/**
* Map of variable names to variables.
Expand Down
8 changes: 5 additions & 3 deletions types/eslint-scope/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"private": true,
"name": "@types/eslint-scope",
"version": "8.3.9999",
"version": "9.0.9999",
"type": "module",
"projects": [
"https://github.com/eslint/eslint-scope"
"https://github.com/eslint/js/tree/main/packages/eslint-scope"
],
"dependencies": {
"@types/eslint": "*",
"@types/esrecurse": "*",
"@types/estree": "*",
"eslint-visitor-keys": "*"
Expand All @@ -16,6 +15,9 @@
"@types/eslint-scope": "workspace:.",
"@types/espree": "*"
},
"peerDependencies": {
"eslint": "*"
},
"exports": {
".": {
"import": "./index.d.mts",
Expand Down
1 change: 1 addition & 0 deletions types/eslint-scope/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"exactOptionalPropertyTypes": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
Expand Down