@vitest/utils 4.0.0-beta.1 → 4.0.0-beta.11

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/index.js CHANGED
@@ -1,633 +1 @@
1
- import { g as getDefaultExportFromCjs } from './chunk-_commonjsHelpers.js';
2
- export { f as format, i as inspect, o as objDisplay, s as stringify } from './chunk-_commonjsHelpers.js';
3
- export { assertTypes, clone, createDefer, createSimpleStackTrace, deepClone, deepMerge, getCallLastIndex, getOwnProperties, getType, isNegativeNaN, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js';
4
- import c from 'tinyrainbow';
5
- import '@vitest/pretty-format';
6
- import 'loupe';
7
1
 
8
- var jsTokens_1;
9
- var hasRequiredJsTokens;
10
-
11
- function requireJsTokens () {
12
- if (hasRequiredJsTokens) return jsTokens_1;
13
- hasRequiredJsTokens = 1;
14
- // Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Simon Lydell
15
- // License: MIT.
16
- var Identifier, JSXIdentifier, JSXPunctuator, JSXString, JSXText, KeywordsWithExpressionAfter, KeywordsWithNoLineTerminatorAfter, LineTerminatorSequence, MultiLineComment, Newline, NumericLiteral, Punctuator, RegularExpressionLiteral, SingleLineComment, StringLiteral, Template, TokensNotPrecedingObjectLiteral, TokensPrecedingExpression, WhiteSpace;
17
- RegularExpressionLiteral = /\/(?![*\/])(?:\[(?:(?![\]\\]).|\\.)*\]|(?![\/\\]).|\\.)*(\/[$_\u200C\u200D\p{ID_Continue}]*|\\)?/yu;
18
- Punctuator = /--|\+\+|=>|\.{3}|\??\.(?!\d)|(?:&&|\|\||\?\?|[+\-%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2}|\/(?![\/*]))=?|[?~,:;[\](){}]/y;
19
- Identifier = /(\x23?)(?=[$_\p{ID_Start}\\])(?:[$_\u200C\u200D\p{ID_Continue}]|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+/yu;
20
- StringLiteral = /(['"])(?:(?!\1)[^\\\n\r]|\\(?:\r\n|[^]))*(\1)?/y;
21
- NumericLiteral = /(?:0[xX][\da-fA-F](?:_?[\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|0n|[1-9](?:_?\d)*n|(?:(?:0(?!\d)|0\d*[89]\d*|[1-9](?:_?\d)*)(?:\.(?:\d(?:_?\d)*)?)?|\.\d(?:_?\d)*)(?:[eE][+-]?\d(?:_?\d)*)?|0[0-7]+/y;
22
- Template = /[`}](?:[^`\\$]|\\[^]|\$(?!\{))*(`|\$\{)?/y;
23
- WhiteSpace = /[\t\v\f\ufeff\p{Zs}]+/yu;
24
- LineTerminatorSequence = /\r?\n|[\r\u2028\u2029]/y;
25
- MultiLineComment = /\/\*(?:[^*]|\*(?!\/))*(\*\/)?/y;
26
- SingleLineComment = /\/\/.*/y;
27
- JSXPunctuator = /[<>.:={}]|\/(?![\/*])/y;
28
- JSXIdentifier = /[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}-]*/yu;
29
- JSXString = /(['"])(?:(?!\1)[^])*(\1)?/y;
30
- JSXText = /[^<>{}]+/y;
31
- TokensPrecedingExpression = /^(?:[\/+-]|\.{3}|\?(?:InterpolationIn(?:JSX|Template)|NoLineTerminatorHere|NonExpressionParenEnd|UnaryIncDec))?$|[{}([,;<>=*%&|^!~?:]$/;
32
- TokensNotPrecedingObjectLiteral = /^(?:=>|[;\]){}]|else|\?(?:NoLineTerminatorHere|NonExpressionParenEnd))?$/;
33
- KeywordsWithExpressionAfter = /^(?:await|case|default|delete|do|else|instanceof|new|return|throw|typeof|void|yield)$/;
34
- KeywordsWithNoLineTerminatorAfter = /^(?:return|throw|yield)$/;
35
- Newline = RegExp(LineTerminatorSequence.source);
36
- jsTokens_1 = function*(input, {jsx = false} = {}) {
37
- var braces, firstCodePoint, isExpression, lastIndex, lastSignificantToken, length, match, mode, nextLastIndex, nextLastSignificantToken, parenNesting, postfixIncDec, punctuator, stack;
38
- ({length} = input);
39
- lastIndex = 0;
40
- lastSignificantToken = "";
41
- stack = [
42
- {tag: "JS"}
43
- ];
44
- braces = [];
45
- parenNesting = 0;
46
- postfixIncDec = false;
47
- while (lastIndex < length) {
48
- mode = stack[stack.length - 1];
49
- switch (mode.tag) {
50
- case "JS":
51
- case "JSNonExpressionParen":
52
- case "InterpolationInTemplate":
53
- case "InterpolationInJSX":
54
- if (input[lastIndex] === "/" && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
55
- RegularExpressionLiteral.lastIndex = lastIndex;
56
- if (match = RegularExpressionLiteral.exec(input)) {
57
- lastIndex = RegularExpressionLiteral.lastIndex;
58
- lastSignificantToken = match[0];
59
- postfixIncDec = true;
60
- yield ({
61
- type: "RegularExpressionLiteral",
62
- value: match[0],
63
- closed: match[1] !== void 0 && match[1] !== "\\"
64
- });
65
- continue;
66
- }
67
- }
68
- Punctuator.lastIndex = lastIndex;
69
- if (match = Punctuator.exec(input)) {
70
- punctuator = match[0];
71
- nextLastIndex = Punctuator.lastIndex;
72
- nextLastSignificantToken = punctuator;
73
- switch (punctuator) {
74
- case "(":
75
- if (lastSignificantToken === "?NonExpressionParenKeyword") {
76
- stack.push({
77
- tag: "JSNonExpressionParen",
78
- nesting: parenNesting
79
- });
80
- }
81
- parenNesting++;
82
- postfixIncDec = false;
83
- break;
84
- case ")":
85
- parenNesting--;
86
- postfixIncDec = true;
87
- if (mode.tag === "JSNonExpressionParen" && parenNesting === mode.nesting) {
88
- stack.pop();
89
- nextLastSignificantToken = "?NonExpressionParenEnd";
90
- postfixIncDec = false;
91
- }
92
- break;
93
- case "{":
94
- Punctuator.lastIndex = 0;
95
- isExpression = !TokensNotPrecedingObjectLiteral.test(lastSignificantToken) && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken));
96
- braces.push(isExpression);
97
- postfixIncDec = false;
98
- break;
99
- case "}":
100
- switch (mode.tag) {
101
- case "InterpolationInTemplate":
102
- if (braces.length === mode.nesting) {
103
- Template.lastIndex = lastIndex;
104
- match = Template.exec(input);
105
- lastIndex = Template.lastIndex;
106
- lastSignificantToken = match[0];
107
- if (match[1] === "${") {
108
- lastSignificantToken = "?InterpolationInTemplate";
109
- postfixIncDec = false;
110
- yield ({
111
- type: "TemplateMiddle",
112
- value: match[0]
113
- });
114
- } else {
115
- stack.pop();
116
- postfixIncDec = true;
117
- yield ({
118
- type: "TemplateTail",
119
- value: match[0],
120
- closed: match[1] === "`"
121
- });
122
- }
123
- continue;
124
- }
125
- break;
126
- case "InterpolationInJSX":
127
- if (braces.length === mode.nesting) {
128
- stack.pop();
129
- lastIndex += 1;
130
- lastSignificantToken = "}";
131
- yield ({
132
- type: "JSXPunctuator",
133
- value: "}"
134
- });
135
- continue;
136
- }
137
- }
138
- postfixIncDec = braces.pop();
139
- nextLastSignificantToken = postfixIncDec ? "?ExpressionBraceEnd" : "}";
140
- break;
141
- case "]":
142
- postfixIncDec = true;
143
- break;
144
- case "++":
145
- case "--":
146
- nextLastSignificantToken = postfixIncDec ? "?PostfixIncDec" : "?UnaryIncDec";
147
- break;
148
- case "<":
149
- if (jsx && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
150
- stack.push({tag: "JSXTag"});
151
- lastIndex += 1;
152
- lastSignificantToken = "<";
153
- yield ({
154
- type: "JSXPunctuator",
155
- value: punctuator
156
- });
157
- continue;
158
- }
159
- postfixIncDec = false;
160
- break;
161
- default:
162
- postfixIncDec = false;
163
- }
164
- lastIndex = nextLastIndex;
165
- lastSignificantToken = nextLastSignificantToken;
166
- yield ({
167
- type: "Punctuator",
168
- value: punctuator
169
- });
170
- continue;
171
- }
172
- Identifier.lastIndex = lastIndex;
173
- if (match = Identifier.exec(input)) {
174
- lastIndex = Identifier.lastIndex;
175
- nextLastSignificantToken = match[0];
176
- switch (match[0]) {
177
- case "for":
178
- case "if":
179
- case "while":
180
- case "with":
181
- if (lastSignificantToken !== "." && lastSignificantToken !== "?.") {
182
- nextLastSignificantToken = "?NonExpressionParenKeyword";
183
- }
184
- }
185
- lastSignificantToken = nextLastSignificantToken;
186
- postfixIncDec = !KeywordsWithExpressionAfter.test(match[0]);
187
- yield ({
188
- type: match[1] === "#" ? "PrivateIdentifier" : "IdentifierName",
189
- value: match[0]
190
- });
191
- continue;
192
- }
193
- StringLiteral.lastIndex = lastIndex;
194
- if (match = StringLiteral.exec(input)) {
195
- lastIndex = StringLiteral.lastIndex;
196
- lastSignificantToken = match[0];
197
- postfixIncDec = true;
198
- yield ({
199
- type: "StringLiteral",
200
- value: match[0],
201
- closed: match[2] !== void 0
202
- });
203
- continue;
204
- }
205
- NumericLiteral.lastIndex = lastIndex;
206
- if (match = NumericLiteral.exec(input)) {
207
- lastIndex = NumericLiteral.lastIndex;
208
- lastSignificantToken = match[0];
209
- postfixIncDec = true;
210
- yield ({
211
- type: "NumericLiteral",
212
- value: match[0]
213
- });
214
- continue;
215
- }
216
- Template.lastIndex = lastIndex;
217
- if (match = Template.exec(input)) {
218
- lastIndex = Template.lastIndex;
219
- lastSignificantToken = match[0];
220
- if (match[1] === "${") {
221
- lastSignificantToken = "?InterpolationInTemplate";
222
- stack.push({
223
- tag: "InterpolationInTemplate",
224
- nesting: braces.length
225
- });
226
- postfixIncDec = false;
227
- yield ({
228
- type: "TemplateHead",
229
- value: match[0]
230
- });
231
- } else {
232
- postfixIncDec = true;
233
- yield ({
234
- type: "NoSubstitutionTemplate",
235
- value: match[0],
236
- closed: match[1] === "`"
237
- });
238
- }
239
- continue;
240
- }
241
- break;
242
- case "JSXTag":
243
- case "JSXTagEnd":
244
- JSXPunctuator.lastIndex = lastIndex;
245
- if (match = JSXPunctuator.exec(input)) {
246
- lastIndex = JSXPunctuator.lastIndex;
247
- nextLastSignificantToken = match[0];
248
- switch (match[0]) {
249
- case "<":
250
- stack.push({tag: "JSXTag"});
251
- break;
252
- case ">":
253
- stack.pop();
254
- if (lastSignificantToken === "/" || mode.tag === "JSXTagEnd") {
255
- nextLastSignificantToken = "?JSX";
256
- postfixIncDec = true;
257
- } else {
258
- stack.push({tag: "JSXChildren"});
259
- }
260
- break;
261
- case "{":
262
- stack.push({
263
- tag: "InterpolationInJSX",
264
- nesting: braces.length
265
- });
266
- nextLastSignificantToken = "?InterpolationInJSX";
267
- postfixIncDec = false;
268
- break;
269
- case "/":
270
- if (lastSignificantToken === "<") {
271
- stack.pop();
272
- if (stack[stack.length - 1].tag === "JSXChildren") {
273
- stack.pop();
274
- }
275
- stack.push({tag: "JSXTagEnd"});
276
- }
277
- }
278
- lastSignificantToken = nextLastSignificantToken;
279
- yield ({
280
- type: "JSXPunctuator",
281
- value: match[0]
282
- });
283
- continue;
284
- }
285
- JSXIdentifier.lastIndex = lastIndex;
286
- if (match = JSXIdentifier.exec(input)) {
287
- lastIndex = JSXIdentifier.lastIndex;
288
- lastSignificantToken = match[0];
289
- yield ({
290
- type: "JSXIdentifier",
291
- value: match[0]
292
- });
293
- continue;
294
- }
295
- JSXString.lastIndex = lastIndex;
296
- if (match = JSXString.exec(input)) {
297
- lastIndex = JSXString.lastIndex;
298
- lastSignificantToken = match[0];
299
- yield ({
300
- type: "JSXString",
301
- value: match[0],
302
- closed: match[2] !== void 0
303
- });
304
- continue;
305
- }
306
- break;
307
- case "JSXChildren":
308
- JSXText.lastIndex = lastIndex;
309
- if (match = JSXText.exec(input)) {
310
- lastIndex = JSXText.lastIndex;
311
- lastSignificantToken = match[0];
312
- yield ({
313
- type: "JSXText",
314
- value: match[0]
315
- });
316
- continue;
317
- }
318
- switch (input[lastIndex]) {
319
- case "<":
320
- stack.push({tag: "JSXTag"});
321
- lastIndex++;
322
- lastSignificantToken = "<";
323
- yield ({
324
- type: "JSXPunctuator",
325
- value: "<"
326
- });
327
- continue;
328
- case "{":
329
- stack.push({
330
- tag: "InterpolationInJSX",
331
- nesting: braces.length
332
- });
333
- lastIndex++;
334
- lastSignificantToken = "?InterpolationInJSX";
335
- postfixIncDec = false;
336
- yield ({
337
- type: "JSXPunctuator",
338
- value: "{"
339
- });
340
- continue;
341
- }
342
- }
343
- WhiteSpace.lastIndex = lastIndex;
344
- if (match = WhiteSpace.exec(input)) {
345
- lastIndex = WhiteSpace.lastIndex;
346
- yield ({
347
- type: "WhiteSpace",
348
- value: match[0]
349
- });
350
- continue;
351
- }
352
- LineTerminatorSequence.lastIndex = lastIndex;
353
- if (match = LineTerminatorSequence.exec(input)) {
354
- lastIndex = LineTerminatorSequence.lastIndex;
355
- postfixIncDec = false;
356
- if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
357
- lastSignificantToken = "?NoLineTerminatorHere";
358
- }
359
- yield ({
360
- type: "LineTerminatorSequence",
361
- value: match[0]
362
- });
363
- continue;
364
- }
365
- MultiLineComment.lastIndex = lastIndex;
366
- if (match = MultiLineComment.exec(input)) {
367
- lastIndex = MultiLineComment.lastIndex;
368
- if (Newline.test(match[0])) {
369
- postfixIncDec = false;
370
- if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
371
- lastSignificantToken = "?NoLineTerminatorHere";
372
- }
373
- }
374
- yield ({
375
- type: "MultiLineComment",
376
- value: match[0],
377
- closed: match[1] !== void 0
378
- });
379
- continue;
380
- }
381
- SingleLineComment.lastIndex = lastIndex;
382
- if (match = SingleLineComment.exec(input)) {
383
- lastIndex = SingleLineComment.lastIndex;
384
- postfixIncDec = false;
385
- yield ({
386
- type: "SingleLineComment",
387
- value: match[0]
388
- });
389
- continue;
390
- }
391
- firstCodePoint = String.fromCodePoint(input.codePointAt(lastIndex));
392
- lastIndex += firstCodePoint.length;
393
- lastSignificantToken = firstCodePoint;
394
- postfixIncDec = false;
395
- yield ({
396
- type: mode.tag.startsWith("JSX") ? "JSXInvalid" : "Invalid",
397
- value: firstCodePoint
398
- });
399
- }
400
- return void 0;
401
- };
402
- return jsTokens_1;
403
- }
404
-
405
- var jsTokensExports = requireJsTokens();
406
- var jsTokens = /*@__PURE__*/getDefaultExportFromCjs(jsTokensExports);
407
-
408
- // src/index.ts
409
- var reservedWords = {
410
- keyword: [
411
- "break",
412
- "case",
413
- "catch",
414
- "continue",
415
- "debugger",
416
- "default",
417
- "do",
418
- "else",
419
- "finally",
420
- "for",
421
- "function",
422
- "if",
423
- "return",
424
- "switch",
425
- "throw",
426
- "try",
427
- "var",
428
- "const",
429
- "while",
430
- "with",
431
- "new",
432
- "this",
433
- "super",
434
- "class",
435
- "extends",
436
- "export",
437
- "import",
438
- "null",
439
- "true",
440
- "false",
441
- "in",
442
- "instanceof",
443
- "typeof",
444
- "void",
445
- "delete"
446
- ],
447
- strict: [
448
- "implements",
449
- "interface",
450
- "let",
451
- "package",
452
- "private",
453
- "protected",
454
- "public",
455
- "static",
456
- "yield"
457
- ]
458
- }, keywords = new Set(reservedWords.keyword), reservedWordsStrictSet = new Set(reservedWords.strict), sometimesKeywords = /* @__PURE__ */ new Set(["as", "async", "from", "get", "of", "set"]);
459
- function isReservedWord(word) {
460
- return word === "await" || word === "enum";
461
- }
462
- function isStrictReservedWord(word) {
463
- return isReservedWord(word) || reservedWordsStrictSet.has(word);
464
- }
465
- function isKeyword(word) {
466
- return keywords.has(word);
467
- }
468
- var BRACKET = /^[()[\]{}]$/, getTokenType = function(token) {
469
- if (token.type === "IdentifierName") {
470
- if (isKeyword(token.value) || isStrictReservedWord(token.value) || sometimesKeywords.has(token.value))
471
- return "Keyword";
472
- if (token.value[0] && token.value[0] !== token.value[0].toLowerCase())
473
- return "IdentifierCapitalized";
474
- }
475
- return token.type === "Punctuator" && BRACKET.test(token.value) ? "Bracket" : token.type === "Invalid" && (token.value === "@" || token.value === "#") ? "Punctuator" : token.type;
476
- };
477
- function getCallableType(token) {
478
- if (token.type === "IdentifierName")
479
- return "IdentifierCallable";
480
- if (token.type === "PrivateIdentifier")
481
- return "PrivateIdentifierCallable";
482
- throw new Error("Not a callable token");
483
- }
484
- var colorize = (defs, type, value) => {
485
- let colorize2 = defs[type];
486
- return colorize2 ? colorize2(value) : value;
487
- }, highlightTokens = (defs, text, jsx) => {
488
- let highlighted = "", lastPotentialCallable = null, stackedHighlight = "";
489
- for (let token of jsTokens(text, { jsx })) {
490
- let type = getTokenType(token);
491
- if (type === "IdentifierName" || type === "PrivateIdentifier") {
492
- lastPotentialCallable && (highlighted += colorize(defs, getTokenType(lastPotentialCallable), lastPotentialCallable.value) + stackedHighlight, stackedHighlight = ""), lastPotentialCallable = token;
493
- continue;
494
- }
495
- if (lastPotentialCallable && (token.type === "WhiteSpace" || token.type === "LineTerminatorSequence" || token.type === "Punctuator" && (token.value === "?." || token.value === "!"))) {
496
- stackedHighlight += colorize(defs, type, token.value);
497
- continue;
498
- }
499
- if (stackedHighlight && !lastPotentialCallable && (highlighted += stackedHighlight, stackedHighlight = ""), lastPotentialCallable) {
500
- let type2 = token.type === "Punctuator" && token.value === "(" ? getCallableType(lastPotentialCallable) : getTokenType(lastPotentialCallable);
501
- highlighted += colorize(defs, type2, lastPotentialCallable.value) + stackedHighlight, stackedHighlight = "", lastPotentialCallable = null;
502
- }
503
- highlighted += colorize(defs, type, token.value);
504
- }
505
- return highlighted;
506
- };
507
- function highlight$1(code, options = { jsx: false, colors: {} }) {
508
- return code && highlightTokens(options.colors || {}, code, options.jsx);
509
- }
510
-
511
- function getDefs(c) {
512
- const Invalid = (text) => c.white(c.bgRed(c.bold(text)));
513
- return {
514
- Keyword: c.magenta,
515
- IdentifierCapitalized: c.yellow,
516
- Punctuator: c.yellow,
517
- StringLiteral: c.green,
518
- NoSubstitutionTemplate: c.green,
519
- MultiLineComment: c.gray,
520
- SingleLineComment: c.gray,
521
- RegularExpressionLiteral: c.cyan,
522
- NumericLiteral: c.blue,
523
- TemplateHead: (text) => c.green(text.slice(0, text.length - 2)) + c.cyan(text.slice(-2)),
524
- TemplateTail: (text) => c.cyan(text.slice(0, 1)) + c.green(text.slice(1)),
525
- TemplateMiddle: (text) => c.cyan(text.slice(0, 1)) + c.green(text.slice(1, text.length - 2)) + c.cyan(text.slice(-2)),
526
- IdentifierCallable: c.blue,
527
- PrivateIdentifierCallable: (text) => `#${c.blue(text.slice(1))}`,
528
- Invalid,
529
- JSXString: c.green,
530
- JSXIdentifier: c.yellow,
531
- JSXInvalid: Invalid,
532
- JSXPunctuator: c.yellow
533
- };
534
- }
535
- function highlight(code, options = { jsx: false }) {
536
- return highlight$1(code, {
537
- jsx: options.jsx,
538
- colors: getDefs(options.colors || c)
539
- });
540
- }
541
-
542
- // port from nanoid
543
- // https://github.com/ai/nanoid
544
- const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
545
- function nanoid(size = 21) {
546
- let id = "";
547
- let i = size;
548
- while (i--) {
549
- id += urlAlphabet[Math.random() * 64 | 0];
550
- }
551
- return id;
552
- }
553
-
554
- const lineSplitRE = /\r?\n/;
555
- function positionToOffset(source, lineNumber, columnNumber) {
556
- const lines = source.split(lineSplitRE);
557
- const nl = /\r\n/.test(source) ? 2 : 1;
558
- let start = 0;
559
- if (lineNumber > lines.length) {
560
- return source.length;
561
- }
562
- for (let i = 0; i < lineNumber - 1; i++) {
563
- start += lines[i].length + nl;
564
- }
565
- return start + columnNumber;
566
- }
567
- function offsetToLineNumber(source, offset) {
568
- if (offset > source.length) {
569
- throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
570
- }
571
- const lines = source.split(lineSplitRE);
572
- const nl = /\r\n/.test(source) ? 2 : 1;
573
- let counted = 0;
574
- let line = 0;
575
- for (; line < lines.length; line++) {
576
- const lineLength = lines[line].length + nl;
577
- if (counted + lineLength >= offset) {
578
- break;
579
- }
580
- counted += lineLength;
581
- }
582
- return line + 1;
583
- }
584
-
585
- const RealDate = Date;
586
- function random(seed) {
587
- const x = Math.sin(seed++) * 1e4;
588
- return x - Math.floor(x);
589
- }
590
- function shuffle(array, seed = RealDate.now()) {
591
- let length = array.length;
592
- while (length) {
593
- const index = Math.floor(random(seed) * length--);
594
- const previous = array[length];
595
- array[length] = array[index];
596
- array[index] = previous;
597
- ++seed;
598
- }
599
- return array;
600
- }
601
-
602
- const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS");
603
- function getSafeTimers() {
604
- const { setTimeout: safeSetTimeout, setInterval: safeSetInterval, clearInterval: safeClearInterval, clearTimeout: safeClearTimeout, setImmediate: safeSetImmediate, clearImmediate: safeClearImmediate, queueMicrotask: safeQueueMicrotask } = globalThis[SAFE_TIMERS_SYMBOL] || globalThis;
605
- const { nextTick: safeNextTick } = globalThis[SAFE_TIMERS_SYMBOL] || globalThis.process || {};
606
- return {
607
- nextTick: safeNextTick,
608
- setTimeout: safeSetTimeout,
609
- setInterval: safeSetInterval,
610
- clearInterval: safeClearInterval,
611
- clearTimeout: safeClearTimeout,
612
- setImmediate: safeSetImmediate,
613
- clearImmediate: safeClearImmediate,
614
- queueMicrotask: safeQueueMicrotask
615
- };
616
- }
617
- function setSafeTimers() {
618
- const { setTimeout: safeSetTimeout, setInterval: safeSetInterval, clearInterval: safeClearInterval, clearTimeout: safeClearTimeout, setImmediate: safeSetImmediate, clearImmediate: safeClearImmediate, queueMicrotask: safeQueueMicrotask } = globalThis;
619
- const { nextTick: safeNextTick } = globalThis.process || {};
620
- const timers = {
621
- nextTick: safeNextTick,
622
- setTimeout: safeSetTimeout,
623
- setInterval: safeSetInterval,
624
- clearInterval: safeClearInterval,
625
- clearTimeout: safeClearTimeout,
626
- setImmediate: safeSetImmediate,
627
- clearImmediate: safeClearImmediate,
628
- queueMicrotask: safeQueueMicrotask
629
- };
630
- globalThis[SAFE_TIMERS_SYMBOL] = timers;
631
- }
632
-
633
- export { getSafeTimers, highlight, lineSplitRE, nanoid, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle };
@@ -0,0 +1,5 @@
1
+ declare const lineSplitRE: RegExp;
2
+ declare function positionToOffset(source: string, lineNumber: number, columnNumber: number): number;
3
+ declare function offsetToLineNumber(source: string, offset: number): number;
4
+
5
+ export { lineSplitRE, offsetToLineNumber, positionToOffset };
package/dist/offset.js ADDED
@@ -0,0 +1,32 @@
1
+ const lineSplitRE = /\r?\n/;
2
+ function positionToOffset(source, lineNumber, columnNumber) {
3
+ const lines = source.split(lineSplitRE);
4
+ const nl = /\r\n/.test(source) ? 2 : 1;
5
+ let start = 0;
6
+ if (lineNumber > lines.length) {
7
+ return source.length;
8
+ }
9
+ for (let i = 0; i < lineNumber - 1; i++) {
10
+ start += lines[i].length + nl;
11
+ }
12
+ return start + columnNumber;
13
+ }
14
+ function offsetToLineNumber(source, offset) {
15
+ if (offset > source.length) {
16
+ throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
17
+ }
18
+ const lines = source.split(lineSplitRE);
19
+ const nl = /\r\n/.test(source) ? 2 : 1;
20
+ let counted = 0;
21
+ let line = 0;
22
+ for (; line < lines.length; line++) {
23
+ const lineLength = lines[line].length + nl;
24
+ if (counted + lineLength >= offset) {
25
+ break;
26
+ }
27
+ counted += lineLength;
28
+ }
29
+ return line + 1;
30
+ }
31
+
32
+ export { lineSplitRE, offsetToLineNumber, positionToOffset };
@@ -0,0 +1,7 @@
1
+ declare function findNearestPackageData(basedir: string): {
2
+ type?: "module" | "commonjs";
3
+ };
4
+ declare function getCachedData<T>(cache: Map<string, T>, basedir: string, originalBasedir: string): NonNullable<T> | undefined;
5
+ declare function setCacheData<T>(cache: Map<string, T>, data: T, basedir: string, originalBasedir: string): void;
6
+
7
+ export { findNearestPackageData, getCachedData, setCacheData };