Skip to content

Commit e0245ae

Browse files
authored
🤖 Merge PR DefinitelyTyped#73284 feat(eslint-scope): add missing PatternVisitor class export by @hkleungai
1 parent 2b745f7 commit e0245ae

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

‎types/eslint-scope/eslint-scope-tests.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,75 @@ variableInstance.defs;
261261
variableInstance.tainted;
262262
// $ExpectType boolean
263263
variableInstance.stack;
264+
265+
let node: any;
266+
if (eslintScope.PatternVisitor.isPattern(node)) {
267+
// $ExpectType Identifier | ObjectPattern | ArrayPattern | SpreadElement | RestElement | AssignmentPattern
268+
node;
269+
}
270+
271+
declare let rootPattern: estree.Pattern;
272+
273+
// // $ExpectType PatternVisitor
274+
const patternVisitor = new eslintScope.PatternVisitor(
275+
{
276+
fallback: (node: any) => Object.keys(node).filter((key) => key !== "parent"),
277+
childVisitorKeys: { TestExpression: ["argument"] },
278+
},
279+
rootPattern,
280+
(pattern, misc) => {
281+
// $ExpectType Identifier
282+
pattern;
283+
// $ExpectType AssignmentPattern[]
284+
misc.assignments;
285+
// $ExpectType boolean
286+
misc.rest;
287+
// $ExpectType boolean
288+
misc.topLevel;
289+
},
290+
);
291+
292+
// $ExpectType Pattern
293+
patternVisitor.rootPattern;
294+
295+
// $ExpectType PatternVisitorCallback
296+
patternVisitor.callback;
297+
298+
// $ExpectType AssignmentPattern[]
299+
patternVisitor.assignments;
300+
301+
// $ExpectType Expression[]
302+
patternVisitor.rightHandNodes;
303+
304+
// $ExpectType RestElement[]
305+
patternVisitor.restElements;
306+
307+
// $ExpectType (pattern: Identifier) => void
308+
patternVisitor.Identifier;
309+
310+
// $ExpectType (pattern: Property) => void
311+
patternVisitor.Property;
312+
313+
// $ExpectType (pattern: ArrayPattern) => void
314+
patternVisitor.ArrayPattern;
315+
316+
// $ExpectType (pattern: AssignmentPattern) => void
317+
patternVisitor.AssignmentPattern;
318+
319+
// $ExpectType (pattern: RestElement) => void
320+
patternVisitor.RestElement;
321+
322+
// $ExpectType (pattern: MemberExpression) => void
323+
patternVisitor.MemberExpression;
324+
325+
// $ExpectType (pattern: SpreadElement) => void
326+
patternVisitor.SpreadElement;
327+
328+
// $ExpectType (pattern: SpreadElement) => void
329+
patternVisitor.ArrayExpression;
330+
331+
// $ExpectType (pattern: AssignmentExpression) => void
332+
patternVisitor.AssignmentExpression;
333+
334+
// $ExpectType (pattern: CallExpression) => void
335+
patternVisitor.CallExpression;

‎types/eslint-scope/index.d.cts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as eslint from "eslint";
22
import { VisitorKeys } from "eslint-visitor-keys";
3+
import { Visitor, VisitorOptions } from "esrecurse";
34
import * as ESTree from "estree";
45

56
/**
@@ -60,6 +61,16 @@ export interface AnalyzeOptions {
6061
*/
6162
jsx?: boolean;
6263
}
64+
65+
export type PatternVisitorCallback = (
66+
pattern: ESTree.Identifier,
67+
misc: {
68+
topLevel: boolean;
69+
rest: boolean;
70+
assignments: ESTree.AssignmentPattern[];
71+
},
72+
) => void;
73+
6374
/**
6475
* Manages the scope hierarchy of an AST.
6576
*/
@@ -511,6 +522,55 @@ export class Definition {
511522
kind: string | null;
512523
}
513524

525+
/**
526+
* Visitor for destructuring patterns.
527+
*/
528+
export class PatternVisitor extends Visitor {
529+
static isPattern(node: ESTree.Node): node is
530+
| ESTree.Identifier
531+
| ESTree.ObjectPattern
532+
| ESTree.ArrayPattern
533+
| ESTree.SpreadElement
534+
| ESTree.RestElement
535+
| ESTree.AssignmentPattern;
536+
537+
constructor(
538+
options: VisitorOptions | null | undefined,
539+
rootPattern: ESTree.Pattern,
540+
callback: PatternVisitorCallback,
541+
);
542+
543+
rootPattern: ESTree.Pattern;
544+
545+
callback: PatternVisitorCallback;
546+
547+
assignments: ESTree.AssignmentPattern[];
548+
549+
rightHandNodes: ESTree.Expression[];
550+
551+
restElements: ESTree.RestElement[];
552+
553+
Identifier(pattern: ESTree.Identifier): void;
554+
555+
Property(pattern: ESTree.Property): void;
556+
557+
ArrayPattern(pattern: ESTree.ArrayPattern): void;
558+
559+
AssignmentPattern(pattern: ESTree.AssignmentPattern): void;
560+
561+
RestElement(pattern: ESTree.RestElement): void;
562+
563+
MemberExpression(pattern: ESTree.MemberExpression): void;
564+
565+
SpreadElement(pattern: ESTree.SpreadElement): void;
566+
567+
ArrayExpression(pattern: ESTree.SpreadElement): void;
568+
569+
AssignmentExpression(pattern: ESTree.AssignmentExpression): void;
570+
571+
CallExpression(pattern: ESTree.CallExpression): void;
572+
}
573+
514574
/**
515575
* Analyzes the scope of an AST.
516576
* @param ast The ESTree-compliant AST to analyze.

‎types/eslint-scope/index.d.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
FunctionScope,
77
GlobalScope,
88
ModuleScope,
9+
PatternVisitor,
10+
type PatternVisitorCallback,
911
Reference,
1012
Scope,
1113
ScopeManager,
@@ -20,6 +22,8 @@ export {
2022
FunctionScope,
2123
GlobalScope,
2224
ModuleScope,
25+
PatternVisitor,
26+
type PatternVisitorCallback,
2327
Reference,
2428
Scope,
2529
ScopeManager,

‎types/eslint-scope/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
],
99
"dependencies": {
1010
"@types/eslint": "*",
11-
"@types/estree": "*"
11+
"@types/esrecurse": "*",
12+
"@types/estree": "*",
13+
"eslint-visitor-keys": "*"
1214
},
1315
"devDependencies": {
1416
"@types/eslint-scope": "workspace:.",

0 commit comments

Comments
 (0)