-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
call-site-like.ts
468 lines (444 loc) · 14.1 KB
/
call-site-like.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import { findSourceMap, SourceMap } from 'module'
import { isAbsolute, relative, resolve } from 'path'
import { fileURLToPath } from 'url'
import {
Compiled,
isCompiledCallSiteLine,
parseCallSiteLine,
} from './parse.js'
const methodRe = /^(.*?) \[as (.*?)\]$/
export interface CallSiteLikeJSON {
fileName?: string
lineNumber?: number
columnNumber?: number
evalOrigin?: CallSiteLikeJSON
typeName?: string
methodName?: string
functionName?: string
isEval?: true
isNative?: true
isToplevel?: true
isConstructor?: true
generated?: {
fileName?: string
lineNumber?: number
columnNumber?: number
}
}
export interface GeneratedResult {
fileName?: string | null
lineNumber?: number | null
columnNumber?: number | null
}
const isCallSite = (c: any): c is NodeJS.CallSite =>
!!c && typeof c === 'object' && c.constructor?.name === 'CallSite'
export class CallSiteLike {
static prepareStackTrace(e: Error, c: NodeJS.CallSite[]) {
return c.map(c => new CallSiteLike(e, c))
}
#fileName?: string | null
#cwd?: string
lineNumber: ReturnType<NodeJS.CallSite['getLineNumber']>
columnNumber: ReturnType<NodeJS.CallSite['getColumnNumber']>
this: ReturnType<NodeJS.CallSite['getThis']>
evalOrigin?: CallSiteLike
function: ReturnType<NodeJS.CallSite['getFunction']>
typeName: ReturnType<NodeJS.CallSite['getTypeName']>
methodName: ReturnType<NodeJS.CallSite['getMethodName']>
functionName: ReturnType<NodeJS.CallSite['getFunctionName']>
isEval: ReturnType<NodeJS.CallSite['isEval']>
isNative: ReturnType<NodeJS.CallSite['isNative']>
isToplevel: ReturnType<NodeJS.CallSite['isToplevel']>
isConstructor: ReturnType<NodeJS.CallSite['isConstructor']>
generated?: GeneratedResult
#sourceMap?: SourceMap
// normalize and relativize filename if cwd is set
get fileName() {
return this.#relativize(this.#fileName)
}
get absoluteFileName() {
if (!this.#fileName) return this.#fileName
else if (this.#fileName.startsWith('file://')) {
return fileURLToPath(this.#fileName)
} else {
return this.#fileName
}
}
get cwd(): string | undefined {
return this.#cwd
}
set cwd(cwd: string | undefined) {
if (cwd === undefined) {
if (this.generated) {
this.generated.fileName = this.#derelativize(
this.generated?.fileName
)
}
}
this.#cwd = cwd?.replace(/\\/g, '/')
if (cwd !== undefined) {
if (this.generated) {
this.generated.fileName = this.#relativize(
this.generated?.fileName
)
}
}
if (this.evalOrigin) this.evalOrigin.cwd = cwd
}
constructor(
e: Error | null,
c: NodeJS.CallSite | string | Compiled
) {
if (typeof c === 'string') {
c = parseCallSiteLine(c)
}
if (isCallSite(c)) {
const fileName = c.getFileName()
this.#fileName = typeof fileName === 'string' ? fileName : null
this.lineNumber = c.getLineNumber()
this.columnNumber = c.getColumnNumber()
this.this = c.getThis()
const evalOrigin = c.getEvalOrigin()
if (evalOrigin) {
this.evalOrigin = new CallSiteLike(e, evalOrigin)
}
this.function = c.getFunction()
this.typeName = c.getTypeName()
this.methodName = c.getMethodName()
this.functionName = c.getFunctionName()
this.isEval = c.isEval()
this.isNative = c.isNative()
this.isToplevel = c.isToplevel()
this.isConstructor = c.isConstructor()
} else if (isCompiledCallSiteLine(c)) {
// compiled object from stack line
this.isEval = !!c.isEval
this.isToplevel = false
if (c.evalOrigin) {
this.evalOrigin = new CallSiteLike(e, c.evalOrigin)
}
this.lineNumber =
c.lineNumber === undefined ? null : c.lineNumber
this.columnNumber =
c.columnNumber === undefined ? null : c.columnNumber
const fileName = c.fileName
this.#fileName = typeof fileName === 'string' ? fileName : null
const { generated } = c
if (generated) {
this.generated = generated
this.generated.fileName = this.#relativize(generated.fileName)
}
let fname = c.fname?.trim()
let method: null | string = null
this.isNative = !!c.isNative
if (fname) {
if (fname.startsWith('new ')) {
this.isConstructor = true
fname = fname.substring('new '.length).trim()
} else {
this.isConstructor = false
}
this.methodName = null
const methodMatch = fname.match(methodRe) as [
string,
string,
string
]
if (methodMatch) {
fname = methodMatch[1]
method = methodMatch[2]
}
const dots = fname.split('.')
const m = dots.pop()
if (m !== undefined) {
this.typeName = dots.join('.').trim() || null
this.methodName = method || m
if (this.methodName.match(/^get |set /)) {
this.methodName = this.methodName.substring(4)
}
this.functionName = m
// we know it's not undefined, but TS is afraid of pop()
/* c8 ignore start */
} else {
this.functionName = null
this.typeName = null
}
/* c8 ignore stop */
} else {
this.isConstructor = false
this.typeName = null
this.functionName = null
this.methodName = null
}
} else {
throw new Error('invalid call site information provided')
}
// This is a slight deviation from the CallSite API, but it's very useful
// to have a field that's the actual function with type and method name.
if (
this.typeName &&
this.functionName &&
!this.functionName.startsWith(this.typeName)
) {
this.functionName = `${this.typeName}.${this.functionName}`
} else if (this.functionName === this.methodName) {
this.methodName = null
}
// We only do the sourcemap lookup if we're parsing from a CallSite
// If we get it from an Error stack line, then Node has already done
// the mapping for us, and when ts-node (and other in-place
// transpilers) create JavaScript, they use the same filename, leading
// to an incorrect double offset,.
if (
this.#fileName &&
isCallSite(c) &&
!this.#sourceMap &&
!this.#fileName.startsWith('node:')
) {
// Passing an object that isn't an actual Error object to
// findSourceMap causes problems in node 16
/* c8 ignore start */
const sme = e && e instanceof Error ? e : undefined
/* c8 ignore stop */
this.#sourceMap = findSourceMap(this.#fileName, sme)
if (this.#sourceMap && typeof this.lineNumber === 'number') {
// SourceMap.findEntry doesn't actually return the line/column
// number, despite the property names, but rather the zero-indexed
// line/column start of a mapping range, and must be looked up
// using the zero-indexed line and column.
// To find the mapping, we look it up with the zero-indexed
// line/col, then figure out how far our line/col is from the
// mapping, and apply that same offset to the start of the origin
// in the mapping.
const payload = this.#sourceMap.findEntry(
// safety bounds around numbers here, impossible to hit
// if we're looking up actual call sites though.
/* c8 ignore start */
Math.max(0, this.lineNumber - 1),
Math.max(0, (this.columnNumber || 0) - 1)
/* c8 ignore stop */
)
if (payload) {
const offset: [number, number] = [
this.lineNumber - payload.generatedLine,
/* c8 ignore start */
(this.columnNumber || 1) - payload.generatedColumn,
/* c8 ignore stop */
]
const originalLine = payload.originalLine + offset[0]
const originalColumn = payload.originalColumn + offset[1]
const genFilename = this.#relativize(this.#fileName)
this.generated = {
/* c8 ignore start */
fileName: this.#relativize(genFilename || null),
/* c8 ignore stop */
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
}
this.#fileName = payload.originalSource
this.lineNumber = originalLine
this.columnNumber = originalColumn
}
}
}
}
#relativize(fileName?: string | null) {
let f = fileName
if (!f) return f
if (f.startsWith('node:')) return f
if (f.startsWith('file://')) f = fileURLToPath(f)
if (this.#cwd === undefined) return f
else f = f.replace(/\\/g, '/')
try {
const rel = relative(this.#cwd, f)
return rel.length < f.length ? rel : f
} catch {
return f
}
}
#derelativize(fileName?: string | null) {
let f = fileName
if (!f) return f
if (f.startsWith('node:')) return f
if (f.startsWith('file://')) f = fileURLToPath(f)
if (this.#cwd === undefined) return f
try {
return resolve(this.#cwd, f)
} catch {
return f
}
}
toString(jsStyle = false): string {
// in js style mode, use the origin source file if it is within
// our cwd. Otherwise, use the generated source location.
const useGen =
jsStyle &&
this.generated?.fileName &&
this.fileName &&
this.generated.fileName !== this.fileName &&
(isAbsolute(this.fileName) || this.fileName.startsWith('..'))
const { fileName, lineNumber, columnNumber, generated } =
useGen && this.generated
? {
fileName: this.#derelativize(this.generated.fileName),
lineNumber: this.generated.lineNumber,
columnNumber: this.generated.columnNumber,
generated: undefined,
}
: jsStyle
? {
fileName: this.#derelativize(this.fileName),
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
generated: undefined,
}
: this
const loc = { fileName, lineNumber, columnNumber, generated }
for (const l of [loc, loc.generated]) {
if (l?.fileName) {
l.fileName = jsStyle
? this.#derelativize(l.fileName)
: this.#relativize(l.fileName)
}
}
let fname = this.functionName || ''
let tn = ''
let tnGet = ''
let tnSet = ''
if (this.typeName) {
const mn = this.methodName || '<anonymous>'
tn = this.typeName + '.' + mn
tnGet = this.typeName + '.get ' + mn
tnSet = this.typeName + '.set ' + mn
}
if (!fname && tn) {
fname = tn
}
const mn = this.methodName
if (
fname &&
tn &&
mn &&
fname !== tn &&
fname !== tnGet &&
fname !== tnSet
) {
fname += ` [as ${mn}]`
}
if (this.isConstructor && fname) {
fname = `new ${fname}`
}
let ev = ''
const nat = this.isNative ? 'native' : ''
let file = loc.fileName || ''
const hasLC = loc.lineNumber && loc.columnNumber
if (this.evalOrigin) {
ev = `eval at ${this.evalOrigin.toString(jsStyle)}`
if (hasLC) {
const f = loc.fileName || '<anonymous>'
let lr = `${f}:${loc.lineNumber}:${loc.columnNumber}`
if (loc.generated && loc.generated.fileName) {
const f = loc.generated.fileName
const { lineNumber: l, columnNumber: c } = loc.generated
lr = `${f}:${l}:${c} (${lr})`
}
ev += `, ${lr}`
}
// should always have an fname at this point
/* c8 ignore start */
ev = fname ? ` (${ev})` : ev
/* c8 ignore stop */
return `${fname}${ev}`
}
if (file || hasLC) {
if (hasLC) {
/* c8 ignore start */
file = file || '<anonymous>'
/* c8 ignore stop */
file += `:${loc.lineNumber}:${loc.columnNumber}`
}
} else if (nat) {
file = 'native'
// impossible in normal cases
/* c8 ignore start */
} else {
file = ''
}
/* c8 ignore stop */
let g = ''
if (loc.generated && loc.generated.fileName) {
const { fileName, lineNumber, columnNumber } = loc.generated
g = fileName
/* c8 ignore start */
if (!g) g = ''
/* c8 ignore stop */
if (g === loc.fileName) g = ''
if (g) {
if (
typeof lineNumber === 'number' &&
typeof columnNumber === 'number'
) {
g += `:${lineNumber}:${columnNumber}`
}
if (ev || fname) {
g = ` (${g})`
}
}
}
if (file && (ev || fname || g)) {
file = ` (${file})`
}
const pre = jsStyle ? ' at ' : ''
return `${pre}${fname}${ev}${g}${file}`
}
toJSON(): CallSiteLikeJSON {
const {
fileName,
lineNumber,
columnNumber,
evalOrigin,
typeName,
methodName,
functionName,
isEval,
isNative,
isToplevel,
isConstructor,
generated,
} = this
const json: CallSiteLikeJSON = {}
if (fileName !== null) json.fileName = fileName
if (lineNumber || lineNumber === 0) json.lineNumber = lineNumber
if (columnNumber || columnNumber === 0)
json.columnNumber = columnNumber
if (evalOrigin) json.evalOrigin = evalOrigin.toJSON()
if (typeName !== null) {
json.typeName = typeName
}
if (methodName !== null) json.methodName = methodName
if (functionName !== null) json.functionName = functionName
if (isEval) json.isEval = isEval
if (isNative) json.isNative = isNative
if (isToplevel) json.isToplevel = isToplevel
if (isConstructor) json.isConstructor = isConstructor
if (generated && generated.fileName) {
const f = this.#relativize(generated.fileName)
if (
f &&
typeof f === 'string' &&
(f !== json.fileName || f === '<anonymous>')
) {
const gen: Record<string, string | number> = {}
gen.fileName = f
if (generated.lineNumber)
gen.lineNumber = generated.lineNumber
if (generated.columnNumber)
gen.columnNumber = generated.columnNumber
if (Object.keys(gen).length > 0) {
json.generated = gen
}
}
}
return json
}
}