@vitest/utils 2.1.0-beta.6 → 2.1.0

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.
package/dist/diff.js CHANGED
@@ -216,33 +216,6 @@ const linebreakRegex_ = /[\r\n]/;
216
216
  const blanklineEndRegex_ = /\n\r?\n$/;
217
217
  const blanklineStartRegex_ = /^\r?\n\r?\n/;
218
218
  function diff_cleanupSemanticLossless(diffs) {
219
- function diff_cleanupSemanticScore_(one, two) {
220
- if (!one || !two) {
221
- return 6;
222
- }
223
- const char1 = one.charAt(one.length - 1);
224
- const char2 = two.charAt(0);
225
- const nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
226
- const nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
227
- const whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
228
- const whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
229
- const lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
230
- const lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
231
- const blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
232
- const blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
233
- if (blankLine1 || blankLine2) {
234
- return 5;
235
- } else if (lineBreak1 || lineBreak2) {
236
- return 4;
237
- } else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
238
- return 3;
239
- } else if (whitespace1 || whitespace2) {
240
- return 2;
241
- } else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
242
- return 1;
243
- }
244
- return 0;
245
- }
246
219
  let pointer = 1;
247
220
  while (pointer < diffs.length - 1) {
248
221
  if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
@@ -396,6 +369,33 @@ function diff_cleanupMerge(diffs) {
396
369
  diff_cleanupMerge(diffs);
397
370
  }
398
371
  }
372
+ function diff_cleanupSemanticScore_(one, two) {
373
+ if (!one || !two) {
374
+ return 6;
375
+ }
376
+ const char1 = one.charAt(one.length - 1);
377
+ const char2 = two.charAt(0);
378
+ const nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
379
+ const nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
380
+ const whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
381
+ const whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
382
+ const lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
383
+ const lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
384
+ const blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
385
+ const blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
386
+ if (blankLine1 || blankLine2) {
387
+ return 5;
388
+ } else if (lineBreak1 || lineBreak2) {
389
+ return 4;
390
+ } else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
391
+ return 3;
392
+ } else if (whitespace1 || whitespace2) {
393
+ return 2;
394
+ } else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
395
+ return 1;
396
+ }
397
+ return 0;
398
+ }
399
399
 
400
400
  const NO_DIFF_MESSAGE = "Compared values have no visual difference.";
401
401
  const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\nPrinting internal object structure without calling `toJSON` instead.";
package/dist/helpers.js CHANGED
@@ -34,7 +34,7 @@ function parseRegexp(input) {
34
34
  return /$^/;
35
35
  }
36
36
  if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
37
- return RegExp(input);
37
+ return new RegExp(input);
38
38
  }
39
39
  return new RegExp(m[2], m[3]);
40
40
  }
@@ -80,7 +80,7 @@ function clone(val, seen, options = defaultCloneOptions) {
80
80
  return seen.get(val);
81
81
  }
82
82
  if (Array.isArray(val)) {
83
- out = Array(k = val.length);
83
+ out = Array.from({ length: k = val.length });
84
84
  seen.set(val, out);
85
85
  while (k--) {
86
86
  out[k] = clone(val[k], seen, options);
@@ -128,7 +128,7 @@ function objectAttr(source, path, defaultValue = void 0) {
128
128
  const paths = path.replace(/\[(\d+)\]/g, ".$1").split(".");
129
129
  let result = source;
130
130
  for (const p of paths) {
131
- result = Object(result)[p];
131
+ result = new Object(result)[p];
132
132
  if (result === void 0) {
133
133
  return defaultValue;
134
134
  }
@@ -104,74 +104,92 @@ for (let i = 0; i < chars.length; i++) {
104
104
  intToChar[i] = c;
105
105
  charToInt[c] = i;
106
106
  }
107
+ function decodeInteger(reader, relative) {
108
+ let value = 0;
109
+ let shift = 0;
110
+ let integer = 0;
111
+ do {
112
+ const c = reader.next();
113
+ integer = charToInt[c];
114
+ value |= (integer & 31) << shift;
115
+ shift += 5;
116
+ } while (integer & 32);
117
+ const shouldNegate = value & 1;
118
+ value >>>= 1;
119
+ if (shouldNegate) {
120
+ value = -0x80000000 | -value;
121
+ }
122
+ return relative + value;
123
+ }
124
+ function hasMoreVlq(reader, max) {
125
+ if (reader.pos >= max)
126
+ return false;
127
+ return reader.peek() !== comma;
128
+ }
129
+ class StringReader {
130
+ constructor(buffer) {
131
+ this.pos = 0;
132
+ this.buffer = buffer;
133
+ }
134
+ next() {
135
+ return this.buffer.charCodeAt(this.pos++);
136
+ }
137
+ peek() {
138
+ return this.buffer.charCodeAt(this.pos);
139
+ }
140
+ indexOf(char) {
141
+ const { buffer, pos } = this;
142
+ const idx = buffer.indexOf(char, pos);
143
+ return idx === -1 ? buffer.length : idx;
144
+ }
145
+ }
146
+
107
147
  function decode(mappings) {
108
- const state = new Int32Array(5);
148
+ const { length } = mappings;
149
+ const reader = new StringReader(mappings);
109
150
  const decoded = [];
110
- let index = 0;
151
+ let genColumn = 0;
152
+ let sourcesIndex = 0;
153
+ let sourceLine = 0;
154
+ let sourceColumn = 0;
155
+ let namesIndex = 0;
111
156
  do {
112
- const semi = indexOf(mappings, index);
157
+ const semi = reader.indexOf(';');
113
158
  const line = [];
114
159
  let sorted = true;
115
160
  let lastCol = 0;
116
- state[0] = 0;
117
- for (let i = index; i < semi; i++) {
161
+ genColumn = 0;
162
+ while (reader.pos < semi) {
118
163
  let seg;
119
- i = decodeInteger(mappings, i, state, 0); // genColumn
120
- const col = state[0];
121
- if (col < lastCol)
164
+ genColumn = decodeInteger(reader, genColumn);
165
+ if (genColumn < lastCol)
122
166
  sorted = false;
123
- lastCol = col;
124
- if (hasMoreVlq(mappings, i, semi)) {
125
- i = decodeInteger(mappings, i, state, 1); // sourcesIndex
126
- i = decodeInteger(mappings, i, state, 2); // sourceLine
127
- i = decodeInteger(mappings, i, state, 3); // sourceColumn
128
- if (hasMoreVlq(mappings, i, semi)) {
129
- i = decodeInteger(mappings, i, state, 4); // namesIndex
130
- seg = [col, state[1], state[2], state[3], state[4]];
167
+ lastCol = genColumn;
168
+ if (hasMoreVlq(reader, semi)) {
169
+ sourcesIndex = decodeInteger(reader, sourcesIndex);
170
+ sourceLine = decodeInteger(reader, sourceLine);
171
+ sourceColumn = decodeInteger(reader, sourceColumn);
172
+ if (hasMoreVlq(reader, semi)) {
173
+ namesIndex = decodeInteger(reader, namesIndex);
174
+ seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
131
175
  }
132
176
  else {
133
- seg = [col, state[1], state[2], state[3]];
177
+ seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
134
178
  }
135
179
  }
136
180
  else {
137
- seg = [col];
181
+ seg = [genColumn];
138
182
  }
139
183
  line.push(seg);
184
+ reader.pos++;
140
185
  }
141
186
  if (!sorted)
142
187
  sort(line);
143
188
  decoded.push(line);
144
- index = semi + 1;
145
- } while (index <= mappings.length);
189
+ reader.pos = semi + 1;
190
+ } while (reader.pos <= length);
146
191
  return decoded;
147
192
  }
148
- function indexOf(mappings, index) {
149
- const idx = mappings.indexOf(';', index);
150
- return idx === -1 ? mappings.length : idx;
151
- }
152
- function decodeInteger(mappings, pos, state, j) {
153
- let value = 0;
154
- let shift = 0;
155
- let integer = 0;
156
- do {
157
- const c = mappings.charCodeAt(pos++);
158
- integer = charToInt[c];
159
- value |= (integer & 31) << shift;
160
- shift += 5;
161
- } while (integer & 32);
162
- const shouldNegate = value & 1;
163
- value >>>= 1;
164
- if (shouldNegate) {
165
- value = -0x80000000 | -value;
166
- }
167
- state[j] += value;
168
- return pos;
169
- }
170
- function hasMoreVlq(mappings, i, length) {
171
- if (i >= length)
172
- return false;
173
- return mappings.charCodeAt(i) !== comma;
174
- }
175
193
  function sort(line) {
176
194
  line.sort(sortComparator$1);
177
195
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/utils",
3
3
  "type": "module",
4
- "version": "2.1.0-beta.6",
4
+ "version": "2.1.0",
5
5
  "description": "Shared Vitest utility functions",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -60,10 +60,9 @@
60
60
  "dist"
61
61
  ],
62
62
  "dependencies": {
63
- "estree-walker": "^3.0.3",
64
63
  "loupe": "^3.1.1",
65
64
  "tinyrainbow": "^1.2.0",
66
- "@vitest/pretty-format": "2.1.0-beta.6"
65
+ "@vitest/pretty-format": "2.1.0"
67
66
  },
68
67
  "devDependencies": {
69
68
  "@jridgewell/trace-mapping": "^0.3.25",
package/dist/ast.d.ts DELETED
@@ -1,723 +0,0 @@
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 DELETED
@@ -1,211 +0,0 @@
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
- }
20
- if (!scopeIds) {
21
- scopeIds = /* @__PURE__ */ new Set();
22
- scopeMap.set(node, scopeIds);
23
- }
24
- scopeIds.add(name);
25
- };
26
- function isInScope(name, parents) {
27
- return parents.some((node) => {
28
- var _a;
29
- return node && ((_a = scopeMap.get(node)) == null ? void 0 : _a.has(name));
30
- });
31
- }
32
- function handlePattern(p, parentScope) {
33
- if (p.type === "Identifier") {
34
- setScope(parentScope, p.name);
35
- } else if (p.type === "RestElement") {
36
- handlePattern(p.argument, parentScope);
37
- } else if (p.type === "ObjectPattern") {
38
- p.properties.forEach((property) => {
39
- if (property.type === "RestElement") {
40
- setScope(parentScope, property.argument.name);
41
- } else {
42
- handlePattern(property.value, parentScope);
43
- }
44
- });
45
- } else if (p.type === "ArrayPattern") {
46
- p.elements.forEach((element) => {
47
- if (element) {
48
- handlePattern(element, parentScope);
49
- }
50
- });
51
- } else if (p.type === "AssignmentPattern") {
52
- handlePattern(p.left, parentScope);
53
- } else {
54
- setScope(parentScope, p.name);
55
- }
56
- }
57
- walk(root, {
58
- enter(node, parent) {
59
- if (node.type === "ImportDeclaration") {
60
- return this.skip();
61
- }
62
- if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
63
- parentStack.unshift(parent);
64
- }
65
- if (node.type === "VariableDeclaration") {
66
- varKindStack.unshift(node.kind);
67
- }
68
- if (node.type === "CallExpression") {
69
- onCallExpression == null ? void 0 : onCallExpression(node);
70
- }
71
- if (node.type === "MetaProperty" && node.meta.name === "import") {
72
- onImportMeta == null ? void 0 : onImportMeta(node);
73
- } else if (node.type === "ImportExpression") {
74
- onDynamicImport == null ? void 0 : onDynamicImport(node);
75
- }
76
- if (node.type === "Identifier") {
77
- if (!isInScope(node.name, parentStack) && isRefIdentifier(node, parent, parentStack)) {
78
- identifiers.push([node, parentStack.slice(0)]);
79
- }
80
- } else if (isFunctionNode(node)) {
81
- if (node.type === "FunctionDeclaration") {
82
- const parentScope = findParentScope(parentStack);
83
- if (parentScope) {
84
- setScope(parentScope, node.id.name);
85
- }
86
- }
87
- node.params.forEach((p) => {
88
- if (p.type === "ObjectPattern" || p.type === "ArrayPattern") {
89
- handlePattern(p, node);
90
- return;
91
- }
92
- walk(p.type === "AssignmentPattern" ? p.left : p, {
93
- enter(child, parent2) {
94
- if ((parent2 == null ? void 0 : parent2.type) === "AssignmentPattern" && (parent2 == null ? void 0 : parent2.right) === child) {
95
- return this.skip();
96
- }
97
- if (child.type !== "Identifier") {
98
- return;
99
- }
100
- if (isStaticPropertyKey(child, parent2)) {
101
- return;
102
- }
103
- 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) {
104
- return;
105
- }
106
- setScope(node, child.name);
107
- }
108
- });
109
- });
110
- } else if (node.type === "Property" && parent.type === "ObjectPattern") {
111
- setIsNodeInPattern(node);
112
- } else if (node.type === "VariableDeclarator") {
113
- const parentFunction = findParentScope(
114
- parentStack,
115
- varKindStack[0] === "var"
116
- );
117
- if (parentFunction) {
118
- handlePattern(node.id, parentFunction);
119
- }
120
- } else if (node.type === "CatchClause" && node.param) {
121
- handlePattern(node.param, node);
122
- }
123
- },
124
- leave(node, parent) {
125
- if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
126
- parentStack.shift();
127
- }
128
- if (node.type === "VariableDeclaration") {
129
- varKindStack.shift();
130
- }
131
- }
132
- });
133
- identifiers.forEach(([node, stack]) => {
134
- if (!isInScope(node.name, stack)) {
135
- const parent = stack[0];
136
- const grandparent = stack[1];
137
- const hasBindingShortcut = isStaticProperty(parent) && parent.shorthand && (!isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack));
138
- const classDeclaration = parent.type === "PropertyDefinition" && (grandparent == null ? void 0 : grandparent.type) === "ClassBody" || parent.type === "ClassDeclaration" && node === parent.superClass;
139
- const classExpression = parent.type === "ClassExpression" && node === parent.id;
140
- onIdentifier == null ? void 0 : onIdentifier(
141
- node,
142
- {
143
- hasBindingShortcut,
144
- classDeclaration,
145
- classExpression
146
- },
147
- stack
148
- );
149
- }
150
- });
151
- }
152
- function isRefIdentifier(id, parent, parentStack) {
153
- if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id) {
154
- return false;
155
- }
156
- if (isFunctionNode(parent)) {
157
- if (parent.id === id) {
158
- return false;
159
- }
160
- if (parent.params.includes(id)) {
161
- return false;
162
- }
163
- }
164
- if (parent.type === "MethodDefinition" && !parent.computed) {
165
- return false;
166
- }
167
- if (isStaticPropertyKey(id, parent)) {
168
- return false;
169
- }
170
- if (isNodeInPattern(parent) && parent.value === id) {
171
- return false;
172
- }
173
- if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack)) {
174
- return false;
175
- }
176
- if (parent.type === "MemberExpression" && parent.property === id && !parent.computed) {
177
- return false;
178
- }
179
- if (parent.type === "ExportSpecifier") {
180
- return false;
181
- }
182
- if (id.name === "arguments") {
183
- return false;
184
- }
185
- return true;
186
- }
187
- function isStaticProperty(node) {
188
- return node && node.type === "Property" && !node.computed;
189
- }
190
- function isStaticPropertyKey(node, parent) {
191
- return isStaticProperty(parent) && parent.key === node;
192
- }
193
- const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/;
194
- function isFunctionNode(node) {
195
- return functionNodeTypeRE.test(node.type);
196
- }
197
- const blockNodeTypeRE = /^BlockStatement$|^For(?:In|Of)?Statement$/;
198
- function isBlock(node) {
199
- return blockNodeTypeRE.test(node.type);
200
- }
201
- function findParentScope(parentStack, isVar = false) {
202
- return parentStack.find(isVar ? isFunctionNode : isBlock);
203
- }
204
- function isInDestructuringAssignment(parent, parentStack) {
205
- if (parent && (parent.type === "Property" || parent.type === "ArrayPattern")) {
206
- return parentStack.some((i) => i.type === "AssignmentExpression");
207
- }
208
- return false;
209
- }
210
-
211
- export { esmWalker, isFunctionNode, isInDestructuringAssignment, isNodeInPattern, isStaticProperty, isStaticPropertyKey, setIsNodeInPattern };