Skip to content

Commit

Permalink
Add fromMap API
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Apr 27, 2022
1 parent 2464c6c commit 6d768ab
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 9 deletions.
9 changes: 3 additions & 6 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"dependencies": {
"@jridgewell/set-array": "^1.0.0",
"@jridgewell/sourcemap-codec": "^1.4.10"
"@jridgewell/sourcemap-codec": "^1.4.10",
"@jridgewell/trace-mapping": "^0.3.9"
}
}
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function configure(esm) {
globals: {
'@jridgewell/set-array': 'setArray',
'@jridgewell/sourcemap-codec': 'sourcemapCodec',
'@jridgewell/trace-mapping': 'traceMapping',
},
},
plugins: [
Expand Down
26 changes: 24 additions & 2 deletions src/gen-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SetArray, put } from '@jridgewell/set-array';
import { encode } from '@jridgewell/sourcemap-codec';
import { TraceMap, decodedMappings } from '@jridgewell/trace-mapping';

import type { SourceMapInput } from '@jridgewell/trace-mapping';
import type { SourceMapSegment } from './sourcemap-segment';
import type { DecodedSourceMap, EncodedSourceMap, Pos, Mapping } from './types';

Expand Down Expand Up @@ -96,6 +98,11 @@ export let decodedMap: (map: GenMapping) => DecodedSourceMap;
*/
export let encodedMap: (map: GenMapping) => EncodedSourceMap;

/**
* Constructs a new GenMapping, using the already present mappings of the input.
*/
export let fromMap: (input: SourceMapInput) => GenMapping;

/**
* Returns an array of high-level mapping objects for every recorded segment, which could then be
* passed to the `source-map` library.
Expand Down Expand Up @@ -128,7 +135,7 @@ export class GenMapping {
} = map;

const line = getLine(mappings, genLine);
if (source == null) {
if (!source) {
const seg: SourceMapSegment = [genColumn];
const index = getColumnIndex(line, genColumn, seg);
return insert(line, index, seg);
Expand Down Expand Up @@ -222,6 +229,18 @@ export class GenMapping {

return out;
};

fromMap = (input) => {
const map = new TraceMap(input);
const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });

putAll(gen._names, map.names);
putAll(gen._sources, map.sources as string[]);
gen._sourcesContent = map.sourcesContent || map.sources.map(() => null);
gen._mappings = decodedMappings(map) as GenMapping['_mappings'];

return gen;
};
}
}

Expand Down Expand Up @@ -274,9 +293,12 @@ function compareNum(a: number, b: number): number {
}

function insert<T>(array: T[], index: number, value: T) {
if (index === -1) return;
for (let i = array.length; i > index; i--) {
array[i] = array[i - 1];
}
array[index] = value;
}

function putAll(strarr: SetArray, array: string[]) {
for (let i = 0; i < array.length; i++) put(strarr, array[i]);
}
121 changes: 121 additions & 0 deletions test/gen-mapping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
decodedMap,
encodedMap,
allMappings,
fromMap,
} = require('..');
const assert = require('assert');

Expand Down Expand Up @@ -430,4 +431,124 @@ describe('GenMapping', () => {
]);
});
});

describe('fromMap', () => {
it('copies version', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
sourcesContent: [],
mappings: [],
});
const map = fromMap(input);

assert.strictEqual(decodedMap(map).version, 3);
});

it('copies file name', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
file: 'output.js',
names: [],
sources: ['input.js'],
sourcesContent: [],
mappings: [],
});
const map = fromMap(input);

assert.strictEqual(decodedMap(map).file, 'output.js');
});

it('copies sourceRoot', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sourceRoot: 'foo/',
sources: ['input.js'],
sourcesContent: [],
mappings: [],
});
const map = fromMap(input);

assert.strictEqual(decodedMap(map).sourceRoot, 'foo/');
});

it('copies sources', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
sourcesContent: [],
mappings: [],
});
const map = fromMap(input);

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

it('copies sourcesContent', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
sourcesContent: ['input'],
mappings: [],
});
const map = fromMap(input);

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

it('creates sourcesContent', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
mappings: [],
});
const map = fromMap(input);

assert.deepEqual(decodedMap(map).sourcesContent, [null]);
});

it('copies names', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: ['foo'],
sources: ['input.js'],
sourcesContent: [],
mappings: [[[0, 0, 0, 0, 0]]],
});
const map = fromMap(input);

assert.deepEqual(decodedMap(map).names, ['foo']);
});

it('copies decoded mappings', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
sourcesContent: [],
mappings: [[[1, 0, 2, 3, 0]]],
});
const map = fromMap(input);

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

it('copies encoded mappings', () => {
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({
version: 3,
names: [],
sources: ['input.js'],
sourcesContent: [],
mappings: 'CAEGA',
});
const map = fromMap(input);

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

0 comments on commit 6d768ab

Please sign in to comment.