Debian Codegraph @

src/com/google/javascript/jscomp/JSModule.java

raw symbols SCIP
1 /*
2 * Copyright 2005 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.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import com.google.javascript.jscomp.deps.DependencyInfo;
24 import com.google.javascript.jscomp.deps.SortedDependencies;
25 import com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException;
26
27 import java.io.Serializable;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Set;
34
35 /**
36 * A JavaScript module has a unique name, consists of a list of compiler inputs,
37 * and can depend on other modules.
38 *
39 */
40 public class JSModule implements DependencyInfo, Serializable {
41 private static final long serialVersionUID = 1;
42
43 static final DiagnosticType CIRCULAR_DEPENDENCY_ERROR =
44 DiagnosticType.error("JSC_CIRCULAR_DEP",
45 "Circular dependency detected: {0}");
46
47 /** Module name */
48 private final String name;
49
50 /** Source code inputs */
51 private final List<CompilerInput> inputs = new ArrayList<CompilerInput>();
52
53 /** Modules that this module depends on */
54 private final List<JSModule> deps = new ArrayList<JSModule>();
55
56 private int depth;
57 /**
58 * Creates an instance.
59 *
60 * @param name A unique name for the module
61 */
62 public JSModule(String name) {
63 this.name = name;
64 this.depth = -1;
65 }
66
67 /** Gets the module name. */
68 @Override
69 public String getName() {
70 return name;
71 }
72
73 @Override
74 public List<String> getProvides() {
75 return ImmutableList.<String>of(name);
76 }
77
78 @Override
79 public List<String> getRequires() {
80 ImmutableList.Builder<String> builder = ImmutableList.builder();
81 for (JSModule m : deps) {
82 builder.add(m.getName());
83 }
84 return builder.build();
85 }
86
87 @Override
88 public String getPathRelativeToClosureBase() {
89 throw new UnsupportedOperationException();
90 }
91
92 /** Adds a source file input to this module. */
93 public void add(SourceFile file) {
94 add(new CompilerInput(file));
95 }
96
97 /** Adds a source file input to this module. */
98 public void addFirst(SourceFile file) {
99 addFirst(new CompilerInput(file));
100 }
101
102 /** Adds a source code input to this module. */
103 public void add(CompilerInput input) {
104 inputs.add(input);
105 input.setModule(this);
106 }
107
108 /**
109 * Adds a source code input to this module. Call only if the input might
110 * already be associated with a module. Otherwise, use
111 * add(CompilerInput input).
112 */
113 void addAndOverrideModule(CompilerInput input) {
114 inputs.add(input);
115 input.overrideModule(this);
116 }
117
118 /** Adds a source code input to this module. */
119 public void addFirst(CompilerInput input) {
120 inputs.add(0, input);
121 input.setModule(this);
122 }
123
124 /** Adds a source code input to this module directly after other. */
125 public void addAfter(CompilerInput input, CompilerInput other) {
126 Preconditions.checkState(inputs.contains(other));
127 inputs.add(inputs.indexOf(other), input);
128 input.setModule(this);
129 }
130
131 /** Adds a dependency on another module. */
132 public void addDependency(JSModule dep) {
133 Preconditions.checkNotNull(dep);
134 Preconditions.checkState(dep != this);
135 deps.add(dep);
136 }
137
138 /** Removes an input from this module. */
139 public void remove(CompilerInput input) {
140 input.setModule(null);
141 inputs.remove(input);
142 }
143
144 /** Removes all of the inputs from this module. */
145 public void removeAll() {
146 for (CompilerInput input : inputs) {
147 input.setModule(null);
148 }
149 inputs.clear();
150 }
151
152 /**
153 * Gets the list of modules that this module depends on.
154 *
155 * @return A list that may be empty but not null
156 */
157 public List<JSModule> getDependencies() {
158 return deps;
159 }
160
161 /**
162 * Gets the names of the modules that this module depends on,
163 * sorted alphabetically.
164 */
165 List<String> getSortedDependencyNames() {
166 List<String> names = Lists.newArrayList();
167 for (JSModule module : getDependencies()) {
168 names.add(module.getName());
169 }
170 Collections.sort(names);
171 return names;
172 }
173
174 /**
175 * Returns the transitive closure of dependencies starting from the
176 * dependencies of this module.
177 */
178 public Set<JSModule> getAllDependencies() {
179 Set<JSModule> allDeps = Sets.newHashSet(deps);
180 List<JSModule> workList = Lists.newArrayList(deps);
181 while (workList.size() > 0) {
182 JSModule module = workList.remove(workList.size() - 1);
183 for (JSModule dep : module.getDependencies()) {
184 if (allDeps.add(dep)) {
185 workList.add(dep);
186 }
187 }
188 }
189 return allDeps;
190 }
191
192 /** Returns this module and all of its dependencies in one list. */
193 public Set<JSModule> getThisAndAllDependencies() {
194 Set<JSModule> deps = getAllDependencies();
195 deps.add(this);
196 return deps;
197 }
198
199 /**
200 * Gets this module's list of source code inputs.
201 *
202 * @return A list that may be empty but not null
203 */
204 public List<CompilerInput> getInputs() {
205 return inputs;
206 }
207
208 /** Returns the input with the given name or null if none. */
209 public CompilerInput getByName(String name) {
210 for (CompilerInput input : inputs) {
211 if (name.equals(input.getName())) {
212 return input;
213 }
214 }
215 return null;
216 }
217
218 /**
219 * Removes any input with the given name. Returns whether any were removed.
220 */
221 public boolean removeByName(String name) {
222 boolean found = false;
223 Iterator<CompilerInput> iter = inputs.iterator();
224 while (iter.hasNext()) {
225 CompilerInput file = iter.next();
226 if (name.equals(file.getName())) {
227 iter.remove();
228 file.setModule(null);
229 found = true;
230 }
231 }
232 return found;
233 }
234
235 /** Returns the module name (primarily for debugging). */
236 @Override
237 public String toString() {
238 return name;
239 }
240
241 /**
242 * Removes any references to nodes of the AST. This method is needed to
243 * allow the ASTs to be garbage collected if the modules are kept around.
244 */
245 public void clearAsts() {
246 for (CompilerInput input : inputs) {
247 input.clearAst();
248 }
249 }
250
251 /**
252 * Puts the JS files into a topologically sorted order by their dependencies.
253 */
254 public void sortInputsByDeps(Compiler compiler) {
255 // Set the compiler, so that we can parse requires/provides and report
256 // errors properly.
257 for (CompilerInput input : inputs) {
258 input.setCompiler(compiler);
259 }
260
261 // Sort the JSModule in this order.
262 try {
263 List<CompilerInput> sortedList =
264 (new SortedDependencies<CompilerInput>(
265 Collections.<CompilerInput>unmodifiableList(inputs)))
266 .getSortedList();
267 inputs.clear();
268 inputs.addAll(sortedList);
269 } catch (CircularDependencyException e) {
270 compiler.report(
271 JSError.make(CIRCULAR_DEPENDENCY_ERROR, e.getMessage()));
272 }
273 }
274
275 /**
276 * Returns the given collection of modules in topological order.
277 *
278 * Note that this will return the modules in the same order if they are
279 * already sorted, and in general, will only change the order as necessary to
280 * satisfy the ordering dependencies. This can be important for cases where
281 * the modules do not properly specify all dependencies.
282 */
283 public static JSModule[] sortJsModules(Collection<JSModule> modules)
284 throws CircularDependencyException {
285 // Sort the JSModule in this order.
286 List<JSModule> sortedList = (new SortedDependencies<JSModule>(
287 Lists.newArrayList(modules))).getSortedList();
288 return sortedList.toArray(new JSModule[sortedList.size()]);
289 }
290
291 /**
292 * @param dep the depth to set
293 */
294 public void setDepth(int dep) {
295 this.depth = dep;
296 }
297
298 /**
299 * @return the depth
300 */
301 public int getDepth() {
302 return depth;
303 }
304 }