prevent input source map mutation (#3781)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 15 Apr 2020 09:25:58 +0000 (10:25 +0100)
committerGitHub <noreply@github.com>
Wed, 15 Apr 2020 09:25:58 +0000 (17:25 +0800)
fixes #3780

lib/sourcemap.js
test/mocha/sourcemaps.js

index 232cfab..25fbe3b 100644 (file)
@@ -103,12 +103,16 @@ function SourceMap(options) {
     if (options.orig) Object.keys(options.orig).forEach(function(name) {
         var map = options.orig[name];
         var indices = [ 0, 0, 1, 0, 0 ];
-        map.mappings = map.mappings.split(/;/).map(function(line) {
-            indices[0] = 0;
-            return line.split(/,/).map(function(segment) {
-                return indices.slice(0, vlq_decode(indices, segment));
-            });
-        });
+        options.orig[name] = {
+            names: map.names,
+            mappings: map.mappings.split(/;/).map(function(line) {
+                indices[0] = 0;
+                return line.split(/,/).map(function(segment) {
+                    return indices.slice(0, vlq_decode(indices, segment));
+                });
+            }),
+            sources: map.sources,
+        };
         if (!sources_content || !map.sourcesContent) return;
         for (var i = 0; i < map.sources.length; i++) {
             var content = map.sourcesContent[i];
index f8e399c..23698c5 100644 (file)
@@ -277,6 +277,12 @@ describe("sourcemaps", function() {
     });
 
     describe("input sourcemaps", function() {
+        it("Should not modify input source map", function() {
+            var orig = get_map();
+            var original = JSON.stringify(orig);
+            var map = prepare_map(orig);
+            assert.strictEqual(JSON.stringify(orig), original);
+        });
         it("Should copy over original sourcesContent", function() {
             var orig = get_map();
             var map = prepare_map(orig);