Fix compressed source-maps have non-terminated segments

Currently when UglifyJS compresses a given input file that comes
with a source-map, but the source-map does not have mappings for
all code parts in the input file. e.g. the input file has been generated
and some specific code-parts have been generated and are not
originating from any source-file.

In that case if mappings before the "generated code" are collected, and
the original file location for the "generated code" is determined after previous
mappings for the same line have been recorded, UglifyJS just ignores the fact that
there is no original location and the previous segment is not terminated. This causes
the "generated code" incorrectly to be mapped to the previous segment/
original source location.
This commit is contained in:
Paul Gschwendtner 2019-04-28 00:49:39 +02:00
parent 34075fc4c4
commit c15af086fd
5 changed files with 64 additions and 8 deletions

View File

@ -70,12 +70,27 @@ function SourceMap(options) {
return {
add: function(source, gen_line, gen_col, orig_line, orig_col, name) {
var map = maps && maps[source];
var generatedPos = {
line: gen_line + options.dest_line_diff,
column: gen_col
};
if (map) {
var info = map.originalPositionFor({
line: orig_line,
column: orig_col
});
if (info.source === null) return;
if (info.source === null) {
if (generatedPos.column !== 0) {
generator.
generator.addMapping({
generated: generatedPos,
original: null,
source: null,
name: null
});
}
return;
}
source = info.source;
orig_line = info.line;
orig_col = info.column;
@ -84,10 +99,7 @@ function SourceMap(options) {
generator.addMapping({
name: name,
source: source,
generated: {
line: gen_line + options.dest_line_diff,
column: gen_col
},
generated: generatedPos,
original: {
line: orig_line + options.orig_line_diff,
column: orig_col

View File

@ -1,2 +1,2 @@
function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}else{return Array.from(arr)}}var _require=require("bar"),foo=_require.foo;var _require2=require("world"),hello=_require2.hello;foo.x.apply(foo,_toConsumableArray(foo.y(hello.z)));
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImlucHV0Mi5qcyJdLCJuYW1lcyI6WyJyZXF1aXJlIiwiYXJyIl0sIm1hcHBpbmdzIjoiMEpBQWNBLEtBQVFDIiwic291cmNlc0NvbnRlbnQiOlsiY29uc3Qge2Zvb30gPSByZXF1aXJlKFwiYmFyXCIpO1xuY29uc3Qge2hlbGxvfSA9IHJlcXVpcmUoXCJ3b3JsZFwiKTtcblxuZm9vLngoLi4uZm9vLnkoaGVsbG8ueikpO1xuIl19
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImlucHV0Mi5qcyJdLCJuYW1lcyI6WyJfcmVxdWlyZSIsInJlcXVpcmUiLCJmb28iLCJfcmVxdWlyZTIiLCJoZWxsbyIsIngiLCJhcHBseSIsIl90b0NvbnN1bWFibGVBcnJheSIsInkiLCJ6Il0sIm1hcHBpbmdzIjoicUtBQUEsSUFBY0EsU0FBQUMsUUFBQSxPQUFQQyxJQUFBRixTQUFBRSxJQUNQLElBQWdCQyxVQUFBRixRQUFBLFNBQVRHLE1BQUFELFVBQUFDLE1BRVBGLElBQUFHLEVBQUFDLE1BQUFKLElBQU1LLG1CQUFHTCxJQUFBTSxFQUFBSixNQUFBSyIsInNvdXJjZXNDb250ZW50IjpbImNvbnN0IHtmb299ID0gcmVxdWlyZShcImJhclwiKTtcbmNvbnN0IHtoZWxsb30gPSByZXF1aXJlKFwid29ybGRcIik7XG5cbmZvby54KC4uLmZvby55KGhlbGxvLnopKTtcbiJdfQ==

View File

@ -8,4 +8,4 @@ var _require2 = require("world"),
foo.x.apply(foo, _toConsumableArray(foo.y(hello.z)));
//# sourceMappingURL=input.js.map
//# sourceMappingURL=input.js.map

View File

@ -1 +1 @@
{"version":3,"sources":["input2.js"],"names":["require","foo","hello","x","apply","_toConsumableArray","y","z"],"mappings":"kLAAcA,QAAQ,OAAfC,aAAAA,kBACSD,QAAQ,SAAjBE,gBAAAA,MAEPD,IAAIE,EAAJC,MAAAH,IAAAI,mBAASJ,IAAIK,EAAEJ,MAAMK","sourcesContent":["const {foo} = require(\"bar\");\nconst {hello} = require(\"world\");\n\nfoo.x(...foo.y(hello.z));\n"]}
{"version":3,"sources":["input2.js"],"names":[],"mappings":";;AAAA,IAAc;CAAP;;AACP,IAAgB;CAAT;;AAEP,iBAAM,mBAAG","sourcesContent":["const {foo} = require(\"bar\");\nconst {hello} = require(\"world\");\n\nfoo.x(...foo.y(hello.z));\n"]}

View File

@ -1,6 +1,7 @@
var assert = require("assert");
var readFileSync = require("fs").readFileSync;
var SourceMapConsumer = require("source-map").SourceMapConsumer;
var SourceMapGenerator = require("source-map").SourceMapGenerator;
var UglifyJS = require("../..");
function read(path) {
@ -284,5 +285,48 @@ describe("sourcemaps", function() {
assert.ok(pos.line <= 2, msg);
});
});
it("Should preserve unmapped segments in output source map", function() {
var generator = new SourceMapGenerator();
generator.addMapping({
source: "source.ts",
generated: {line: 1, column: 0},
original: {line: 1, column: 0},
});
generator.addMapping({
source: null,
original: null,
generated: {line: 1, column: 38}
});
generator.addMapping({
source: "source.ts",
generated: {line: 1, column: 51},
original: {line: 2, column: 0},
});
// Everything except the "say('hello');" part is mapped to "source.ts". The "say"
// function call is not mapped to any original source location. e.g. this can
// happen when a code transformer inserts generated code in between existing code.
var inputFile = "function say(msg) {console.log(msg)};say('hello');process.exit(1);";
var result = UglifyJS.minify(inputFile, {
sourceMap: {
content: JSON.parse(generator.toString())
}
});
var transformedMap = new SourceMapConsumer(result.map);
var hasMappedSource = true;
for (let i = 0; i < result.code.length; i++) {
var info = transformedMap.originalPositionFor({line: 1, column: i});
hasMappedSource = hasMappedSource && !!info.source;
}
assert.equal(hasMappedSource, false, "Expected transformed source map to preserve the " +
"mapping without original source location");
});
});
});