Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 import com.google.javascript.jscomp.CheckLevel;
20 import com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter;
21 import com.google.javascript.rhino.Node;
22
23 /**
24 * Tests for {@link RemoveUnusedPrototypeProperties}.
25 *
26 * @author [email protected] (Nick Santos)
27 */
28 public class RemoveUnusedNamesTest extends CompilerTestCase {
29 private static final String EXTERNS =
30 "/** @constructor */\n " +
31 "function IFoo() { } \n" +
32 "IFoo.prototype.bar; \n" +
33
34 "/** @constructor */\n " +
35 "var mExtern; \n" +
36 "mExtern.bExtern; \n" +
37 "mExtern['cExtern']; \n";
38
39 public RemoveUnusedNamesTest() {
40 super(EXTERNS);
41 }
42
43 private boolean canRemoveExterns = false;
44
45 @Override
46 protected int getNumRepetitions() {
47 return 1;
48 }
49
50 @Override
51 protected CompilerPass getProcessor(final Compiler compiler) {
52 return new CompilerPass() {
53
54 @Override
55 public void process(Node externs, Node root) {
56 new TypeCheck(compiler,
57 new SemanticReverseAbstractInterpreter(
58 compiler.getCodingConvention(),
59 compiler.getTypeRegistry()),
60 compiler.getTypeRegistry(),
61 CheckLevel.ERROR,
62 CheckLevel.ERROR).processForTesting(externs, root);
63
64 new RemoveUnusedNames(
65 compiler, canRemoveExterns).process(externs, root);
66
67 // Use to remove side-effect-free artifacts that are left over.
68 new UnreachableCodeElimination(compiler, true).process(externs, root);
69 }
70 };
71 }
72
73 @Override
74 public void setUp() {
75 canRemoveExterns = false;
76 }
77
78 public void testAnalyzeUnusedPrototypeProperties() {
79 // Basic removal for prototype properties
80 test("/** @constructor */ \n" +
81 "function e(){} \n" +
82 "e.prototype.a = function(){};" +
83 "e.prototype.b = function(){};" +
84 "var x = new e; x.a()",
85
86 "function e(){}" +
87 " e.prototype.a = function(){};" +
88 "var x = new e; x.a()");
89 }
90
91 public void testAnalyzeUnusedPrototypeProperties2() {
92 // TODO(user): Prototype literal not yet supported.
93 // Basic removal for prototype replacement
94 //test("/** @constructor */ \n" +
95 // "function e(){} \n" +
96 // "e.prototype = {a: function(){}, b: function(){}};" +
97 // "var x=new e; x.a()",
98 //
99 // "function e(){}" +
100 // "e.prototype = {a: function(){}};" +
101 // "var x = new e; x.a()");
102 }
103
104 public void testAnalyzeUnusedPrototypeProperties3() {
105 // Even if bExtern is out there somewhere. The type system tells us they
106 // are unrelated.
107 test("/** @constructor */ \n" +
108 "function e(){} \n" +
109 "e.prototype.a = function(){};" +
110 "e.prototype.bExtern = function(){};" +
111 "var x = new e;x.a()",
112 "function e(){}" +
113 "e.prototype.a = function(){};" +
114 //"e.prototype.bExtern = function(){};" +
115 "var x = new e; x.a()");
116
117 // TODO(user): Prototype literal not yet supported.
118 //test("/** @constructor */ \n" +
119 // "function e(){} \n" +
120 // "e.prototype = {a: function(){}, bExtern: function(){}};" +
121 // "var x = new e; x.a()",
122 // "function e(){}" +
123 // "e.prototype = {a: function(){}, bExtern: function(){}};" +
124 // "var x = new e; x.a()");
125 }
126
127 public void testAliasing() {
128 // TODO(user): Not fully supported.
129 }
130
131 public void testStatement() {
132 test("/**\n" +
133 " * @fileoverview\n" +
134 " * @notypecheck\n" +
135 " */ \n" +
136 "/** @constructor */" +
137 "function e(){}" +
138 "var x = e.prototype.method1 = function(){};" +
139 "var y = new e; x()",
140 "function e(){}" +
141 "var x = function(){};" +
142 "var y = new e; x()");
143 }
144
145 public void testExportedMethodsByNamingConvention() {
146 // TODO(user): Not fully supported.
147 }
148
149 public void testExportedMethodsByNamingConventionAlwaysExported() {
150 // TODO(user): Not fully supported.
151 }
152 }