diff --git a/package-lock.json b/package-lock.json index 00c33c0..0ad9614 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@jridgewell/source-map", - "version": "0.3.2", + "version": "0.3.5", "license": "MIT", "dependencies": { "@jridgewell/gen-mapping": "^0.3.0", diff --git a/src/source-map.ts b/src/source-map.ts index 074f249..61709f0 100644 --- a/src/source-map.ts +++ b/src/source-map.ts @@ -15,7 +15,6 @@ import { toEncodedMap, setSourceContent, fromMap, - applySourceMap, } from '@jridgewell/gen-mapping'; import type { @@ -167,20 +166,6 @@ export class SourceMapGenerator { setSourceContent(this._map, source, content); } - applySourceMap( - sourceMapConsumer: SourceMapConsumer, - sourceFile?: string, - sourceMapPath?: string, - ) { - // TODO :: not sure how sourceMapConsumer should be typed / converted - applySourceMap( - this._map, - new AnyMap(sourceMapConsumer) as unknown as any, - sourceFile, - sourceMapPath, - ); - } - toJSON(): ReturnType { return toEncodedMap(this._map); } diff --git a/test/source-map-generator.test.js b/test/source-map-generator.test.js index e3a7037..78b6d64 100644 --- a/test/source-map-generator.test.js +++ b/test/source-map-generator.test.js @@ -247,255 +247,3 @@ describe('SourceMapGenerator.fromSourceMap', () => { utils.assertEqualMaps(assert, map.toJSON(), fixtures.testMapMultiSourcesMappingRefersSingleSourceOnly); }); }) - - -describe('SourceMapGenerator.applySourceMap', () => { - it('test applySourceMap', () => { - - var mapStep1 = { - version: 3, - sources: [ 'fileX', 'fileY' ], - names: [], - mappings: 'AACA;;ACAA;;ADDA;;ACAA', - file: 'fileA', - sourcesContent: [ 'lineX1\nlineX2\n', null ] - }; - - var mapStep2 = { - version: 3, - sources: [ 'fileA', 'fileB' ], - names: [], - mappings: ';AAAA;AACA;AACA;AACA;ACHA;AACA', - file: 'fileGen', - sourcesContent: [ null, 'lineB1\nlineB2\n' ] - }; - - var expectedMap = { - version: 3, - sources: [ 'fileX', 'fileA', 'fileY', 'fileB' ], - names: [], - mappings: ';AACA;ACAA;ACAA;ADEA;AEHA;AACA', - file: 'fileGen', - sourcesContent: [ 'lineX1\nlineX2\n', null, null, 'lineB1\nlineB2\n' ] - }; - - // apply source map "mapStep1" to "mapStep2" - var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2)); - generator.applySourceMap(new SourceMapConsumer(mapStep1)); - var actualMap = generator.toJSON(); - - utils.assertEqualMaps(assert, actualMap, expectedMap); - }); - - it('test applySourceMap throws when file is missing', () => { - var map = new SourceMapGenerator({ - file: 'test.js' - }); - var map2 = new SourceMapGenerator(); - assert.throws(function() { - map.applySourceMap(new SourceMapConsumer(map2.toJSON())); - }); - }); - - it('test the two additional parameters of applySourceMap', () => { - // Assume the following directory structure: - // - // http://foo.org/ - // bar.coffee - // app/ - // coffee/ - // foo.coffee - // temp/ - // bundle.js - // temp_maps/ - // bundle.js.map - // public/ - // bundle.min.js - // bundle.min.js.map - // - // http://www.example.com/ - // baz.coffee - - var bundleMap = new SourceMapGenerator({ - file: 'bundle.js' - }); - bundleMap.addMapping({ - generated: { line: 3, column: 3 }, - original: { line: 2, column: 2 }, - source: '../../coffee/foo.coffee' - }); - bundleMap.setSourceContent('../../coffee/foo.coffee', 'foo coffee'); - bundleMap.addMapping({ - generated: { line: 13, column: 13 }, - original: { line: 12, column: 12 }, - source: '/bar.coffee' - }); - bundleMap.setSourceContent('/bar.coffee', 'bar coffee'); - bundleMap.addMapping({ - generated: { line: 23, column: 23 }, - original: { line: 22, column: 22 }, - source: 'http://www.example.com/baz.coffee' - }); - bundleMap.setSourceContent( - 'http://www.example.com/baz.coffee', - 'baz coffee' - ); - bundleMap = new SourceMapConsumer(bundleMap.toJSON()); - - var minifiedMap = new SourceMapGenerator({ - file: 'bundle.min.js', - sourceRoot: '..' - }); - minifiedMap.addMapping({ - generated: { line: 1, column: 1 }, - original: { line: 3, column: 3 }, - source: 'temp/bundle.js' - }); - minifiedMap.addMapping({ - generated: { line: 11, column: 11 }, - original: { line: 13, column: 13 }, - source: 'temp/bundle.js' - }); - minifiedMap.addMapping({ - generated: { line: 21, column: 21 }, - original: { line: 23, column: 23 }, - source: 'temp/bundle.js' - }); - minifiedMap = new SourceMapConsumer(minifiedMap.toJSON()); - - var expectedMap = function (sources) { - var map = new SourceMapGenerator({ - file: 'bundle.min.js', - sourceRoot: '..' - }); - map.addMapping({ - generated: { line: 1, column: 1 }, - original: { line: 2, column: 2 }, - source: sources[0] - }); - map.setSourceContent(sources[0], 'foo coffee'); - map.addMapping({ - generated: { line: 11, column: 11 }, - original: { line: 12, column: 12 }, - source: sources[1] - }); - map.setSourceContent(sources[1], 'bar coffee'); - map.addMapping({ - generated: { line: 21, column: 21 }, - original: { line: 22, column: 22 }, - source: sources[2] - }); - map.setSourceContent(sources[2], 'baz coffee'); - return map.toJSON(); - } - - var actualMap = function (aSourceMapPath) { - var map = SourceMapGenerator.fromSourceMap(minifiedMap); - // Note that relying on `bundleMap.file` (which is simply 'bundle.js') - // instead of supplying the second parameter wouldn't work here. - map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath); - return map.toJSON(); - } - - utils.assertEqualMaps(assert, actualMap('../temp/temp_maps'), expectedMap([ - 'coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - utils.assertEqualMaps(assert, actualMap('/app/temp/temp_maps'), expectedMap([ - '/app/coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - utils.assertEqualMaps(assert, actualMap('http://foo.org/app/temp/temp_maps'), expectedMap([ - 'http://foo.org/app/coffee/foo.coffee', - 'http://foo.org/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - // If the third parameter is omitted or set to the current working - // directory we get incorrect source paths: - - utils.assertEqualMaps(assert, actualMap(), expectedMap([ - '../coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - utils.assertEqualMaps(assert, actualMap(''), expectedMap([ - '../coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - utils.assertEqualMaps(assert, actualMap('.'), expectedMap([ - '../coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - - utils.assertEqualMaps(assert, actualMap('./'), expectedMap([ - '../coffee/foo.coffee', - '/bar.coffee', - 'http://www.example.com/baz.coffee' - ])); - }); - - it('test applySourceMap name handling', () => { - // Imagine some CoffeeScript code being compiled into JavaScript and then - // minified. - - var assertName = function(coffeeName, jsName, expectedName) { - var minifiedMap = new SourceMapGenerator({ - file: 'test.js.min' - }); - minifiedMap.addMapping({ - generated: { line: 1, column: 4 }, - original: { line: 1, column: 4 }, - source: 'test.js', - name: jsName - }); - - var coffeeMap = new SourceMapGenerator({ - file: 'test.js' - }); - coffeeMap.addMapping({ - generated: { line: 1, column: 4 }, - original: { line: 1, column: 0 }, - source: 'test.coffee', - name: coffeeName - }); - - minifiedMap.applySourceMap(new SourceMapConsumer(coffeeMap.toJSON())); - - new SourceMapConsumer(minifiedMap.toJSON()).eachMapping(function(mapping) { - assert.equal(mapping.name, expectedName); - }); - }; - - // `foo = 1` -> `var foo = 1;` -> `var a=1` - // CoffeeScript doesn’t rename variables, so there’s no need for it to - // provide names in its source maps. Minifiers do rename variables and - // therefore do provide names in their source maps. So that name should be - // retained if the original map lacks names. - assertName(null, 'foo', 'foo'); - - // `foo = 1` -> `var coffee$foo = 1;` -> `var a=1` - // Imagine that CoffeeScript prefixed all variables with `coffee$`. Even - // though the minifier then also provides a name, the original name is - // what corresponds to the source. - assertName('foo', 'coffee$foo', 'foo'); - - // `foo = 1` -> `var coffee$foo = 1;` -> `var coffee$foo=1` - // Minifiers can turn off variable mangling. Then there’s no need to - // provide names in the source map, but the names from the original map are - // still needed. - assertName('foo', null, 'foo'); - - // `foo = 1` -> `var foo = 1;` -> `var foo=1` - // No renaming at all. - assertName(null, null, null); - }); -})