Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2004 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 /**
21 * Ensures that the InlineVariables pass in constants-only mode
22 * is functionally equivalent to the old InlineVariablesConstants pass.
23 */
24 public class InlineVariablesConstantsTest extends CompilerTestCase {
25
26 private boolean inlineAllStrings = false;
27
28 public InlineVariablesConstantsTest() {
29 enableNormalize();
30 }
31
32 @Override
33 protected CompilerPass getProcessor(final Compiler compiler) {
34 return new InlineVariables(
35 compiler, InlineVariables.Mode.CONSTANTS_ONLY, inlineAllStrings);
36 }
37
38 @Override
39 public void tearDown() {
40 inlineAllStrings = false;
41 }
42
43 public void testInlineVariablesConstants() {
44 test("var ABC=2; var x = ABC;", "var x=2");
45 test("var AA = 'aa'; AA;", "'aa'");
46 test("var A_A=10; A_A + A_A;", "10+10");
47 test("var AA=1", "");
48 test("var AA; AA=1", "1");
49 test("var AA; if (false) AA=1; AA;", "if (false) 1; 1;");
50 testSame("var AA; if (false) AA=1; else AA=2; AA;");
51
52 test("var AA;(function () {AA=1})()",
53 "(function () {1})()");
54
55 // Make sure that nothing explodes if there are undeclared variables.
56 testSame("var x = AA;");
57
58 // Don't inline if it will make the output larger.
59 testSame("var AA = '1234567890'; foo(AA); foo(AA); foo(AA);");
60
61 test("var AA = '123456789012345';AA;",
62 "'123456789012345'");
63 }
64
65 public void testNoInlineArraysOrRegexps() {
66 testSame("var AA = [10,20]; AA[0]");
67 testSame("var AA = [10,20]; AA.push(1); AA[0]");
68 testSame("var AA = /x/; AA.test('1')");
69 testSame("/** @const */ var aa = /x/; aa.test('1')");
70 }
71
72 public void testInlineVariablesConstantsJsDocStyle() {
73 test("/** @const */var abc=2; var x = abc;", "var x=2");
74 test("/** @const */var aa = 'aa'; aa;", "'aa'");
75 test("/** @const */var a_a=10; a_a + a_a;", "10+10");
76 test("/** @const */var aa=1;", "");
77 test("/** @const */var aa; aa=1;", "1");
78 test("/** @const */var aa;(function () {aa=1})()", "(function () {1})()");
79 test("/** @const */var aa;(function () {aa=1})(); var z=aa",
80 "(function () {1})(); var z=1");
81 testSame("/** @const */var aa;(function () {var y; aa=y})(); var z=aa");
82
83 // Don't inline if it will make the output larger.
84 testSame("/** @const */var aa = '1234567890'; foo(aa); foo(aa); foo(aa);");
85
86 test("/** @const */var aa = '123456789012345';aa;",
87 "'123456789012345'");
88 }
89
90 public void testInlineConditionallyDefinedConstant1() {
91 // Note that inlining conditionally defined constants can change the
92 // run-time behavior of code (e.g. when y is true and x is false in the
93 // example below). We inline them anyway because if the code author didn't
94 // want one inlined, he/she could define it as a non-const variable instead.
95 test("if (x) var ABC = 2; if (y) f(ABC);",
96 "if (x); if (y) f(2);");
97 }
98
99 public void testInlineConditionallyDefinedConstant2() {
100 test("if (x); else var ABC = 2; if (y) f(ABC);",
101 "if (x); else; if (y) f(2);");
102 }
103
104 public void testInlineConditionallyDefinedConstant3() {
105 test("if (x) { var ABC = 2; } if (y) { f(ABC); }",
106 "if (x) {} if (y) { f(2); }");
107 }
108
109 public void testInlineDefinedConstant() {
110 test(
111 "/**\n" +
112 " * @define {string}\n" +
113 " */\n" +
114 "var aa = '1234567890';\n" +
115 "foo(aa); foo(aa); foo(aa);",
116 "foo('1234567890');foo('1234567890');foo('1234567890')");
117
118 test(
119 "/**\n" +
120 " * @define {string}\n" +
121 " */\n" +
122 "var ABC = '1234567890';\n" +
123 "foo(ABC); foo(ABC); foo(ABC);",
124 "foo('1234567890');foo('1234567890');foo('1234567890')");
125 }
126
127 public void testInlineVariablesConstantsWithInlineAllStringsOn() {
128 inlineAllStrings = true;
129 test("var AA = '1234567890'; foo(AA); foo(AA); foo(AA);",
130 "foo('1234567890'); foo('1234567890'); foo('1234567890')");
131 }
132
133 public void testNoInlineWithoutConstDeclaration() {
134 testSame("var abc = 2; var x = abc;");
135 }
136
137 // TODO(nicksantos): enable this again once we allow constant aliasing.
138 // public void testInlineConstantAlias() {
139 // test("var XXX = new Foo(); var YYY = XXX; bar(YYY)",
140 // "var XXX = new Foo(); bar(XXX)");
141 // }
142
143 public void testNoInlineAliases() {
144 testSame("var XXX = new Foo(); var yyy = XXX; bar(yyy)");
145 testSame("var xxx = new Foo(); var YYY = xxx; bar(YYY)");
146 }
147 }