Skip to content
Merged
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
72 changes: 72 additions & 0 deletions types/eslint-scope/eslint-scope-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,75 @@ variableInstance.defs;
variableInstance.tainted;
// $ExpectType boolean
variableInstance.stack;

let node: any;
if (eslintScope.PatternVisitor.isPattern(node)) {
// $ExpectType Identifier | ObjectPattern | ArrayPattern | SpreadElement | RestElement | AssignmentPattern
node;
}

declare let rootPattern: estree.Pattern;

// // $ExpectType PatternVisitor
const patternVisitor = new eslintScope.PatternVisitor(
{
fallback: (node: any) => Object.keys(node).filter((key) => key !== "parent"),
childVisitorKeys: { TestExpression: ["argument"] },
},
rootPattern,
(pattern, misc) => {
// $ExpectType Identifier
pattern;
// $ExpectType AssignmentPattern[]
misc.assignments;
// $ExpectType boolean
misc.rest;
// $ExpectType boolean
misc.topLevel;
},
);

// $ExpectType Pattern
patternVisitor.rootPattern;

// $ExpectType PatternVisitorCallback
patternVisitor.callback;

// $ExpectType AssignmentPattern[]
patternVisitor.assignments;

// $ExpectType Expression[]
patternVisitor.rightHandNodes;

// $ExpectType RestElement[]
patternVisitor.restElements;

// $ExpectType (pattern: Identifier) => void
patternVisitor.Identifier;

// $ExpectType (pattern: Property) => void
patternVisitor.Property;

// $ExpectType (pattern: ArrayPattern) => void
patternVisitor.ArrayPattern;

// $ExpectType (pattern: AssignmentPattern) => void
patternVisitor.AssignmentPattern;

// $ExpectType (pattern: RestElement) => void
patternVisitor.RestElement;

// $ExpectType (pattern: MemberExpression) => void
patternVisitor.MemberExpression;

// $ExpectType (pattern: SpreadElement) => void
patternVisitor.SpreadElement;

// $ExpectType (pattern: SpreadElement) => void
patternVisitor.ArrayExpression;

// $ExpectType (pattern: AssignmentExpression) => void
patternVisitor.AssignmentExpression;

// $ExpectType (pattern: CallExpression) => void
patternVisitor.CallExpression;
60 changes: 60 additions & 0 deletions types/eslint-scope/index.d.cts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as eslint from "eslint";
import { VisitorKeys } from "eslint-visitor-keys";
import { Visitor, VisitorOptions } from "esrecurse";
import * as ESTree from "estree";

/**
Expand Down Expand Up @@ -60,6 +61,16 @@ export interface AnalyzeOptions {
*/
jsx?: boolean;
}

export type PatternVisitorCallback = (
pattern: ESTree.Identifier,
misc: {
topLevel: boolean;
rest: boolean;
assignments: ESTree.AssignmentPattern[];
},
) => void;

/**
* Manages the scope hierarchy of an AST.
*/
Expand Down Expand Up @@ -511,6 +522,55 @@ export class Definition {
kind: string | null;
}

/**
* Visitor for destructuring patterns.
*/
export class PatternVisitor extends Visitor {
static isPattern(node: ESTree.Node): node is
| ESTree.Identifier
| ESTree.ObjectPattern
| ESTree.ArrayPattern
| ESTree.SpreadElement
| ESTree.RestElement
| ESTree.AssignmentPattern;

constructor(
options: VisitorOptions | null | undefined,
rootPattern: ESTree.Pattern,
callback: PatternVisitorCallback,
);

rootPattern: ESTree.Pattern;

callback: PatternVisitorCallback;

assignments: ESTree.AssignmentPattern[];

rightHandNodes: ESTree.Expression[];

restElements: ESTree.RestElement[];

Identifier(pattern: ESTree.Identifier): void;

Property(pattern: ESTree.Property): void;

ArrayPattern(pattern: ESTree.ArrayPattern): void;

AssignmentPattern(pattern: ESTree.AssignmentPattern): void;

RestElement(pattern: ESTree.RestElement): void;

MemberExpression(pattern: ESTree.MemberExpression): void;

SpreadElement(pattern: ESTree.SpreadElement): void;

ArrayExpression(pattern: ESTree.SpreadElement): void;

AssignmentExpression(pattern: ESTree.AssignmentExpression): void;

CallExpression(pattern: ESTree.CallExpression): void;
}

/**
* Analyzes the scope of an AST.
* @param ast The ESTree-compliant AST to analyze.
Expand Down
4 changes: 4 additions & 0 deletions types/eslint-scope/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
FunctionScope,
GlobalScope,
ModuleScope,
PatternVisitor,
type PatternVisitorCallback,
Reference,
Scope,
ScopeManager,
Expand All @@ -20,6 +22,8 @@ export {
FunctionScope,
GlobalScope,
ModuleScope,
PatternVisitor,
type PatternVisitorCallback,
Reference,
Scope,
ScopeManager,
Expand Down
4 changes: 3 additions & 1 deletion types/eslint-scope/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
],
"dependencies": {
"@types/eslint": "*",
"@types/estree": "*"
"@types/esrecurse": "*",
"@types/estree": "*",
"eslint-visitor-keys": "*"
},
"devDependencies": {
"@types/eslint-scope": "workspace:.",
Expand Down