From 879443e032465c2a1387cf12e7b5d4a8add6d7e9 Mon Sep 17 00:00:00 2001 From: Damir Murat Date: Sun, 6 Nov 2016 14:48:41 +0100 Subject: [PATCH 1/4] Empty mappings fix implemented. --- lib/source-map-generator.js | 11 ++++++++++ test/test-source-map-generator.js | 12 +++++++++++ test/util.js | 36 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/lib/source-map-generator.js b/lib/source-map-generator.js index 8fbb8e8b..859932a8 100644 --- a/lib/source-map-generator.js +++ b/lib/source-map-generator.js @@ -72,6 +72,17 @@ SourceMapGenerator.fromSourceMap = generator.addMapping(newMapping); }); aSourceMapConsumer.sources.forEach(function (sourceFile) { + if (aSourceMapConsumer._mappings === '') { + var sourceRelative = sourceFile; + if (sourceRoot !== null) { + sourceRelative = util.relative(sourceRoot, sourceFile); + } + + if (!generator._sources.has(sourceRelative)) { + generator._sources.add(sourceRelative); + } + } + var content = aSourceMapConsumer.sourceContentFor(sourceFile); if (content != null) { generator.setSourceContent(sourceFile, content); diff --git a/test/test-source-map-generator.js b/test/test-source-map-generator.js index 2d37daa2..79cb8fd9 100644 --- a/test/test-source-map-generator.js +++ b/test/test-source-map-generator.js @@ -252,6 +252,18 @@ exports['test .fromSourceMap with sourcesContent'] = function (assert) { util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); }; +exports['test .fromSourceMap with empty mappings'] = function (assert) { + var map = SourceMapGenerator.fromSourceMap( + new SourceMapConsumer(util.testMapEmptyMappings)); + util.assertEqualMaps(assert, map.toJSON(), util.testMapEmptyMappings); +}; + +exports['test .fromSourceMap with empty mappings and relative sources'] = function (assert) { + var map = SourceMapGenerator.fromSourceMap( + new SourceMapConsumer(util.testMapEmptyMappingsRelativeSources)); + util.assertEqualMaps(assert, map.toJSON(), util.testMapEmptyMappingsRelativeSources_generatedExpected); +}; + exports['test applySourceMap'] = function (assert) { var node = new SourceNode(null, null, null, [ new SourceNode(2, 0, 'fileX', 'lineX2\n'), diff --git a/test/util.js b/test/util.js index 7bb3ab17..4dfcdbba 100644 --- a/test/util.js +++ b/test/util.js @@ -51,6 +51,42 @@ exports.testMapEmptySourceRoot = { sourceRoot: '', mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA' }; +exports.testMapEmptyMappings = { + version: 3, + file: 'min.js', + names: [], + sources: ['one.js', 'two.js'], + sourcesContent: [ + ' ONE.foo = 1;', + ' TWO.inc = 2;' + ], + sourceRoot: '', + mappings: '' +}; +exports.testMapEmptyMappingsRelativeSources = { + version: 3, + file: 'min.js', + names: [], + sources: ['./one.js', './two.js'], + sourcesContent: [ + ' ONE.foo = 1;', + ' TWO.inc = 2;' + ], + sourceRoot: '/the/root', + mappings: '' +}; +exports.testMapEmptyMappingsRelativeSources_generatedExpected = { + version: 3, + file: 'min.js', + names: [], + sources: ['one.js', 'two.js'], + sourcesContent: [ + ' ONE.foo = 1;', + ' TWO.inc = 2;' + ], + sourceRoot: '/the/root', + mappings: '' +}; // This mapping is identical to above, but uses the indexed format instead. exports.indexedTestMap = { version: 3, From 4192bf444c5af0b9cd1f8e4309fa3aa967806efa Mon Sep 17 00:00:00 2001 From: Damir Murat Date: Sat, 19 Nov 2016 09:33:47 +0100 Subject: [PATCH 2/4] SourceMapGenerator.fromSourceMap() test case added for a map with single source. --- test/test-source-map-generator.js | 6 ++++++ test/util.js | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/test/test-source-map-generator.js b/test/test-source-map-generator.js index 79cb8fd9..45f9b547 100644 --- a/test/test-source-map-generator.js +++ b/test/test-source-map-generator.js @@ -252,6 +252,12 @@ exports['test .fromSourceMap with sourcesContent'] = function (assert) { util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent); }; +exports['test .fromSourceMap with single source'] = function (assert) { + var map = SourceMapGenerator.fromSourceMap( + new SourceMapConsumer(util.testMapSingleSource)); + util.assertEqualMaps(assert, map.toJSON(), util.testMapSingleSource); +}; + exports['test .fromSourceMap with empty mappings'] = function (assert) { var map = SourceMapGenerator.fromSourceMap( new SourceMapConsumer(util.testMapEmptyMappings)); diff --git a/test/util.js b/test/util.js index 4dfcdbba..1975b1d4 100644 --- a/test/util.js +++ b/test/util.js @@ -51,6 +51,14 @@ exports.testMapEmptySourceRoot = { sourceRoot: '', mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA' }; +exports.testMapSingleSource = { + version: 3, + file: 'min.js', + names: ['bar', 'baz'], + sources: ['one.js'], + sourceRoot: '', + mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID' +}; exports.testMapEmptyMappings = { version: 3, file: 'min.js', From 0f1607caeb2c42abf1ae9fcb74f3b2ee531af127 Mon Sep 17 00:00:00 2001 From: Damir Murat Date: Sat, 19 Nov 2016 09:36:45 +0100 Subject: [PATCH 3/4] .gitignore updated --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 8d87b1d2..ab5c793b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +*.iml +*.log +.idea node_modules/* From d08bc2f1aaa799159a9fc9f01a4df22486e1c206 Mon Sep 17 00:00:00 2001 From: Damir Murat Date: Wed, 23 Nov 2016 18:51:17 +0100 Subject: [PATCH 4/4] Implementing fix and test for case when we have multiple sources but mapping refers to the single source only. --- lib/source-map-generator.js | 14 ++++++-------- test/test-source-map-generator.js | 6 ++++++ test/util.js | 8 ++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/source-map-generator.js b/lib/source-map-generator.js index 859932a8..d08734a6 100644 --- a/lib/source-map-generator.js +++ b/lib/source-map-generator.js @@ -72,15 +72,13 @@ SourceMapGenerator.fromSourceMap = generator.addMapping(newMapping); }); aSourceMapConsumer.sources.forEach(function (sourceFile) { - if (aSourceMapConsumer._mappings === '') { - var sourceRelative = sourceFile; - if (sourceRoot !== null) { - sourceRelative = util.relative(sourceRoot, sourceFile); - } + var sourceRelative = sourceFile; + if (sourceRoot !== null) { + sourceRelative = util.relative(sourceRoot, sourceFile); + } - if (!generator._sources.has(sourceRelative)) { - generator._sources.add(sourceRelative); - } + if (!generator._sources.has(sourceRelative)) { + generator._sources.add(sourceRelative); } var content = aSourceMapConsumer.sourceContentFor(sourceFile); diff --git a/test/test-source-map-generator.js b/test/test-source-map-generator.js index 45f9b547..d05d9fe0 100644 --- a/test/test-source-map-generator.js +++ b/test/test-source-map-generator.js @@ -270,6 +270,12 @@ exports['test .fromSourceMap with empty mappings and relative sources'] = functi util.assertEqualMaps(assert, map.toJSON(), util.testMapEmptyMappingsRelativeSources_generatedExpected); }; +exports['test .fromSourceMap with multiple sources where mappings refers only to single source'] = function (assert) { + var map = SourceMapGenerator.fromSourceMap( + new SourceMapConsumer(util.testMapMultiSourcesMappingRefersSingleSourceOnly)); + util.assertEqualMaps(assert, map.toJSON(), util.testMapMultiSourcesMappingRefersSingleSourceOnly); +}; + exports['test applySourceMap'] = function (assert) { var node = new SourceNode(null, null, null, [ new SourceNode(2, 0, 'fileX', 'lineX2\n'), diff --git a/test/util.js b/test/util.js index 1975b1d4..74b0dfc1 100644 --- a/test/util.js +++ b/test/util.js @@ -95,6 +95,14 @@ exports.testMapEmptyMappingsRelativeSources_generatedExpected = { sourceRoot: '/the/root', mappings: '' }; +exports.testMapMultiSourcesMappingRefersSingleSourceOnly = { + version: 3, + file: 'min.js', + names: ['bar', 'baz'], + sources: ['one.js', 'withoutMappings.js'], + sourceRoot: '', + mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID' +}; // This mapping is identical to above, but uses the indexed format instead. exports.indexedTestMap = { version: 3,