Skip to content

Commit

Permalink
fix: normalize file name path to contain unix slashes only
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 12, 2024
1 parent 8966e6c commit 352a17a
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { parse, StackFrame as ESFrame } from 'error-stack-parser-es'
import debug from './debug.js'
import { SourceFile } from './source_file.js'
import type { Chunk, ParsedError, Parser, SourceLoader, StackFrame, Transformer } from './types.js'
import { fileURLToPath } from 'node:url'

/**
* ErrorParser exposes the API to parse an thrown value and extract
Expand Down Expand Up @@ -137,6 +138,26 @@ export class ErrorParser {
return frames
}

/**
* Replaces windows slash to unix slash
*/
#toUnixSlash(fileName: string) {
const isExtendedLengthPath = fileName.startsWith('\\\\?\\')
return isExtendedLengthPath ? fileName : fileName.replace(/\\/g, '/')
}

/**
* Normalizes the filename to be a path with unix slash. The
* URL style paths are also converted to normalized file
* paths
*/
#normalizeFileName(fileName: string) {
if (fileName.startsWith('file:')) {
return this.#toUnixSlash(fileURLToPath(fileName))
}
return this.#toUnixSlash(fileName)
}

/**
* Returns the type of the frame.
*/
Expand Down Expand Up @@ -164,16 +185,16 @@ export class ErrorParser {
*/
async #enhanceFrames(frames: ESFrame[]): Promise<StackFrame[]> {
let stackFrames: StackFrame[] = []
for (let frame of frames) {
const { source: raw, ...rest } = frame
for (const { source: raw, ...frame } of frames) {
if (!frame.fileName) {
stackFrames.push({
...rest,
...frame,
raw,
})
continue
}

frame.fileName = this.#normalizeFileName(frame.fileName)
const type = this.#getFrameType(frame.fileName)
const fileType = this.#getFrameSourceType(frame.fileName)
const source =
Expand All @@ -182,7 +203,7 @@ export class ErrorParser {
: undefined

const stackFrame = {
...rest,
...frame,
source,
raw,
type,
Expand Down Expand Up @@ -300,7 +321,7 @@ export class ErrorParser {
raw: error,
} satisfies ParsedError

for (let transformer of this.#transformers) {
for (const transformer of this.#transformers) {
await transformer(parsedError, error)
}

Expand Down

0 comments on commit 352a17a

Please sign in to comment.