MCPcopy
hub / github.com/vitest-dev/vitest / parseSingleV8Stack

Function parseSingleV8Stack

packages/utils/src/source-map.ts:147–214  ·  view source on GitHub ↗
(raw: string)

Source from the content-addressed store, hash-verified

145// Based on https://github.com/stacktracejs/error-stack-parser
146// Credit to stacktracejs
147export function parseSingleV8Stack(raw: string): ParsedStack | null {
148 let line = raw.trim()
149
150 if (!CHROME_IE_STACK_REGEXP.test(line)) {
151 return null
152 }
153
154 if (line.includes('(eval ')) {
155 line = line
156 .replace(/eval code/g, 'eval')
157 .replace(/(\(eval at [^()]*)|(,.*$)/g, '')
158 }
159
160 let sanitizedLine = line
161 .replace(/^\s+/, '')
162 .replace(/\(eval code/g, '(')
163 .replace(/^.*?\s+/, '')
164
165 // capture and preserve the parenthesized location "(/foo/my bar.js:12:87)" in
166 // case it has spaces in it, as the string is split on \s+ later on
167 const location = sanitizedLine.match(/ (\(.+\)$)/)
168
169 // remove the parenthesized location from the line, if it was matched
170 sanitizedLine = location
171 ? sanitizedLine.replace(location[0], '')
172 : sanitizedLine
173
174 // if a location was matched, pass it to extractLocation() otherwise pass all sanitizedLine
175 // because this line doesn't have function name
176 const [url, lineNumber, columnNumber] = extractLocation(
177 location ? location[1] : sanitizedLine,
178 )
179 let method = (location && sanitizedLine) || ''
180 let file = url && ['eval', '<anonymous>'].includes(url) ? undefined : url
181
182 if (!file || !lineNumber || !columnNumber) {
183 return null
184 }
185
186 if (method.startsWith('async ')) {
187 method = method.slice(6)
188 }
189
190 if (file.startsWith('file://')) {
191 file = file.slice(7)
192 }
193
194 // normalize Windows path (\ -> /)
195 file = file.startsWith('node:') || file.startsWith('internal:')
196 ? file
197 : resolve(file)
198
199 if (method) {
200 method = method
201 // vite 7+
202 .replace(/\(0\s?,\s?__vite_ssr_import_\d+__.(\w+)\)/g, '$1')
203 // vite <7
204 .replace(/__(vite_ssr_import|vi_import)_\d+__\./g, '')

Callers 2

parseSingleStackFunction · 0.85
parseV8StacktraceFunction · 0.85

Calls 4

extractLocationFunction · 0.85
resolveFunction · 0.50
testMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected