@vitest/utils 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/ast.d.ts +723 -0
  2. package/dist/ast.js +180 -0
  3. package/package.json +8 -2
package/dist/ast.d.ts ADDED
@@ -0,0 +1,723 @@
1
+ // This definition file follows a somewhat unusual format. ESTree allows
2
+ // runtime type checks based on the `type` parameter. In order to explain this
3
+ // to typescript we want to use discriminated union types:
4
+ // https://github.com/Microsoft/TypeScript/pull/9163
5
+ //
6
+ // For ESTree this is a bit tricky because the high level interfaces like
7
+ // Node or Function are pulling double duty. We want to pass common fields down
8
+ // to the interfaces that extend them (like Identifier or
9
+ // ArrowFunctionExpression), but you can't extend a type union or enforce
10
+ // common fields on them. So we've split the high level interfaces into two
11
+ // types, a base type which passes down inherited fields, and a type union of
12
+ // all types which extend the base type. Only the type union is exported, and
13
+ // the union is how other types refer to the collection of inheriting types.
14
+ //
15
+ // This makes the definitions file here somewhat more difficult to maintain,
16
+ // but it has the notable advantage of making ESTree much easier to use as
17
+ // an end user.
18
+
19
+ interface BaseNodeWithoutComments {
20
+ // Every leaf interface that extends BaseNode must specify a type property.
21
+ // The type property should be a string literal. For example, Identifier
22
+ // has: `type: "Identifier"`
23
+ type: string;
24
+ loc?: SourceLocation | null | undefined;
25
+ range?: [number, number] | undefined;
26
+ }
27
+
28
+ interface BaseNode extends BaseNodeWithoutComments {
29
+ leadingComments?: Comment[] | undefined;
30
+ trailingComments?: Comment[] | undefined;
31
+ }
32
+
33
+ interface NodeMap {
34
+ AssignmentProperty: AssignmentProperty;
35
+ CatchClause: CatchClause;
36
+ Class: Class;
37
+ ClassBody: ClassBody;
38
+ Expression: Expression;
39
+ Function: Function;
40
+ Identifier: Identifier;
41
+ Literal: Literal;
42
+ MethodDefinition: MethodDefinition;
43
+ ModuleDeclaration: ModuleDeclaration;
44
+ ModuleSpecifier: ModuleSpecifier;
45
+ Pattern: Pattern;
46
+ PrivateIdentifier: PrivateIdentifier;
47
+ Program: Program;
48
+ Property: Property;
49
+ PropertyDefinition: PropertyDefinition;
50
+ SpreadElement: SpreadElement;
51
+ Statement: Statement;
52
+ Super: Super;
53
+ SwitchCase: SwitchCase;
54
+ TemplateElement: TemplateElement;
55
+ VariableDeclarator: VariableDeclarator;
56
+ }
57
+
58
+ type Node$1 = NodeMap[keyof NodeMap];
59
+
60
+ interface Comment extends BaseNodeWithoutComments {
61
+ type: "Line" | "Block";
62
+ value: string;
63
+ }
64
+
65
+ interface SourceLocation {
66
+ source?: string | null | undefined;
67
+ start: Position;
68
+ end: Position;
69
+ }
70
+
71
+ interface Position {
72
+ /** >= 1 */
73
+ line: number;
74
+ /** >= 0 */
75
+ column: number;
76
+ }
77
+
78
+ interface Program extends BaseNode {
79
+ type: "Program";
80
+ sourceType: "script" | "module";
81
+ body: Array<Directive | Statement | ModuleDeclaration>;
82
+ comments?: Comment[] | undefined;
83
+ }
84
+
85
+ interface Directive extends BaseNode {
86
+ type: "ExpressionStatement";
87
+ expression: Literal;
88
+ directive: string;
89
+ }
90
+
91
+ interface BaseFunction extends BaseNode {
92
+ params: Pattern[];
93
+ generator?: boolean | undefined;
94
+ async?: boolean | undefined;
95
+ // The body is either BlockStatement or Expression because arrow functions
96
+ // can have a body that's either. FunctionDeclarations and
97
+ // FunctionExpressions have only BlockStatement bodies.
98
+ body: BlockStatement | Expression;
99
+ }
100
+
101
+ type Function = FunctionDeclaration | FunctionExpression | ArrowFunctionExpression;
102
+
103
+ type Statement =
104
+ | ExpressionStatement
105
+ | BlockStatement
106
+ | StaticBlock
107
+ | EmptyStatement
108
+ | DebuggerStatement
109
+ | WithStatement
110
+ | ReturnStatement
111
+ | LabeledStatement
112
+ | BreakStatement
113
+ | ContinueStatement
114
+ | IfStatement
115
+ | SwitchStatement
116
+ | ThrowStatement
117
+ | TryStatement
118
+ | WhileStatement
119
+ | DoWhileStatement
120
+ | ForStatement
121
+ | ForInStatement
122
+ | ForOfStatement
123
+ | Declaration;
124
+
125
+ interface BaseStatement extends BaseNode {}
126
+
127
+ interface EmptyStatement extends BaseStatement {
128
+ type: "EmptyStatement";
129
+ }
130
+
131
+ interface BlockStatement extends BaseStatement {
132
+ type: "BlockStatement";
133
+ body: Statement[];
134
+ innerComments?: Comment[] | undefined;
135
+ }
136
+
137
+ interface StaticBlock extends Omit<BlockStatement, "type"> {
138
+ type: "StaticBlock";
139
+ }
140
+
141
+ interface ExpressionStatement extends BaseStatement {
142
+ type: "ExpressionStatement";
143
+ expression: Expression;
144
+ }
145
+
146
+ interface IfStatement extends BaseStatement {
147
+ type: "IfStatement";
148
+ test: Expression;
149
+ consequent: Statement;
150
+ alternate?: Statement | null | undefined;
151
+ }
152
+
153
+ interface LabeledStatement extends BaseStatement {
154
+ type: "LabeledStatement";
155
+ label: Identifier;
156
+ body: Statement;
157
+ }
158
+
159
+ interface BreakStatement extends BaseStatement {
160
+ type: "BreakStatement";
161
+ label?: Identifier | null | undefined;
162
+ }
163
+
164
+ interface ContinueStatement extends BaseStatement {
165
+ type: "ContinueStatement";
166
+ label?: Identifier | null | undefined;
167
+ }
168
+
169
+ interface WithStatement extends BaseStatement {
170
+ type: "WithStatement";
171
+ object: Expression;
172
+ body: Statement;
173
+ }
174
+
175
+ interface SwitchStatement extends BaseStatement {
176
+ type: "SwitchStatement";
177
+ discriminant: Expression;
178
+ cases: SwitchCase[];
179
+ }
180
+
181
+ interface ReturnStatement extends BaseStatement {
182
+ type: "ReturnStatement";
183
+ argument?: Expression | null | undefined;
184
+ }
185
+
186
+ interface ThrowStatement extends BaseStatement {
187
+ type: "ThrowStatement";
188
+ argument: Expression;
189
+ }
190
+
191
+ interface TryStatement extends BaseStatement {
192
+ type: "TryStatement";
193
+ block: BlockStatement;
194
+ handler?: CatchClause | null | undefined;
195
+ finalizer?: BlockStatement | null | undefined;
196
+ }
197
+
198
+ interface WhileStatement extends BaseStatement {
199
+ type: "WhileStatement";
200
+ test: Expression;
201
+ body: Statement;
202
+ }
203
+
204
+ interface DoWhileStatement extends BaseStatement {
205
+ type: "DoWhileStatement";
206
+ body: Statement;
207
+ test: Expression;
208
+ }
209
+
210
+ interface ForStatement extends BaseStatement {
211
+ type: "ForStatement";
212
+ init?: VariableDeclaration | Expression | null | undefined;
213
+ test?: Expression | null | undefined;
214
+ update?: Expression | null | undefined;
215
+ body: Statement;
216
+ }
217
+
218
+ interface BaseForXStatement extends BaseStatement {
219
+ left: VariableDeclaration | Pattern;
220
+ right: Expression;
221
+ body: Statement;
222
+ }
223
+
224
+ interface ForInStatement extends BaseForXStatement {
225
+ type: "ForInStatement";
226
+ }
227
+
228
+ interface DebuggerStatement extends BaseStatement {
229
+ type: "DebuggerStatement";
230
+ }
231
+
232
+ type Declaration = FunctionDeclaration | VariableDeclaration | ClassDeclaration;
233
+
234
+ interface BaseDeclaration extends BaseStatement {}
235
+
236
+ interface MaybeNamedFunctionDeclaration extends BaseFunction, BaseDeclaration {
237
+ type: "FunctionDeclaration";
238
+ /** It is null when a function declaration is a part of the `export default function` statement */
239
+ id: Identifier | null;
240
+ body: BlockStatement;
241
+ }
242
+
243
+ interface FunctionDeclaration extends MaybeNamedFunctionDeclaration {
244
+ id: Identifier;
245
+ }
246
+
247
+ interface VariableDeclaration extends BaseDeclaration {
248
+ type: "VariableDeclaration";
249
+ declarations: VariableDeclarator[];
250
+ kind: "var" | "let" | "const";
251
+ }
252
+
253
+ interface VariableDeclarator extends BaseNode {
254
+ type: "VariableDeclarator";
255
+ id: Pattern;
256
+ init?: Expression | null | undefined;
257
+ }
258
+
259
+ interface ExpressionMap {
260
+ ArrayExpression: ArrayExpression;
261
+ ArrowFunctionExpression: ArrowFunctionExpression;
262
+ AssignmentExpression: AssignmentExpression;
263
+ AwaitExpression: AwaitExpression;
264
+ BinaryExpression: BinaryExpression;
265
+ CallExpression: CallExpression;
266
+ ChainExpression: ChainExpression;
267
+ ClassExpression: ClassExpression;
268
+ ConditionalExpression: ConditionalExpression;
269
+ FunctionExpression: FunctionExpression;
270
+ Identifier: Identifier;
271
+ ImportExpression: ImportExpression;
272
+ Literal: Literal;
273
+ LogicalExpression: LogicalExpression;
274
+ MemberExpression: MemberExpression;
275
+ MetaProperty: MetaProperty;
276
+ NewExpression: NewExpression;
277
+ ObjectExpression: ObjectExpression;
278
+ SequenceExpression: SequenceExpression;
279
+ TaggedTemplateExpression: TaggedTemplateExpression;
280
+ TemplateLiteral: TemplateLiteral;
281
+ ThisExpression: ThisExpression;
282
+ UnaryExpression: UnaryExpression;
283
+ UpdateExpression: UpdateExpression;
284
+ YieldExpression: YieldExpression;
285
+ }
286
+
287
+ type Expression = ExpressionMap[keyof ExpressionMap];
288
+
289
+ interface BaseExpression extends BaseNode {}
290
+
291
+ type ChainElement = SimpleCallExpression | MemberExpression;
292
+
293
+ interface ChainExpression extends BaseExpression {
294
+ type: "ChainExpression";
295
+ expression: ChainElement;
296
+ }
297
+
298
+ interface ThisExpression extends BaseExpression {
299
+ type: "ThisExpression";
300
+ }
301
+
302
+ interface ArrayExpression extends BaseExpression {
303
+ type: "ArrayExpression";
304
+ elements: Array<Expression | SpreadElement | null>;
305
+ }
306
+
307
+ interface ObjectExpression extends BaseExpression {
308
+ type: "ObjectExpression";
309
+ properties: Array<Property | SpreadElement>;
310
+ }
311
+
312
+ interface PrivateIdentifier extends BaseNode {
313
+ type: "PrivateIdentifier";
314
+ name: string;
315
+ }
316
+
317
+ interface Property extends BaseNode {
318
+ type: "Property";
319
+ key: Expression | PrivateIdentifier;
320
+ value: Expression | Pattern; // Could be an AssignmentProperty
321
+ kind: "init" | "get" | "set";
322
+ method: boolean;
323
+ shorthand: boolean;
324
+ computed: boolean;
325
+ }
326
+
327
+ interface PropertyDefinition extends BaseNode {
328
+ type: "PropertyDefinition";
329
+ key: Expression | PrivateIdentifier;
330
+ value?: Expression | null | undefined;
331
+ computed: boolean;
332
+ static: boolean;
333
+ }
334
+
335
+ interface FunctionExpression extends BaseFunction, BaseExpression {
336
+ id?: Identifier | null | undefined;
337
+ type: "FunctionExpression";
338
+ body: BlockStatement;
339
+ }
340
+
341
+ interface SequenceExpression extends BaseExpression {
342
+ type: "SequenceExpression";
343
+ expressions: Expression[];
344
+ }
345
+
346
+ interface UnaryExpression extends BaseExpression {
347
+ type: "UnaryExpression";
348
+ operator: UnaryOperator;
349
+ prefix: true;
350
+ argument: Expression;
351
+ }
352
+
353
+ interface BinaryExpression extends BaseExpression {
354
+ type: "BinaryExpression";
355
+ operator: BinaryOperator;
356
+ left: Expression;
357
+ right: Expression;
358
+ }
359
+
360
+ interface AssignmentExpression extends BaseExpression {
361
+ type: "AssignmentExpression";
362
+ operator: AssignmentOperator;
363
+ left: Pattern | MemberExpression;
364
+ right: Expression;
365
+ }
366
+
367
+ interface UpdateExpression extends BaseExpression {
368
+ type: "UpdateExpression";
369
+ operator: UpdateOperator;
370
+ argument: Expression;
371
+ prefix: boolean;
372
+ }
373
+
374
+ interface LogicalExpression extends BaseExpression {
375
+ type: "LogicalExpression";
376
+ operator: LogicalOperator;
377
+ left: Expression;
378
+ right: Expression;
379
+ }
380
+
381
+ interface ConditionalExpression extends BaseExpression {
382
+ type: "ConditionalExpression";
383
+ test: Expression;
384
+ alternate: Expression;
385
+ consequent: Expression;
386
+ }
387
+
388
+ interface BaseCallExpression extends BaseExpression {
389
+ callee: Expression | Super;
390
+ arguments: Array<Expression | SpreadElement>;
391
+ }
392
+ type CallExpression = SimpleCallExpression | NewExpression;
393
+
394
+ interface SimpleCallExpression extends BaseCallExpression {
395
+ type: "CallExpression";
396
+ optional: boolean;
397
+ }
398
+
399
+ interface NewExpression extends BaseCallExpression {
400
+ type: "NewExpression";
401
+ }
402
+
403
+ interface MemberExpression extends BaseExpression, BasePattern {
404
+ type: "MemberExpression";
405
+ object: Expression | Super;
406
+ property: Expression | PrivateIdentifier;
407
+ computed: boolean;
408
+ optional: boolean;
409
+ }
410
+
411
+ type Pattern = Identifier | ObjectPattern | ArrayPattern | RestElement | AssignmentPattern | MemberExpression;
412
+
413
+ interface BasePattern extends BaseNode {}
414
+
415
+ interface SwitchCase extends BaseNode {
416
+ type: "SwitchCase";
417
+ test?: Expression | null | undefined;
418
+ consequent: Statement[];
419
+ }
420
+
421
+ interface CatchClause extends BaseNode {
422
+ type: "CatchClause";
423
+ param: Pattern | null;
424
+ body: BlockStatement;
425
+ }
426
+
427
+ interface Identifier extends BaseNode, BaseExpression, BasePattern {
428
+ type: "Identifier";
429
+ name: string;
430
+ }
431
+
432
+ type Literal = SimpleLiteral | RegExpLiteral | BigIntLiteral;
433
+
434
+ interface SimpleLiteral extends BaseNode, BaseExpression {
435
+ type: "Literal";
436
+ value: string | boolean | number | null;
437
+ raw?: string | undefined;
438
+ }
439
+
440
+ interface RegExpLiteral extends BaseNode, BaseExpression {
441
+ type: "Literal";
442
+ value?: RegExp | null | undefined;
443
+ regex: {
444
+ pattern: string;
445
+ flags: string;
446
+ };
447
+ raw?: string | undefined;
448
+ }
449
+
450
+ interface BigIntLiteral extends BaseNode, BaseExpression {
451
+ type: "Literal";
452
+ value?: bigint | null | undefined;
453
+ bigint: string;
454
+ raw?: string | undefined;
455
+ }
456
+
457
+ type UnaryOperator = "-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
458
+
459
+ type BinaryOperator =
460
+ | "=="
461
+ | "!="
462
+ | "==="
463
+ | "!=="
464
+ | "<"
465
+ | "<="
466
+ | ">"
467
+ | ">="
468
+ | "<<"
469
+ | ">>"
470
+ | ">>>"
471
+ | "+"
472
+ | "-"
473
+ | "*"
474
+ | "/"
475
+ | "%"
476
+ | "**"
477
+ | "|"
478
+ | "^"
479
+ | "&"
480
+ | "in"
481
+ | "instanceof";
482
+
483
+ type LogicalOperator = "||" | "&&" | "??";
484
+
485
+ type AssignmentOperator =
486
+ | "="
487
+ | "+="
488
+ | "-="
489
+ | "*="
490
+ | "/="
491
+ | "%="
492
+ | "**="
493
+ | "<<="
494
+ | ">>="
495
+ | ">>>="
496
+ | "|="
497
+ | "^="
498
+ | "&="
499
+ | "||="
500
+ | "&&="
501
+ | "??=";
502
+
503
+ type UpdateOperator = "++" | "--";
504
+
505
+ interface ForOfStatement extends BaseForXStatement {
506
+ type: "ForOfStatement";
507
+ await: boolean;
508
+ }
509
+
510
+ interface Super extends BaseNode {
511
+ type: "Super";
512
+ }
513
+
514
+ interface SpreadElement extends BaseNode {
515
+ type: "SpreadElement";
516
+ argument: Expression;
517
+ }
518
+
519
+ interface ArrowFunctionExpression extends BaseExpression, BaseFunction {
520
+ type: "ArrowFunctionExpression";
521
+ expression: boolean;
522
+ body: BlockStatement | Expression;
523
+ }
524
+
525
+ interface YieldExpression extends BaseExpression {
526
+ type: "YieldExpression";
527
+ argument?: Expression | null | undefined;
528
+ delegate: boolean;
529
+ }
530
+
531
+ interface TemplateLiteral extends BaseExpression {
532
+ type: "TemplateLiteral";
533
+ quasis: TemplateElement[];
534
+ expressions: Expression[];
535
+ }
536
+
537
+ interface TaggedTemplateExpression extends BaseExpression {
538
+ type: "TaggedTemplateExpression";
539
+ tag: Expression;
540
+ quasi: TemplateLiteral;
541
+ }
542
+
543
+ interface TemplateElement extends BaseNode {
544
+ type: "TemplateElement";
545
+ tail: boolean;
546
+ value: {
547
+ /** It is null when the template literal is tagged and the text has an invalid escape (e.g. - tag`\unicode and \u{55}`) */
548
+ cooked?: string | null | undefined;
549
+ raw: string;
550
+ };
551
+ }
552
+
553
+ interface AssignmentProperty extends Property {
554
+ value: Pattern;
555
+ kind: "init";
556
+ method: boolean; // false
557
+ }
558
+
559
+ interface ObjectPattern extends BasePattern {
560
+ type: "ObjectPattern";
561
+ properties: Array<AssignmentProperty | RestElement>;
562
+ }
563
+
564
+ interface ArrayPattern extends BasePattern {
565
+ type: "ArrayPattern";
566
+ elements: Array<Pattern | null>;
567
+ }
568
+
569
+ interface RestElement extends BasePattern {
570
+ type: "RestElement";
571
+ argument: Pattern;
572
+ }
573
+
574
+ interface AssignmentPattern extends BasePattern {
575
+ type: "AssignmentPattern";
576
+ left: Pattern;
577
+ right: Expression;
578
+ }
579
+
580
+ type Class = ClassDeclaration | ClassExpression;
581
+ interface BaseClass extends BaseNode {
582
+ superClass?: Expression | null | undefined;
583
+ body: ClassBody;
584
+ }
585
+
586
+ interface ClassBody extends BaseNode {
587
+ type: "ClassBody";
588
+ body: Array<MethodDefinition | PropertyDefinition | StaticBlock>;
589
+ }
590
+
591
+ interface MethodDefinition extends BaseNode {
592
+ type: "MethodDefinition";
593
+ key: Expression | PrivateIdentifier;
594
+ value: FunctionExpression;
595
+ kind: "constructor" | "method" | "get" | "set";
596
+ computed: boolean;
597
+ static: boolean;
598
+ }
599
+
600
+ interface MaybeNamedClassDeclaration extends BaseClass, BaseDeclaration {
601
+ type: "ClassDeclaration";
602
+ /** It is null when a class declaration is a part of the `export default class` statement */
603
+ id: Identifier | null;
604
+ }
605
+
606
+ interface ClassDeclaration extends MaybeNamedClassDeclaration {
607
+ id: Identifier;
608
+ }
609
+
610
+ interface ClassExpression extends BaseClass, BaseExpression {
611
+ type: "ClassExpression";
612
+ id?: Identifier | null | undefined;
613
+ }
614
+
615
+ interface MetaProperty extends BaseExpression {
616
+ type: "MetaProperty";
617
+ meta: Identifier;
618
+ property: Identifier;
619
+ }
620
+
621
+ type ModuleDeclaration =
622
+ | ImportDeclaration
623
+ | ExportNamedDeclaration
624
+ | ExportDefaultDeclaration
625
+ | ExportAllDeclaration;
626
+ interface BaseModuleDeclaration extends BaseNode {}
627
+
628
+ type ModuleSpecifier = ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier;
629
+ interface BaseModuleSpecifier extends BaseNode {
630
+ local: Identifier;
631
+ }
632
+
633
+ interface ImportDeclaration extends BaseModuleDeclaration {
634
+ type: "ImportDeclaration";
635
+ specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
636
+ source: Literal;
637
+ }
638
+
639
+ interface ImportSpecifier extends BaseModuleSpecifier {
640
+ type: "ImportSpecifier";
641
+ imported: Identifier;
642
+ }
643
+
644
+ interface ImportExpression extends BaseExpression {
645
+ type: "ImportExpression";
646
+ source: Expression;
647
+ }
648
+
649
+ interface ImportDefaultSpecifier extends BaseModuleSpecifier {
650
+ type: "ImportDefaultSpecifier";
651
+ }
652
+
653
+ interface ImportNamespaceSpecifier extends BaseModuleSpecifier {
654
+ type: "ImportNamespaceSpecifier";
655
+ }
656
+
657
+ interface ExportNamedDeclaration extends BaseModuleDeclaration {
658
+ type: "ExportNamedDeclaration";
659
+ declaration?: Declaration | null | undefined;
660
+ specifiers: ExportSpecifier[];
661
+ source?: Literal | null | undefined;
662
+ }
663
+
664
+ interface ExportSpecifier extends BaseModuleSpecifier {
665
+ type: "ExportSpecifier";
666
+ exported: Identifier;
667
+ }
668
+
669
+ interface ExportDefaultDeclaration extends BaseModuleDeclaration {
670
+ type: "ExportDefaultDeclaration";
671
+ declaration: MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | Expression;
672
+ }
673
+
674
+ interface ExportAllDeclaration extends BaseModuleDeclaration {
675
+ type: "ExportAllDeclaration";
676
+ exported: Identifier | null;
677
+ source: Literal;
678
+ }
679
+
680
+ interface AwaitExpression extends BaseExpression {
681
+ type: "AwaitExpression";
682
+ argument: Expression;
683
+ }
684
+
685
+ type Positioned<T> = T & {
686
+ start: number;
687
+ end: number;
688
+ };
689
+ type Node = Positioned<Node$1>;
690
+ interface IdentifierInfo {
691
+ /**
692
+ * If the identifier is used in a property shorthand
693
+ * { foo } -> { foo: __import_x__.foo }
694
+ */
695
+ hasBindingShortcut: boolean;
696
+ /**
697
+ * The identifier is used in a class declaration
698
+ */
699
+ classDeclaration: boolean;
700
+ /**
701
+ * The identifier is a name for a class expression
702
+ */
703
+ classExpression: boolean;
704
+ }
705
+ interface Visitors {
706
+ onIdentifier?: (node: Positioned<Identifier>, info: IdentifierInfo, parentStack: Node[]) => void;
707
+ onImportMeta?: (node: Node) => void;
708
+ onDynamicImport?: (node: Positioned<ImportExpression>) => void;
709
+ onCallExpression?: (node: Positioned<CallExpression>) => void;
710
+ }
711
+ declare function setIsNodeInPattern(node: Property): WeakSet<Node$1>;
712
+ declare function isNodeInPattern(node: Node$1): node is Property;
713
+ /**
714
+ * Same logic from \@vue/compiler-core & \@vue/compiler-sfc
715
+ * Except this is using acorn AST
716
+ */
717
+ declare function esmWalker(root: Node, { onIdentifier, onImportMeta, onDynamicImport, onCallExpression }: Visitors): void;
718
+ declare function isStaticProperty(node: Node$1): node is Property;
719
+ declare function isStaticPropertyKey(node: Node$1, parent: Node$1): boolean;
720
+ declare function isFunctionNode(node: Node$1): node is Function;
721
+ declare function isInDestructuringAssignment(parent: Node$1, parentStack: Node$1[]): boolean;
722
+
723
+ export { type ArrayExpression, type ArrayPattern, type ArrowFunctionExpression, type AssignmentExpression, type AssignmentOperator, type AssignmentPattern, type AssignmentProperty, type AwaitExpression, type BaseCallExpression, type BaseClass, type BaseDeclaration, type BaseExpression, type BaseForXStatement, type BaseFunction, type BaseModuleDeclaration, type BaseModuleSpecifier, type BaseNode, type BaseNodeWithoutComments, type BasePattern, type BaseStatement, type BigIntLiteral, type BinaryExpression, type BinaryOperator, type BlockStatement, type BreakStatement, type CallExpression, type CatchClause, type ChainElement, type ChainExpression, type Class, type ClassBody, type ClassDeclaration, type ClassExpression, type Comment, type ConditionalExpression, type ContinueStatement, type DebuggerStatement, type Declaration, type Directive, type DoWhileStatement, type EmptyStatement, type ExportAllDeclaration, type ExportDefaultDeclaration, type ExportNamedDeclaration, type ExportSpecifier, type Expression, type ExpressionMap, type ExpressionStatement, type ForInStatement, type ForOfStatement, type ForStatement, type Function, type FunctionDeclaration, type FunctionExpression, type Identifier, type IfStatement, type ImportDeclaration, type ImportDefaultSpecifier, type ImportExpression, type ImportNamespaceSpecifier, type ImportSpecifier, type LabeledStatement, type Literal, type LogicalExpression, type LogicalOperator, type MaybeNamedClassDeclaration, type MaybeNamedFunctionDeclaration, type MemberExpression, type MetaProperty, type MethodDefinition, type ModuleDeclaration, type ModuleSpecifier, type NewExpression, type Node, type NodeMap, type ObjectExpression, type ObjectPattern, type Pattern, type Position, type Positioned, type PrivateIdentifier, type Program, type Property, type PropertyDefinition, type RegExpLiteral, type RestElement, type ReturnStatement, type SequenceExpression, type SimpleCallExpression, type SimpleLiteral, type SourceLocation, type SpreadElement, type Statement, type StaticBlock, type Super, type SwitchCase, type SwitchStatement, type TaggedTemplateExpression, type TemplateElement, type TemplateLiteral, type ThisExpression, type ThrowStatement, type TryStatement, type UnaryExpression, type UnaryOperator, type UpdateExpression, type UpdateOperator, type VariableDeclaration, type VariableDeclarator, type WhileStatement, type WithStatement, type YieldExpression, esmWalker, isFunctionNode, isInDestructuringAssignment, isNodeInPattern, isStaticProperty, isStaticPropertyKey, setIsNodeInPattern };
package/dist/ast.js ADDED
@@ -0,0 +1,180 @@
1
+ import { walk } from 'estree-walker';
2
+
3
+ const isNodeInPatternWeakSet = /* @__PURE__ */ new WeakSet();
4
+ function setIsNodeInPattern(node) {
5
+ return isNodeInPatternWeakSet.add(node);
6
+ }
7
+ function isNodeInPattern(node) {
8
+ return isNodeInPatternWeakSet.has(node);
9
+ }
10
+ function esmWalker(root, { onIdentifier, onImportMeta, onDynamicImport, onCallExpression }) {
11
+ const parentStack = [];
12
+ const varKindStack = [];
13
+ const scopeMap = /* @__PURE__ */ new WeakMap();
14
+ const identifiers = [];
15
+ const setScope = (node, name) => {
16
+ let scopeIds = scopeMap.get(node);
17
+ if (scopeIds && scopeIds.has(name))
18
+ return;
19
+ if (!scopeIds) {
20
+ scopeIds = /* @__PURE__ */ new Set();
21
+ scopeMap.set(node, scopeIds);
22
+ }
23
+ scopeIds.add(name);
24
+ };
25
+ function isInScope(name, parents) {
26
+ return parents.some((node) => {
27
+ var _a;
28
+ return node && ((_a = scopeMap.get(node)) == null ? void 0 : _a.has(name));
29
+ });
30
+ }
31
+ function handlePattern(p, parentScope) {
32
+ if (p.type === "Identifier") {
33
+ setScope(parentScope, p.name);
34
+ } else if (p.type === "RestElement") {
35
+ handlePattern(p.argument, parentScope);
36
+ } else if (p.type === "ObjectPattern") {
37
+ p.properties.forEach((property) => {
38
+ if (property.type === "RestElement")
39
+ setScope(parentScope, property.argument.name);
40
+ else
41
+ handlePattern(property.value, parentScope);
42
+ });
43
+ } else if (p.type === "ArrayPattern") {
44
+ p.elements.forEach((element) => {
45
+ if (element)
46
+ handlePattern(element, parentScope);
47
+ });
48
+ } else if (p.type === "AssignmentPattern") {
49
+ handlePattern(p.left, parentScope);
50
+ } else {
51
+ setScope(parentScope, p.name);
52
+ }
53
+ }
54
+ walk(root, {
55
+ enter(node, parent) {
56
+ if (node.type === "ImportDeclaration")
57
+ return this.skip();
58
+ if (parent && !(parent.type === "IfStatement" && node === parent.alternate))
59
+ parentStack.unshift(parent);
60
+ if (node.type === "VariableDeclaration")
61
+ varKindStack.unshift(node.kind);
62
+ if (node.type === "CallExpression")
63
+ onCallExpression == null ? void 0 : onCallExpression(node);
64
+ if (node.type === "MetaProperty" && node.meta.name === "import")
65
+ onImportMeta == null ? void 0 : onImportMeta(node);
66
+ else if (node.type === "ImportExpression")
67
+ onDynamicImport == null ? void 0 : onDynamicImport(node);
68
+ if (node.type === "Identifier") {
69
+ if (!isInScope(node.name, parentStack) && isRefIdentifier(node, parent, parentStack)) {
70
+ identifiers.push([node, parentStack.slice(0)]);
71
+ }
72
+ } else if (isFunctionNode(node)) {
73
+ if (node.type === "FunctionDeclaration") {
74
+ const parentScope = findParentScope(parentStack);
75
+ if (parentScope)
76
+ setScope(parentScope, node.id.name);
77
+ }
78
+ node.params.forEach((p) => {
79
+ if (p.type === "ObjectPattern" || p.type === "ArrayPattern") {
80
+ handlePattern(p, node);
81
+ return;
82
+ }
83
+ walk(p.type === "AssignmentPattern" ? p.left : p, {
84
+ enter(child, parent2) {
85
+ if ((parent2 == null ? void 0 : parent2.type) === "AssignmentPattern" && (parent2 == null ? void 0 : parent2.right) === child)
86
+ return this.skip();
87
+ if (child.type !== "Identifier")
88
+ return;
89
+ if (isStaticPropertyKey(child, parent2))
90
+ return;
91
+ if ((parent2 == null ? void 0 : parent2.type) === "TemplateLiteral" && (parent2 == null ? void 0 : parent2.expressions.includes(child)) || (parent2 == null ? void 0 : parent2.type) === "CallExpression" && (parent2 == null ? void 0 : parent2.callee) === child)
92
+ return;
93
+ setScope(node, child.name);
94
+ }
95
+ });
96
+ });
97
+ } else if (node.type === "Property" && parent.type === "ObjectPattern") {
98
+ setIsNodeInPattern(node);
99
+ } else if (node.type === "VariableDeclarator") {
100
+ const parentFunction = findParentScope(
101
+ parentStack,
102
+ varKindStack[0] === "var"
103
+ );
104
+ if (parentFunction)
105
+ handlePattern(node.id, parentFunction);
106
+ } else if (node.type === "CatchClause" && node.param) {
107
+ handlePattern(node.param, node);
108
+ }
109
+ },
110
+ leave(node, parent) {
111
+ if (parent && !(parent.type === "IfStatement" && node === parent.alternate))
112
+ parentStack.shift();
113
+ if (node.type === "VariableDeclaration")
114
+ varKindStack.shift();
115
+ }
116
+ });
117
+ identifiers.forEach(([node, stack]) => {
118
+ if (!isInScope(node.name, stack)) {
119
+ const parent = stack[0];
120
+ const grandparent = stack[1];
121
+ const hasBindingShortcut = isStaticProperty(parent) && parent.shorthand && (!isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack));
122
+ const classDeclaration = parent.type === "PropertyDefinition" && (grandparent == null ? void 0 : grandparent.type) === "ClassBody" || parent.type === "ClassDeclaration" && node === parent.superClass;
123
+ const classExpression = parent.type === "ClassExpression" && node === parent.id;
124
+ onIdentifier == null ? void 0 : onIdentifier(node, {
125
+ hasBindingShortcut,
126
+ classDeclaration,
127
+ classExpression
128
+ }, stack);
129
+ }
130
+ });
131
+ }
132
+ function isRefIdentifier(id, parent, parentStack) {
133
+ if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id)
134
+ return false;
135
+ if (isFunctionNode(parent)) {
136
+ if (parent.id === id)
137
+ return false;
138
+ if (parent.params.includes(id))
139
+ return false;
140
+ }
141
+ if (parent.type === "MethodDefinition" && !parent.computed)
142
+ return false;
143
+ if (isStaticPropertyKey(id, parent))
144
+ return false;
145
+ if (isNodeInPattern(parent) && parent.value === id)
146
+ return false;
147
+ if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack))
148
+ return false;
149
+ if (parent.type === "MemberExpression" && parent.property === id && !parent.computed)
150
+ return false;
151
+ if (parent.type === "ExportSpecifier")
152
+ return false;
153
+ if (id.name === "arguments")
154
+ return false;
155
+ return true;
156
+ }
157
+ function isStaticProperty(node) {
158
+ return node && node.type === "Property" && !node.computed;
159
+ }
160
+ function isStaticPropertyKey(node, parent) {
161
+ return isStaticProperty(parent) && parent.key === node;
162
+ }
163
+ const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/;
164
+ function isFunctionNode(node) {
165
+ return functionNodeTypeRE.test(node.type);
166
+ }
167
+ const blockNodeTypeRE = /^BlockStatement$|^For(?:In|Of)?Statement$/;
168
+ function isBlock(node) {
169
+ return blockNodeTypeRE.test(node.type);
170
+ }
171
+ function findParentScope(parentStack, isVar = false) {
172
+ return parentStack.find(isVar ? isFunctionNode : isBlock);
173
+ }
174
+ function isInDestructuringAssignment(parent, parentStack) {
175
+ if (parent && (parent.type === "Property" || parent.type === "ArrayPattern"))
176
+ return parentStack.some((i) => i.type === "AssignmentExpression");
177
+ return false;
178
+ }
179
+
180
+ export { esmWalker, isFunctionNode, isInDestructuringAssignment, isNodeInPattern, isStaticProperty, isStaticPropertyKey, setIsNodeInPattern };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -24,6 +24,10 @@
24
24
  "types": "./dist/diff.d.ts",
25
25
  "default": "./dist/diff.js"
26
26
  },
27
+ "./ast": {
28
+ "types": "./dist/ast.d.ts",
29
+ "default": "./dist/ast.js"
30
+ },
27
31
  "./error": {
28
32
  "types": "./dist/error.d.ts",
29
33
  "default": "./dist/error.js"
@@ -54,11 +58,13 @@
54
58
  ],
55
59
  "dependencies": {
56
60
  "diff-sequences": "^29.6.3",
61
+ "estree-walker": "^3.0.3",
57
62
  "loupe": "^2.3.7",
58
63
  "pretty-format": "^29.7.0"
59
64
  },
60
65
  "devDependencies": {
61
- "@jridgewell/trace-mapping": "^0.3.20"
66
+ "@jridgewell/trace-mapping": "^0.3.20",
67
+ "@types/estree": "^1.0.5"
62
68
  },
63
69
  "scripts": {
64
70
  "build": "rimraf dist && rollup -c",