| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 56 |
testSame("var x = AA;"); |
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
} |