drop lone "use strict" in function body (#2963)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 26 Feb 2018 07:22:52 +0000 (15:22 +0800)
committerGitHub <noreply@github.com>
Mon, 26 Feb 2018 07:22:52 +0000 (15:22 +0800)
fixes #2961

lib/compress.js
test/compress/functions.js

index c402e3b..f9382f0 100644 (file)
@@ -3024,6 +3024,16 @@ merge(Compressor.prototype, {
         return self;
     });
 
+    OPT(AST_Lambda, function(self, compressor){
+        tighten_body(self.body, compressor);
+        if (compressor.option("side_effects")
+            && self.body.length == 1
+            && self.body[0] === compressor.has_directive("use strict")) {
+            self.body.length = 0;
+        }
+        return self;
+    });
+
     AST_Scope.DEFMETHOD("drop_unused", function(compressor){
         if (!compressor.option("unused")) return;
         if (compressor.has_directive("use asm")) return;
index 5d0be0f..ddf5fe0 100644 (file)
@@ -2022,3 +2022,32 @@ deduplicate_parenthesis: {
     }
     expect_exact: "({}).a=b;({}.a=b)();(function(){}).a=b;(function(){}.a=b)();"
 }
+
+drop_lone_use_strict: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        function f1() {
+            "use strict";
+        }
+        function f2() {
+            "use strict";
+            function f3() {
+                "use strict";
+            }
+        }
+        (function f4() {
+            "use strict";
+        })();
+    }
+    expect: {
+        function f1() {
+        }
+        function f2() {
+            "use strict";
+            function f3() {
+            }
+        }
+    }
+}