Skip to content

Commit

Permalink
improve source mapping accuracy (#5608)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Aug 8, 2022
1 parent f451a7a commit 2c3c4ec
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 198 deletions.
272 changes: 120 additions & 152 deletions lib/compress.js

Large diffs are not rendered by default.

51 changes: 30 additions & 21 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,11 @@ function OutputStream(options) {
print(";");
}

function with_block(cont) {
function with_block(cont, end) {
print("{");
newline();
with_indent(cont);
add_mapping(end);
indent();
print("}");
}
Expand Down Expand Up @@ -953,7 +954,7 @@ function OutputStream(options) {
if (self.body.length > 0) {
output.with_block(function() {
display_body(self.body, false, output, allow_directives);
});
}, self.end);
} else print_braced_empty(self, output);
}
DEFPRINT(AST_BlockStatement, function(output) {
Expand Down Expand Up @@ -1088,7 +1089,7 @@ function OutputStream(options) {
print_entry(i);
}
output.newline();
});
}, self.end);
output.space();
output.print("from");
output.space();
Expand Down Expand Up @@ -1352,7 +1353,7 @@ function OutputStream(options) {
if (i < last && branch.body.length > 0)
output.newline();
});
});
}, self.end);
});
function print_branch_body(self, output) {
output.newline();
Expand Down Expand Up @@ -1472,14 +1473,12 @@ function OutputStream(options) {
output.print("/*@__PURE__*/");
}
function print_call_args(self, output) {
if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
output.add_mapping(self.start);
}
output.with_parens(function() {
self.args.forEach(function(expr, i) {
if (i) output.comma();
expr.print(output);
});
output.add_mapping(self.end);
});
}
DEFPRINT(AST_Call, function(output) {
Expand Down Expand Up @@ -1515,10 +1514,11 @@ function OutputStream(options) {
expr.print(output);
var prop = self.property;
if (output.option("ie") && RESERVED_WORDS[prop]) {
output.print(self.optional ? "?.[" : "[");
output.add_mapping(self.end);
output.print_string(prop);
output.print("]");
if (self.optional) output.print("?.");
output.with_square(function() {
output.add_mapping(self.end);
output.print_string(prop);
});
} else {
if (expr instanceof AST_Number && !/[ex.)]/i.test(output.last())) output.print(".");
output.print(self.optional ? "?." : ".");
Expand All @@ -1530,9 +1530,10 @@ function OutputStream(options) {
DEFPRINT(AST_Sub, function(output) {
var self = this;
self.expression.print(output);
output.print(self.optional ? "?.[" : "[");
self.property.print(output);
output.print("]");
if (self.optional) output.print("?.");
output.with_square(function() {
self.property.print(output);
});
});
DEFPRINT(AST_Spread, function(output) {
output.print("...");
Expand All @@ -1551,8 +1552,10 @@ function OutputStream(options) {
exp.print(output);
});
DEFPRINT(AST_UnaryPostfix, function(output) {
this.expression.print(output);
output.print(this.operator);
var self = this;
self.expression.print(output);
output.add_mapping(self.end);
output.print(self.operator);
});
DEFPRINT(AST_Binary, function(output) {
var self = this;
Expand Down Expand Up @@ -1645,7 +1648,8 @@ function OutputStream(options) {
value.print(output);
});
DEFPRINT(AST_DestructuredObject, function(output) {
var props = this.properties, len = props.length, rest = this.rest;
var self = this;
var props = self.properties, len = props.length, rest = self.rest;
if (len || rest) output.with_block(function() {
props.forEach(function(prop, i) {
if (i) {
Expand All @@ -1665,8 +1669,8 @@ function OutputStream(options) {
rest.print(output);
}
output.newline();
});
else print_braced_empty(this, output);
}, self.end);
else print_braced_empty(self, output);
});
function print_properties(self, output, no_comma) {
var props = self.properties;
Expand All @@ -1680,7 +1684,7 @@ function OutputStream(options) {
prop.print(output);
});
output.newline();
});
}, self.end);
else print_braced_empty(self, output);
}
DEFPRINT(AST_Object, function(output) {
Expand Down Expand Up @@ -1890,7 +1894,7 @@ function OutputStream(options) {
output.indent();
stmt.print(output);
output.newline();
});
}, stmt.end);
}

/* -----[ source map generators ]----- */
Expand All @@ -1913,22 +1917,27 @@ function OutputStream(options) {
// or if we should add even more.
DEFMAP([
AST_Array,
AST_Await,
AST_BlockStatement,
AST_Catch,
AST_Constant,
AST_Debugger,
AST_Definitions,
AST_Destructured,
AST_Directive,
AST_Finally,
AST_Jump,
AST_Lambda,
AST_New,
AST_Object,
AST_Spread,
AST_StatementWithBody,
AST_Symbol,
AST_Switch,
AST_SwitchBranch,
AST_Try,
AST_UnaryPrefix,
AST_Yield,
], function(output) {
output.add_mapping(this.start);
});
Expand Down
2 changes: 1 addition & 1 deletion test/input/issue-3040/expect.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/input/issue-3294/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/input/issue-505/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/input/issue-520/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions test/mocha/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("bin/uglifyjs", function() {
if (err) throw err;
assert.strictEqual(stdout, [
"var bar=function(){function foo(bar){return bar}return foo}();",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxHQUNYLENBRUEsT0FBT0MsR0FDVixFQUFFIn0=",
"",
].join("\n"));
done();
Expand Down Expand Up @@ -330,7 +330,7 @@ describe("bin/uglifyjs", function() {
if (err) throw err;
assert.strictEqual(stdout, [
"var bar=function(){function foo(bar){return bar}return foo}();",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxJQUdYLE9BQU9DLElBTEQifQ==",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxHQUNYLENBRUEsT0FBT0MsR0FDVixFQUFFIn0=",
"",
].join("\n"));
var stderrLines = stderr.split("\n");
Expand All @@ -350,7 +350,7 @@ describe("bin/uglifyjs", function() {
if (err) throw err;
assert.strictEqual(stdout, [
"var Foo=function Foo(){console.log(1+2)};new Foo;var bar=function(){function foo(bar){return bar}return foo}();",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwidGVzdC9pbnB1dC9pc3N1ZS0xMzIzL3NhbXBsZS5qcyJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFNBQUVBLE1BQWNDLFFBQVFDLElBQUksRUFBRSxJQUFPLElBQUlGLElDQW5ELElBQUlHLElBQU0sV0FDTixTQUFTQyxJQUFLRCxLQUNWLE9BQU9BLElBR1gsT0FBT0MsSUFMRCJ9",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwidGVzdC9pbnB1dC9pc3N1ZS0xMzIzL3NhbXBsZS5qcyJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFNBQUVBLE1BQWNDLFFBQVFDLElBQUksRUFBRSxDQUFDLENBQUUsRUFBSSxJQUFJRixJQ0FuRCxJQUFJRyxJQUFNLFdBQ04sU0FBU0MsSUFBS0QsS0FDVixPQUFPQSxHQUNYLENBRUEsT0FBT0MsR0FDVixFQUFFIn0=",
"",
].join("\n"));
var stderrLines = stderr.split("\n");
Expand Down Expand Up @@ -791,7 +791,7 @@ describe("bin/uglifyjs", function() {
if (err) throw err;
assert.strictEqual(stdout, [
'"use strict";var foo=function foo(x){return"foo "+x};console.log(foo("bar"));',
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbImZvbyIsIngiLCJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiYUFBQSxJQUFJQSxJQUFNLFNBQU5BLElBQU1DLEdBQUEsTUFBSyxPQUFTQSxHQUN4QkMsUUFBUUMsSUFBSUgsSUFBSSJ9",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbImZvbyIsIngiLCJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiYUFBQSxJQUFJQSxJQUFNLFNBQU5BLElBQU1DLEdBQUEsTUFBSyxPQUFTQSxDQUFkLEVBQ1ZDLFFBQVFDLElBQUlILElBQUksS0FBSixDQUFaIn0=",
""
].join("\n"));
done();
Expand All @@ -814,7 +814,7 @@ describe("bin/uglifyjs", function() {
if (err) throw err;
assert.strictEqual(stdout, [
'function foo(){return function(){console.log("PASS")}}foo()();',
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMjMxMC9pbnB1dC5qcyJdLCJuYW1lcyI6WyJmb28iLCJjb25zb2xlIiwibG9nIiwiZiJdLCJtYXBwaW5ncyI6IkFBQUEsU0FBU0EsTUFDTCxPQUFPLFdBQ0hDLFFBQVFDLElBQUksU0FLUkYsS0FDUkcifQ==",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMjMxMC9pbnB1dC5qcyJdLCJuYW1lcyI6WyJmb28iLCJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiQUFBQSxTQUFTQSxNQUNMLE9BQU8sV0FDSEMsUUFBUUMsSUFBSSxNQUFNLENBQ3RCLENBQ0osQ0FHWUYsSUFBSSxFQUNWIn0=",
""
].join("\n"));
done();
Expand Down
32 changes: 16 additions & 16 deletions test/mocha/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function source_map(code) {
function get_map() {
return {
"version": 3,
"sources": ["index.js"],
"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\"));"]
"sourcesContent": [ "let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));" ],
};
}

Expand All @@ -42,7 +42,7 @@ function prepare_map(sourceMap) {
sourceMap: {
content: sourceMap,
includeSources: true,
}
},
});
if (result.error) throw result.error;
return JSON.parse(result.map);
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("sourcemaps", function() {
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "class A{static P=42;set#q(s){}}");
assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["A","P","#q","v"],"mappings":"MAAMA,EACFC,SAAW,GACXC,MAAOC"}');
assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["A","P","#q","v"],"mappings":"MAAMA,EACFC,SAAW,GACXC,MAAOC,IACX"}');
});
it("Should mark array/object literals", function() {
var result = UglifyJS.minify([
Expand All @@ -124,7 +124,7 @@ describe("sourcemaps", function() {
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "({}).wat([]);");
assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["wat"],"mappings":"CAAU,IACNA,IAAI"}');
assert.strictEqual(result.map, '{"version":3,"sources":["0"],"names":["wat"],"mappings":"CAAU,IACNA,IAAI,EAAE"}');
});
it("Should give correct sourceRoot", function() {
var code = "console.log(42);";
Expand All @@ -135,7 +135,7 @@ describe("sourcemaps", function() {
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
assert.strictEqual(result.map, '{"version":3,"sourceRoot":"//foo.bar/","sources":["0"],"names":["console","log"],"mappings":"AAAAA,QAAQC,IAAI"}');
assert.strictEqual(result.map, '{"version":3,"sourceRoot":"//foo.bar/","sources":["0"],"names":["console","log"],"mappings":"AAAAA,QAAQC,IAAI,EAAE"}');
});
it("Should produce same source map with DOS or UNIX line endings", function() {
var code = [
Expand All @@ -160,8 +160,8 @@ describe("sourcemaps", function() {
sourceMap: {
content: read("test/input/issue-1236/simple.js.map"),
filename: "simple.min.js",
includeSources: true
}
includeSources: true,
},
});
if (result.error) throw result.error;
var map = JSON.parse(result.map);
Expand All @@ -175,8 +175,8 @@ describe("sourcemaps", function() {
sourceMap: {
content: "inline",
includeSources: true,
url: "inline"
}
url: "inline",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code + "\n", read("test/input/issue-520/output.js"));
Expand All @@ -185,7 +185,7 @@ describe("sourcemaps", function() {
var result = UglifyJS.minify(read("test/input/issue-1323/sample.js"), {
mangle: false,
sourceMap: {
content: "inline"
content: "inline",
},
warnings: true,
});
Expand All @@ -206,7 +206,7 @@ describe("sourcemaps", function() {
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"var Foo=function(){console.log(3)},bar=(new Foo,function(o){return o});",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwiMSJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFdBQWdCQyxRQUFRQyxJQUFJLElDQWxDQyxLREEyQyxJQUFJSCxJQ0MvQyxTQUFjRyxHQUNWLE9BQU9BIn0=",
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIiwiMSJdLCJuYW1lcyI6WyJGb28iLCJjb25zb2xlIiwibG9nIiwiYmFyIl0sIm1hcHBpbmdzIjoiQUFBQSxJQUFNQSxJQUFJLFdBQWdCQyxRQUFRQyxJQUFJLENBQUcsQ0FBRSxFQ0F2Q0MsS0RBMkMsSUFBSUgsSUNDL0MsU0FBY0csR0FDVixPQUFPQSxDQUNYIn0=",
].join("\n"));
assert.deepEqual(result.warnings, [ "WARN: inline source map not found: 1" ]);
});
Expand Down Expand Up @@ -239,8 +239,8 @@ describe("sourcemaps", function() {
sourceMap: {
content: "inline",
includeSources: true,
url: "inline"
}
url: "inline",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code + "\n", read("test/input/issue-3294/output.js"));
Expand All @@ -255,7 +255,7 @@ describe("sourcemaps", function() {
});
if (result.error) throw result.error;
assert.strictEqual(result.code, '(function(){console.log("hello")}).call(this);');
assert.strictEqual(result.map, '{"version":3,"sources":["main.coffee"],"names":["console","log"],"mappings":"CAAA,WAAAA,QAAQC,IAAI"}');
assert.strictEqual(result.map, '{"version":3,"sources":["main.coffee"],"names":["console","log"],"mappings":"CAAA,WAAAA,QAAQC,IAAI,OAAZ"}');
});
it("Should not overwrite existing sourcesContent", function() {
var result = UglifyJS.minify({
Expand Down Expand Up @@ -302,7 +302,7 @@ describe("sourcemaps", function() {
if (result.error) throw result.error;
var code = result.code;
assert.strictEqual(code, "var a=function(n){return n};\n" +
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0=");
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BLENBQUsifQ==");
});
it("Should not append source map to output js when sourceMapInline is not enabled", function() {
var result = UglifyJS.minify('var a = function(foo) { return foo; };');
Expand Down

0 comments on commit 2c3c4ec

Please sign in to comment.