improve source mapping accuracy (#5608)

This commit is contained in:
Alex Lam S.L
2022-08-08 19:02:43 +01:00
committed by GitHub
parent f451a7ad79
commit 2c3c4ec323
8 changed files with 175 additions and 198 deletions

View File

@@ -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("}");
}
@@ -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) {
@@ -1088,7 +1089,7 @@ function OutputStream(options) {
print_entry(i);
}
output.newline();
});
}, self.end);
output.space();
output.print("from");
output.space();
@@ -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();
@@ -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) {
@@ -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 ? "?." : ".");
@@ -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("...");
@@ -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;
@@ -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) {
@@ -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;
@@ -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) {
@@ -1890,7 +1894,7 @@ function OutputStream(options) {
output.indent();
stmt.print(output);
output.newline();
});
}, stmt.end);
}
/* -----[ source map generators ]----- */
@@ -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);
});