import { EncodedSourceMap, TraceMap } from "@jridgewell/trace-mapping"; import { CoverageMap, FileCoverage, FileCoverageData, Range } from "@vitest/istanbul-lib-coverage"; //#region src/map-store.d.ts /** file coverage data optionally carrying an inline input source map */ interface FileCoverageDataWithSourceMap extends FileCoverageData { inputSourceMap?: EncodedSourceMap; } /** minimal storage contract used to keep original source text */ interface SourceStore { get(filePath: string): string | undefined; set(filePath: string, sourceText: string): unknown; clear(): void; } interface MapStoreOptions { /** verbose mode */ verbose?: boolean; /** alternate base directory to resolve sourcemap files */ baseDir?: string | null; /** * class to use for SourceStore. Must support `get`, `set` and `clear` * methods. */ SourceStore?: new (...args: any[]) => SourceStore; /** arguments to use in the SourceStore constructor */ sourceStoreOpts?: unknown[]; } /** a registered source map: a file path, encoded data or a source map object */ type SourceMapEntry = { type: "file"; data: string; } | { type: "encoded"; data: string; } | { type: "object"; data: EncodedSourceMap; }; /** * Tracks source maps for registered files */ declare class MapStore { baseDir: string | null; verbose: boolean; sourceStore: SourceStore; data: Record; /** * @param {Object} opts [opts=undefined] options. * @param {Boolean} opts.verbose [opts.verbose=false] verbose mode * @param {String} opts.baseDir [opts.baseDir=null] alternate base directory * to resolve sourcemap files * @param {Class} opts.SourceStore [opts.SourceStore=Map] class to use for * SourceStore. Must support `get`, `set` and `clear` methods. * @param {Array} opts.sourceStoreOpts [opts.sourceStoreOpts=[]] arguments * to use in the SourceStore constructor. * @constructor */ constructor(opts?: MapStoreOptions); /** * Registers a source map URL with this store. It makes some input sanity checks * and silently fails on malformed input. * @param transformedFilePath - the file path for which the source map is valid. * This must *exactly* match the path stashed for the coverage object to be * useful. * @param sourceMapUrl - the source map URL, **not** a comment */ registerURL(transformedFilePath: string, sourceMapUrl: string): void; /** * Registers a source map object with this store. Makes some basic sanity checks * and silently fails on malformed input. * @param transformedFilePath - the file path for which the source map is valid * @param sourceMap - the source map object */ registerMap(transformedFilePath: string, sourceMap: EncodedSourceMap | null | undefined): void; /** * Retrieve a source map object from this store. * @param filePath - the file path for which the source map is valid * @returns a parsed source map object */ getSourceMapSync(filePath: string): EncodedSourceMap | undefined; /** * Add inputSourceMap property to coverage data * @param coverageData - the __coverage__ object */ addInputSourceMapsSync(coverageData: Record): void; sourceFinder(filePath: string): string; /** * Transforms the coverage map provided into one that refers to original * sources when valid mappings have been registered with this store. * @param coverageMap - the coverage map to transform * @returns the transformed coverage map */ transformCoverage(coverageMap: CoverageMap): Promise; /** * Disposes temporary resources allocated by this map store */ dispose(): void; } //#endregion //#region src/get-mapping.d.ts /** the result of mapping a generated range back to an original source */ interface Mapping { source: string; loc: Range; } //#endregion //#region src/mapped.d.ts declare const FileCoverageCls: typeof FileCoverage; /** bookkeeping metadata used to resolve duplicate mappings */ interface MappedCoverageMeta { last: { s: number; f: number; b: number; }; seen: Record; } declare class MappedCoverage extends FileCoverageCls { meta: MappedCoverageMeta; constructor(pathOrObj: string | FileCoverage | FileCoverageData); addStatement(loc: Range, hits: number): number; addFunction(name: string | undefined, decl: Range, loc: Range, hits: number): number; addBranch(type: string, loc: Range, branchLocations: Range[], hits: number[]): number; cloneLocation(loc: Range): Range; } //#endregion //#region src/transformer.d.ts /** * a function that resolves the source map for a file, or a falsy value when * no source map applies to the file */ type SourceMapFinder = (filePath: string, fileCoverage: FileCoverage) => TraceMap | null | undefined | Promise; /** a function that maps a generated range back to an original source */ type GetMapping = (sourceMap: TraceMap, generatedLocation: Range, origFile: string) => Mapping | null; interface SourceMapTransformerOptions { baseDir?: string; getMapping?: GetMapping; } declare class SourceMapTransformer { finder: SourceMapFinder; baseDir: string; resolveMapping: GetMapping; constructor(finder: SourceMapFinder, opts?: SourceMapTransformerOptions); processFile(fc: FileCoverage, sourceMap: TraceMap, coverageMapper: (source: string) => MappedCoverage): boolean; transform(coverageMap: CoverageMap): Promise; } //#endregion //#region src/index.d.ts /** * @module Exports */ export declare function createSourceMapStore(opts?: MapStoreOptions): MapStore; //#endregion export type { FileCoverageDataWithSourceMap, GetMapping, MapStore, MapStoreOptions, MappedCoverage, Mapping, SourceMapFinder, SourceMapTransformer, SourceMapTransformerOptions, SourceStore };