Skip to content

Commit

Permalink
Add ignoreList support
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 1, 2024
1 parent eb146e8 commit 9add0c2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"typescript": "4.6.3"
},
"dependencies": {
"@jridgewell/set-array": "^1.0.1",
"@jridgewell/set-array": "^1.2.1",
"@jridgewell/sourcemap-codec": "^1.4.10",
"@jridgewell/trace-mapping": "^0.3.9"
"@jridgewell/trace-mapping": "^0.3.24"
}
}
27 changes: 21 additions & 6 deletions src/gen-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SetArray, put } from '@jridgewell/set-array';
import { SetArray, put, remove } from '@jridgewell/set-array';
import { encode } from '@jridgewell/sourcemap-codec';
import { TraceMap, decodedMappings } from '@jridgewell/trace-mapping';

Expand Down Expand Up @@ -27,10 +27,11 @@ const NO_NAME = -1;
* Provides the state to generate a sourcemap.
*/
export class GenMapping {
private declare _names;
private declare _sources;
private declare _names: SetArray<string>;
private declare _sources: SetArray<string>;
private declare _sourcesContent: (string | null)[];
private declare _mappings: SourceMapSegment[][];
private declare _ignoreList: SetArray<number>;
declare file: string | null | undefined;
declare sourceRoot: string | null | undefined;

Expand All @@ -41,6 +42,7 @@ export class GenMapping {
this._mappings = [];
this.file = file;
this.sourceRoot = sourceRoot;
this._ignoreList = new SetArray();
}
}

Expand All @@ -49,6 +51,7 @@ interface PublicMap {
_sources: GenMapping['_sources'];
_sourcesContent: GenMapping['_sourcesContent'];
_mappings: GenMapping['_mappings'];
_ignoreList: GenMapping['_ignoreList'];
}

/**
Expand Down Expand Up @@ -205,7 +208,16 @@ export const maybeAddMapping: typeof addMapping = (map, mapping) => {
*/
export function setSourceContent(map: GenMapping, source: string, content: string | null): void {
const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
sourcesContent[put(sources, source)] = content;
const index = put(sources, source);
sourcesContent[index] = content;
}

export function setIgnore(map: GenMapping, source: string, ignore = true) {
const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
const index = put(sources, source);
if (index === sourcesContent.length) sourcesContent[index] = null;
if (ignore) put(ignoreList, index);
else remove(ignoreList, index);
}

/**
Expand All @@ -218,6 +230,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
_sources: sources,
_sourcesContent: sourcesContent,
_names: names,
_ignoreList: ignoreList,
} = cast(map);
removeEmptyFinalLines(mappings);

Expand All @@ -229,6 +242,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
sources: sources.array,
sourcesContent,
mappings,
ignoreList: ignoreList.array,
};
}

Expand All @@ -255,6 +269,7 @@ export function fromMap(input: SourceMapInput): GenMapping {
putAll(cast(gen)._sources, map.sources as string[]);
cast(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
cast(gen)._mappings = decodedMappings(map) as GenMapping['_mappings'];
if (map.ignoreList) putAll(cast(gen)._ignoreList, map.ignoreList);

return gen;
}
Expand Down Expand Up @@ -375,8 +390,8 @@ function removeEmptyFinalLines(mappings: SourceMapSegment[][]) {
if (len < length) mappings.length = len;
}

function putAll(strarr: SetArray, array: string[]) {
for (let i = 0; i < array.length; i++) put(strarr, array[i]);
function putAll<T extends string | number>(setarr: SetArray<T>, array: T[]) {
for (let i = 0; i < array.length; i++) put(setarr, array[i]);
}

function skipSourceless(line: SourceMapSegment[], index: number): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface SourceMapV3 {
sources: readonly (string | null)[];
sourcesContent?: readonly (string | null)[];
version: 3;
ignoreList?: readonly number[];
}

export interface EncodedSourceMap extends SourceMapV3 {
Expand Down
88 changes: 88 additions & 0 deletions test/gen-mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fromMap,
maybeAddSegment,
maybeAddMapping,
setIgnore,
} from '../src/gen-mapping';

describe('GenMapping', () => {
Expand Down Expand Up @@ -59,6 +60,13 @@ describe('GenMapping', () => {

assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 2, 3, 0]]]);
});

it('has ignoreList', () => {
const map = new GenMapping();
setIgnore(map, 'input.js');

assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
});
});

describe('toEncodedMap', () => {
Expand Down Expand Up @@ -107,6 +115,13 @@ describe('GenMapping', () => {

assert.deepEqual(toEncodedMap(map).mappings, 'CAEGA');
});

it('has ignoreList', () => {
const map = new GenMapping();
setIgnore(map, 'input.js');

assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
});
});

describe('addSegment', () => {
Expand Down Expand Up @@ -844,4 +859,77 @@ describe('GenMapping', () => {
});
});
});

describe('setSourceContent', () => {
it('sets source content for source', () => {
const map = new GenMapping();
addSegment(map, 0, 0, 'input.js', 0, 0);

setSourceContent(map, 'input.js', 'input');

assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']);
});

it('sets source content for many sources', () => {
const map = new GenMapping();
addSegment(map, 0, 0, 'first.js', 0, 0);
addSegment(map, 0, 1, 'second.js', 0, 0);

setSourceContent(map, 'second.js', 'second');
setSourceContent(map, 'first.js', 'first');

assert.deepEqual(toDecodedMap(map).sourcesContent, ['first', 'second']);
});

it('adds unknown source file', () => {
const map = new GenMapping();

setSourceContent(map, 'input.js', 'input');

assert.deepEqual(toDecodedMap(map).sources, ['input.js']);
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']);
});
});

describe('setIgnore', () => {
it('marks source file as ignored', () => {
const map = new GenMapping();
addSegment(map, 0, 0, 'input.js', 0, 0);

setIgnore(map, 'input.js');

assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
});

it('marks many source files as ignored', () => {
const map = new GenMapping();
addSegment(map, 0, 0, 'first.js', 0, 0);
addSegment(map, 0, 1, 'second.js', 0, 0);

setIgnore(map, 'second.js');
setIgnore(map, 'first.js');

assert.deepEqual(toDecodedMap(map).ignoreList, [1, 0]);
});

it('adds unknown source file', () => {
const map = new GenMapping();

setIgnore(map, 'input.js');

assert.deepEqual(toDecodedMap(map).sources, ['input.js']);
assert.deepEqual(toDecodedMap(map).sourcesContent, [null]);
assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
});

it('unignores source file', () => {
const map = new GenMapping();
addSegment(map, 0, 0, 'input.js', 0, 0);

setIgnore(map, 'input.js');
setIgnore(map, 'input.js', false);

assert.deepEqual(toDecodedMap(map).ignoreList, []);
});
});
});

0 comments on commit 9add0c2

Please sign in to comment.