11/* eslint-disable prefer-template */
22import { existsSync , readFileSync } from 'fs'
3- import { join , normalize , relative } from 'pathe'
3+ import { normalize , relative } from 'pathe'
44import c from 'picocolors'
55import cliTruncate from 'cli-truncate'
6- import type { ErrorWithDiff , ParsedStack , Position } from '../types'
7- import { lineSplitRE , parseStacktrace , posToNumber } from '../utils/source-map'
6+ import type { ErrorWithDiff , ParsedStack } from '../types'
7+ import { lineSplitRE , parseStacktrace , positionToOffset } from '../utils/source-map'
88import { F_POINTER } from '../utils/figures'
99import { stringify } from '../integrations/chai/jest-matcher-utils'
1010import { TypeCheckError } from '../typecheck/typechecker'
@@ -13,12 +13,6 @@ import type { Vitest } from './core'
1313import { divider } from './reporters/renderers/utils'
1414import type { Logger } from './logger'
1515
16- export function fileFromParsedStack ( stack : ParsedStack ) {
17- if ( stack ?. sourcePos ?. source ?. startsWith ( '..' ) )
18- return join ( stack . file , '../' , stack . sourcePos . source )
19- return stack . file
20- }
21-
2216interface PrintErrorOptions {
2317 type ?: string
2418 fullStack ?: boolean
@@ -64,15 +58,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
6458 ctx . logger . error ( c . yellow ( e . frame ) )
6559 }
6660 else {
67- printStack ( ctx , stacks , nearest , errorProperties , ( s , pos ) => {
61+ printStack ( ctx , stacks , nearest , errorProperties , ( s ) => {
6862 if ( showCodeFrame && s === nearest && nearest ) {
69- const file = fileFromParsedStack ( nearest )
70- // could point to non-existing original file
71- // for example, when there is a source map file, but no source in node_modules
72- if ( nearest . file === file || existsSync ( file ) ) {
73- const sourceCode = readFileSync ( file , 'utf-8' )
74- ctx . logger . error ( c . yellow ( generateCodeFrame ( sourceCode , 4 , pos ) ) )
75- }
63+ const sourceCode = readFileSync ( nearest . file , 'utf-8' )
64+ ctx . logger . error ( c . yellow ( generateCodeFrame ( sourceCode , 4 , s . line , s . column ) ) )
7665 }
7766 } )
7867 }
@@ -181,21 +170,19 @@ function printStack(
181170 stack : ParsedStack [ ] ,
182171 highlight : ParsedStack | undefined ,
183172 errorProperties : Record < string , unknown > ,
184- onStack ?: ( ( stack : ParsedStack , pos : Position ) => void ) ,
173+ onStack ?: ( ( stack : ParsedStack ) => void ) ,
185174) {
186175 if ( ! stack . length )
187176 return
188177
189178 const logger = ctx . logger
190179
191180 for ( const frame of stack ) {
192- const pos = frame . sourcePos || frame
193181 const color = frame === highlight ? c . yellow : c . gray
194- const file = fileFromParsedStack ( frame )
195- const path = relative ( ctx . config . root , file )
182+ const path = relative ( ctx . config . root , frame . file )
196183
197- logger . error ( color ( ` ${ c . dim ( F_POINTER ) } ${ [ frame . method , c . dim ( `${ path } :${ pos . line } :${ pos . column } ` ) ] . filter ( Boolean ) . join ( ' ' ) } ` ) )
198- onStack ?.( frame , pos )
184+ logger . error ( color ( ` ${ c . dim ( F_POINTER ) } ${ [ frame . method , c . dim ( `${ path } :${ frame . line } :${ frame . column } ` ) ] . filter ( Boolean ) . join ( ' ' ) } ` ) )
185+ onStack ?.( frame )
199186
200187 // reached at test file, skip the follow stack
201188 if ( frame . file in ctx . state . filesMap )
@@ -213,12 +200,12 @@ function printStack(
213200export function generateCodeFrame (
214201 source : string ,
215202 indent = 0 ,
216- start : number | Position = 0 ,
217- end ? : number ,
203+ lineNumber : number ,
204+ columnNumber : number ,
218205 range = 2 ,
219206) : string {
220- start = posToNumber ( source , start )
221- end = end || start
207+ const start = positionToOffset ( source , lineNumber , columnNumber )
208+ const end = start
222209 const lines = source . split ( lineSplitRE )
223210 let count = 0
224211 let res : string [ ] = [ ]
0 commit comments