Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate mappings to the last line with a valid segment #118

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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