From e2b00814a8b23ce9c03260b8a99817593bdd30d0 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 3 Mar 2022 20:05:31 +0000 Subject: [PATCH] fix corner cases in `inline` (#5375) --- lib/compress.js | 4 +++- test/compress/functions.js | 42 ++++++++++++++++++++++++++++++++++++ test/compress/reduce_vars.js | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index cbc0d48c..42aae488 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10524,7 +10524,7 @@ Compressor.prototype.compress = function(node) { insert = scope.body.indexOf(child) + 1; if (!insert) return false; if (!safe_from_await_yield(fn, avoid_await_yield(scope))) return false; - var safe_to_inject = exp !== fn || fn.parent_scope.resolve() === scope; + var safe_to_inject = (exp !== fn || fn.parent_scope.resolve() === scope) && !scope.pinned(); if (scope instanceof AST_Toplevel) { if (compressor.toplevel.vars) { defined.set("arguments", true); @@ -13252,6 +13252,7 @@ Compressor.prototype.compress = function(node) { if (!(fn instanceof AST_LambdaExpression)) return; if (fn.name) return; if (fn.uses_arguments) return; + if (fn.pinned()) return; if (is_generator(fn)) return; var arrow = is_arrow(fn); if (arrow && fn.value) return; @@ -13267,6 +13268,7 @@ Compressor.prototype.compress = function(node) { scope = scope.parent_scope; } if (!member(scope, compressor.stack)) return; + if (scope.pinned() && fn.variables.size() > (arrow ? 0 : 1)) return; if (scope instanceof AST_Toplevel) { if (fn.variables.size() > (arrow ? 0 : 1)) { if (!compressor.toplevel.vars) return; diff --git a/test/compress/functions.js b/test/compress/functions.js index 5204df66..2b393ba1 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1508,6 +1508,48 @@ unsafe_call_3: { expect_stdout: "3" } +inline_eval_inner: { + options = { + inline: true, + } + input: { + (function() { + console.log(typeof eval("arguments")); + })(); + } + expect: { + (function() { + console.log(typeof eval("arguments")); + })(); + } + expect_stdout: "object" +} + +inline_eval_outer: { + options = { + inline: true, + toplevel: true, + } + input: { + A = 42; + (function(a) { + console.log(a); + })(A); + console.log(eval("typeof a")); + } + expect: { + A = 42; + (function(a) { + console.log(a); + })(A); + console.log(eval("typeof a")); + } + expect_stdout: [ + "42", + "undefined", + ] +} + issue_2616: { options = { evaluate: true, diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 99cbdfe2..c2bd380a 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -6,6 +6,7 @@ reduce_vars: { C: 0, }, inline: true, + passes: 2, reduce_funcs: true, reduce_vars: true, toplevel: true,