extend `__PURE__` to `AST_New` (#2706)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 2 Jan 2018 20:48:07 +0000 (04:48 +0800)
committerGitHub <noreply@github.com>
Tue, 2 Jan 2018 20:48:07 +0000 (04:48 +0800)
fixes #2705

lib/parse.js
test/compress/pure_funcs.js

index 5eb7544..0345534 100644 (file)
@@ -1225,12 +1225,14 @@ function parse($TEXT, options) {
         } else {
             args = [];
         }
-        return subscripts(new AST_New({
+        var call = new AST_New({
             start      : start,
             expression : newexp,
             args       : args,
             end        : prev()
-        }), allow_calls);
+        });
+        mark_pure(call);
+        return subscripts(call, allow_calls);
     };
 
     function as_atom_node() {
index d15bcca..0df51e5 100644 (file)
@@ -414,3 +414,124 @@ issue_2638: {
         "/* */(a()||b())(c(),d());",
     ]
 }
+
+issue_2705_1: {
+    options = {
+        side_effects: true,
+    }
+    beautify = {
+        comments: "all",
+    }
+    input: {
+        /*@__PURE__*/ new a();
+        /*@__PURE__*/ (new b());
+        new (/*@__PURE__*/ c)();
+        (/*@__PURE__*/ new d());
+    }
+    expect_exact: [
+        "new/* */c;",
+    ]
+}
+
+issue_2705_2: {
+    options = {
+        side_effects: true,
+    }
+    beautify = {
+        comments: "all",
+    }
+    input: {
+        /*@__PURE__*/ new a(1)(2)(3);
+        /*@__PURE__*/ new (b(1))(2)(3);
+        /*@__PURE__*/ new (c(1)(2))(3);
+        /*@__PURE__*/ new (d(1)(2)(3));
+        new (/*@__PURE__*/ e)(1)(2)(3);
+        (/*@__PURE__*/ new f(1))(2)(3);
+        (/*@__PURE__*/ new g(1)(2))(3);
+        (/*@__PURE__*/ new h(1)(2)(3));
+    }
+    expect_exact: [
+        "new/* */e(1)(2)(3);",
+        "/* */new f(1)(2)(3);",
+        "/* */new g(1)(2)(3);",
+    ]
+}
+
+issue_2705_3: {
+    options = {
+        side_effects: true,
+    }
+    beautify = {
+        comments: "all",
+    }
+    input: {
+        /*@__PURE__*/ new a.x(1).y(2).z(3);
+        /*@__PURE__*/ new (b.x)(1).y(2).z(3);
+        /*@__PURE__*/ new (c.x(1)).y(2).z(3);
+        /*@__PURE__*/ new (d.x(1).y)(2).z(3);
+        /*@__PURE__*/ new (e.x(1).y(2)).z(3);
+        /*@__PURE__*/ new (f.x(1).y(2).z)(3);
+        /*@__PURE__*/ new (g.x(1).y(2).z(3));
+        new (/*@__PURE__*/ h).x(1).y(2).z(3);
+        /* */ new (/*@__PURE__*/ i.x)(1).y(2).z(3);
+        (/*@__PURE__*/ new j.x(1)).y(2).z(3);
+        (/*@__PURE__*/ new k.x(1).y)(2).z(3);
+        (/*@__PURE__*/ new l.x(1).y(2)).z(3);
+        (/*@__PURE__*/ new m.x(1).y(2).z)(3);
+        (/*@__PURE__*/ new n.x(1).y(2).z(3));
+    }
+    expect_exact: [
+        "new/* */h.x(1).y(2).z(3);",
+        "/* */new/* */i.x(1).y(2).z(3);",
+        "/* */new j.x(1).y(2).z(3);",
+        "/* */new k.x(1).y(2).z(3);",
+        "/* */new l.x(1).y(2).z(3);",
+        "/* */new m.x(1).y(2).z(3);",
+    ]
+}
+
+issue_2705_4: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        (/*@__PURE__*/ new x(), y());
+        (w(), /*@__PURE__*/ new x(), y());
+    }
+    expect: {
+        y();
+        w(), y();
+    }
+}
+
+issue_2705_5: {
+    options = {
+        side_effects: true,
+    }
+    input: {
+        [ /*@__PURE__*/ new x() ];
+        [ /*@__PURE__*/ new x(), y() ];
+        [ w(), /*@__PURE__*/ new x(), y() ];
+    }
+    expect: {
+        y();
+        w(), y();
+    }
+}
+
+issue_2705_6: {
+    options = {
+        side_effects: true,
+    }
+    beautify = {
+        comments: "all",
+    }
+    input: {
+        /*@__PURE__*/new (g() || h())(x(), y());
+        /* */ new (/*@__PURE__*/ (a() || b()))(c(), d());
+    }
+    expect_exact: [
+        "/* */x(),y();",
+        "/* */new(/* */a()||b())(c(),d());",
+    ]
+}