Skip to content

Commit

Permalink
Cleanup types
Browse files Browse the repository at this point in the history
Uses consistent `SourceMapSegmentObject` to avoid TS issues with multiple typed parameters
  • Loading branch information
jridgewell committed Apr 30, 2022
1 parent 3429b0b commit bee5fd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
64 changes: 33 additions & 31 deletions src/source-map-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@ import { traceSegment, decodedMappings } from '@jridgewell/trace-mapping';

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

export type SourceMapSegmentObject =
| {
column: number;
line: number;
name: string;
source: string;
content: string | null;
}
| {
column: null;
line: null;
name: null;
source: null;
content: null;
};

const SOURCELESS_MAPPING = {
source: null,
column: null,
line: null,
name: null,
content: null,
export type SourceMapSegmentObject = {
column: number;
line: number;
name: string;
source: string;
content: string | null;
};
const EMPTY_SOURCES: Sources[] = [];

export type OriginalSource = {
map: TraceMap;
map: null;
sources: Sources[];
source: string;
content: string | null;
Expand All @@ -39,17 +22,37 @@ export type MapSource = {
map: TraceMap;
sources: Sources[];
source: string;
content: string | null;
content: null;
};

export type Sources = OriginalSource | MapSource;

function Source<M extends TraceMap | null>(
map: TraceMap | null,
const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null);
const EMPTY_SOURCES: Sources[] = [];

function SegmentObject(
source: string,
line: number,
column: number,
name: string,
content: string | null
): SourceMapSegmentObject {
return { source, line, column, name, content };
}

function Source(map: TraceMap, sources: Sources[], source: '', content: null): MapSource;
function Source(
map: null,
sources: Sources[],
source: string,
content: string | null
): M extends null ? OriginalSource : MapSource {
): OriginalSource;
function Source(
map: TraceMap | null,
sources: Sources[],
source: string | '',
content: string | null
): Sources {
return {
map,
sources,
Expand Down Expand Up @@ -112,8 +115,7 @@ export function traceMappings(tree: MapSource): GenMapping {

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

// Sigh, TypeScript can't figure out source/line/column are either all null, or all non-null...
(maybeAddSegment as any)(gen, i, genCol, source, line, column, name);
maybeAddSegment(gen, i, genCol, source, line, column, name);
if (source && content != null) setSourceContent(gen, source, content);
}
}
Expand All @@ -132,7 +134,7 @@ export function originalPositionFor(
name: string
): SourceMapSegmentObject | null {
if (!source.map) {
return { column, line, name, source: source.source, content: source.content };
return SegmentObject(source.source, line, column, name, source.content);
}

const segment = traceSegment(source.map, line, column);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/source-map-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe('MapSource', () => {
for (let genCol = 0; genCol < expectedCols.length; genCol++) {
const trace = originalPositionFor(tree, 5, genCol, '');
if (expectedCols[genCol] == null) {
expect(trace).toMatchObject({ source: null });
expect(trace).toMatchObject({ source: '' });
} else {
expect(trace).toMatchObject({ line: 5, column: expectedCols[genCol] });
}
Expand All @@ -277,7 +277,7 @@ describe('MapSource', () => {

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

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

0 comments on commit bee5fd0

Please sign in to comment.