Skip to content

Commit

Permalink
Accept readonly types
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 2, 2024
1 parent 0a08a6a commit f8a42eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/flatten-map.ts
Expand Up @@ -6,6 +6,7 @@ import {
SOURCE_COLUMN,
NAMES_INDEX,
} from './sourcemap-segment';
import { parse } from './types';

import type {
DecodedSourceMap,
Expand All @@ -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<SectionedSourceMapInput>, mapUrl?: string | null): TraceMap;
(map: Ro<SectionedSourceMapInput>, mapUrl?: string | null): TraceMap;
};

export const FlattenMap: FlattenMap = function (map, mapUrl) {
Expand Down Expand Up @@ -62,10 +64,6 @@ export const FlattenMap: FlattenMap = function (map, mapUrl) {
return presortedDecodedMap(joined);
} as FlattenMap;

function parse<T>(map: T): Exclude<T, string> {
return typeof map === 'string' ? JSON.parse(map) : map;
}

function recurse(
input: SectionedSourceMapXInput,
mapUrl: string | null | undefined,
Expand Down
9 changes: 5 additions & 4 deletions src/trace-mapping.ts
Expand Up @@ -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 {
Expand All @@ -38,6 +39,7 @@ import type {
Bias,
XInput,
SectionedSourceMap,
Ro,
} from './types';
import type { Source } from './by-source';
import type { MemoState } from './binary-search';
Expand Down Expand Up @@ -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'];
Expand All @@ -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<SourceMapInput>, 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<SourceMapInput, TraceMap>);

const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
this.version = version;
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Expand Up @@ -102,3 +102,15 @@ export abstract class SourceMap {
declare resolvedSources: SourceMapV3['sources'];
declare ignoreList: SourceMapV3['ignoreList'];
}

export type Ro<T> = T extends Array<infer V>
? V[] | Readonly<V[]> | RoArray<V> | Readonly<RoArray<V>>
: T extends object
? T | Readonly<T> | RoObject<T> | Readonly<RoObject<T>>
: T;
type RoArray<T> = Ro<T>[];
type RoObject<T> = { [K in keyof T]: T[K] | Ro<T[K]> };

export function parse<T>(map: T): Exclude<T, string> {
return typeof map === 'string' ? JSON.parse(map) : map;
}

0 comments on commit f8a42eb

Please sign in to comment.