Skip to content

Commit

Permalink
feat: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 12, 2023
1 parent abd6d73 commit fcdb5af
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 133 deletions.
1 change: 0 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export default defineBuildConfig({
clean: true,
rollup: {
emitCJS: true,
inlineDependencies: true,
},
})
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,11 @@
"pnpm": "^8.6.7",
"rimraf": "^5.0.1",
"simple-git-hooks": "^2.8.1",
"stackframe": "^1.3.4",
"typescript": "^5.1.6",
"unbuild": "^1.2.1",
"vite": "^4.4.3",
"vitest": "^0.33.0"
},
"pnpm": {
"patchedDependencies": {
"stackframe@1.3.4": "patches/stackframe@1.3.4.patch"
}
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
},
Expand Down
15 changes: 0 additions & 15 deletions patches/stackframe@1.3.4.patch

This file was deleted.

40 changes: 6 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 18 additions & 77 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,17 @@
* Port from https://github.com/stacktracejs/error-stack-parser-es
*/

// @ts-expect-error patched to remove the broken type
import _StackFrameConstructor from 'stackframe'

const StackFrameConstructor = _StackFrameConstructor as unknown as StackFrame

export interface StackFrameOptions {
isConstructor?: boolean
isEval?: boolean
isNative?: boolean
isToplevel?: boolean
columnNumber?: number
lineNumber?: number
fileName?: string
functionName?: string
source?: string
args?: any[]
evalOrigin?: StackFrame
}

export interface StackFrame {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (obj: StackFrameOptions): StackFrame

args?: any[]
getArgs(): any[] | undefined
setArgs(args: any[]): void

evalOrigin?: StackFrame
getEvalOrigin(): StackFrame | undefined
setEvalOrigin(stackframe: StackFrame): void

isConstructor?: boolean
getIsConstructor(): boolean | undefined
setIsConstructor(isConstructor: boolean): void

isEval?: boolean
getIsEval(): boolean | undefined
setIsEval(isEval: boolean): void

isNative?: boolean
getIsNative(): boolean | undefined
setIsNative(isNative: boolean): void

isToplevel?: boolean
getIsToplevel(): boolean | undefined
setIsToplevel(isToplevel: boolean): void

columnNumber?: number
getColumnNumber(): number | undefined
setColumnNumber(columnNumber: number): void

lineNumber?: number
getLineNumber(): number | undefined
setLineNumber(lineNumber: number): void

fileName?: string
getFileName(): string | undefined
setFileName(fileName: string): void

functionName?: string
getFunctionName(): string | undefined
setFunctionName(functionName: string): void

source?: string
getSource(): string | undefined
setSource(source: string): void

toString(): string
}

const FIREFOX_SAFARI_STACK_REGEXP = /(^|@)\S+:\d+/
Expand Down Expand Up @@ -114,7 +57,7 @@ export function parseV8OrIE(error: Error) {
return !!line.match(CHROME_IE_STACK_REGEXP)
})

return filtered.map((line) => {
return filtered.map((line): StackFrame => {
if (line.includes('(eval ')) {
// Throw away eval information until we implement stacktrace.js/stackframe#8
line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^()]*)|(,.*$)/g, '')
Expand All @@ -134,13 +77,13 @@ export function parseV8OrIE(error: Error) {
const functionName = (location && sanitizedLine) || undefined
const fileName = ['eval', '<anonymous>'].includes(locationParts[0]) ? undefined : locationParts[0]

return new StackFrameConstructor({
return {
functionName,
fileName,
lineNumber: locationParts[1] ? +locationParts[1] : undefined,
columnNumber: locationParts[2] ? +locationParts[2] : undefined,
source: line,
})
}
})
}

Expand All @@ -150,30 +93,30 @@ export function parseFFOrSafari(error: Error) {
return !line.match(SAFARI_NATIVE_CODE_REGEXP)
})

return filtered.map((line) => {
return filtered.map((line): StackFrame => {
// Throw away eval information until we implement stacktrace.js/stackframe#8
if (line.includes(' > eval'))
line = line.replace(/ line (\d+)(?: > eval line \d+)* > eval:\d+:\d+/g, ':$1')

if (!line.includes('@') && !line.includes(':')) {
// Safari eval frames only have function names and nothing else
return new StackFrameConstructor({
return {
functionName: line,
})
}
}
else {
const functionNameRegex = /((.*".+"[^@]*)?[^@]*)(?:@)/
const matches = line.match(functionNameRegex)
const functionName = (matches && matches[1]) ? matches[1] : undefined
const locationParts = extractLocation(line.replace(functionNameRegex, ''))

return new StackFrameConstructor({
return {
functionName,
fileName: locationParts[0],
lineNumber: locationParts[1] ? +locationParts[1] : undefined,
columnNumber: locationParts[2] ? +locationParts[2] : undefined,
source: line,
})
}
}
})
}
Expand All @@ -198,11 +141,11 @@ export function parseOpera9(e: Error) {
for (let i = 2, len = lines.length; i < len; i += 2) {
const match = lineRE.exec(lines[i])
if (match) {
result.push(new StackFrameConstructor({
result.push({
fileName: match[2],
lineNumber: +match[1],
source: lines[i],
}))
})
}
}

Expand All @@ -218,14 +161,12 @@ export function parseOpera10(e: Error) {
for (let i = 0, len = lines.length; i < len; i += 2) {
const match = lineRE.exec(lines[i])
if (match) {
result.push(
new StackFrameConstructor({
functionName: match[3] || undefined,
fileName: match[2],
lineNumber: match[1] ? +match[1] : undefined,
source: lines[i],
}),
)
result.push({
functionName: match[3] || undefined,
fileName: match[2],
lineNumber: match[1] ? +match[1] : undefined,
source: lines[i],
})
}
}

Expand Down Expand Up @@ -254,13 +195,13 @@ export function parseOpera11(error: Error) {
? undefined
: argsRaw.split(',')

return new StackFrameConstructor({
return {
functionName,
args,
fileName: locationParts[0],
lineNumber: locationParts[1] ? +locationParts[1] : undefined,
columnNumber: locationParts[2] ? +locationParts[2] : undefined,
source: line,
})
}
})
}

0 comments on commit fcdb5af

Please sign in to comment.