From 8d8fc353673c9dbd6b03d68c09430dc28880095a Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Sat, 2 Mar 2024 01:47:22 -0500 Subject: [PATCH] Support `string | TraceMap` in `Section`s --- src/any-map.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/any-map.ts b/src/any-map.ts index 4ea033d..347ce81 100644 --- a/src/any-map.ts +++ b/src/any-map.ts @@ -9,11 +9,10 @@ import { import type { DecodedSourceMap, - SectionedSourceMapInput, - Ro, DecodedSourceMapXInput, EncodedSourceMapXInput, SectionedSourceMapXInput, + SectionedSourceMapInput, SectionXInput, } from './types'; import type { SourceMapSegment } from './sourcemap-segment'; @@ -24,8 +23,7 @@ type AnyMap = { }; export const AnyMap: AnyMap = function (map, mapUrl) { - const parsed = - typeof map === 'string' ? (JSON.parse(map) as Exclude) : map; + const parsed = parse(map); if (!('sections' in parsed)) { return new TraceMap(parsed as DecodedSourceMapXInput | EncodedSourceMapXInput, mapUrl); @@ -64,8 +62,12 @@ export const AnyMap: AnyMap = function (map, mapUrl) { return presortedDecodedMap(joined); } as AnyMap; +function parse(map: T): Exclude { + return typeof map === 'string' ? JSON.parse(map) : map; +} + function recurse( - input: Ro, + input: SectionedSourceMapXInput, mapUrl: string | null | undefined, mappings: SourceMapSegment[][], sources: string[], @@ -111,7 +113,7 @@ function recurse( } function addSection( - input: Ro, + input: SectionXInput['map'], mapUrl: string | null | undefined, mappings: SourceMapSegment[][], sources: string[], @@ -123,9 +125,10 @@ function addSection( stopLine: number, stopColumn: number, ) { - if ('sections' in input) return recurse(...(arguments as unknown as Parameters)); + const parsed = parse(input); + if ('sections' in parsed) return recurse(...(arguments as unknown as Parameters)); - const map = new TraceMap(input, mapUrl); + const map = new TraceMap(parsed, mapUrl); const sourcesOffset = sources.length; const namesOffset = names.length; const decoded = decodedMappings(map);