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

Commit 9add0c2

Browse files
committed
Add ignoreList support
1 parent eb146e8 commit 9add0c2

5 files changed

Lines changed: 120 additions & 16 deletions

File tree

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
"typescript": "4.6.3"
7070
},
7171
"dependencies": {
72-
"@jridgewell/set-array": "^1.0.1",
72+
"@jridgewell/set-array": "^1.2.1",
7373
"@jridgewell/sourcemap-codec": "^1.4.10",
74-
"@jridgewell/trace-mapping": "^0.3.9"
74+
"@jridgewell/trace-mapping": "^0.3.24"
7575
}
7676
}

src/gen-mapping.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SetArray, put } from '@jridgewell/set-array';
1+
import { SetArray, put, remove } from '@jridgewell/set-array';
22
import { encode } from '@jridgewell/sourcemap-codec';
33
import { TraceMap, decodedMappings } from '@jridgewell/trace-mapping';
44

@@ -27,10 +27,11 @@ const NO_NAME = -1;
2727
* Provides the state to generate a sourcemap.
2828
*/
2929
export class GenMapping {
30-
private declare _names;
31-
private declare _sources;
30+
private declare _names: SetArray<string>;
31+
private declare _sources: SetArray<string>;
3232
private declare _sourcesContent: (string | null)[];
3333
private declare _mappings: SourceMapSegment[][];
34+
private declare _ignoreList: SetArray<number>;
3435
declare file: string | null | undefined;
3536
declare sourceRoot: string | null | undefined;
3637

@@ -41,6 +42,7 @@ export class GenMapping {
4142
this._mappings = [];
4243
this.file = file;
4344
this.sourceRoot = sourceRoot;
45+
this._ignoreList = new SetArray();
4446
}
4547
}
4648

@@ -49,6 +51,7 @@ interface PublicMap {
4951
_sources: GenMapping['_sources'];
5052
_sourcesContent: GenMapping['_sourcesContent'];
5153
_mappings: GenMapping['_mappings'];
54+
_ignoreList: GenMapping['_ignoreList'];
5255
}
5356

5457
/**
@@ -205,7 +208,16 @@ export const maybeAddMapping: typeof addMapping = (map, mapping) => {
205208
*/
206209
export function setSourceContent(map: GenMapping, source: string, content: string | null): void {
207210
const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
208-
sourcesContent[put(sources, source)] = content;
211+
const index = put(sources, source);
212+
sourcesContent[index] = content;
213+
}
214+
215+
export function setIgnore(map: GenMapping, source: string, ignore = true) {
216+
const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
217+
const index = put(sources, source);
218+
if (index === sourcesContent.length) sourcesContent[index] = null;
219+
if (ignore) put(ignoreList, index);
220+
else remove(ignoreList, index);
209221
}
210222

211223
/**
@@ -218,6 +230,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
218230
_sources: sources,
219231
_sourcesContent: sourcesContent,
220232
_names: names,
233+
_ignoreList: ignoreList,
221234
} = cast(map);
222235
removeEmptyFinalLines(mappings);
223236

@@ -229,6 +242,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
229242
sources: sources.array,
230243
sourcesContent,
231244
mappings,
245+
ignoreList: ignoreList.array,
232246
};
233247
}
234248

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

259274
return gen;
260275
}
@@ -375,8 +390,8 @@ function removeEmptyFinalLines(mappings: SourceMapSegment[][]) {
375390
if (len < length) mappings.length = len;
376391
}
377392

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

382397
function skipSourceless(line: SourceMapSegment[], index: number): boolean {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface SourceMapV3 {
77
sources: readonly (string | null)[];
88
sourcesContent?: readonly (string | null)[];
99
version: 3;
10+
ignoreList?: readonly number[];
1011
}
1112

1213
export interface EncodedSourceMap extends SourceMapV3 {

test/gen-mapping.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
fromMap,
1111
maybeAddSegment,
1212
maybeAddMapping,
13+
setIgnore,
1314
} from '../src/gen-mapping';
1415

1516
describe('GenMapping', () => {
@@ -59,6 +60,13 @@ describe('GenMapping', () => {
5960

6061
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 2, 3, 0]]]);
6162
});
63+
64+
it('has ignoreList', () => {
65+
const map = new GenMapping();
66+
setIgnore(map, 'input.js');
67+
68+
assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
69+
});
6270
});
6371

6472
describe('toEncodedMap', () => {
@@ -107,6 +115,13 @@ describe('GenMapping', () => {
107115

108116
assert.deepEqual(toEncodedMap(map).mappings, 'CAEGA');
109117
});
118+
119+
it('has ignoreList', () => {
120+
const map = new GenMapping();
121+
setIgnore(map, 'input.js');
122+
123+
assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
124+
});
110125
});
111126

112127
describe('addSegment', () => {
@@ -844,4 +859,77 @@ describe('GenMapping', () => {
844859
});
845860
});
846861
});
862+
863+
describe('setSourceContent', () => {
864+
it('sets source content for source', () => {
865+
const map = new GenMapping();
866+
addSegment(map, 0, 0, 'input.js', 0, 0);
867+
868+
setSourceContent(map, 'input.js', 'input');
869+
870+
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']);
871+
});
872+
873+
it('sets source content for many sources', () => {
874+
const map = new GenMapping();
875+
addSegment(map, 0, 0, 'first.js', 0, 0);
876+
addSegment(map, 0, 1, 'second.js', 0, 0);
877+
878+
setSourceContent(map, 'second.js', 'second');
879+
setSourceContent(map, 'first.js', 'first');
880+
881+
assert.deepEqual(toDecodedMap(map).sourcesContent, ['first', 'second']);
882+
});
883+
884+
it('adds unknown source file', () => {
885+
const map = new GenMapping();
886+
887+
setSourceContent(map, 'input.js', 'input');
888+
889+
assert.deepEqual(toDecodedMap(map).sources, ['input.js']);
890+
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']);
891+
});
892+
});
893+
894+
describe('setIgnore', () => {
895+
it('marks source file as ignored', () => {
896+
const map = new GenMapping();
897+
addSegment(map, 0, 0, 'input.js', 0, 0);
898+
899+
setIgnore(map, 'input.js');
900+
901+
assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
902+
});
903+
904+
it('marks many source files as ignored', () => {
905+
const map = new GenMapping();
906+
addSegment(map, 0, 0, 'first.js', 0, 0);
907+
addSegment(map, 0, 1, 'second.js', 0, 0);
908+
909+
setIgnore(map, 'second.js');
910+
setIgnore(map, 'first.js');
911+
912+
assert.deepEqual(toDecodedMap(map).ignoreList, [1, 0]);
913+
});
914+
915+
it('adds unknown source file', () => {
916+
const map = new GenMapping();
917+
918+
setIgnore(map, 'input.js');
919+
920+
assert.deepEqual(toDecodedMap(map).sources, ['input.js']);
921+
assert.deepEqual(toDecodedMap(map).sourcesContent, [null]);
922+
assert.deepEqual(toDecodedMap(map).ignoreList, [0]);
923+
});
924+
925+
it('unignores source file', () => {
926+
const map = new GenMapping();
927+
addSegment(map, 0, 0, 'input.js', 0, 0);
928+
929+
setIgnore(map, 'input.js');
930+
setIgnore(map, 'input.js', false);
931+
932+
assert.deepEqual(toDecodedMap(map).ignoreList, []);
933+
});
934+
});
847935
});

0 commit comments

Comments
 (0)