[Pkg-javascript-commits] [uglifyjs] 05/49: lib/sourcemap.js: Copy sourceContent from old souce-map to the new source-map. Should fix #882

Jonas Smedegaard dr at jones.dk
Fri Dec 9 11:43:24 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 41a9329409ca0f1273b6ccdcc9770713f44ec55a
Author: Lauri Pokka <larpo at iki.fi>
Date:   Tue Jul 5 02:06:14 2016 +0900

    lib/sourcemap.js: Copy sourceContent from old souce-map to the new source-map. Should fix #882
---
 lib/sourcemap.js               | 10 ++++++++++
 test/mocha/input-sourcemaps.js | 43 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/lib/sourcemap.js b/lib/sourcemap.js
index e5d7df6..3714027 100644
--- a/lib/sourcemap.js
+++ b/lib/sourcemap.js
@@ -58,6 +58,16 @@ function SourceMap(options) {
         sourceRoot : options.root
     });
     var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig);
+
+    if (orig_map && Array.isArray(options.orig.sources)) {
+        options.orig.sources.forEach(function(source) {
+            var sourceContent = orig_map.sourceContentFor(source, true);
+            if (sourceContent) {
+                generator.setSourceContent(source, sourceContent);
+            }
+        });
+    }
+
     function add(source, gen_line, gen_col, orig_line, orig_col, name) {
         if (orig_map) {
             var info = orig_map.originalPositionFor({
diff --git a/test/mocha/input-sourcemaps.js b/test/mocha/input-sourcemaps.js
new file mode 100644
index 0000000..30ee5b9
--- /dev/null
+++ b/test/mocha/input-sourcemaps.js
@@ -0,0 +1,43 @@
+var Uglify = require('../../');
+var assert = require("assert");
+var SourceMapConsumer = require("source-map").SourceMapConsumer;
+
+describe("input sourcemaps", function() {
+    var transpiled = '"use strict";\n\n' +
+        'var foo = function foo(x) {\n  return "foo " + x;\n};\n' +
+        'console.log(foo("bar"));\n\n' +
+        '//# sourceMappingURL=bundle.js.map';
+
+    var transpilemap = {
+        "version": 3,
+        "sources": ["index.js"],
+        "names": [],
+        "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
+        "file": "bundle.js",
+        "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
+    };
+
+    var result = Uglify.minify(transpiled, {
+        fromString: true,
+        inSourceMap: transpilemap,
+        outSourceMap: true
+    });
+    var map = new SourceMapConsumer(result.map);
+
+    it("Should copy over original sourcesContent", function() {
+        assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
+    });
+
+    it("Final sourcemap should not have invalid mappings from inputSourceMap (issue #882) ", function() {
+        // The original source has only 2 lines, check that mappings don't have more lines
+
+        var msg = "Mapping should not have higher line number than the original file had";
+        map.eachMapping(function(mapping) {
+            assert.ok(mapping.originalLine <= 2, msg)
+        });
+
+        map.allGeneratedPositionsFor({source: "index.js", line: 1, column: 1}).forEach(function(pos) {
+            assert.ok(pos.line <= 2, msg);
+        })
+    });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list