improve compatibility with use strict (#5440)

This commit is contained in:
Alex Lam S.L
2022-05-14 05:15:54 +01:00
committed by GitHub
parent 8946c87011
commit e31bbe329a
6 changed files with 441 additions and 37 deletions

View File

@@ -2552,8 +2552,7 @@ Compressor.prototype.compress = function(node) {
&& all(iife.args, function(arg) {
return !(arg instanceof AST_Spread);
})) {
var fn_strict = compressor.has_directive("use strict");
if (fn_strict && !member(fn_strict, fn.body)) fn_strict = false;
var fn_strict = fn.in_strict_mode() && !fn.parent_scope.resolve(true).in_strict_mode();
var has_await = is_async(fn) ? function(node) {
return node instanceof AST_Symbol && node.name == "await";
} : function(node) {
@@ -4120,6 +4119,25 @@ Compressor.prototype.compress = function(node) {
&& !(compressor && node.expression.has_side_effects(compressor));
}
// in_strict_mode()
// return true if scope executes in Strict Mode
(function(def) {
def(AST_Class, return_this);
def(AST_Scope, function() {
var body = this.body;
for (var i = 0; i < body.length; i++) {
var stat = body[i];
if (!(stat instanceof AST_Directive)) break;
if (stat.value == "use strict") return true;
}
var parent = this.parent_scope;
if (!parent) return false;
return parent.resolve(true).in_strict_mode();
});
})(function(node, func) {
node.DEFMETHOD("in_strict_mode", func);
});
// is_truthy()
// return true if `!!node === true`
(function(def) {
@@ -9882,6 +9900,10 @@ Compressor.prototype.compress = function(node) {
return safe;
}
function safe_from_strict_mode(fn, compressor) {
return fn.in_strict_mode() || !compressor.has_directive("use strict");
}
OPT(AST_Call, function(self, compressor) {
var exp = self.expression;
var terminated = trim_optional_chain(self, compressor);
@@ -10206,7 +10228,10 @@ Compressor.prototype.compress = function(node) {
}
return true;
}) && !(fn.rest instanceof AST_Destructured && has_arg_refs(fn, fn.rest));
var can_inline = can_drop && compressor.option("inline") && !self.is_expr_pure(compressor);
var can_inline = can_drop
&& compressor.option("inline")
&& !self.is_expr_pure(compressor)
&& (exp === fn || safe_from_strict_mode(fn, compressor));
if (can_inline && stat instanceof AST_Return) {
var value = stat.value;
if (exp === fn
@@ -11813,8 +11838,9 @@ Compressor.prototype.compress = function(node) {
} else if (fixed.name && fixed.name.definition() !== def) {
single_use = false;
} else if (fixed.parent_scope !== self.scope || is_funarg(def)) {
single_use = fixed.is_constant_expression(self.scope);
if (single_use == "f") {
if (!safe_from_strict_mode(fixed, compressor)) {
single_use = false;
} else if ((single_use = fixed.is_constant_expression(self.scope)) == "f") {
var scope = self.scope;
do {
if (scope instanceof AST_LambdaDefinition || scope instanceof AST_LambdaExpression) {