From 76803d6b45421699e201fbc4ee3173af1b1033df Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L." Date: Thu, 1 Aug 2024 22:06:08 +0300 Subject: [PATCH] enhance `unsafe` (#5903) --- lib/compress.js | 1 + test/compress/comparisons.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index c78c7ab92c..22a3001ff3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12642,6 +12642,7 @@ Compressor.prototype.compress = function(node) { } function is_indexFn(node) { + while (node instanceof AST_Assign && node.operator == "=") node = node.right; return node.TYPE == "Call" && node.expression instanceof AST_Dot && indexFns[node.expression.property]; diff --git a/test/compress/comparisons.js b/test/compress/comparisons.js index 9faf34437c..227c787850 100644 --- a/test/compress/comparisons.js +++ b/test/compress/comparisons.js @@ -471,6 +471,28 @@ unsafe_indexOf: { ] } +unsafe_indexOf_assignment: { + options = { + booleans: true, + comparisons: true, + unsafe: true, + } + input: { + var a; + if ((a = Object.keys({ foo: 42 }).indexOf("bar")) < 0) console.log("PASS"); + if (0 > (a = Object.keys({ foo: 42 }).indexOf("bar"))) console.log("PASS"); + } + expect: { + var a; + if (!~(a = Object.keys({ foo: 42 }).indexOf("bar"))) console.log("PASS"); + if (!~(a = Object.keys({ foo: 42 }).indexOf("bar"))) console.log("PASS"); + } + expect_stdout: [ + "PASS", + "PASS", + ] +} + issue_3413: { options = { comparisons: true,