Debian Codegraph @

src/com/google/javascript/jscomp/RemoveUnusedNames.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.AnalyzeNameReferences.NameInfo;
20 import com.google.javascript.jscomp.NameReferenceGraph.Name;
21 import com.google.javascript.jscomp.NameReferenceGraph.Reference;
22 import com.google.javascript.jscomp.graph.GraphNode;
23 import com.google.javascript.rhino.Node;
24
25 import java.util.logging.Logger;
26
27 /**
28 * Removes unused names.
29 *
30 */
31 class RemoveUnusedNames implements CompilerPass {
32
33 private static final Logger logger =
34 Logger.getLogger(RemoveUnusedNames.class.getName());
35
36 private final AbstractCompiler compiler;
37
38 private final boolean canModifyExterns;
39
40 /**
41 * Creates a new pass for removing unused prototype properties, based
42 * on the uniqueness of property names.
43 * @param compiler The compiler.
44 */
45 RemoveUnusedNames(AbstractCompiler compiler,
46 boolean canModifyExterns) {
47 this.compiler = compiler;
48 this.canModifyExterns = canModifyExterns;
49 }
50
51 @Override
52 public void process(Node externRoot, Node root) {
53 AnalyzeNameReferences analyzer =
54 new AnalyzeNameReferences(compiler);
55 analyzer.process(externRoot, root);
56 removeUnusedProperties(analyzer.getGraph());
57 }
58
59 /**
60 * Remove all properties under a given name if the property name is
61 * never referenced.
62 */
63 private void removeUnusedProperties(NameReferenceGraph graph) {
64 for (GraphNode<Name, Reference> node : graph.getNodes()) {
65 Name name = node.getValue();
66 NameInfo nameInfo = node.getAnnotation();
67 if (nameInfo == null || !nameInfo.isReferenced()) {
68 if (canModifyExterns || !name.isExtern()) {
69 name.remove();
70 compiler.reportCodeChange();
71 logger.fine("Removed unused name" + name);
72 }
73 }
74 }
75 }
76 }