@vitest/utils
Advanced tools
Comparing version 0.33.0 to 0.34.0
@@ -1,3 +0,2 @@ | ||
import { notNullish, isPrimitive } from './helpers.js'; | ||
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isObject, noop, objectAttr, parseRegexp, slash, toArray } from './helpers.js'; | ||
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js'; | ||
export { f as format, i as inspect, o as objDisplay, s as stringify } from './chunk-display.js'; | ||
@@ -85,161 +84,3 @@ import { S as SAFE_TIMERS_SYMBOL } from './chunk-colors.js'; | ||
function normalizeWindowsPath(input = "") { | ||
if (!input || !input.includes("\\")) { | ||
return input; | ||
} | ||
return input.replace(/\\/g, "/"); | ||
} | ||
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/; | ||
function cwd() { | ||
if (typeof process !== "undefined") { | ||
return process.cwd().replace(/\\/g, "/"); | ||
} | ||
return "/"; | ||
} | ||
const resolve = function(...arguments_) { | ||
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument)); | ||
let resolvedPath = ""; | ||
let resolvedAbsolute = false; | ||
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) { | ||
const path = index >= 0 ? arguments_[index] : cwd(); | ||
if (!path || path.length === 0) { | ||
continue; | ||
} | ||
resolvedPath = `${path}/${resolvedPath}`; | ||
resolvedAbsolute = isAbsolute(path); | ||
} | ||
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute); | ||
if (resolvedAbsolute && !isAbsolute(resolvedPath)) { | ||
return `/${resolvedPath}`; | ||
} | ||
return resolvedPath.length > 0 ? resolvedPath : "."; | ||
}; | ||
function normalizeString(path, allowAboveRoot) { | ||
let res = ""; | ||
let lastSegmentLength = 0; | ||
let lastSlash = -1; | ||
let dots = 0; | ||
let char = null; | ||
for (let index = 0; index <= path.length; ++index) { | ||
if (index < path.length) { | ||
char = path[index]; | ||
} else if (char === "/") { | ||
break; | ||
} else { | ||
char = "/"; | ||
} | ||
if (char === "/") { | ||
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) { | ||
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") { | ||
if (res.length > 2) { | ||
const lastSlashIndex = res.lastIndexOf("/"); | ||
if (lastSlashIndex === -1) { | ||
res = ""; | ||
lastSegmentLength = 0; | ||
} else { | ||
res = res.slice(0, lastSlashIndex); | ||
lastSegmentLength = res.length - 1 - res.lastIndexOf("/"); | ||
} | ||
lastSlash = index; | ||
dots = 0; | ||
continue; | ||
} else if (res.length > 0) { | ||
res = ""; | ||
lastSegmentLength = 0; | ||
lastSlash = index; | ||
dots = 0; | ||
continue; | ||
} | ||
} | ||
if (allowAboveRoot) { | ||
res += res.length > 0 ? "/.." : ".."; | ||
lastSegmentLength = 2; | ||
} | ||
} else { | ||
if (res.length > 0) { | ||
res += `/${path.slice(lastSlash + 1, index)}`; | ||
} else { | ||
res = path.slice(lastSlash + 1, index); | ||
} | ||
lastSegmentLength = index - lastSlash - 1; | ||
} | ||
lastSlash = index; | ||
dots = 0; | ||
} else if (char === "." && dots !== -1) { | ||
++dots; | ||
} else { | ||
dots = -1; | ||
} | ||
} | ||
return res; | ||
} | ||
const isAbsolute = function(p) { | ||
return _IS_ABSOLUTE_RE.test(p); | ||
}; | ||
const lineSplitRE = /\r?\n/; | ||
const stackIgnorePatterns = [ | ||
"node:internal", | ||
/\/packages\/\w+\/dist\//, | ||
/\/@vitest\/\w+\/dist\//, | ||
"/vitest/dist/", | ||
"/vitest/src/", | ||
"/vite-node/dist/", | ||
"/vite-node/src/", | ||
"/node_modules/chai/", | ||
"/node_modules/tinypool/", | ||
"/node_modules/tinyspy/" | ||
]; | ||
function extractLocation(urlLike) { | ||
if (!urlLike.includes(":")) | ||
return [urlLike]; | ||
const regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/; | ||
const parts = regExp.exec(urlLike.replace(/^\(|\)$/g, "")); | ||
if (!parts) | ||
return [urlLike]; | ||
return [parts[1], parts[2] || void 0, parts[3] || void 0]; | ||
} | ||
function parseSingleStack(raw) { | ||
let line = raw.trim(); | ||
if (line.includes("(eval ")) | ||
line = line.replace(/eval code/g, "eval").replace(/(\(eval at [^()]*)|(,.*$)/g, ""); | ||
let sanitizedLine = line.replace(/^\s+/, "").replace(/\(eval code/g, "(").replace(/^.*?\s+/, ""); | ||
const location = sanitizedLine.match(/ (\(.+\)$)/); | ||
sanitizedLine = location ? sanitizedLine.replace(location[0], "") : sanitizedLine; | ||
const [url, lineNumber, columnNumber] = extractLocation(location ? location[1] : sanitizedLine); | ||
let method = location && sanitizedLine || ""; | ||
let file = url && ["eval", "<anonymous>"].includes(url) ? void 0 : url; | ||
if (!file || !lineNumber || !columnNumber) | ||
return null; | ||
if (method.startsWith("async ")) | ||
method = method.slice(6); | ||
if (file.startsWith("file://")) | ||
file = file.slice(7); | ||
file = resolve(file); | ||
return { | ||
method, | ||
file, | ||
line: Number.parseInt(lineNumber), | ||
column: Number.parseInt(columnNumber) | ||
}; | ||
} | ||
function parseStacktrace(stack, ignore = stackIgnorePatterns) { | ||
const stackFrames = stack.split("\n").map((raw) => { | ||
const stack2 = parseSingleStack(raw); | ||
if (!stack2 || ignore.length && ignore.some((p) => stack2.file.match(p))) | ||
return null; | ||
return stack2; | ||
}).filter(notNullish); | ||
return stackFrames; | ||
} | ||
function parseErrorStacktrace(e, ignore = stackIgnorePatterns) { | ||
if (!e || isPrimitive(e)) | ||
return []; | ||
if (e.stacks) | ||
return e.stacks; | ||
const stackStr = e.stack || e.stackStr || ""; | ||
const stackFrames = parseStacktrace(stackStr, ignore); | ||
e.stacks = stackFrames; | ||
return stackFrames; | ||
} | ||
function positionToOffset(source, lineNumber, columnNumber) { | ||
@@ -274,2 +115,2 @@ const lines = source.split(lineSplitRE); | ||
export { SAFE_TIMERS_SYMBOL, createSimpleStackTrace, getSafeTimers, isPrimitive, lineSplitRE, notNullish, offsetToLineNumber, parseErrorStacktrace, parseSingleStack, parseStacktrace, positionToOffset, setSafeTimers, shuffle }; | ||
export { SAFE_TIMERS_SYMBOL, createSimpleStackTrace, getSafeTimers, lineSplitRE, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle }; |
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js'; | ||
import { ParsedStack, ErrorWithDiff } from './types.js'; | ||
export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, MutableArray, Nullable } from './types.js'; | ||
export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, ErrorWithDiff, MergeInsertions, MutableArray, Nullable, ParsedStack } from './types.js'; | ||
import { PrettyFormatOptions } from 'pretty-format'; | ||
@@ -100,8 +99,5 @@ | ||
declare const lineSplitRE: RegExp; | ||
declare function parseSingleStack(raw: string): ParsedStack | null; | ||
declare function parseStacktrace(stack: string, ignore?: (string | RegExp)[]): ParsedStack[]; | ||
declare function parseErrorStacktrace(e: ErrorWithDiff, ignore?: (string | RegExp)[]): ParsedStack[]; | ||
declare function positionToOffset(source: string, lineNumber: number, columnNumber: number): number; | ||
declare function offsetToLineNumber(source: string, offset: number): number; | ||
export { ErrorWithDiff, ParsedStack, SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, createColors, createSimpleStackTrace, format, getColors, getDefaultColors, getSafeTimers, inspect, lineSplitRE, objDisplay, offsetToLineNumber, parseErrorStacktrace, parseSingleStack, parseStacktrace, positionToOffset, setSafeTimers, setupColors, shuffle, stringify }; | ||
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, createColors, createSimpleStackTrace, format, getColors, getDefaultColors, getSafeTimers, inspect, lineSplitRE, objDisplay, offsetToLineNumber, positionToOffset, setSafeTimers, setupColors, shuffle, stringify }; |
@@ -1,3 +0,2 @@ | ||
import { notNullish, isPrimitive } from './helpers.js'; | ||
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isObject, noop, objectAttr, parseRegexp, slash, toArray } from './helpers.js'; | ||
export { assertTypes, clone, createDefer, deepClone, getCallLastIndex, getOwnProperties, getType, isObject, isPrimitive, noop, notNullish, objectAttr, parseRegexp, slash, toArray } from './helpers.js'; | ||
export { f as format, i as inspect, o as objDisplay, s as stringify } from './chunk-display.js'; | ||
@@ -85,161 +84,3 @@ import { S as SAFE_TIMERS_SYMBOL } from './chunk-colors.js'; | ||
function normalizeWindowsPath(input = "") { | ||
if (!input || !input.includes("\\")) { | ||
return input; | ||
} | ||
return input.replace(/\\/g, "/"); | ||
} | ||
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/; | ||
function cwd() { | ||
if (typeof process !== "undefined") { | ||
return process.cwd().replace(/\\/g, "/"); | ||
} | ||
return "/"; | ||
} | ||
const resolve = function(...arguments_) { | ||
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument)); | ||
let resolvedPath = ""; | ||
let resolvedAbsolute = false; | ||
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) { | ||
const path = index >= 0 ? arguments_[index] : cwd(); | ||
if (!path || path.length === 0) { | ||
continue; | ||
} | ||
resolvedPath = `${path}/${resolvedPath}`; | ||
resolvedAbsolute = isAbsolute(path); | ||
} | ||
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute); | ||
if (resolvedAbsolute && !isAbsolute(resolvedPath)) { | ||
return `/${resolvedPath}`; | ||
} | ||
return resolvedPath.length > 0 ? resolvedPath : "."; | ||
}; | ||
function normalizeString(path, allowAboveRoot) { | ||
let res = ""; | ||
let lastSegmentLength = 0; | ||
let lastSlash = -1; | ||
let dots = 0; | ||
let char = null; | ||
for (let index = 0; index <= path.length; ++index) { | ||
if (index < path.length) { | ||
char = path[index]; | ||
} else if (char === "/") { | ||
break; | ||
} else { | ||
char = "/"; | ||
} | ||
if (char === "/") { | ||
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) { | ||
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") { | ||
if (res.length > 2) { | ||
const lastSlashIndex = res.lastIndexOf("/"); | ||
if (lastSlashIndex === -1) { | ||
res = ""; | ||
lastSegmentLength = 0; | ||
} else { | ||
res = res.slice(0, lastSlashIndex); | ||
lastSegmentLength = res.length - 1 - res.lastIndexOf("/"); | ||
} | ||
lastSlash = index; | ||
dots = 0; | ||
continue; | ||
} else if (res.length > 0) { | ||
res = ""; | ||
lastSegmentLength = 0; | ||
lastSlash = index; | ||
dots = 0; | ||
continue; | ||
} | ||
} | ||
if (allowAboveRoot) { | ||
res += res.length > 0 ? "/.." : ".."; | ||
lastSegmentLength = 2; | ||
} | ||
} else { | ||
if (res.length > 0) { | ||
res += `/${path.slice(lastSlash + 1, index)}`; | ||
} else { | ||
res = path.slice(lastSlash + 1, index); | ||
} | ||
lastSegmentLength = index - lastSlash - 1; | ||
} | ||
lastSlash = index; | ||
dots = 0; | ||
} else if (char === "." && dots !== -1) { | ||
++dots; | ||
} else { | ||
dots = -1; | ||
} | ||
} | ||
return res; | ||
} | ||
const isAbsolute = function(p) { | ||
return _IS_ABSOLUTE_RE.test(p); | ||
}; | ||
const lineSplitRE = /\r?\n/; | ||
const stackIgnorePatterns = [ | ||
"node:internal", | ||
/\/packages\/\w+\/dist\//, | ||
/\/@vitest\/\w+\/dist\//, | ||
"/vitest/dist/", | ||
"/vitest/src/", | ||
"/vite-node/dist/", | ||
"/vite-node/src/", | ||
"/node_modules/chai/", | ||
"/node_modules/tinypool/", | ||
"/node_modules/tinyspy/" | ||
]; | ||
function extractLocation(urlLike) { | ||
if (!urlLike.includes(":")) | ||
return [urlLike]; | ||
const regExp = /(.+?)(?::(\d+))?(?::(\d+))?$/; | ||
const parts = regExp.exec(urlLike.replace(/^\(|\)$/g, "")); | ||
if (!parts) | ||
return [urlLike]; | ||
return [parts[1], parts[2] || void 0, parts[3] || void 0]; | ||
} | ||
function parseSingleStack(raw) { | ||
let line = raw.trim(); | ||
if (line.includes("(eval ")) | ||
line = line.replace(/eval code/g, "eval").replace(/(\(eval at [^()]*)|(,.*$)/g, ""); | ||
let sanitizedLine = line.replace(/^\s+/, "").replace(/\(eval code/g, "(").replace(/^.*?\s+/, ""); | ||
const location = sanitizedLine.match(/ (\(.+\)$)/); | ||
sanitizedLine = location ? sanitizedLine.replace(location[0], "") : sanitizedLine; | ||
const [url, lineNumber, columnNumber] = extractLocation(location ? location[1] : sanitizedLine); | ||
let method = location && sanitizedLine || ""; | ||
let file = url && ["eval", "<anonymous>"].includes(url) ? void 0 : url; | ||
if (!file || !lineNumber || !columnNumber) | ||
return null; | ||
if (method.startsWith("async ")) | ||
method = method.slice(6); | ||
if (file.startsWith("file://")) | ||
file = file.slice(7); | ||
file = resolve(file); | ||
return { | ||
method, | ||
file, | ||
line: Number.parseInt(lineNumber), | ||
column: Number.parseInt(columnNumber) | ||
}; | ||
} | ||
function parseStacktrace(stack, ignore = stackIgnorePatterns) { | ||
const stackFrames = stack.split("\n").map((raw) => { | ||
const stack2 = parseSingleStack(raw); | ||
if (!stack2 || ignore.length && ignore.some((p) => stack2.file.match(p))) | ||
return null; | ||
return stack2; | ||
}).filter(notNullish); | ||
return stackFrames; | ||
} | ||
function parseErrorStacktrace(e, ignore = stackIgnorePatterns) { | ||
if (!e || isPrimitive(e)) | ||
return []; | ||
if (e.stacks) | ||
return e.stacks; | ||
const stackStr = e.stack || e.stackStr || ""; | ||
const stackFrames = parseStacktrace(stackStr, ignore); | ||
e.stacks = stackFrames; | ||
return stackFrames; | ||
} | ||
function positionToOffset(source, lineNumber, columnNumber) { | ||
@@ -274,2 +115,2 @@ const lines = source.split(lineSplitRE); | ||
export { SAFE_TIMERS_SYMBOL, createSimpleStackTrace, getSafeTimers, isPrimitive, lineSplitRE, notNullish, offsetToLineNumber, parseErrorStacktrace, parseSingleStack, parseStacktrace, positionToOffset, setSafeTimers, shuffle }; | ||
export { SAFE_TIMERS_SYMBOL, createSimpleStackTrace, getSafeTimers, lineSplitRE, offsetToLineNumber, positionToOffset, setSafeTimers, shuffle }; |
{ | ||
"name": "@vitest/utils", | ||
"type": "module", | ||
"version": "0.33.0", | ||
"version": "0.34.0", | ||
"description": "Shared Vitest utility functions", | ||
@@ -35,2 +35,6 @@ "license": "MIT", | ||
}, | ||
"./source-map": { | ||
"types": "./dist/source-map.d.ts", | ||
"import": "./dist/source-map.js" | ||
}, | ||
"./*": "./*" | ||
@@ -50,2 +54,5 @@ }, | ||
}, | ||
"devDependencies": { | ||
"@jridgewell/trace-mapping": "^0.3.18" | ||
}, | ||
"scripts": { | ||
@@ -52,0 +59,0 @@ "build": "rimraf dist && rollup -c", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109014
20
3031
1