-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Es6SortedDependencies.java
225 lines (196 loc) · 7.67 KB
/
Es6SortedDependencies.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright 2015 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.deps;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* A sorted list of inputs following the ES6 module ordering spec.
* <p>
* Orders such that each input always comes after its dependencies.
* Circular references are allowed by emitting the current input in the moment
* before a loop would complete.
* <p>
* The resulting order is not the same as the user-provided order but is
* influenced by it since it may take more than one graph traversal to account
* for all provided inputs and the graph traversals start with the first user
* provided input and continue from there.
* <p>
* Also exposes other information about the inputs, like which inputs
* do not provide symbols.
*
* @author stalcup@google.com (John Stalcup)
*/
public final class Es6SortedDependencies<INPUT extends DependencyInfo>
implements SortedDependencies<INPUT> {
private final List<INPUT> userOrderedInputs = new ArrayList<>();
private final List<INPUT> importOrderedInputs = new ArrayList<>();
private final Set<INPUT> completedInputs = new HashSet<>();
private final Map<String, INPUT> nonExportingInputs = new LinkedHashMap<>();
private final Map<String, INPUT> exportingInputBySymbolName = new HashMap<>();
// Maps an input A to the inputs it depends on, ie, inputs that provide stuff that A requires.
private final Multimap<INPUT, INPUT> importedInputByImportingInput = LinkedHashMultimap.create();
public Es6SortedDependencies(List<INPUT> userOrderedInputs) {
this.userOrderedInputs.addAll(userOrderedInputs);
processInputs();
}
@Override
public ImmutableList<INPUT> getStrongDependenciesOf(List<INPUT> rootInputs, boolean sorted) {
checkArgument(userOrderedInputs.containsAll(rootInputs));
Set<INPUT> includedInputs = new HashSet<>();
Deque<INPUT> worklist = new ArrayDeque<>(rootInputs);
while (!worklist.isEmpty()) {
INPUT input = worklist.pop();
if (includedInputs.add(input)) {
for (String symbolName : input.getRequiredSymbols()) {
INPUT importedSymbolName = exportingInputBySymbolName.get(symbolName);
if (importedSymbolName != null) {
worklist.add(importedSymbolName);
}
}
}
}
ImmutableList.Builder<INPUT> builder = ImmutableList.builder();
for (INPUT input : (sorted ? importOrderedInputs : userOrderedInputs)) {
if (includedInputs.contains(input)) {
builder.add(input);
}
}
return builder.build();
}
@Override
public INPUT getInputProviding(String symbolName) throws MissingProvideException {
INPUT input = maybeGetInputProviding(symbolName);
if (input != null) {
return input;
}
throw new MissingProvideException(symbolName);
}
@Override
public ImmutableList<INPUT> getInputsWithoutProvides() {
return ImmutableList.copyOf(nonExportingInputs.values());
}
@Override
public ImmutableList<INPUT> getSortedStrongDependenciesOf(List<INPUT> roots) {
return getStrongDependenciesOf(roots, true);
}
@Override
public List<INPUT> getSortedWeakDependenciesOf(List<INPUT> rootInputs) {
Set<INPUT> strongInputs = new HashSet<>(getSortedStrongDependenciesOf(rootInputs));
Set<INPUT> weakInputs = new HashSet<>();
Deque<INPUT> worklist = new ArrayDeque<>(strongInputs);
while (!worklist.isEmpty()) {
INPUT input = worklist.pop();
boolean isStrong = strongInputs.contains(input);
Iterable<String> edges =
isStrong
? input.getTypeRequires()
: Iterables.concat(input.getRequiredSymbols(), input.getTypeRequires());
if (!isStrong) {
weakInputs.add(input);
}
for (String symbolName : edges) {
INPUT importedSymbolName = exportingInputBySymbolName.get(symbolName);
if (importedSymbolName != null) {
worklist.add(importedSymbolName);
}
}
}
ImmutableList.Builder<INPUT> builder = ImmutableList.builder();
for (INPUT input : importOrderedInputs) {
if (weakInputs.contains(input)) {
builder.add(input);
}
}
return builder.build();
}
@Override
public List<INPUT> getSortedList() {
return Collections.unmodifiableList(importOrderedInputs);
}
@Override
public INPUT maybeGetInputProviding(String symbol) {
if (exportingInputBySymbolName.containsKey(symbol)) {
return exportingInputBySymbolName.get(symbol);
}
return nonExportingInputs.get(ModuleNames.fileToModuleName(symbol));
}
private void orderInput(INPUT input) {
if (completedInputs.contains(input)) {
return;
}
completedInputs.add(input);
for (INPUT importedInput : importedInputByImportingInput.get(input)) {
orderInput(importedInput);
}
// Emit an input after its imports have been emitted.
importOrderedInputs.add(input);
}
private void processInputs() {
// Index.
for (INPUT userOrderedInput : userOrderedInputs) {
Collection<String> provides = userOrderedInput.getProvides();
String firstProvide = Iterables.getFirst(provides, null);
if (firstProvide == null
// "module$" indicates the provide is generated from the path. If this is the only thing
// the module provides and it is not an ES6 module then it is just a script and doesn't
// export anything.
|| (provides.size() == 1
&& firstProvide.startsWith("module$")
// ES6 modules should always be considered as exporting something.
&& !"es6".equals(userOrderedInput.getLoadFlags().get("module")))) {
nonExportingInputs.put(
ModuleNames.fileToModuleName(userOrderedInput.getName()), userOrderedInput);
}
for (String providedSymbolName : userOrderedInput.getProvides()) {
exportingInputBySymbolName.put(providedSymbolName, userOrderedInput);
}
}
for (INPUT userOrderedInput : userOrderedInputs) {
for (String symbolName :
Iterables.concat(
userOrderedInput.getRequiredSymbols(), userOrderedInput.getTypeRequires())) {
INPUT importedInput = exportingInputBySymbolName.get(symbolName);
if (importedInput != null) {
importedInputByImportingInput.put(userOrderedInput, importedInput);
}
}
}
// Order.
// For each input, traverse in user-provided order.
for (INPUT userOrderedInput : userOrderedInputs) {
// Traverse the graph starting from this input and record any
// newly-reached inputs.
orderInput(userOrderedInput);
}
// Free temporary indexes.
completedInputs.clear();
importedInputByImportingInput.clear();
}
}