Skip to content

Commit 72864ba

Browse files
lauraharkercopybara-github
authored andcommitted
Ban passing @extern files under --js flag in a TypedAST compilation
This is something we could in theory support, but has no known use cases at the moment. PiperOrigin-RevId: 505173097
1 parent 3487e88 commit 72864ba

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.google.javascript.rhino.JSDocInfo;
8787
import com.google.javascript.rhino.Node;
8888
import com.google.javascript.rhino.StaticScope;
89+
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
8990
import com.google.javascript.rhino.jstype.JSTypeRegistry;
9091
import java.io.IOException;
9192
import java.io.InputStream;
@@ -2932,7 +2933,7 @@ ControlFlowGraph<Node> computeCFG() {
29322933
*/
29332934
@VisibleForTesting
29342935
final SourceFile SYNTHETIC_EXTERNS_FILE =
2935-
SourceFile.fromCode(SYNTHETIC_FILE_NAME_PREFIX + "externs] ", "");
2936+
SourceFile.fromCode(SYNTHETIC_FILE_NAME_PREFIX + "externs] ", "", SourceKind.EXTERN);
29362937

29372938
private @Nullable CompilerInput syntheticExternsInput; // matches SYNTHETIC_EXTERNS_FILE
29382939

src/com/google/javascript/jscomp/serialization/TypedAstDeserializer.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.auto.value.AutoValue;
2424
import com.google.common.annotations.GwtIncompatible;
2525
import com.google.common.base.Optional;
26+
import com.google.common.base.Preconditions;
2627
import com.google.common.collect.ImmutableList;
2728
import com.google.common.collect.ImmutableMap;
2829
import com.google.common.collect.ImmutableSet;
@@ -35,14 +36,17 @@
3536
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
3637
import com.google.javascript.rhino.IR;
3738
import com.google.javascript.rhino.Node;
39+
import com.google.javascript.rhino.StaticSourceFile.SourceKind;
3840
import com.google.protobuf.CodedInputStream;
3941
import com.google.protobuf.ExtensionRegistry;
4042
import com.google.protobuf.InvalidProtocolBufferException;
4143
import com.google.protobuf.WireFormat;
4244
import java.io.IOException;
4345
import java.io.InputStream;
4446
import java.util.ArrayList;
47+
import java.util.HashSet;
4548
import java.util.LinkedHashMap;
49+
import java.util.List;
4650
import java.util.concurrent.ConcurrentHashMap;
4751
import java.util.concurrent.ConcurrentMap;
4852
import java.util.function.Function;
@@ -279,9 +283,28 @@ private void deserializeTypedAst(
279283

280284
private ImmutableList<SourceFile> toFileShard(TypedAst typedAstProto) {
281285
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);
285308
}
286309
return fileShardBuilder.build();
287310
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void test3StageCompile() throws Exception {
237237
"--source_map_include_content",
238238
"--translations_file",
239239
msgBundle.toString(),
240-
"--js",
240+
"--externs",
241241
externsFile.toString(),
242242
"--js",
243243
srcFile.toString());

0 commit comments

Comments
 (0)