Skip to content

Commit

Permalink
Be more permissive with readonly input types
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Oct 8, 2022
1 parent d58ae00 commit c0a20cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/types.ts
Expand Up @@ -57,8 +57,17 @@ export type InvalidGeneratedMapping = {

export type Bias = typeof GREATEST_LOWER_BOUND | typeof LEAST_UPPER_BOUND;

export type SourceMapInput = string | EncodedSourceMap | DecodedSourceMap | TraceMap;
export type SectionedSourceMapInput = SourceMapInput | SectionedSourceMap;
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 type SourceMapInput = string | Ro<EncodedSourceMap> | Ro<DecodedSourceMap> | TraceMap;

export type SectionedSourceMapInput = SourceMapInput | Ro<SectionedSourceMap>;

export type Needle = { line: number; column: number; bias?: Bias };
export type SourceNeedle = { source: string; line: number; column: number; bias?: Bias };
Expand Down
19 changes: 18 additions & 1 deletion test/any-map.test.ts
Expand Up @@ -3,7 +3,7 @@
import { test, describe } from './setup';
import { AnyMap, encodedMappings, decodedMappings } from '../src/trace-mapping';

import type { SectionedSourceMap } from '../src/trace-mapping';
import type { SectionedSourceMap, SourceMapSegment } from '../src/trace-mapping';

describe('AnyMap', () => {
const map: SectionedSourceMap = {
Expand Down Expand Up @@ -126,4 +126,21 @@ describe('AnyMap', () => {
]);
});
});

describe('typescript readonly type', () => {
test('decoded source map', (t) => {
// This is a TS lint test, not a real one.
t.pass();

const decodedMap = {
version: 3 as const,
sources: ['input.js'] as readonly string[],
names: [] as readonly string[],
mappings: [] as readonly SourceMapSegment[][],
sourcesContent: [] as readonly string[],
};

new AnyMap(decodedMap);
});
});
});
18 changes: 18 additions & 0 deletions test/trace-mapping.test.ts
Expand Up @@ -24,6 +24,7 @@ import type {
EncodedSourceMap,
DecodedSourceMap,
EachMapping,
SourceMapSegment,
} from '../src/trace-mapping';

describe('TraceMap', () => {
Expand Down Expand Up @@ -462,4 +463,21 @@ describe('TraceMap', () => {
t.is(encodedMappings(tracer), '');
});
});

describe('typescript readonly type', () => {
test('decoded source map', (t) => {
// This is a TS lint test, not a real one.
t.pass();

const decodedMap = {
version: 3 as const,
sources: ['input.js'] as readonly string[],
names: [] as readonly string[],
mappings: [] as readonly SourceMapSegment[][],
sourcesContent: [] as readonly string[],
};

new TraceMap(decodedMap);
});
});
});

0 comments on commit c0a20cc

Please sign in to comment.