Skip to content

Commit

Permalink
chore: replace source-map with trace-mapping (#3236)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 24, 2023
1 parent f3ba401 commit be0844c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 199 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Expand Up @@ -152,7 +152,6 @@
"magic-string": "^0.30.0",
"pathe": "^1.1.0",
"picocolors": "^1.0.0",
"source-map": "^0.6.1",
"std-env": "^3.3.2",
"strip-literal": "^1.0.1",
"tinybench": "^2.4.0",
Expand All @@ -165,6 +164,7 @@
"@ampproject/remapping": "^2.2.0",
"@antfu/install-pkg": "^0.1.1",
"@edge-runtime/vm": "2.1.2",
"@jridgewell/trace-mapping": "^0.3.17",
"@sinonjs/fake-timers": "^10.0.2",
"@types/diff": "^5.0.3",
"@types/istanbul-lib-coverage": "^2.0.4",
Expand Down
17 changes: 10 additions & 7 deletions packages/vitest/src/typecheck/typechecker.ts
Expand Up @@ -2,7 +2,8 @@ import { rm } from 'node:fs/promises'
import type { ExecaChildProcess } from 'execa'
import { execa } from 'execa'
import { basename, extname, resolve } from 'pathe'
import { SourceMapConsumer } from 'source-map'
import { TraceMap, generatedPositionFor } from '@jridgewell/trace-mapping'
import type { RawSourceMap } from '@ampproject/remapping'
import { getTasks } from '../utils'
import { ensurePackageInstalled } from '../node/pkg'
import type { Awaitable, File, ParsedStack, Task, TaskResultPack, TaskState, TscErrorInfo } from '../types'
Expand Down Expand Up @@ -119,7 +120,7 @@ export class Typechecker {
}
const sortedDefinitions = [...definitions.sort((a, b) => b.start - a.start)]
// has no map for ".js" files that use // @ts-check
const mapConsumer = map && new SourceMapConsumer(map)
const traceMap = map && new TraceMap(map as unknown as RawSourceMap)
const indexMap = createIndexMap(parsed)
const markState = (task: Task, state: TaskState) => {
task.result = {
Expand All @@ -129,11 +130,13 @@ export class Typechecker {
markState(task.suite, state)
}
errors.forEach(({ error, originalError }) => {
const processedPos = mapConsumer?.generatedPositionFor({
line: originalError.line,
column: originalError.column,
source: basename(path),
}) || originalError
const processedPos = traceMap
? generatedPositionFor(traceMap, {
line: originalError.line,
column: originalError.column,
source: basename(path),
})
: originalError
const line = processedPos.line ?? originalError.line
const column = processedPos.column ?? originalError.column
const index = indexMap.get(`${line}:${column}`)
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/types/rpc.ts
@@ -1,5 +1,4 @@
import type { RawSourceMap } from 'source-map'
import type { FetchResult, ViteNodeResolveId } from 'vite-node'
import type { FetchResult, RawSourceMap, ViteNodeResolveId } from 'vite-node'
import type { EnvironmentOptions, ResolvedConfig, VitestEnvironment } from './config'
import type { UserConsoleLog } from './general'
import type { SnapshotResult } from './snapshot'
Expand Down

0 comments on commit be0844c

Please sign in to comment.