diff --git a/src/flatten-map.ts b/src/flatten-map.ts index 2b8c558..61ac40c 100644 --- a/src/flatten-map.ts +++ b/src/flatten-map.ts @@ -6,6 +6,7 @@ import { SOURCE_COLUMN, NAMES_INDEX, } from './sourcemap-segment'; +import { parse } from './types'; import type { DecodedSourceMap, @@ -14,12 +15,13 @@ import type { SectionedSourceMapXInput, SectionedSourceMapInput, SectionXInput, + Ro, } from './types'; import type { SourceMapSegment } from './sourcemap-segment'; type FlattenMap = { - new (map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap; - (map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap; + new (map: Ro, mapUrl?: string | null): TraceMap; + (map: Ro, mapUrl?: string | null): TraceMap; }; export const FlattenMap: FlattenMap = function (map, mapUrl) { @@ -62,10 +64,6 @@ export const FlattenMap: FlattenMap = function (map, mapUrl) { return presortedDecodedMap(joined); } as FlattenMap; -function parse(map: T): Exclude { - return typeof map === 'string' ? JSON.parse(map) : map; -} - function recurse( input: SectionedSourceMapXInput, mapUrl: string | null | undefined, diff --git a/src/trace-mapping.ts b/src/trace-mapping.ts index d9be093..a5e85bf 100644 --- a/src/trace-mapping.ts +++ b/src/trace-mapping.ts @@ -20,6 +20,7 @@ import { REV_GENERATED_LINE, REV_GENERATED_COLUMN, } from './sourcemap-segment'; +import { parse } from './types'; import type { SourceMapSegment, ReverseSegment } from './sourcemap-segment'; import type { @@ -38,6 +39,7 @@ import type { Bias, XInput, SectionedSourceMap, + Ro, } from './types'; import type { Source } from './by-source'; import type { MemoState } from './binary-search'; @@ -82,7 +84,7 @@ const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns st export const LEAST_UPPER_BOUND = -1; export const GREATEST_LOWER_BOUND = 1; -export { FlattenMap, FlattenMap as AnyMap } from './any-map'; +export { FlattenMap, FlattenMap as AnyMap } from './flatten-map'; export class TraceMap implements SourceMap { declare version: SourceMapV3['version']; @@ -102,12 +104,11 @@ export class TraceMap implements SourceMap { private declare _bySources: Source[] | undefined; private declare _bySourceMemos: MemoState[] | undefined; - constructor(map: SourceMapInput, mapUrl?: string | null) { + constructor(map: Ro, mapUrl?: string | null) { const isString = typeof map === 'string'; - if (!isString && (map as unknown as { _decodedMemo: any })._decodedMemo) return map as TraceMap; - const parsed = (isString ? JSON.parse(map) : map) as DecodedSourceMap | EncodedSourceMap; + const parsed = parse(map as Exclude); const { version, file, names, sourceRoot, sources, sourcesContent } = parsed; this.version = version; diff --git a/src/types.ts b/src/types.ts index 6b54b24..96a4370 100644 --- a/src/types.ts +++ b/src/types.ts @@ -102,3 +102,15 @@ export abstract class SourceMap { declare resolvedSources: SourceMapV3['sources']; declare ignoreList: SourceMapV3['ignoreList']; } + +export type Ro = T extends Array + ? V[] | Readonly | RoArray | Readonly> + : T extends object + ? T | Readonly | RoObject | Readonly> + : T; +type RoArray = Ro[]; +type RoObject = { [K in keyof T]: T[K] | Ro }; + +export function parse(map: T): Exclude { + return typeof map === 'string' ? JSON.parse(map) : map; +}