From c15af086fd9aeebe48c9742d7e3d26d3923d095e Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sun, 28 Apr 2019 00:49:39 +0200 Subject: [PATCH] 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. --- lib/sourcemap.js | 22 +++++++++++++---- test/input/pr-3040/expect.js | 2 +- test/input/pr-3040/input.js | 2 +- test/input/pr-3040/input.js.map | 2 +- test/mocha/sourcemaps.js | 44 +++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/lib/sourcemap.js b/lib/sourcemap.js index 2feec45d..72c4709a 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -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 diff --git a/test/input/pr-3040/expect.js b/test/input/pr-3040/expect.js index b911f3f8..1b8674eb 100644 --- a/test/input/pr-3040/expect.js +++ b/test/input/pr-3040/expect.js @@ -1,2 +1,2 @@ function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i