suppress invalid AST transform in `--reduce-test` (#4498)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 3 Jan 2021 02:34:46 +0000 (02:34 +0000)
committerGitHub <noreply@github.com>
Sun, 3 Jan 2021 02:34:46 +0000 (10:34 +0800)
test/input/reduce/destructured_assign.js [new file with mode: 0644]
test/input/reduce/destructured_assign.reduced.js [new file with mode: 0644]
test/mocha/reduce.js
test/reduce.js

diff --git a/test/input/reduce/destructured_assign.js b/test/input/reduce/destructured_assign.js
new file mode 100644 (file)
index 0000000..26e1580
--- /dev/null
@@ -0,0 +1,3 @@
+var o = {};
+[ o[1 + .1 + .1] ] = [ 42 ];
+console.log(o[1.2]);
diff --git a/test/input/reduce/destructured_assign.reduced.js b/test/input/reduce/destructured_assign.reduced.js
new file mode 100644 (file)
index 0000000..887810d
--- /dev/null
@@ -0,0 +1,17 @@
+// (beautified)
+var o = {};
+
+[ o[1 + .1 + .1] ] = [];
+
+console.log(o);
+// output: { '1.2000000000000002': undefined }
+// 
+// minify: { '1.2': undefined }
+// 
+// options: {
+//   "compress": {
+//     "unsafe_math": true
+//   },
+//   "mangle": false,
+//   "validate": true
+// }
\ No newline at end of file
index ce931fe..e365331 100644 (file)
@@ -307,6 +307,18 @@ describe("test/reduce.js", function() {
         if (result.error) throw result.error;
         assert.strictEqual(result.code, read("test/input/reduce/diff_error.reduced.js"));
     });
+    it("Should maintain valid LHS in destructuring assignments", function() {
+        if (semver.satisfies(process.version, "<6")) return;
+        var result = reduce_test(read("test/input/reduce/destructured_assign.js"), {
+            compress: {
+                unsafe_math: true,
+            },
+            mangle: false,
+            validate: true,
+        });
+        if (result.error) throw result.error;
+        assert.strictEqual(result.code, read("test/input/reduce/destructured_assign.reduced.js"));
+    });
     it("Should handle destructured catch expressions", function() {
         if (semver.satisfies(process.version, "<6")) return;
         var result = reduce_test(read("test/input/reduce/destructured_catch.js"), {
index e45cfa5..54494c5 100644 (file)
@@ -341,7 +341,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
             else if (node instanceof U.AST_PropAccess) {
                 var expr = [
                     node.expression,
-                    node.property instanceof U.AST_Node && node.property,
+                    node.property instanceof U.AST_Node && !(parent instanceof U.AST_Destructured) && node.property,
                 ][ node.start._permute++ % 2 ];
                 if (expr) {
                     CHANGED = true;