From 9add0c20b3058a0f98c5d1a730405b3726479269 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 1 Mar 2024 02:38:26 -0500 Subject: [PATCH] Add `ignoreList` support --- package-lock.json | 16 ++++---- package.json | 4 +- src/gen-mapping.ts | 27 +++++++++--- src/types.ts | 1 + test/gen-mapping.test.ts | 88 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4be0aac..a27fe1a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,9 @@ "version": "0.3.4", "license": "MIT", "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" }, "devDependencies": { "@rollup/plugin-typescript": "8.3.2", @@ -478,9 +478,9 @@ } }, "node_modules/@jridgewell/set-array": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.1.2.tgz", - "integrity": "sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.2.1.tgz", + "integrity": "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A==", "engines": { "node": ">=6.0.0" } @@ -491,9 +491,9 @@ "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" }, "node_modules/@jridgewell/trace-mapping": { - "version": "0.3.23", - "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.23.tgz", - "integrity": "sha512-9/4foRoUKp8s96tSkh8DlAAc5A0Ty8vLXld+l9gjKKY6ckwI8G15f0hskGmuLZu78ZlGa1vtsfOa+lnB4vG6Jg==", + "version": "0.3.24", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.24.tgz", + "integrity": "sha512-+VaWXDa6+l6MhflBvVXjIEAzb59nQ2JUK3bwRp2zRpPtU+8TFRy9Gg/5oIcNlkEL5PGlBFGfemUVvIgLnTzq7Q==", "dependencies": { "@jridgewell/resolve-uri": "^3.1.0", "@jridgewell/sourcemap-codec": "^1.4.14" diff --git a/package.json b/package.json index 94a6efe..30745c9 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/gen-mapping.ts b/src/gen-mapping.ts index 5f04eb7..36228ac 100644 --- a/src/gen-mapping.ts +++ b/src/gen-mapping.ts @@ -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'; @@ -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; + private declare _sources: SetArray; private declare _sourcesContent: (string | null)[]; private declare _mappings: SourceMapSegment[][]; + private declare _ignoreList: SetArray; declare file: string | null | undefined; declare sourceRoot: string | null | undefined; @@ -41,6 +42,7 @@ export class GenMapping { this._mappings = []; this.file = file; this.sourceRoot = sourceRoot; + this._ignoreList = new SetArray(); } } @@ -49,6 +51,7 @@ interface PublicMap { _sources: GenMapping['_sources']; _sourcesContent: GenMapping['_sourcesContent']; _mappings: GenMapping['_mappings']; + _ignoreList: GenMapping['_ignoreList']; } /** @@ -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); } /** @@ -218,6 +230,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap { _sources: sources, _sourcesContent: sourcesContent, _names: names, + _ignoreList: ignoreList, } = cast(map); removeEmptyFinalLines(mappings); @@ -229,6 +242,7 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap { sources: sources.array, sourcesContent, mappings, + ignoreList: ignoreList.array, }; } @@ -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; } @@ -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(setarr: SetArray, array: T[]) { + for (let i = 0; i < array.length; i++) put(setarr, array[i]); } function skipSourceless(line: SourceMapSegment[], index: number): boolean { diff --git a/src/types.ts b/src/types.ts index dd11331..064f076 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 { diff --git a/test/gen-mapping.test.ts b/test/gen-mapping.test.ts index 469e967..f0c2a08 100644 --- a/test/gen-mapping.test.ts +++ b/test/gen-mapping.test.ts @@ -10,6 +10,7 @@ import { fromMap, maybeAddSegment, maybeAddMapping, + setIgnore, } from '../src/gen-mapping'; describe('GenMapping', () => { @@ -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', () => { @@ -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', () => { @@ -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, []); + }); + }); });