/
source-map.ts
147 lines (118 loc) · 3.88 KB
/
source-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { resolve } from 'pathe'
import type { ErrorWithDiff, ParsedStack } from './types'
import { isPrimitive, notNullish } from './helpers'
export 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: string) {
// Fail-fast but return locations like "(native)"
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] || undefined, parts[3] || undefined]
}
// Based on https://github.com/stacktracejs/error-stack-parser
// Credit to stacktracejs
export function parseSingleStack(raw: string): ParsedStack | null {
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+/, '')
// capture and preserve the parenthesized location "(/foo/my bar.js:12:87)" in
// case it has spaces in it, as the string is split on \s+ later on
const location = sanitizedLine.match(/ (\(.+\)$)/)
// remove the parenthesized location from the line, if it was matched
sanitizedLine = location ? sanitizedLine.replace(location[0], '') : sanitizedLine
// if a location was matched, pass it to extractLocation() otherwise pass all sanitizedLine
// because this line doesn't have function name
const [url, lineNumber, columnNumber] = extractLocation(location ? location[1] : sanitizedLine)
let method = (location && sanitizedLine) || ''
let file = (url && ['eval', '<anonymous>'].includes(url)) ? undefined : url
if (!file || !lineNumber || !columnNumber)
return null
if (method.startsWith('async '))
method = method.slice(6)
if (file.startsWith('file://'))
file = file.slice(7)
// normalize Windows path (\ -> /)
file = resolve(file)
return {
method,
file,
line: parseInt(lineNumber),
column: parseInt(columnNumber),
}
}
export function parseStacktrace(stack: string, ignore = stackIgnorePatterns): ParsedStack[] {
const stackFrames = stack
.split('\n')
.map((raw): ParsedStack | null => {
const stack = parseSingleStack(raw)
if (!stack || (ignore.length && ignore.some(p => stack.file.match(p))))
return null
return stack
})
.filter(notNullish)
return stackFrames
}
export function parseErrorStacktrace(e: ErrorWithDiff, ignore = stackIgnorePatterns): ParsedStack[] {
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
}
export function positionToOffset(
source: string,
lineNumber: number,
columnNumber: number,
): number {
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let start = 0
if (lineNumber > lines.length)
return source.length
for (let i = 0; i < lineNumber - 1; i++)
start += lines[i].length + nl
return start + columnNumber
}
export function offsetToLineNumber(
source: string,
offset: number,
): number {
if (offset > source.length) {
throw new Error(
`offset is longer than source length! offset ${offset} > length ${source.length}`,
)
}
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let counted = 0
let line = 0
for (; line < lines.length; line++) {
const lineLength = lines[line].length + nl
if (counted + lineLength >= offset)
break
counted += lineLength
}
return line + 1
}