|
23 | 23 | import com.google.auto.value.AutoValue;
|
24 | 24 | import com.google.common.annotations.GwtIncompatible;
|
25 | 25 | import com.google.common.base.Optional;
|
| 26 | +import com.google.common.base.Preconditions; |
26 | 27 | import com.google.common.collect.ImmutableList;
|
27 | 28 | import com.google.common.collect.ImmutableMap;
|
28 | 29 | import com.google.common.collect.ImmutableSet;
|
|
35 | 36 | import com.google.javascript.jscomp.parsing.parser.FeatureSet;
|
36 | 37 | import com.google.javascript.rhino.IR;
|
37 | 38 | import com.google.javascript.rhino.Node;
|
| 39 | +import com.google.javascript.rhino.StaticSourceFile.SourceKind; |
38 | 40 | import com.google.protobuf.CodedInputStream;
|
39 | 41 | import com.google.protobuf.ExtensionRegistry;
|
40 | 42 | import com.google.protobuf.InvalidProtocolBufferException;
|
41 | 43 | import com.google.protobuf.WireFormat;
|
42 | 44 | import java.io.IOException;
|
43 | 45 | import java.io.InputStream;
|
44 | 46 | import java.util.ArrayList;
|
| 47 | +import java.util.HashSet; |
45 | 48 | import java.util.LinkedHashMap;
|
| 49 | +import java.util.List; |
46 | 50 | import java.util.concurrent.ConcurrentHashMap;
|
47 | 51 | import java.util.concurrent.ConcurrentMap;
|
48 | 52 | import java.util.function.Function;
|
@@ -279,9 +283,28 @@ private void deserializeTypedAst(
|
279 | 283 |
|
280 | 284 | private ImmutableList<SourceFile> toFileShard(TypedAst typedAstProto) {
|
281 | 285 | ImmutableList.Builder<SourceFile> fileShardBuilder = ImmutableList.builder();
|
282 |
| - for (SourceFileProto p : typedAstProto.getSourceFilePool().getSourceFileList()) { |
283 |
| - fileShardBuilder.add( |
284 |
| - filePoolBuilder.computeIfAbsent(p.getFilename(), (n) -> SourceFile.fromProto(p))); |
| 286 | + HashSet<Integer> externFileIndices = new HashSet<>(); |
| 287 | + for (LazyAst lazyAst : typedAstProto.getExternAstList()) { |
| 288 | + externFileIndices.add(lazyAst.getSourceFile() - 1); |
| 289 | + } |
| 290 | + List<SourceFileProto> protos = typedAstProto.getSourceFilePool().getSourceFileList(); |
| 291 | + for (int i = 0; i < protos.size(); i++) { |
| 292 | + SourceFileProto p = protos.get(i); |
| 293 | + SourceFile file = |
| 294 | + filePoolBuilder.computeIfAbsent(p.getFilename(), (n) -> SourceFile.fromProto(p)); |
| 295 | + // Verify that the TypedAST SourceFileProto files and the 'requiredInputFiles' SourceFiles |
| 296 | + // agree on which files are externs vs. code. In non-TypedAST builds, we allow passing @extern |
| 297 | + // files under the --js flag. This means 'SourceFile' objects may have a non-EXTERN |
| 298 | + // SourceKind but point to a file with @externs. |
| 299 | + // For TypedAST builds, we could support this, but it's an uncommon pattern and trickier to |
| 300 | + // support than ban. |
| 301 | + if (externFileIndices.contains(i)) { |
| 302 | + Preconditions.checkState( |
| 303 | + file.getKind() == SourceKind.EXTERN, |
| 304 | + "TypedAST compilations must pass all extern files as externs, not js, but found %s", |
| 305 | + file.getName()); |
| 306 | + } |
| 307 | + fileShardBuilder.add(file); |
285 | 308 | }
|
286 | 309 | return fileShardBuilder.build();
|
287 | 310 | }
|
|
0 commit comments