Skip to content

Commit

Permalink
Truncate mappings to the last line with a valid segment
Browse files Browse the repository at this point in the history
Re: #116, which has an sourcemap without any segments. There's no point in keeping trailing lines
without any segments, and truncating better matches the output from `source-map`.
  • Loading branch information
jridgewell committed Mar 16, 2021
1 parent 9f70eba commit 0ae6dbd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/source-map-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class SourceMapTree {
const sourcesContent: (string | null)[] = [];
const { mappings: rootMappings, names: rootNames } = this.map;

let lastLineWithSegment = -1;
for (let i = 0; i < rootMappings.length; i++) {
const segments = rootMappings[i];
const tracedSegments: SourceMapSegment[] = [];
Expand Down Expand Up @@ -105,10 +106,14 @@ export default class SourceMapTree {
lastTraced = [segment[0], sourceIndex, line, column];
}
tracedSegments.push(lastTraced);
lastLineWithSegment = i;
}

mappings.push(tracedSegments);
}
if (mappings.length > lastLineWithSegment + 1) {
mappings.length = lastLineWithSegment + 1;
}

// TODO: Make all sources relative to the sourceRoot.

Expand Down
32 changes: 30 additions & 2 deletions test/unit/source-map-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('SourceMapTree', () => {

const source = new SourceMapTree(map, [child]);
const traced = source.traceMappings();
expect(traced.mappings).toEqual([[]]);
expect(traced.mappings).toEqual([]);
});

test('skips segment if trace returns null', () => {
Expand All @@ -68,7 +68,7 @@ describe('SourceMapTree', () => {

const source = new SourceMapTree(map, [child]);
const traced = source.traceMappings();
expect(traced.mappings).toEqual([[]]);
expect(traced.mappings).toEqual([]);
});

test('traces name if segment is 5-length', () => {
Expand Down Expand Up @@ -155,6 +155,34 @@ describe('SourceMapTree', () => {
});
});

test('truncates mappings to the last line with segment', () => {
const map: DecodedSourceMap = {
...baseMap,
mappings: [[[0, 0, 0, 0]], [], []],
sourceRoot,
};

const source = new SourceMapTree(map, [child]);
const traced = source.traceMappings();
expect(traced).toMatchObject({
mappings: [[[0, 0, 0, 0]]],
});
});

test('truncates empty mappings', () => {
const map: DecodedSourceMap = {
...baseMap,
mappings: [[], [], []],
sourceRoot,
};

const source = new SourceMapTree(map, [child]);
const traced = source.traceMappings();
expect(traced).toMatchObject({
mappings: [],
});
});

describe('redundant segments', () => {
it('skips redundant segments on the same line', () => {
const map: DecodedSourceMap = {
Expand Down

0 comments on commit 0ae6dbd

Please sign in to comment.