vitest 4.0.0-beta.18 → 4.0.0-beta.19

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 (52) hide show
  1. package/dist/browser.d.ts +3 -3
  2. package/dist/browser.js +1 -1
  3. package/dist/{worker-base.js → chunks/base.CtHM3ryk.js} +18 -91
  4. package/dist/chunks/{browser.d.CCG7W26I.d.ts → browser.d.B9iJzZyn.d.ts} +3 -2
  5. package/dist/chunks/{cac.DYnuYoJK.js → cac.DCrQhweU.js} +14 -61
  6. package/dist/chunks/{cli-api.xhe4uqTX.js → cli-api.BjHteKX0.js} +1312 -53
  7. package/dist/chunks/{config.d.C4PpNy7v.d.ts → config.d.u2CUDWwS.d.ts} +2 -16
  8. package/dist/chunks/{coverage.Ds84cgzV.js → coverage.FU3w4IrQ.js} +25 -1241
  9. package/dist/chunks/{defaults.CXFFjsi8.js → defaults.BOqNVLsY.js} +0 -1
  10. package/dist/chunks/evaluatedModules.Dg1zASAC.js +17 -0
  11. package/dist/chunks/{global.d.RTA0rbJI.d.ts → global.d.BgJSTpgQ.d.ts} +1 -1
  12. package/dist/chunks/{globals.CwYe1aG7.js → globals.BGT_RUsD.js} +4 -2
  13. package/dist/chunks/{index.eEkl9h8v.js → index.BdSLhLDZ.js} +1 -1
  14. package/dist/chunks/{index.D2gVI9Ck.js → index.CcRZ6fUh.js} +1506 -11
  15. package/dist/chunks/{index.Bcjk8TKX.js → index.RwjEGCQ0.js} +2 -2
  16. package/dist/chunks/init-forks.DSafeltJ.js +54 -0
  17. package/dist/chunks/init-threads.SUtZ-067.js +17 -0
  18. package/dist/chunks/{worker.CdzokOSx.js → init.B2EESLQM.js} +97 -80
  19. package/dist/chunks/{inspector.Br76Q2Mb.js → inspector.DLZxSeU3.js} +1 -2
  20. package/dist/chunks/{moduleRunner.d.aXWuQhZN.d.ts → moduleRunner.d.YtNsMIoJ.d.ts} +1 -1
  21. package/dist/chunks/{plugin.d.XtKKWlOO.d.ts → plugin.d.BB__S31E.d.ts} +1 -1
  22. package/dist/chunks/{reporters.d.BJ_OuJGZ.d.ts → reporters.d.C6nGyY9_.d.ts} +1113 -1152
  23. package/dist/chunks/{resolveSnapshotEnvironment.tw2a5ux8.js → resolveSnapshotEnvironment.DJJKMKxb.js} +1 -1
  24. package/dist/chunks/{setup-common.DgXU7Yho.js → setup-common.DR1sucx6.js} +1 -1
  25. package/dist/chunks/{startModuleRunner.DPBo3mme.js → startModuleRunner.C2tTvmF9.js} +3 -1
  26. package/dist/{worker-vm.js → chunks/vm.DBeOXrP9.js} +6 -66
  27. package/dist/chunks/{worker.d.DSgBAZPX.d.ts → worker.d.BFk-vvBU.d.ts} +79 -4
  28. package/dist/cli.js +8 -9
  29. package/dist/config.cjs +0 -1
  30. package/dist/config.d.ts +6 -7
  31. package/dist/config.js +1 -1
  32. package/dist/coverage.d.ts +4 -4
  33. package/dist/coverage.js +2 -13
  34. package/dist/index.d.ts +13 -9
  35. package/dist/index.js +4 -2
  36. package/dist/module-evaluator.d.ts +3 -3
  37. package/dist/module-runner.js +1 -1
  38. package/dist/node.d.ts +79 -15
  39. package/dist/node.js +25 -26
  40. package/dist/reporters.d.ts +4 -4
  41. package/dist/reporters.js +9 -10
  42. package/dist/runners.d.ts +1 -1
  43. package/dist/worker.d.ts +26 -0
  44. package/dist/worker.js +46 -0
  45. package/dist/workers/forks.js +50 -0
  46. package/dist/workers/runVmTests.js +6 -5
  47. package/dist/workers/threads.js +50 -0
  48. package/dist/workers/vmForks.js +35 -0
  49. package/dist/workers/vmThreads.js +35 -0
  50. package/package.json +16 -13
  51. package/worker.d.ts +1 -0
  52. package/dist/chunks/typechecker.DsKAhua5.js +0 -1522
@@ -1,1522 +0,0 @@
1
- import nodeos__default from 'node:os';
2
- import { performance } from 'node:perf_hooks';
3
- import { relative, basename, resolve as resolve$1, join } from 'pathe';
4
- import { x } from 'tinyexec';
5
- import { distDir } from '../path.js';
6
- import { getTests, generateHash, calculateSuiteHash, someTasksAreOnly, interpretTaskModes } from '@vitest/runner/utils';
7
- import '@vitest/utils/helpers';
8
- import { parseAstAsync } from 'vite';
9
-
10
- /// <reference types="../types/index.d.ts" />
11
-
12
- // (c) 2020-present Andrea Giammarchi
13
-
14
- const {parse: $parse, stringify: $stringify} = JSON;
15
- const {keys} = Object;
16
-
17
- const Primitive = String; // it could be Number
18
- const primitive = 'string'; // it could be 'number'
19
-
20
- const ignore$1 = {};
21
- const object = 'object';
22
-
23
- const noop = (_, value) => value;
24
-
25
- const primitives = value => (
26
- value instanceof Primitive ? Primitive(value) : value
27
- );
28
-
29
- const Primitives = (_, value) => (
30
- typeof value === primitive ? new Primitive(value) : value
31
- );
32
-
33
- const revive = (input, parsed, output, $) => {
34
- const lazy = [];
35
- for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
36
- const k = ke[y];
37
- const value = output[k];
38
- if (value instanceof Primitive) {
39
- const tmp = input[value];
40
- if (typeof tmp === object && !parsed.has(tmp)) {
41
- parsed.add(tmp);
42
- output[k] = ignore$1;
43
- lazy.push({k, a: [input, parsed, tmp, $]});
44
- }
45
- else
46
- output[k] = $.call(output, k, tmp);
47
- }
48
- else if (output[k] !== ignore$1)
49
- output[k] = $.call(output, k, value);
50
- }
51
- for (let {length} = lazy, i = 0; i < length; i++) {
52
- const {k, a} = lazy[i];
53
- output[k] = $.call(output, k, revive.apply(null, a));
54
- }
55
- return output;
56
- };
57
-
58
- const set = (known, input, value) => {
59
- const index = Primitive(input.push(value) - 1);
60
- known.set(value, index);
61
- return index;
62
- };
63
-
64
- /**
65
- * Converts a specialized flatted string into a JS value.
66
- * @param {string} text
67
- * @param {(this: any, key: string, value: any) => any} [reviver]
68
- * @returns {any}
69
- */
70
- const parse$1 = (text, reviver) => {
71
- const input = $parse(text, Primitives).map(primitives);
72
- const value = input[0];
73
- const $ = reviver || noop;
74
- const tmp = typeof value === object && value ?
75
- revive(input, new Set, value, $) :
76
- value;
77
- return $.call({'': tmp}, '', tmp);
78
- };
79
-
80
- /**
81
- * Converts a JS value into a specialized flatted string.
82
- * @param {any} value
83
- * @param {((this: any, key: string, value: any) => any) | (string | number)[] | null | undefined} [replacer]
84
- * @param {string | number | undefined} [space]
85
- * @returns {string}
86
- */
87
- const stringify = (value, replacer, space) => {
88
- const $ = replacer && typeof replacer === object ?
89
- (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
90
- (replacer || noop);
91
- const known = new Map;
92
- const input = [];
93
- const output = [];
94
- let i = +set(known, input, $.call({'': value}, '', value));
95
- let firstRun = !i;
96
- while (i < input.length) {
97
- firstRun = true;
98
- output[i] = $stringify(input[i++], replace, space);
99
- }
100
- return '[' + output.join(',') + ']';
101
- function replace(key, value) {
102
- if (firstRun) {
103
- firstRun = !firstRun;
104
- return value;
105
- }
106
- const after = $.call(this, key, value);
107
- switch (typeof after) {
108
- case object:
109
- if (after === null) return after;
110
- case primitive:
111
- return known.get(after) || set(known, input, after);
112
- }
113
- return after;
114
- }
115
- };
116
-
117
- const REGEXP_WRAP_PREFIX = "$$vitest:";
118
- function getOutputFile(config, reporter) {
119
- if (config?.outputFile) return typeof config.outputFile === "string" ? config.outputFile : config.outputFile[reporter];
120
- }
121
- /**
122
- * Prepares `SerializedConfig` for serialization, e.g. `node:v8.serialize`
123
- */
124
- function wrapSerializableConfig(config) {
125
- let testNamePattern = config.testNamePattern, defines = config.defines;
126
- // v8 serialize does not support regex
127
- if (testNamePattern && typeof testNamePattern !== "string") testNamePattern = `${REGEXP_WRAP_PREFIX}${testNamePattern.toString()}`;
128
- // v8 serialize drops properties with undefined value
129
- if (defines) defines = {
130
- keys: Object.keys(defines),
131
- original: defines
132
- };
133
- return {
134
- ...config,
135
- testNamePattern,
136
- defines
137
- };
138
- }
139
- function createDefinesScript(define) {
140
- return !define || serializeDefine(define) === "{}" ? "" : `
141
- const defines = ${serializeDefine(define)}
142
- Object.keys(defines).forEach((key) => {
143
- const segments = key.split('.')
144
- let target = globalThis
145
- for (let i = 0; i < segments.length; i++) {
146
- const segment = segments[i]
147
- if (i === segments.length - 1) {
148
- target[segment] = defines[key]
149
- } else {
150
- target = target[segment] || (target[segment] = {})
151
- }
152
- }
153
- })
154
- `;
155
- }
156
- /**
157
- * Like `JSON.stringify` but keeps raw string values as a literal
158
- * in the generated code. For example: `"window"` would refer to
159
- * the global `window` object directly.
160
- */
161
- function serializeDefine(define) {
162
- const userDefine = {};
163
- for (const key in define) {
164
- // vitest sets this to avoid vite:client-inject plugin
165
- if (key === "process.env.NODE_ENV" && define[key] === "process.env.NODE_ENV") continue;
166
- // import.meta.env.* is handled in `importAnalysis` plugin
167
- if (!key.startsWith("import.meta.env.")) userDefine[key] = define[key];
168
- }
169
- let res = `{`;
170
- const keys = Object.keys(userDefine).sort();
171
- for (let i = 0; i < keys.length; i++) {
172
- const key = keys[i], val = userDefine[key];
173
- if (res += `${JSON.stringify(key)}: ${handleDefineValue(val)}`, i !== keys.length - 1) res += `, `;
174
- }
175
- return `${res}}`;
176
- }
177
- function handleDefineValue(value) {
178
- return typeof value === "undefined" ? "undefined" : typeof value === "string" ? value : JSON.stringify(value);
179
- }
180
-
181
- function hasFailedSnapshot(suite) {
182
- return getTests(suite).some((s) => {
183
- return s.result?.errors?.some((e) => typeof e?.message === "string" && e.message.match(/Snapshot .* mismatched/));
184
- });
185
- }
186
- function convertTasksToEvents(file, onTask) {
187
- const packs = [], events = [];
188
- function visit(suite) {
189
- onTask?.(suite), packs.push([
190
- suite.id,
191
- suite.result,
192
- suite.meta
193
- ]), events.push([
194
- suite.id,
195
- "suite-prepare",
196
- void 0
197
- ]), suite.tasks.forEach((task) => {
198
- if (task.type === "suite") visit(task);
199
- else if (onTask?.(task), suite.mode !== "skip" && suite.mode !== "todo") packs.push([
200
- task.id,
201
- task.result,
202
- task.meta
203
- ]), events.push([
204
- task.id,
205
- "test-prepare",
206
- void 0
207
- ]), task.annotations.forEach((annotation) => {
208
- events.push([
209
- task.id,
210
- "test-annotation",
211
- { annotation }
212
- ]);
213
- }), events.push([
214
- task.id,
215
- "test-finished",
216
- void 0
217
- ]);
218
- }), events.push([
219
- suite.id,
220
- "suite-finished",
221
- void 0
222
- ]);
223
- }
224
- return visit(file), {
225
- packs,
226
- events
227
- };
228
- }
229
-
230
- // src/vlq.ts
231
- var comma = ",".charCodeAt(0);
232
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
233
- var intToChar = new Uint8Array(64);
234
- var charToInt = new Uint8Array(128);
235
- for (let i = 0; i < chars.length; i++) {
236
- const c = chars.charCodeAt(i);
237
- intToChar[i] = c;
238
- charToInt[c] = i;
239
- }
240
- function decodeInteger(reader, relative) {
241
- let value = 0;
242
- let shift = 0;
243
- let integer = 0;
244
- do {
245
- const c = reader.next();
246
- integer = charToInt[c];
247
- value |= (integer & 31) << shift;
248
- shift += 5;
249
- } while (integer & 32);
250
- const shouldNegate = value & 1;
251
- value >>>= 1;
252
- if (shouldNegate) {
253
- value = -2147483648 | -value;
254
- }
255
- return relative + value;
256
- }
257
- function hasMoreVlq(reader, max) {
258
- if (reader.pos >= max) return false;
259
- return reader.peek() !== comma;
260
- }
261
- var StringReader = class {
262
- constructor(buffer) {
263
- this.pos = 0;
264
- this.buffer = buffer;
265
- }
266
- next() {
267
- return this.buffer.charCodeAt(this.pos++);
268
- }
269
- peek() {
270
- return this.buffer.charCodeAt(this.pos);
271
- }
272
- indexOf(char) {
273
- const { buffer, pos } = this;
274
- const idx = buffer.indexOf(char, pos);
275
- return idx === -1 ? buffer.length : idx;
276
- }
277
- };
278
-
279
- // src/sourcemap-codec.ts
280
- function decode(mappings) {
281
- const { length } = mappings;
282
- const reader = new StringReader(mappings);
283
- const decoded = [];
284
- let genColumn = 0;
285
- let sourcesIndex = 0;
286
- let sourceLine = 0;
287
- let sourceColumn = 0;
288
- let namesIndex = 0;
289
- do {
290
- const semi = reader.indexOf(";");
291
- const line = [];
292
- let sorted = true;
293
- let lastCol = 0;
294
- genColumn = 0;
295
- while (reader.pos < semi) {
296
- let seg;
297
- genColumn = decodeInteger(reader, genColumn);
298
- if (genColumn < lastCol) sorted = false;
299
- lastCol = genColumn;
300
- if (hasMoreVlq(reader, semi)) {
301
- sourcesIndex = decodeInteger(reader, sourcesIndex);
302
- sourceLine = decodeInteger(reader, sourceLine);
303
- sourceColumn = decodeInteger(reader, sourceColumn);
304
- if (hasMoreVlq(reader, semi)) {
305
- namesIndex = decodeInteger(reader, namesIndex);
306
- seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
307
- } else {
308
- seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
309
- }
310
- } else {
311
- seg = [genColumn];
312
- }
313
- line.push(seg);
314
- reader.pos++;
315
- }
316
- if (!sorted) sort(line);
317
- decoded.push(line);
318
- reader.pos = semi + 1;
319
- } while (reader.pos <= length);
320
- return decoded;
321
- }
322
- function sort(line) {
323
- line.sort(sortComparator$1);
324
- }
325
- function sortComparator$1(a, b) {
326
- return a[0] - b[0];
327
- }
328
-
329
- // Matches the scheme of a URL, eg "http://"
330
- const schemeRegex = /^[\w+.-]+:\/\//;
331
- /**
332
- * Matches the parts of a URL:
333
- * 1. Scheme, including ":", guaranteed.
334
- * 2. User/password, including "@", optional.
335
- * 3. Host, guaranteed.
336
- * 4. Port, including ":", optional.
337
- * 5. Path, including "/", optional.
338
- * 6. Query, including "?", optional.
339
- * 7. Hash, including "#", optional.
340
- */
341
- const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
342
- /**
343
- * File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start
344
- * with a leading `/`, they can have a domain (but only if they don't start with a Windows drive).
345
- *
346
- * 1. Host, optional.
347
- * 2. Path, which may include "/", guaranteed.
348
- * 3. Query, including "?", optional.
349
- * 4. Hash, including "#", optional.
350
- */
351
- const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
352
- function isAbsoluteUrl(input) {
353
- return schemeRegex.test(input);
354
- }
355
- function isSchemeRelativeUrl(input) {
356
- return input.startsWith('//');
357
- }
358
- function isAbsolutePath(input) {
359
- return input.startsWith('/');
360
- }
361
- function isFileUrl(input) {
362
- return input.startsWith('file:');
363
- }
364
- function isRelative(input) {
365
- return /^[.?#]/.test(input);
366
- }
367
- function parseAbsoluteUrl(input) {
368
- const match = urlRegex.exec(input);
369
- return makeUrl(match[1], match[2] || '', match[3], match[4] || '', match[5] || '/', match[6] || '', match[7] || '');
370
- }
371
- function parseFileUrl(input) {
372
- const match = fileRegex.exec(input);
373
- const path = match[2];
374
- return makeUrl('file:', '', match[1] || '', '', isAbsolutePath(path) ? path : '/' + path, match[3] || '', match[4] || '');
375
- }
376
- function makeUrl(scheme, user, host, port, path, query, hash) {
377
- return {
378
- scheme,
379
- user,
380
- host,
381
- port,
382
- path,
383
- query,
384
- hash,
385
- type: 7 /* Absolute */,
386
- };
387
- }
388
- function parseUrl(input) {
389
- if (isSchemeRelativeUrl(input)) {
390
- const url = parseAbsoluteUrl('http:' + input);
391
- url.scheme = '';
392
- url.type = 6 /* SchemeRelative */;
393
- return url;
394
- }
395
- if (isAbsolutePath(input)) {
396
- const url = parseAbsoluteUrl('http://foo.com' + input);
397
- url.scheme = '';
398
- url.host = '';
399
- url.type = 5 /* AbsolutePath */;
400
- return url;
401
- }
402
- if (isFileUrl(input))
403
- return parseFileUrl(input);
404
- if (isAbsoluteUrl(input))
405
- return parseAbsoluteUrl(input);
406
- const url = parseAbsoluteUrl('http://foo.com/' + input);
407
- url.scheme = '';
408
- url.host = '';
409
- url.type = input
410
- ? input.startsWith('?')
411
- ? 3 /* Query */
412
- : input.startsWith('#')
413
- ? 2 /* Hash */
414
- : 4 /* RelativePath */
415
- : 1 /* Empty */;
416
- return url;
417
- }
418
- function stripPathFilename(path) {
419
- // If a path ends with a parent directory "..", then it's a relative path with excess parent
420
- // paths. It's not a file, so we can't strip it.
421
- if (path.endsWith('/..'))
422
- return path;
423
- const index = path.lastIndexOf('/');
424
- return path.slice(0, index + 1);
425
- }
426
- function mergePaths(url, base) {
427
- normalizePath(base, base.type);
428
- // If the path is just a "/", then it was an empty path to begin with (remember, we're a relative
429
- // path).
430
- if (url.path === '/') {
431
- url.path = base.path;
432
- }
433
- else {
434
- // Resolution happens relative to the base path's directory, not the file.
435
- url.path = stripPathFilename(base.path) + url.path;
436
- }
437
- }
438
- /**
439
- * The path can have empty directories "//", unneeded parents "foo/..", or current directory
440
- * "foo/.". We need to normalize to a standard representation.
441
- */
442
- function normalizePath(url, type) {
443
- const rel = type <= 4 /* RelativePath */;
444
- const pieces = url.path.split('/');
445
- // We need to preserve the first piece always, so that we output a leading slash. The item at
446
- // pieces[0] is an empty string.
447
- let pointer = 1;
448
- // Positive is the number of real directories we've output, used for popping a parent directory.
449
- // Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo".
450
- let positive = 0;
451
- // We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will
452
- // generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a
453
- // real directory, we won't need to append, unless the other conditions happen again.
454
- let addTrailingSlash = false;
455
- for (let i = 1; i < pieces.length; i++) {
456
- const piece = pieces[i];
457
- // An empty directory, could be a trailing slash, or just a double "//" in the path.
458
- if (!piece) {
459
- addTrailingSlash = true;
460
- continue;
461
- }
462
- // If we encounter a real directory, then we don't need to append anymore.
463
- addTrailingSlash = false;
464
- // A current directory, which we can always drop.
465
- if (piece === '.')
466
- continue;
467
- // A parent directory, we need to see if there are any real directories we can pop. Else, we
468
- // have an excess of parents, and we'll need to keep the "..".
469
- if (piece === '..') {
470
- if (positive) {
471
- addTrailingSlash = true;
472
- positive--;
473
- pointer--;
474
- }
475
- else if (rel) {
476
- // If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute
477
- // URL, protocol relative URL, or an absolute path, we don't need to keep excess.
478
- pieces[pointer++] = piece;
479
- }
480
- continue;
481
- }
482
- // We've encountered a real directory. Move it to the next insertion pointer, which accounts for
483
- // any popped or dropped directories.
484
- pieces[pointer++] = piece;
485
- positive++;
486
- }
487
- let path = '';
488
- for (let i = 1; i < pointer; i++) {
489
- path += '/' + pieces[i];
490
- }
491
- if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
492
- path += '/';
493
- }
494
- url.path = path;
495
- }
496
- /**
497
- * Attempts to resolve `input` URL/path relative to `base`.
498
- */
499
- function resolve(input, base) {
500
- if (!input && !base)
501
- return '';
502
- const url = parseUrl(input);
503
- let inputType = url.type;
504
- if (base && inputType !== 7 /* Absolute */) {
505
- const baseUrl = parseUrl(base);
506
- const baseType = baseUrl.type;
507
- switch (inputType) {
508
- case 1 /* Empty */:
509
- url.hash = baseUrl.hash;
510
- // fall through
511
- case 2 /* Hash */:
512
- url.query = baseUrl.query;
513
- // fall through
514
- case 3 /* Query */:
515
- case 4 /* RelativePath */:
516
- mergePaths(url, baseUrl);
517
- // fall through
518
- case 5 /* AbsolutePath */:
519
- // The host, user, and port are joined, you can't copy one without the others.
520
- url.user = baseUrl.user;
521
- url.host = baseUrl.host;
522
- url.port = baseUrl.port;
523
- // fall through
524
- case 6 /* SchemeRelative */:
525
- // The input doesn't have a schema at least, so we need to copy at least that over.
526
- url.scheme = baseUrl.scheme;
527
- }
528
- if (baseType > inputType)
529
- inputType = baseType;
530
- }
531
- normalizePath(url, inputType);
532
- const queryHash = url.query + url.hash;
533
- switch (inputType) {
534
- // This is impossible, because of the empty checks at the start of the function.
535
- // case UrlType.Empty:
536
- case 2 /* Hash */:
537
- case 3 /* Query */:
538
- return queryHash;
539
- case 4 /* RelativePath */: {
540
- // The first char is always a "/", and we need it to be relative.
541
- const path = url.path.slice(1);
542
- if (!path)
543
- return queryHash || '.';
544
- if (isRelative(base || input) && !isRelative(path)) {
545
- // If base started with a leading ".", or there is no base and input started with a ".",
546
- // then we need to ensure that the relative path starts with a ".". We don't know if
547
- // relative starts with a "..", though, so check before prepending.
548
- return './' + path + queryHash;
549
- }
550
- return path + queryHash;
551
- }
552
- case 5 /* AbsolutePath */:
553
- return url.path + queryHash;
554
- default:
555
- return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
556
- }
557
- }
558
-
559
- // src/trace-mapping.ts
560
-
561
- // src/strip-filename.ts
562
- function stripFilename(path) {
563
- if (!path) return "";
564
- const index = path.lastIndexOf("/");
565
- return path.slice(0, index + 1);
566
- }
567
-
568
- // src/resolve.ts
569
- function resolver(mapUrl, sourceRoot) {
570
- const from = stripFilename(mapUrl);
571
- const prefix = sourceRoot ? sourceRoot + "/" : "";
572
- return (source) => resolve(prefix + (source || ""), from);
573
- }
574
-
575
- // src/sourcemap-segment.ts
576
- var COLUMN = 0;
577
- var SOURCES_INDEX = 1;
578
- var SOURCE_LINE = 2;
579
- var SOURCE_COLUMN = 3;
580
- var NAMES_INDEX = 4;
581
- var REV_GENERATED_LINE = 1;
582
- var REV_GENERATED_COLUMN = 2;
583
-
584
- // src/sort.ts
585
- function maybeSort(mappings, owned) {
586
- const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
587
- if (unsortedIndex === mappings.length) return mappings;
588
- if (!owned) mappings = mappings.slice();
589
- for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
590
- mappings[i] = sortSegments(mappings[i], owned);
591
- }
592
- return mappings;
593
- }
594
- function nextUnsortedSegmentLine(mappings, start) {
595
- for (let i = start; i < mappings.length; i++) {
596
- if (!isSorted(mappings[i])) return i;
597
- }
598
- return mappings.length;
599
- }
600
- function isSorted(line) {
601
- for (let j = 1; j < line.length; j++) {
602
- if (line[j][COLUMN] < line[j - 1][COLUMN]) {
603
- return false;
604
- }
605
- }
606
- return true;
607
- }
608
- function sortSegments(line, owned) {
609
- if (!owned) line = line.slice();
610
- return line.sort(sortComparator);
611
- }
612
- function sortComparator(a, b) {
613
- return a[COLUMN] - b[COLUMN];
614
- }
615
-
616
- // src/by-source.ts
617
- function buildBySources(decoded, memos) {
618
- const sources = memos.map(() => []);
619
- for (let i = 0; i < decoded.length; i++) {
620
- const line = decoded[i];
621
- for (let j = 0; j < line.length; j++) {
622
- const seg = line[j];
623
- if (seg.length === 1) continue;
624
- const sourceIndex2 = seg[SOURCES_INDEX];
625
- const sourceLine = seg[SOURCE_LINE];
626
- const sourceColumn = seg[SOURCE_COLUMN];
627
- const source = sources[sourceIndex2];
628
- const segs = source[sourceLine] || (source[sourceLine] = []);
629
- segs.push([sourceColumn, i, seg[COLUMN]]);
630
- }
631
- }
632
- for (let i = 0; i < sources.length; i++) {
633
- const source = sources[i];
634
- for (let j = 0; j < source.length; j++) {
635
- const line = source[j];
636
- if (line) line.sort(sortComparator);
637
- }
638
- }
639
- return sources;
640
- }
641
-
642
- // src/binary-search.ts
643
- var found = false;
644
- function binarySearch(haystack, needle, low, high) {
645
- while (low <= high) {
646
- const mid = low + (high - low >> 1);
647
- const cmp = haystack[mid][COLUMN] - needle;
648
- if (cmp === 0) {
649
- found = true;
650
- return mid;
651
- }
652
- if (cmp < 0) {
653
- low = mid + 1;
654
- } else {
655
- high = mid - 1;
656
- }
657
- }
658
- found = false;
659
- return low - 1;
660
- }
661
- function upperBound(haystack, needle, index) {
662
- for (let i = index + 1; i < haystack.length; index = i++) {
663
- if (haystack[i][COLUMN] !== needle) break;
664
- }
665
- return index;
666
- }
667
- function lowerBound(haystack, needle, index) {
668
- for (let i = index - 1; i >= 0; index = i--) {
669
- if (haystack[i][COLUMN] !== needle) break;
670
- }
671
- return index;
672
- }
673
- function memoizedState() {
674
- return {
675
- lastKey: -1,
676
- lastNeedle: -1,
677
- lastIndex: -1
678
- };
679
- }
680
- function memoizedBinarySearch(haystack, needle, state, key) {
681
- const { lastKey, lastNeedle, lastIndex } = state;
682
- let low = 0;
683
- let high = haystack.length - 1;
684
- if (key === lastKey) {
685
- if (needle === lastNeedle) {
686
- found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
687
- return lastIndex;
688
- }
689
- if (needle >= lastNeedle) {
690
- low = lastIndex === -1 ? 0 : lastIndex;
691
- } else {
692
- high = lastIndex;
693
- }
694
- }
695
- state.lastKey = key;
696
- state.lastNeedle = needle;
697
- return state.lastIndex = binarySearch(haystack, needle, low, high);
698
- }
699
-
700
- // src/types.ts
701
- function parse(map) {
702
- return typeof map === "string" ? JSON.parse(map) : map;
703
- }
704
-
705
- // src/trace-mapping.ts
706
- var LINE_GTR_ZERO = "`line` must be greater than 0 (lines start at line 1)";
707
- var COL_GTR_EQ_ZERO = "`column` must be greater than or equal to 0 (columns start at column 0)";
708
- var LEAST_UPPER_BOUND = -1;
709
- var GREATEST_LOWER_BOUND = 1;
710
- var TraceMap = class {
711
- constructor(map, mapUrl) {
712
- const isString = typeof map === "string";
713
- if (!isString && map._decodedMemo) return map;
714
- const parsed = parse(map);
715
- const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
716
- this.version = version;
717
- this.file = file;
718
- this.names = names || [];
719
- this.sourceRoot = sourceRoot;
720
- this.sources = sources;
721
- this.sourcesContent = sourcesContent;
722
- this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || void 0;
723
- const resolve = resolver(mapUrl, sourceRoot);
724
- this.resolvedSources = sources.map(resolve);
725
- const { mappings } = parsed;
726
- if (typeof mappings === "string") {
727
- this._encoded = mappings;
728
- this._decoded = void 0;
729
- } else if (Array.isArray(mappings)) {
730
- this._encoded = void 0;
731
- this._decoded = maybeSort(mappings, isString);
732
- } else if (parsed.sections) {
733
- throw new Error(`TraceMap passed sectioned source map, please use FlattenMap export instead`);
734
- } else {
735
- throw new Error(`invalid source map: ${JSON.stringify(parsed)}`);
736
- }
737
- this._decodedMemo = memoizedState();
738
- this._bySources = void 0;
739
- this._bySourceMemos = void 0;
740
- }
741
- };
742
- function cast(map) {
743
- return map;
744
- }
745
- function decodedMappings(map) {
746
- var _a;
747
- return (_a = cast(map))._decoded || (_a._decoded = decode(cast(map)._encoded));
748
- }
749
- function originalPositionFor(map, needle) {
750
- let { line, column, bias } = needle;
751
- line--;
752
- if (line < 0) throw new Error(LINE_GTR_ZERO);
753
- if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
754
- const decoded = decodedMappings(map);
755
- if (line >= decoded.length) return OMapping(null, null, null, null);
756
- const segments = decoded[line];
757
- const index = traceSegmentInternal(
758
- segments,
759
- cast(map)._decodedMemo,
760
- line,
761
- column,
762
- bias || GREATEST_LOWER_BOUND
763
- );
764
- if (index === -1) return OMapping(null, null, null, null);
765
- const segment = segments[index];
766
- if (segment.length === 1) return OMapping(null, null, null, null);
767
- const { names, resolvedSources } = map;
768
- return OMapping(
769
- resolvedSources[segment[SOURCES_INDEX]],
770
- segment[SOURCE_LINE] + 1,
771
- segment[SOURCE_COLUMN],
772
- segment.length === 5 ? names[segment[NAMES_INDEX]] : null
773
- );
774
- }
775
- function generatedPositionFor(map, needle) {
776
- const { source, line, column, bias } = needle;
777
- return generatedPosition(map, source, line, column, bias || GREATEST_LOWER_BOUND, false);
778
- }
779
- function eachMapping(map, cb) {
780
- const decoded = decodedMappings(map);
781
- const { names, resolvedSources } = map;
782
- for (let i = 0; i < decoded.length; i++) {
783
- const line = decoded[i];
784
- for (let j = 0; j < line.length; j++) {
785
- const seg = line[j];
786
- const generatedLine = i + 1;
787
- const generatedColumn = seg[0];
788
- let source = null;
789
- let originalLine = null;
790
- let originalColumn = null;
791
- let name = null;
792
- if (seg.length !== 1) {
793
- source = resolvedSources[seg[1]];
794
- originalLine = seg[2] + 1;
795
- originalColumn = seg[3];
796
- }
797
- if (seg.length === 5) name = names[seg[4]];
798
- cb({
799
- generatedLine,
800
- generatedColumn,
801
- source,
802
- originalLine,
803
- originalColumn,
804
- name
805
- });
806
- }
807
- }
808
- }
809
- function OMapping(source, line, column, name) {
810
- return { source, line, column, name };
811
- }
812
- function GMapping(line, column) {
813
- return { line, column };
814
- }
815
- function traceSegmentInternal(segments, memo, line, column, bias) {
816
- let index = memoizedBinarySearch(segments, column, memo, line);
817
- if (found) {
818
- index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
819
- } else if (bias === LEAST_UPPER_BOUND) index++;
820
- if (index === -1 || index === segments.length) return -1;
821
- return index;
822
- }
823
- function generatedPosition(map, source, line, column, bias, all) {
824
- var _a, _b;
825
- line--;
826
- if (line < 0) throw new Error(LINE_GTR_ZERO);
827
- if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
828
- const { sources, resolvedSources } = map;
829
- let sourceIndex2 = sources.indexOf(source);
830
- if (sourceIndex2 === -1) sourceIndex2 = resolvedSources.indexOf(source);
831
- if (sourceIndex2 === -1) return all ? [] : GMapping(null, null);
832
- const bySourceMemos = (_a = cast(map))._bySourceMemos || (_a._bySourceMemos = sources.map(memoizedState));
833
- const generated = (_b = cast(map))._bySources || (_b._bySources = buildBySources(decodedMappings(map), bySourceMemos));
834
- const segments = generated[sourceIndex2][line];
835
- if (segments == null) return all ? [] : GMapping(null, null);
836
- const memo = bySourceMemos[sourceIndex2];
837
- const index = traceSegmentInternal(segments, memo, line, column, bias);
838
- if (index === -1) return GMapping(null, null);
839
- const segment = segments[index];
840
- return GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]);
841
- }
842
-
843
- // AST walker module for ESTree compatible trees
844
-
845
-
846
- // An ancestor walk keeps an array of ancestor nodes (including the
847
- // current node) and passes them to the callback as third parameter
848
- // (and also as state parameter when no other state is present).
849
- function ancestor(node, visitors, baseVisitor, state, override) {
850
- var ancestors = [];
851
- if (!baseVisitor) { baseVisitor = base
852
- ; }(function c(node, st, override) {
853
- var type = override || node.type;
854
- var isNew = node !== ancestors[ancestors.length - 1];
855
- if (isNew) { ancestors.push(node); }
856
- baseVisitor[type](node, st, c);
857
- if (visitors[type]) { visitors[type](node, st || ancestors, ancestors); }
858
- if (isNew) { ancestors.pop(); }
859
- })(node, state, override);
860
- }
861
-
862
- function skipThrough(node, st, c) { c(node, st); }
863
- function ignore(_node, _st, _c) {}
864
-
865
- // Node walkers.
866
-
867
- var base = {};
868
-
869
- base.Program = base.BlockStatement = base.StaticBlock = function (node, st, c) {
870
- for (var i = 0, list = node.body; i < list.length; i += 1)
871
- {
872
- var stmt = list[i];
873
-
874
- c(stmt, st, "Statement");
875
- }
876
- };
877
- base.Statement = skipThrough;
878
- base.EmptyStatement = ignore;
879
- base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =
880
- function (node, st, c) { return c(node.expression, st, "Expression"); };
881
- base.IfStatement = function (node, st, c) {
882
- c(node.test, st, "Expression");
883
- c(node.consequent, st, "Statement");
884
- if (node.alternate) { c(node.alternate, st, "Statement"); }
885
- };
886
- base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };
887
- base.BreakStatement = base.ContinueStatement = ignore;
888
- base.WithStatement = function (node, st, c) {
889
- c(node.object, st, "Expression");
890
- c(node.body, st, "Statement");
891
- };
892
- base.SwitchStatement = function (node, st, c) {
893
- c(node.discriminant, st, "Expression");
894
- for (var i = 0, list = node.cases; i < list.length; i += 1) {
895
- var cs = list[i];
896
-
897
- c(cs, st);
898
- }
899
- };
900
- base.SwitchCase = function (node, st, c) {
901
- if (node.test) { c(node.test, st, "Expression"); }
902
- for (var i = 0, list = node.consequent; i < list.length; i += 1)
903
- {
904
- var cons = list[i];
905
-
906
- c(cons, st, "Statement");
907
- }
908
- };
909
- base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {
910
- if (node.argument) { c(node.argument, st, "Expression"); }
911
- };
912
- base.ThrowStatement = base.SpreadElement =
913
- function (node, st, c) { return c(node.argument, st, "Expression"); };
914
- base.TryStatement = function (node, st, c) {
915
- c(node.block, st, "Statement");
916
- if (node.handler) { c(node.handler, st); }
917
- if (node.finalizer) { c(node.finalizer, st, "Statement"); }
918
- };
919
- base.CatchClause = function (node, st, c) {
920
- if (node.param) { c(node.param, st, "Pattern"); }
921
- c(node.body, st, "Statement");
922
- };
923
- base.WhileStatement = base.DoWhileStatement = function (node, st, c) {
924
- c(node.test, st, "Expression");
925
- c(node.body, st, "Statement");
926
- };
927
- base.ForStatement = function (node, st, c) {
928
- if (node.init) { c(node.init, st, "ForInit"); }
929
- if (node.test) { c(node.test, st, "Expression"); }
930
- if (node.update) { c(node.update, st, "Expression"); }
931
- c(node.body, st, "Statement");
932
- };
933
- base.ForInStatement = base.ForOfStatement = function (node, st, c) {
934
- c(node.left, st, "ForInit");
935
- c(node.right, st, "Expression");
936
- c(node.body, st, "Statement");
937
- };
938
- base.ForInit = function (node, st, c) {
939
- if (node.type === "VariableDeclaration") { c(node, st); }
940
- else { c(node, st, "Expression"); }
941
- };
942
- base.DebuggerStatement = ignore;
943
-
944
- base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };
945
- base.VariableDeclaration = function (node, st, c) {
946
- for (var i = 0, list = node.declarations; i < list.length; i += 1)
947
- {
948
- var decl = list[i];
949
-
950
- c(decl, st);
951
- }
952
- };
953
- base.VariableDeclarator = function (node, st, c) {
954
- c(node.id, st, "Pattern");
955
- if (node.init) { c(node.init, st, "Expression"); }
956
- };
957
-
958
- base.Function = function (node, st, c) {
959
- if (node.id) { c(node.id, st, "Pattern"); }
960
- for (var i = 0, list = node.params; i < list.length; i += 1)
961
- {
962
- var param = list[i];
963
-
964
- c(param, st, "Pattern");
965
- }
966
- c(node.body, st, node.expression ? "Expression" : "Statement");
967
- };
968
-
969
- base.Pattern = function (node, st, c) {
970
- if (node.type === "Identifier")
971
- { c(node, st, "VariablePattern"); }
972
- else if (node.type === "MemberExpression")
973
- { c(node, st, "MemberPattern"); }
974
- else
975
- { c(node, st); }
976
- };
977
- base.VariablePattern = ignore;
978
- base.MemberPattern = skipThrough;
979
- base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };
980
- base.ArrayPattern = function (node, st, c) {
981
- for (var i = 0, list = node.elements; i < list.length; i += 1) {
982
- var elt = list[i];
983
-
984
- if (elt) { c(elt, st, "Pattern"); }
985
- }
986
- };
987
- base.ObjectPattern = function (node, st, c) {
988
- for (var i = 0, list = node.properties; i < list.length; i += 1) {
989
- var prop = list[i];
990
-
991
- if (prop.type === "Property") {
992
- if (prop.computed) { c(prop.key, st, "Expression"); }
993
- c(prop.value, st, "Pattern");
994
- } else if (prop.type === "RestElement") {
995
- c(prop.argument, st, "Pattern");
996
- }
997
- }
998
- };
999
-
1000
- base.Expression = skipThrough;
1001
- base.ThisExpression = base.Super = base.MetaProperty = ignore;
1002
- base.ArrayExpression = function (node, st, c) {
1003
- for (var i = 0, list = node.elements; i < list.length; i += 1) {
1004
- var elt = list[i];
1005
-
1006
- if (elt) { c(elt, st, "Expression"); }
1007
- }
1008
- };
1009
- base.ObjectExpression = function (node, st, c) {
1010
- for (var i = 0, list = node.properties; i < list.length; i += 1)
1011
- {
1012
- var prop = list[i];
1013
-
1014
- c(prop, st);
1015
- }
1016
- };
1017
- base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;
1018
- base.SequenceExpression = function (node, st, c) {
1019
- for (var i = 0, list = node.expressions; i < list.length; i += 1)
1020
- {
1021
- var expr = list[i];
1022
-
1023
- c(expr, st, "Expression");
1024
- }
1025
- };
1026
- base.TemplateLiteral = function (node, st, c) {
1027
- for (var i = 0, list = node.quasis; i < list.length; i += 1)
1028
- {
1029
- var quasi = list[i];
1030
-
1031
- c(quasi, st);
1032
- }
1033
-
1034
- for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)
1035
- {
1036
- var expr = list$1[i$1];
1037
-
1038
- c(expr, st, "Expression");
1039
- }
1040
- };
1041
- base.TemplateElement = ignore;
1042
- base.UnaryExpression = base.UpdateExpression = function (node, st, c) {
1043
- c(node.argument, st, "Expression");
1044
- };
1045
- base.BinaryExpression = base.LogicalExpression = function (node, st, c) {
1046
- c(node.left, st, "Expression");
1047
- c(node.right, st, "Expression");
1048
- };
1049
- base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {
1050
- c(node.left, st, "Pattern");
1051
- c(node.right, st, "Expression");
1052
- };
1053
- base.ConditionalExpression = function (node, st, c) {
1054
- c(node.test, st, "Expression");
1055
- c(node.consequent, st, "Expression");
1056
- c(node.alternate, st, "Expression");
1057
- };
1058
- base.NewExpression = base.CallExpression = function (node, st, c) {
1059
- c(node.callee, st, "Expression");
1060
- if (node.arguments)
1061
- { for (var i = 0, list = node.arguments; i < list.length; i += 1)
1062
- {
1063
- var arg = list[i];
1064
-
1065
- c(arg, st, "Expression");
1066
- } }
1067
- };
1068
- base.MemberExpression = function (node, st, c) {
1069
- c(node.object, st, "Expression");
1070
- if (node.computed) { c(node.property, st, "Expression"); }
1071
- };
1072
- base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {
1073
- if (node.declaration)
1074
- { c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }
1075
- if (node.source) { c(node.source, st, "Expression"); }
1076
- };
1077
- base.ExportAllDeclaration = function (node, st, c) {
1078
- if (node.exported)
1079
- { c(node.exported, st); }
1080
- c(node.source, st, "Expression");
1081
- };
1082
- base.ImportDeclaration = function (node, st, c) {
1083
- for (var i = 0, list = node.specifiers; i < list.length; i += 1)
1084
- {
1085
- var spec = list[i];
1086
-
1087
- c(spec, st);
1088
- }
1089
- c(node.source, st, "Expression");
1090
- };
1091
- base.ImportExpression = function (node, st, c) {
1092
- c(node.source, st, "Expression");
1093
- };
1094
- base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.PrivateIdentifier = base.Literal = ignore;
1095
-
1096
- base.TaggedTemplateExpression = function (node, st, c) {
1097
- c(node.tag, st, "Expression");
1098
- c(node.quasi, st, "Expression");
1099
- };
1100
- base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };
1101
- base.Class = function (node, st, c) {
1102
- if (node.id) { c(node.id, st, "Pattern"); }
1103
- if (node.superClass) { c(node.superClass, st, "Expression"); }
1104
- c(node.body, st);
1105
- };
1106
- base.ClassBody = function (node, st, c) {
1107
- for (var i = 0, list = node.body; i < list.length; i += 1)
1108
- {
1109
- var elt = list[i];
1110
-
1111
- c(elt, st);
1112
- }
1113
- };
1114
- base.MethodDefinition = base.PropertyDefinition = base.Property = function (node, st, c) {
1115
- if (node.computed) { c(node.key, st, "Expression"); }
1116
- if (node.value) { c(node.value, st, "Expression"); }
1117
- };
1118
-
1119
- async function collectTests(ctx, filepath) {
1120
- const request = await ctx.vite.environments.ssr.transformRequest(filepath);
1121
- if (!request) return null;
1122
- const ast = await parseAstAsync(request.code), testFilepath = relative(ctx.config.root, filepath), projectName = ctx.name, typecheckSubprojectName = projectName ? `${projectName}:__typecheck__` : "__typecheck__", file = {
1123
- filepath,
1124
- type: "suite",
1125
- id: generateHash(`${testFilepath}${typecheckSubprojectName}`),
1126
- name: testFilepath,
1127
- mode: "run",
1128
- tasks: [],
1129
- start: ast.start,
1130
- end: ast.end,
1131
- projectName,
1132
- meta: { typecheck: true },
1133
- file: null
1134
- };
1135
- file.file = file;
1136
- const definitions = [], getName = (callee) => {
1137
- if (!callee) return null;
1138
- if (callee.type === "Identifier") return callee.name;
1139
- if (callee.type === "CallExpression") return getName(callee.callee);
1140
- if (callee.type === "TaggedTemplateExpression") return getName(callee.tag);
1141
- if (callee.type === "MemberExpression")
1142
- // call as `__vite_ssr__.test.skip()`
1143
- return callee.object?.type === "Identifier" && [
1144
- "it",
1145
- "test",
1146
- "describe",
1147
- "suite"
1148
- ].includes(callee.object.name) ? callee.object?.name : callee.object?.name?.startsWith("__vite_ssr_") ? getName(callee.property) : getName(callee.object?.property);
1149
- // unwrap (0, ...)
1150
- if (callee.type === "SequenceExpression" && callee.expressions.length === 2) {
1151
- const [e0, e1] = callee.expressions;
1152
- if (e0.type === "Literal" && e0.value === 0) return getName(e1);
1153
- }
1154
- return null;
1155
- };
1156
- ancestor(ast, { CallExpression(node) {
1157
- const { callee } = node, name = getName(callee);
1158
- if (!name || ![
1159
- "it",
1160
- "test",
1161
- "describe",
1162
- "suite"
1163
- ].includes(name)) return;
1164
- const property = callee?.property?.name;
1165
- let mode = !property || property === name ? "run" : property;
1166
- // they will be picked up in the next iteration
1167
- if ([
1168
- "each",
1169
- "for",
1170
- "skipIf",
1171
- "runIf"
1172
- ].includes(mode)) return;
1173
- let start;
1174
- const end = node.end;
1175
- // .each
1176
- if (callee.type === "CallExpression") start = callee.end;
1177
- else if (callee.type === "TaggedTemplateExpression") start = callee.end + 1;
1178
- else start = node.start;
1179
- const { arguments: [messageNode] } = node, message = messageNode?.type === "Literal" || messageNode?.type === "TemplateLiteral" ? request.code.slice(messageNode.start + 1, messageNode.end - 1) : request.code.slice(messageNode.start, messageNode.end);
1180
- // cannot statically analyze, so we always skip it
1181
- if (mode === "skipIf" || mode === "runIf") mode = "skip";
1182
- definitions.push({
1183
- start,
1184
- end,
1185
- name: message,
1186
- type: name === "it" || name === "test" ? "test" : "suite",
1187
- mode,
1188
- task: null
1189
- });
1190
- } });
1191
- let lastSuite = file;
1192
- const updateLatestSuite = (index) => {
1193
- while (lastSuite.suite && lastSuite.end < index) lastSuite = lastSuite.suite;
1194
- return lastSuite;
1195
- };
1196
- definitions.sort((a, b) => a.start - b.start).forEach((definition) => {
1197
- const latestSuite = updateLatestSuite(definition.start);
1198
- let mode = definition.mode;
1199
- if (latestSuite.mode !== "run")
1200
- // inherit suite mode, if it's set
1201
- mode = latestSuite.mode;
1202
- if (definition.type === "suite") {
1203
- const task = {
1204
- type: definition.type,
1205
- id: "",
1206
- suite: latestSuite,
1207
- file,
1208
- tasks: [],
1209
- mode,
1210
- name: definition.name,
1211
- end: definition.end,
1212
- start: definition.start,
1213
- meta: { typecheck: true }
1214
- };
1215
- definition.task = task, latestSuite.tasks.push(task), lastSuite = task;
1216
- return;
1217
- }
1218
- const task = {
1219
- type: definition.type,
1220
- id: "",
1221
- suite: latestSuite,
1222
- file,
1223
- mode,
1224
- timeout: 0,
1225
- context: {},
1226
- name: definition.name,
1227
- end: definition.end,
1228
- start: definition.start,
1229
- annotations: [],
1230
- meta: { typecheck: true }
1231
- };
1232
- definition.task = task, latestSuite.tasks.push(task);
1233
- }), calculateSuiteHash(file);
1234
- const hasOnly = someTasksAreOnly(file);
1235
- return interpretTaskModes(file, ctx.config.testNamePattern, void 0, hasOnly, false, ctx.config.allowOnly), {
1236
- file,
1237
- parsed: request.code,
1238
- filepath,
1239
- map: request.map,
1240
- definitions
1241
- };
1242
- }
1243
-
1244
- const newLineRegExp = /\r?\n/, errCodeRegExp = /error TS(?<errCode>\d+)/;
1245
- async function makeTscErrorInfo(errInfo) {
1246
- const [errFilePathPos = "", ...errMsgRawArr] = errInfo.split(":");
1247
- if (!errFilePathPos || errMsgRawArr.length === 0 || errMsgRawArr.join("").length === 0) return ["unknown filepath", null];
1248
- const errMsgRaw = errMsgRawArr.join("").trim(), [errFilePath, errPos] = errFilePathPos.slice(0, -1).split("(");
1249
- if (!errFilePath || !errPos) return ["unknown filepath", null];
1250
- const [errLine, errCol] = errPos.split(",");
1251
- if (!errLine || !errCol) return [errFilePath, null];
1252
- // get errCode, errMsg
1253
- const execArr = errCodeRegExp.exec(errMsgRaw);
1254
- if (!execArr) return [errFilePath, null];
1255
- const errCodeStr = execArr.groups?.errCode ?? "";
1256
- if (!errCodeStr) return [errFilePath, null];
1257
- const line = Number(errLine), col = Number(errCol), errCode = Number(errCodeStr);
1258
- return [errFilePath, {
1259
- filePath: errFilePath,
1260
- errCode,
1261
- line,
1262
- column: col,
1263
- errMsg: errMsgRaw.slice(`error TS${errCode} `.length)
1264
- }];
1265
- }
1266
- async function getRawErrsMapFromTsCompile(tscErrorStdout) {
1267
- const rawErrsMap = /* @__PURE__ */ new Map();
1268
- return (await Promise.all(tscErrorStdout.split(newLineRegExp).reduce((prev, next) => {
1269
- if (!next) return prev;
1270
- if (next[0] !== " ") prev.push(next);
1271
- else prev[prev.length - 1] += `\n${next}`;
1272
- return prev;
1273
- }, []).map((errInfoLine) => makeTscErrorInfo(errInfoLine)))).forEach(([errFilePath, errInfo]) => {
1274
- if (errInfo) if (!rawErrsMap.has(errFilePath)) rawErrsMap.set(errFilePath, [errInfo]);
1275
- else rawErrsMap.get(errFilePath)?.push(errInfo);
1276
- }), rawErrsMap;
1277
- }
1278
-
1279
- function createIndexMap(source) {
1280
- const map = /* @__PURE__ */ new Map();
1281
- let index = 0, line = 1, column = 1;
1282
- for (const char of source) if (map.set(`${line}:${column}`, index++), char === "\n" || char === "\r\n") line++, column = 0;
1283
- else column++;
1284
- return map;
1285
- }
1286
-
1287
- class TypeCheckError extends Error {
1288
- name = "TypeCheckError";
1289
- constructor(message, stacks) {
1290
- super(message), this.message = message, this.stacks = stacks;
1291
- }
1292
- }
1293
- class Typechecker {
1294
- _onParseStart;
1295
- _onParseEnd;
1296
- _onWatcherRerun;
1297
- _result = {
1298
- files: [],
1299
- sourceErrors: [],
1300
- time: 0
1301
- };
1302
- _startTime = 0;
1303
- _output = "";
1304
- _tests = {};
1305
- process;
1306
- files = [];
1307
- constructor(project) {
1308
- this.project = project;
1309
- }
1310
- setFiles(files) {
1311
- this.files = files;
1312
- }
1313
- onParseStart(fn) {
1314
- this._onParseStart = fn;
1315
- }
1316
- onParseEnd(fn) {
1317
- this._onParseEnd = fn;
1318
- }
1319
- onWatcherRerun(fn) {
1320
- this._onWatcherRerun = fn;
1321
- }
1322
- async collectFileTests(filepath) {
1323
- return collectTests(this.project, filepath);
1324
- }
1325
- getFiles() {
1326
- return this.files;
1327
- }
1328
- async collectTests() {
1329
- const tests = (await Promise.all(this.getFiles().map((filepath) => this.collectFileTests(filepath)))).reduce((acc, data) => {
1330
- return data && (acc[data.filepath] = data), acc;
1331
- }, {});
1332
- return this._tests = tests, tests;
1333
- }
1334
- markPassed(file) {
1335
- if (!file.result?.state) file.result = { state: "pass" };
1336
- const markTasks = (tasks) => {
1337
- for (const task of tasks) {
1338
- if ("tasks" in task) markTasks(task.tasks);
1339
- if (!task.result?.state && (task.mode === "run" || task.mode === "queued")) task.result = { state: "pass" };
1340
- }
1341
- };
1342
- markTasks(file.tasks);
1343
- }
1344
- async prepareResults(output) {
1345
- const typeErrors = await this.parseTscLikeOutput(output), testFiles = new Set(this.getFiles());
1346
- if (!this._tests) this._tests = await this.collectTests();
1347
- const sourceErrors = [], files = [];
1348
- return testFiles.forEach((path) => {
1349
- const { file, definitions, map, parsed } = this._tests[path], errors = typeErrors.get(path);
1350
- if (files.push(file), !errors) {
1351
- this.markPassed(file);
1352
- return;
1353
- }
1354
- const sortedDefinitions = [...definitions.sort((a, b) => b.start - a.start)], traceMap = map && new TraceMap(map), indexMap = createIndexMap(parsed), markState = (task, state) => {
1355
- if (task.result = { state: task.mode === "run" || task.mode === "only" ? state : task.mode }, task.suite) markState(task.suite, state);
1356
- else if (task.file && task !== task.file) markState(task.file, state);
1357
- };
1358
- errors.forEach(({ error, originalError }) => {
1359
- const processedPos = traceMap ? findGeneratedPosition(traceMap, {
1360
- line: originalError.line,
1361
- column: originalError.column,
1362
- source: basename(path)
1363
- }) : originalError, line = processedPos.line ?? originalError.line, column = processedPos.column ?? originalError.column, index = indexMap.get(`${line}:${column}`), definition = index != null && sortedDefinitions.find((def) => def.start <= index && def.end >= index), suite = definition ? definition.task : file, state = suite.mode === "run" || suite.mode === "only" ? "fail" : suite.mode, errors = suite.result?.errors || [];
1364
- if (suite.result = {
1365
- state,
1366
- errors
1367
- }, errors.push(error), state === "fail") {
1368
- if (suite.suite) markState(suite.suite, "fail");
1369
- else if (suite.file && suite !== suite.file) markState(suite.file, "fail");
1370
- }
1371
- }), this.markPassed(file);
1372
- }), typeErrors.forEach((errors, path) => {
1373
- if (!testFiles.has(path)) sourceErrors.push(...errors.map(({ error }) => error));
1374
- }), {
1375
- files,
1376
- sourceErrors,
1377
- time: performance.now() - this._startTime
1378
- };
1379
- }
1380
- async parseTscLikeOutput(output) {
1381
- const errorsMap = await getRawErrsMapFromTsCompile(output), typesErrors = /* @__PURE__ */ new Map();
1382
- return errorsMap.forEach((errors, path) => {
1383
- const filepath = resolve$1(this.project.config.root, path), suiteErrors = errors.map((info) => {
1384
- const limit = Error.stackTraceLimit;
1385
- Error.stackTraceLimit = 0;
1386
- // Some expect-type errors have the most useful information on the second line e.g. `This expression is not callable.\n Type 'ExpectString<number>' has no call signatures.`
1387
- const errMsg = info.errMsg.replace(/\r?\n\s*(Type .* has no call signatures)/g, " $1"), error = new TypeCheckError(errMsg, [{
1388
- file: filepath,
1389
- line: info.line,
1390
- column: info.column,
1391
- method: ""
1392
- }]);
1393
- return Error.stackTraceLimit = limit, {
1394
- originalError: info,
1395
- error: {
1396
- name: error.name,
1397
- message: errMsg,
1398
- stacks: error.stacks,
1399
- stack: ""
1400
- }
1401
- };
1402
- });
1403
- typesErrors.set(filepath, suiteErrors);
1404
- }), typesErrors;
1405
- }
1406
- async stop() {
1407
- this.process?.kill(), this.process = void 0;
1408
- }
1409
- async ensurePackageInstalled(ctx, checker) {
1410
- if (checker !== "tsc" && checker !== "vue-tsc") return;
1411
- const packageName = checker === "tsc" ? "typescript" : "vue-tsc";
1412
- await ctx.packageInstaller.ensureInstalled(packageName, ctx.config.root);
1413
- }
1414
- getExitCode() {
1415
- return this.process?.exitCode != null && this.process.exitCode;
1416
- }
1417
- getOutput() {
1418
- return this._output;
1419
- }
1420
- async spawn() {
1421
- const { root, watch, typecheck } = this.project.config, args = [
1422
- "--noEmit",
1423
- "--pretty",
1424
- "false",
1425
- "--incremental",
1426
- "--tsBuildInfoFile",
1427
- join(process.versions.pnp ? join(nodeos__default.tmpdir(), this.project.hash) : distDir, "tsconfig.tmp.tsbuildinfo")
1428
- ];
1429
- // use builtin watcher because it's faster
1430
- if (watch) args.push("--watch");
1431
- if (typecheck.allowJs) args.push("--allowJs", "--checkJs");
1432
- if (typecheck.tsconfig) args.push("-p", resolve$1(root, typecheck.tsconfig));
1433
- this._output = "", this._startTime = performance.now();
1434
- const child = x(typecheck.checker, args, {
1435
- nodeOptions: {
1436
- cwd: root,
1437
- stdio: "pipe"
1438
- },
1439
- throwOnError: false
1440
- });
1441
- this.process = child.process;
1442
- let rerunTriggered = false, dataReceived = false;
1443
- return new Promise((resolve, reject) => {
1444
- if (!child.process || !child.process.stdout) {
1445
- reject(/* @__PURE__ */ new Error(`Failed to initialize ${typecheck.checker}. This is a bug in Vitest - please, open an issue with reproduction.`));
1446
- return;
1447
- }
1448
- child.process.stdout.on("data", (chunk) => {
1449
- if (dataReceived = true, this._output += chunk, watch) {
1450
- if (this._output.includes("File change detected") && !rerunTriggered) this._onWatcherRerun?.(), this._startTime = performance.now(), this._result.sourceErrors = [], this._result.files = [], this._tests = null, rerunTriggered = true;
1451
- if (/Found \w+ errors*. Watching for/.test(this._output)) rerunTriggered = false, this.prepareResults(this._output).then((result) => {
1452
- this._result = result, this._onParseEnd?.(result);
1453
- }), this._output = "";
1454
- }
1455
- });
1456
- const timeout = setTimeout(() => reject(/* @__PURE__ */ new Error(`${typecheck.checker} spawn timed out`)), this.project.config.typecheck.spawnTimeout);
1457
- function onError(cause) {
1458
- clearTimeout(timeout), reject(new Error("Spawning typechecker failed - is typescript installed?", { cause }));
1459
- }
1460
- if (child.process.once("spawn", () => {
1461
- if (this._onParseStart?.(), child.process?.off("error", onError), clearTimeout(timeout), process.platform === "win32")
1462
- // on Windows, the process might be spawned but fail to start
1463
- // we wait for a potential error here. if "close" event didn't trigger,
1464
- // we resolve the promise
1465
- setTimeout(() => {
1466
- resolve({ result: child });
1467
- }, 200);
1468
- else resolve({ result: child });
1469
- }), process.platform === "win32") child.process.once("close", (code) => {
1470
- if (code != null && code !== 0 && !dataReceived) onError(/* @__PURE__ */ new Error(`The ${typecheck.checker} command exited with code ${code}.`));
1471
- });
1472
- child.process.once("error", onError);
1473
- });
1474
- }
1475
- async start() {
1476
- if (this.process) return;
1477
- const { watch } = this.project.config, { result: child } = await this.spawn();
1478
- if (!watch) await child, this._result = await this.prepareResults(this._output), await this._onParseEnd?.(this._result);
1479
- }
1480
- getResult() {
1481
- return this._result;
1482
- }
1483
- getTestFiles() {
1484
- return Object.values(this._tests || {}).map((i) => i.file);
1485
- }
1486
- getTestPacksAndEvents() {
1487
- const packs = [], events = [];
1488
- for (const { file } of Object.values(this._tests || {})) {
1489
- const result = convertTasksToEvents(file);
1490
- packs.push(...result.packs), events.push(...result.events);
1491
- }
1492
- return {
1493
- packs,
1494
- events
1495
- };
1496
- }
1497
- }
1498
- function findGeneratedPosition(traceMap, { line, column, source }) {
1499
- const found = generatedPositionFor(traceMap, {
1500
- line,
1501
- column,
1502
- source
1503
- });
1504
- if (found.line !== null) return found;
1505
- // find the next source token position when the exact error position doesn't exist in source map.
1506
- // this can happen, for example, when the type error is in the comment "// @ts-expect-error"
1507
- // and comments are stripped away in the generated code.
1508
- const mappings = [];
1509
- eachMapping(traceMap, (m) => {
1510
- if (m.source === source && m.originalLine !== null && m.originalColumn !== null && (line === m.originalLine ? column < m.originalColumn : line < m.originalLine)) mappings.push(m);
1511
- });
1512
- const next = mappings.sort((a, b) => a.originalLine === b.originalLine ? a.originalColumn - b.originalColumn : a.originalLine - b.originalLine).at(0);
1513
- return next ? {
1514
- line: next.generatedLine,
1515
- column: next.generatedColumn
1516
- } : {
1517
- line: null,
1518
- column: null
1519
- };
1520
- }
1521
-
1522
- export { TypeCheckError as T, Typechecker as a, TraceMap as b, ancestor as c, createDefinesScript as d, convertTasksToEvents as e, getOutputFile as g, hasFailedSnapshot as h, originalPositionFor as o, parse$1 as p, stringify as s, wrapSerializableConfig as w };