Repository: vitejs/vite
File: packages/vite/src/node/ssr/ssrStacktrace.ts (TypeScript)

Code before:
  moduleGraph: EnvironmentModuleGraph,
): string {
  calculateOffsetOnce()
  return stack
    .split('\n')
    .map((line) => {
      return line.replace(
        /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
        (input, varName, id, line, column) => {
          if (!id) return input

          const mod = moduleGraph.getModuleById(id)
          const rawSourceMap = mod?.transformResult?.map

          if (!rawSourceMap) {
            return input
          }

          const traced = new TraceMap(rawSourceMap as any)

          const pos = originalPositionFor(traced, {
            line: Number(line) - offset,
            // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
            column: Number(column) - 1,
          })

          if (!pos.source) {
            return input
          }

          const trimmedVarName = varName?.trim()
          const sourceFile = path.resolve(path.dirname(id), pos.source)
          // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
          const source = `${sourceFile}:${pos.line}:${pos.column + 1}`
          if (!trimmedVarName || trimmedVarName === 'eval') {
            return `    at ${source}`
          } else {
            return `    at ${trimmedVarName} (${source})`
          }
        },
      )
    })
    .join('\n')
}

export function rebindErrorStacktrace(e: Error, stacktrace: string): void {
  const { configurable, writable } = Object.getOwnPropertyDescriptor(
    e,
    'stack',
  )!
  if (configurable) {

Review comment (suggestion):
Maybe `alreadyRewritten = line` while showing the problematic line in the warning. (Also maybe one sentence explaining what `line` is expected to be instead.)

Just an idea, the warning alone is already quite a neat improvement. This PR LGTM as-is.

Code after:
  moduleGraph: EnvironmentModuleGraph,
): { result: string; alreadyRewritten: boolean } {
  calculateOffsetOnce()

  let alreadyRewritten = false
  const rewritten = stack
    .split('\n')
    .map((line) => {
      return line.replace(
        /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
        (input, varName, id, originalLine, originalColumn) => {
          if (!id) return input

          const mod = moduleGraph.getModuleById(id)
          const rawSourceMap = mod?.transformResult?.map

          if (!rawSourceMap) {
            return input
          }

          const traced = new TraceMap(rawSourceMap as any)
          const line = Number(originalLine) - offset
          // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
          const column = Number(originalColumn) - 1
          if (line <= 0 || column < 0) {
            alreadyRewritten = true
            return input
          }

          const pos = originalPositionFor(traced, { line, column })
          if (!pos.source) {
            return input
          }

          const trimmedVarName = varName?.trim()
          const sourceFile = path.resolve(path.dirname(id), pos.source)
          // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
          const source = `${sourceFile}:${pos.line}:${pos.column + 1}`
          if (!trimmedVarName || trimmedVarName === 'eval') {
            return `    at ${source}`
          } else {
            return `    at ${trimmedVarName} (${source})`
          }
        },
      )
    })
    .join('\n')
  return { result: rewritten, alreadyRewritten }
}

export function rebindErrorStacktrace(e: Error, stacktrace: string): void {