Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jridgewell/gen-mapping
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.4
Choose a base ref
...
head repository: jridgewell/gen-mapping
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.3.5
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 24, 2024

  1. Fix GH CI

    jridgewell committed Feb 24, 2024
    Copy the full SHA
    d515a7e View commit details
  2. Run benchmarks on CI

    jridgewell committed Feb 24, 2024
    Copy the full SHA
    eb146e8 View commit details

Commits on Mar 1, 2024

  1. Add ignoreList support

    jridgewell committed Mar 1, 2024
    Copy the full SHA
    9add0c2 View commit details
  2. 0.3.5

    jridgewell committed Mar 1, 2024
    Copy the full SHA
    75eff9d View commit details
Showing with 130 additions and 23 deletions.
  1. +7 −4 .github/workflows/test.yml
  2. +10 −10 package-lock.json
  3. +3 −3 package.json
  4. +21 −6 src/gen-mapping.ts
  5. +1 −0 src/types.ts
  6. +88 −0 test/gen-mapping.test.ts
11 changes: 7 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -21,14 +21,17 @@ jobs:
run: |
npm install
npm run build
npm run test:coverage
npm run test:only
- name: Switch to Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Test legacy node
- name: Test node
run: |
npm install mocha@6.2.3
npm run test:only
node dist/gen-mapping.umd.js
- name: Benchmark
run: |
npm run benchmark:install
npm run benchmark:only
env:
CI: true
20 changes: 10 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jridgewell/gen-mapping",
"version": "0.3.4",
"version": "0.3.5",
"description": "Generate source maps",
"keywords": [
"source",
@@ -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';

@@ -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;

@@ -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<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 {
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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 {
88 changes: 88 additions & 0 deletions test/gen-mapping.test.ts
Original file line number Diff line number Diff line change
@@ -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, []);
});
});
});