Skip to content

Commit cdf32c7

Browse files
committed
Accept readonly types
1 parent 5676069 commit cdf32c7

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

packages/trace-mapping/src/flatten-map.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SOURCE_COLUMN,
77
NAMES_INDEX,
88
} from './sourcemap-segment';
9+
import { parse } from './types';
910

1011
import type {
1112
DecodedSourceMap,
@@ -14,12 +15,13 @@ import type {
1415
SectionedSourceMapXInput,
1516
SectionedSourceMapInput,
1617
SectionXInput,
18+
Ro,
1719
} from './types';
1820
import type { SourceMapSegment } from './sourcemap-segment';
1921

2022
type FlattenMap = {
21-
new (map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap;
22-
(map: SectionedSourceMapInput, mapUrl?: string | null): TraceMap;
23+
new (map: Ro<SectionedSourceMapInput>, mapUrl?: string | null): TraceMap;
24+
(map: Ro<SectionedSourceMapInput>, mapUrl?: string | null): TraceMap;
2325
};
2426

2527
export const FlattenMap: FlattenMap = function (map, mapUrl) {
@@ -62,10 +64,6 @@ export const FlattenMap: FlattenMap = function (map, mapUrl) {
6264
return presortedDecodedMap(joined);
6365
} as FlattenMap;
6466

65-
function parse<T>(map: T): Exclude<T, string> {
66-
return typeof map === 'string' ? JSON.parse(map) : map;
67-
}
68-
6967
function recurse(
7068
input: SectionedSourceMapXInput,
7169
mapUrl: string | null | undefined,

packages/trace-mapping/src/trace-mapping.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
REV_GENERATED_LINE,
2121
REV_GENERATED_COLUMN,
2222
} from './sourcemap-segment';
23+
import { parse } from './types';
2324

2425
import type { SourceMapSegment, ReverseSegment } from './sourcemap-segment';
2526
import type {
@@ -38,6 +39,7 @@ import type {
3839
Bias,
3940
XInput,
4041
SectionedSourceMap,
42+
Ro,
4143
} from './types';
4244
import type { Source } from './by-source';
4345
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
8284
export const LEAST_UPPER_BOUND = -1;
8385
export const GREATEST_LOWER_BOUND = 1;
8486

85-
export { FlattenMap, FlattenMap as AnyMap } from './any-map';
87+
export { FlattenMap, FlattenMap as AnyMap } from './flatten-map';
8688

8789
export class TraceMap implements SourceMap {
8890
declare version: SourceMapV3['version'];
@@ -102,12 +104,11 @@ export class TraceMap implements SourceMap {
102104
private declare _bySources: Source[] | undefined;
103105
private declare _bySourceMemos: MemoState[] | undefined;
104106

105-
constructor(map: SourceMapInput, mapUrl?: string | null) {
107+
constructor(map: Ro<SourceMapInput>, mapUrl?: string | null) {
106108
const isString = typeof map === 'string';
107-
108109
if (!isString && (map as unknown as { _decodedMemo: any })._decodedMemo) return map as TraceMap;
109110

110-
const parsed = (isString ? JSON.parse(map) : map) as DecodedSourceMap | EncodedSourceMap;
111+
const parsed = parse(map as Exclude<SourceMapInput, TraceMap>);
111112

112113
const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
113114
this.version = version;

packages/trace-mapping/src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,15 @@ export abstract class SourceMap {
102102
declare resolvedSources: SourceMapV3['sources'];
103103
declare ignoreList: SourceMapV3['ignoreList'];
104104
}
105+
106+
export type Ro<T> = T extends Array<infer V>
107+
? V[] | Readonly<V[]> | RoArray<V> | Readonly<RoArray<V>>
108+
: T extends object
109+
? T | Readonly<T> | RoObject<T> | Readonly<RoObject<T>>
110+
: T;
111+
type RoArray<T> = Ro<T>[];
112+
type RoObject<T> = { [K in keyof T]: T[K] | Ro<T[K]> };
113+
114+
export function parse<T>(map: T): Exclude<T, string> {
115+
return typeof map === 'string' ? JSON.parse(map) : map;
116+
}

0 commit comments

Comments
 (0)