Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit bee5fd0

Browse files
committed
Cleanup types
Uses consistent `SourceMapSegmentObject` to avoid TS issues with multiple typed parameters
1 parent 3429b0b commit bee5fd0

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

src/source-map-tree.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,16 @@ import { traceSegment, decodedMappings } from '@jridgewell/trace-mapping';
33

44
import type { TraceMap } from '@jridgewell/trace-mapping';
55

6-
export type SourceMapSegmentObject =
7-
| {
8-
column: number;
9-
line: number;
10-
name: string;
11-
source: string;
12-
content: string | null;
13-
}
14-
| {
15-
column: null;
16-
line: null;
17-
name: null;
18-
source: null;
19-
content: null;
20-
};
21-
22-
const SOURCELESS_MAPPING = {
23-
source: null,
24-
column: null,
25-
line: null,
26-
name: null,
27-
content: null,
6+
export type SourceMapSegmentObject = {
7+
column: number;
8+
line: number;
9+
name: string;
10+
source: string;
11+
content: string | null;
2812
};
29-
const EMPTY_SOURCES: Sources[] = [];
3013

3114
export type OriginalSource = {
32-
map: TraceMap;
15+
map: null;
3316
sources: Sources[];
3417
source: string;
3518
content: string | null;
@@ -39,17 +22,37 @@ export type MapSource = {
3922
map: TraceMap;
4023
sources: Sources[];
4124
source: string;
42-
content: string | null;
25+
content: null;
4326
};
4427

4528
export type Sources = OriginalSource | MapSource;
4629

47-
function Source<M extends TraceMap | null>(
48-
map: TraceMap | null,
30+
const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null);
31+
const EMPTY_SOURCES: Sources[] = [];
32+
33+
function SegmentObject(
34+
source: string,
35+
line: number,
36+
column: number,
37+
name: string,
38+
content: string | null
39+
): SourceMapSegmentObject {
40+
return { source, line, column, name, content };
41+
}
42+
43+
function Source(map: TraceMap, sources: Sources[], source: '', content: null): MapSource;
44+
function Source(
45+
map: null,
4946
sources: Sources[],
5047
source: string,
5148
content: string | null
52-
): M extends null ? OriginalSource : MapSource {
49+
): OriginalSource;
50+
function Source(
51+
map: TraceMap | null,
52+
sources: Sources[],
53+
source: string | '',
54+
content: string | null
55+
): Sources {
5356
return {
5457
map,
5558
sources,
@@ -112,8 +115,7 @@ export function traceMappings(tree: MapSource): GenMapping {
112115

113116
const { column, line, name, content, source } = traced;
114117

115-
// Sigh, TypeScript can't figure out source/line/column are either all null, or all non-null...
116-
(maybeAddSegment as any)(gen, i, genCol, source, line, column, name);
118+
maybeAddSegment(gen, i, genCol, source, line, column, name);
117119
if (source && content != null) setSourceContent(gen, source, content);
118120
}
119121
}
@@ -132,7 +134,7 @@ export function originalPositionFor(
132134
name: string
133135
): SourceMapSegmentObject | null {
134136
if (!source.map) {
135-
return { column, line, name, source: source.source, content: source.content };
137+
return SegmentObject(source.source, line, column, name, source.content);
136138
}
137139

138140
const segment = traceSegment(source.map, line, column);

test/unit/source-map-tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe('MapSource', () => {
257257
for (let genCol = 0; genCol < expectedCols.length; genCol++) {
258258
const trace = originalPositionFor(tree, 5, genCol, '');
259259
if (expectedCols[genCol] == null) {
260-
expect(trace).toMatchObject({ source: null });
260+
expect(trace).toMatchObject({ source: '' });
261261
} else {
262262
expect(trace).toMatchObject({ line: 5, column: expectedCols[genCol] });
263263
}
@@ -277,7 +277,7 @@ describe('MapSource', () => {
277277

278278
test('returns sourceless segment object if segment is 1-length', () => {
279279
const trace = originalPositionFor(tree, 2, 0, '');
280-
expect(trace).toMatchObject({ source: null });
280+
expect(trace).toMatchObject({ source: '' });
281281
});
282282

283283
test('passes in outer name to trace', () => {

0 commit comments

Comments
 (0)