版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

108 regels
3.5 KiB

  1. import { getNames, getTests } from '@vitest/runner/utils';
  2. import { resolve } from 'pathe';
  3. import { a as isPrimitive, n as notNullish } from './chunk-utils-base.b5ddfcc9.js';
  4. function hasFailedSnapshot(suite) {
  5. return getTests(suite).some((s) => {
  6. var _a, _b;
  7. return (_b = (_a = s.result) == null ? void 0 : _a.errors) == null ? void 0 : _b.some((e) => e && e.message && e.message.match(/Snapshot .* mismatched/));
  8. });
  9. }
  10. function getFullName(task, separator = " > ") {
  11. return getNames(task).join(separator);
  12. }
  13. const lineSplitRE = /\r?\n/;
  14. const stackIgnorePatterns = [
  15. "node:internal",
  16. /\/packages\/\w+\/dist\//,
  17. /\/@vitest\/\w+\/dist\//,
  18. "/vitest/dist/",
  19. "/vitest/src/",
  20. "/vite-node/dist/",
  21. "/vite-node/src/",
  22. "/node_modules/chai/",
  23. "/node_modules/tinypool/",
  24. "/node_modules/tinyspy/"
  25. ];
  26. function extractLocation(urlLike) {
  27. if (!urlLike.includes(":"))
  28. return [urlLike];
  29. const regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/;
  30. const parts = regExp.exec(urlLike.replace(/[()]/g, ""));
  31. if (!parts)
  32. return [urlLike];
  33. return [parts[1], parts[2] || void 0, parts[3] || void 0];
  34. }
  35. function parseSingleStack(raw) {
  36. let line = raw.trim();
  37. if (line.includes("(eval "))
  38. line = line.replace(/eval code/g, "eval").replace(/(\(eval at [^()]*)|(,.*$)/g, "");
  39. let sanitizedLine = line.replace(/^\s+/, "").replace(/\(eval code/g, "(").replace(/^.*?\s+/, "");
  40. const location = sanitizedLine.match(/ (\(.+\)$)/);
  41. sanitizedLine = location ? sanitizedLine.replace(location[0], "") : sanitizedLine;
  42. const [url, lineNumber, columnNumber] = extractLocation(location ? location[1] : sanitizedLine);
  43. let method = location && sanitizedLine || "";
  44. let file = url && ["eval", "<anonymous>"].includes(url) ? void 0 : url;
  45. if (!file || !lineNumber || !columnNumber)
  46. return null;
  47. if (method.startsWith("async "))
  48. method = method.slice(6);
  49. if (file.startsWith("file://"))
  50. file = file.slice(7);
  51. file = resolve(file);
  52. return {
  53. method,
  54. file,
  55. line: parseInt(lineNumber),
  56. column: parseInt(columnNumber)
  57. };
  58. }
  59. function parseStacktrace(stack, full = false) {
  60. const stackFrames = stack.split("\n").map((raw) => {
  61. const stack2 = parseSingleStack(raw);
  62. if (!stack2 || !full && stackIgnorePatterns.some((p) => stack2.file.match(p)))
  63. return null;
  64. return stack2;
  65. }).filter(notNullish);
  66. return stackFrames;
  67. }
  68. function parseErrorStacktrace(e, full = false) {
  69. if (!e || isPrimitive(e))
  70. return [];
  71. if (e.stacks)
  72. return e.stacks;
  73. const stackStr = e.stack || e.stackStr || "";
  74. const stackFrames = parseStacktrace(stackStr, full);
  75. e.stacks = stackFrames;
  76. return stackFrames;
  77. }
  78. function positionToOffset(source, lineNumber, columnNumber) {
  79. const lines = source.split(lineSplitRE);
  80. let start = 0;
  81. if (lineNumber > lines.length)
  82. return source.length;
  83. for (let i = 0; i < lineNumber - 1; i++)
  84. start += lines[i].length + 1;
  85. return start + columnNumber;
  86. }
  87. function offsetToLineNumber(source, offset) {
  88. if (offset > source.length) {
  89. throw new Error(
  90. `offset is longer than source length! offset ${offset} > length ${source.length}`
  91. );
  92. }
  93. const lines = source.split(lineSplitRE);
  94. let counted = 0;
  95. let line = 0;
  96. for (; line < lines.length; line++) {
  97. const lineLength = lines[line].length + 1;
  98. if (counted + lineLength >= offset)
  99. break;
  100. counted += lineLength;
  101. }
  102. return line + 1;
  103. }
  104. export { parseErrorStacktrace as a, parseSingleStack as b, getFullName as g, hasFailedSnapshot as h, lineSplitRE as l, offsetToLineNumber as o, positionToOffset as p };