From ae9f56be109ccc27003868e997143823957f874d Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 15 Jun 2020 01:28:44 +0800 Subject: [PATCH] fix corner case in `evaluate` (#3998) fixes #3997 --- lib/compress.js | 16 ++++++++-------- test/compress/evaluate.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index cf286187..b312c64d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3295,11 +3295,8 @@ merge(Compressor.prototype, { if (node instanceof AST_Unary && unary_arithmetic[node.operator]) modified(node.expression); }); function modified(node) { - if (node instanceof AST_Dot) { - modified(node.expression); - } else if (node instanceof AST_Sub) { + if (node instanceof AST_PropAccess) { modified(node.expression); - node.property.walk(scan_modified); } else if (node instanceof AST_SymbolRef) { node.definition().references.forEach(function(ref) { delete ref._eval; @@ -3329,14 +3326,17 @@ merge(Compressor.prototype, { } var op = this.operator; var node; - if (HOP(lhs, "_eval") || !(lhs instanceof AST_SymbolRef) || !lhs.fixed || !lhs.definition().fixed) { - node = op == "=" ? this.right : make_node(AST_Binary, this, { + if (!HOP(lhs, "_eval") && lhs instanceof AST_SymbolRef && lhs.fixed && lhs.definition().fixed) { + node = lhs; + } else if (op == "=") { + lhs.walk(scan_modified); + node = this.right; + } else { + node = make_node(AST_Binary, this, { operator: op.slice(0, -1), left: lhs, right: this.right, }); - } else { - node = lhs; } var value = node._eval(compressor, ignore_side_effects, cached, depth); if (value === node) return this; diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index 4fecd701..dd94f613 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -2812,3 +2812,24 @@ operator_in: { "true", ] } + +issue_3997: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + var a = function f(b) { + return b[b += this] = b; + }(0); + console.log(typeof a); + } + expect: { + var a = function f(b) { + return b[b += this] = b; + }(0); + console.log(typeof a); + } + expect_stdout: "string" +} -- 2.34.1