Debian Codegraph @

test/com/google/javascript/jscomp/ExploitAssignsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2006 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Unit tests for {@link ExploitAssigns}
21 *
22 * @author [email protected] (Nick Santos)
23 * @author [email protected] (Alan Leung)
24 */
25 public class ExploitAssignsTest extends CompilerTestCase {
26
27 public void testExprExploitationTypes() {
28 test("a = true; b = true",
29 "b = a = true");
30 test("a = !0; b = !0",
31 "b = a = !0");
32 test("a = !1; b = !1",
33 "b = a = !1");
34 test("a = void 0; b = void 0",
35 "b = a = void 0");
36 test("a = -Infinity; b = -Infinity",
37 "b = a = -Infinity");
38 }
39
40 public void testExprExploitationTypes2() {
41 test("a = !0; b = !0",
42 "b = a = !0");
43 }
44
45 public void testExprExploitation() {
46 test("a = null; b = null; var c = b",
47 "var c = b = a = null");
48 test("a = null; b = null",
49 "b = a = null");
50 test("a = undefined; b = undefined",
51 "b = a = undefined");
52 test("a = 0; b = 0", "b=a=0");
53 test("a = 'foo'; b = 'foo'",
54 "b = a = \"foo\"");
55 test("a = c; b = c", "b=a=c");
56
57 testSame("a = 0; b = 1");
58 testSame("a = \"foo\"; b = \"foox\"");
59
60 test("a = null; a && b;", "(a = null)&&b");
61 test("a = null; a || b;", "(a = null)||b");
62
63 test("a = null; a ? b : c;", "(a = null) ? b : c");
64
65 test("a = null; this.foo = null;",
66 "this.foo = a = null");
67 test("function f(){ a = null; return null; }",
68 "function f(){return a = null}");
69
70 test("a = true; if (a) { foo(); }",
71 "if (a = true) { foo() }");
72 test("a = true; if (a && a) { foo(); }",
73 "if ((a = true) && a) { foo() }");
74 test("a = false; if (a) { foo(); }",
75 "if (a = false) { foo() }");
76
77 test("a = !0; if (a) { foo(); }",
78 "if (a = !0) { foo() }");
79 test("a = !0; if (a && a) { foo(); }",
80 "if ((a = !0) && a) { foo() }");
81 test("a = !1; if (a) { foo(); }",
82 "if (a = !1) { foo() }");
83
84 testSame("a = this.foo; a();");
85 test("a = b; b = a;",
86 "b = a = b");
87 testSame("a = b; a.c = a");
88 test("this.foo = null; this.bar = null;",
89 "this.bar = this.foo = null");
90 test("this.foo = null; this.bar = null; this.baz = this.bar",
91 "this.baz = this.bar = this.foo = null");
92 test("this.foo = null; a = null;",
93 "a = this.foo = null");
94 test("this.foo = null; a = this.foo;",
95 "a = this.foo = null");
96 test("a.b.c=null; a=null;",
97 "a = a.b.c = null");
98 testSame("a = null; a.b.c = null");
99 test("(a=b).c = null; this.b = null;",
100 "this.b = (a=b).c = null");
101 testSame("if(x) a = null; else b = a");
102 }
103
104 public void testNestedExprExploitation() {
105 test("this.foo = null; this.bar = null; this.baz = null;",
106 "this.baz = this.bar = this.foo = null");
107
108 test("a = 3; this.foo = a; this.bar = a; this.baz = 3;",
109 "this.baz = this.bar = this.foo = a = 3");
110 test("a = 3; this.foo = a; this.bar = this.foo; this.baz = a;",
111 "this.baz = this.bar = this.foo = a = 3");
112 test("a = 3; this.foo = a; this.bar = 3; this.baz = this.foo;",
113 "this.baz = this.bar = this.foo = a = 3");
114 test("a = 3; this.foo = a; a = 3; this.bar = 3; " +
115 "a = 3; this.baz = this.foo;",
116 "this.baz = a = this.bar = a = this.foo = a = 3");
117
118 test("a = 4; this.foo = a; a = 3; this.bar = 3; " +
119 "a = 3; this.baz = this.foo;",
120 "this.foo = a = 4; a = this.bar = a = 3; this.baz = this.foo");
121 test("a = 3; this.foo = a; a = 4; this.bar = 3; " +
122 "a = 3; this.baz = this.foo;",
123 "this.foo = a = 3; a = 4; a = this.bar = 3; this.baz = this.foo");
124 test("a = 3; this.foo = a; a = 3; this.bar = 3; " +
125 "a = 4; this.baz = this.foo;",
126 "this.bar = a = this.foo = a = 3; a = 4; this.baz = this.foo");
127 }
128
129 public void testBug1840071() {
130 // Some external properties are implemented as setters. Let's
131 // make sure that we don't collapse them inappropriately.
132 test("a.b = a.x; if (a.x) {}", "if (a.b = a.x) {}");
133 testSame("a.b = a.x; if (a.b) {}");
134 test("a.b = a.c = a.x; if (a.x) {}", "if (a.b = a.c = a.x) {}");
135 testSame("a.b = a.c = a.x; if (a.c) {}");
136 testSame("a.b = a.c = a.x; if (a.b) {}");
137 }
138
139 public void testBug2072343() {
140 testSame("a = a.x;a = a.x");
141 testSame("a = a.x;b = a.x");
142 test("b = a.x;a = a.x", "a = b = a.x");
143 testSame("a.x = a;a = a.x");
144 testSame("a.b = a.b.x;a.b = a.b.x");
145 testSame("a.y = a.y.x;b = a.y;c = a.y.x");
146 test("a = a.x;b = a;c = a.x", "b = a = a.x;c = a.x");
147 test("b = a.x;a = b;c = a.x", "a = b = a.x;c = a.x");
148 }
149
150 public void testBadCollapseIntoCall() {
151 // Can't collapse this, because if we did, 'foo' would be called
152 // in the wrong 'this' context.
153 testSame("this.foo = function() {}; this.foo();");
154 }
155
156 public void testBadCollapse() {
157 testSame("this.$e$ = []; this.$b$ = null;");
158 }
159
160 @Override
161 protected CompilerPass getProcessor(Compiler compiler) {
162 return new PeepholeOptimizationsPass(compiler,new ExploitAssigns());
163 }
164 }