From dd90135944fb3f980b8ff2fcbef9222261d05cd8 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 13 Aug 2022 19:54:06 +0100 Subject: [PATCH] fix corner case in `pure_getters` (#5617) --- lib/compress.js | 10 ++++++-- test/compress/classes.js | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index a01dcfc1..f408195e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4469,7 +4469,13 @@ Compressor.prototype.compress = function(node) { def(AST_Binary, function(compressor) { return lazy_op[this.operator] && (this.left._dot_throw(compressor) || this.right._dot_throw(compressor)); }); - def(AST_Class, return_false); + def(AST_Class, function(compressor, force) { + return is_strict(compressor, force) && !all(this.properties, function(prop) { + if (prop.private) return true; + if (!prop.static) return true; + return !(prop instanceof AST_ClassGetter || prop instanceof AST_ClassSetter); + }); + }); def(AST_Conditional, function(compressor) { return this.consequent._dot_throw(compressor) || this.alternative._dot_throw(compressor); }); @@ -4484,7 +4490,7 @@ Compressor.prototype.compress = function(node) { def(AST_Null, return_true); def(AST_Object, function(compressor, force) { return is_strict(compressor, force) && !all(this.properties, function(prop) { - if (!(prop instanceof AST_ObjectKeyVal)) return false; + if (prop instanceof AST_ObjectGetter || prop instanceof AST_ObjectSetter) return false; return !(prop.key === "__proto__" && prop.value._dot_throw(compressor, force)); }); }); diff --git a/test/compress/classes.js b/test/compress/classes.js index fe49b56c..fbdaa427 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -746,6 +746,56 @@ separate_name: { node_version: ">=4" } +static_getter: { + options = { + pure_getters: "strict", + side_effects: true, + } + input: { + "use strict"; + (class { + static get p() { + console.log("PASS"); + }; + }).p; + } + expect: { + "use strict"; + (class { + static get p() { + console.log("PASS"); + }; + }).p; + } + expect_stdout: "PASS" + node_version: ">=4" +} + +static_setter: { + options = { + pure_getters: "strict", + side_effects: true, + } + input: { + "use strict"; + (class { + static set p(v) { + console.log(v); + }; + }).p = "PASS"; + } + expect: { + "use strict"; + (class { + static set p(v) { + console.log(v); + }; + }).p = "PASS"; + } + expect_stdout: "PASS" + node_version: ">=4" +} + static_side_effects: { options = { inline: true,