aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-10-03 11:27:17 +0100
committerGitHub <noreply@github.com>2020-10-03 18:27:17 +0800
commit142bd1bd1a77c0893e7b88268ef1a4d4f1be13d0 (patch)
tree0597b1cbf48a89b518033903cae3e87e7edbb491
parent8cb509d50e8bbc4c79d9bd0274a625f5ec37fe42 (diff)
downloadtracifyjs-142bd1bd1a77c0893e7b88268ef1a4d4f1be13d0.tar.gz
tracifyjs-142bd1bd1a77c0893e7b88268ef1a4d4f1be13d0.zip
workaround quirks on latter specs (#4172)
closes #4171
-rw-r--r--lib/compress.js11
-rw-r--r--test/compress/functions.js68
2 files changed, 77 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 513528e7..a7e5eaca 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4851,8 +4851,8 @@ merge(Compressor.prototype, {
&& (old_def.name == def.name.name || all(old_def.references, function(ref) {
return ref.scope.find_variable(def.name) === def.name.definition();
})))
- && can_rename(def.value, def.name.name)
- && (!compressor.has_directive("use strict") || parent instanceof AST_Scope)) {
+ && can_declare_defun()
+ && can_rename(def.value, def.name.name)) {
AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
var defun = make_node(AST_Defun, def, def.value);
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
@@ -4908,6 +4908,13 @@ merge(Compressor.prototype, {
var def = fn.variables.get(name);
return !def || fn.name && def === fn.name.definition();
}
+
+ function can_declare_defun() {
+ if (compressor.has_directive("use strict")) return parent instanceof AST_Scope;
+ return parent instanceof AST_Block
+ || parent instanceof AST_For && parent.init === node
+ || parent instanceof AST_If;
+ }
});
switch (head.length) {
case 0:
diff --git a/test/compress/functions.js b/test/compress/functions.js
index 13368169..745de9db 100644
--- a/test/compress/functions.js
+++ b/test/compress/functions.js
@@ -4892,3 +4892,71 @@ direct_inline_catch_redefined: {
}
expect_stdout: true
}
+
+issue_4171_1: {
+ options = {
+ functions: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ console.log(function(a) {
+ try {
+ while (a)
+ var e = function() {};
+ } catch (e) {
+ return function() {
+ return e;
+ };
+ }
+ }(!console));
+ }
+ expect: {
+ console.log(function(a) {
+ try {
+ while (a)
+ var e = function() {};
+ } catch (e) {
+ return function() {
+ return e;
+ };
+ }
+ }(!console));
+ }
+ expect_stdout: "undefined"
+}
+
+issue_4171_2: {
+ options = {
+ functions: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ console.log(function(a) {
+ try {
+ while (a);
+ } catch (e) {
+ return function() {
+ return e;
+ };
+ } finally {
+ var e = function() {};
+ }
+ }(!console));
+ }
+ expect: {
+ console.log(function(a) {
+ try {
+ while (a);
+ } catch (e) {
+ return function() {
+ return e;
+ };
+ } finally {
+ function e() {}
+ }
+ }(!console));
+ }
+ expect_stdout: "undefined"
+}