use of com.google.javascript.jscomp.AstValidator in project closure-compiler by google.
the class SerializeAndDeserializeAstTest method testAndReturnResult.
private Result testAndReturnResult(Externs externs, Sources code, Expected expected) {
InputStream serializedStream = toInputStream(externs, code, expected);
Compiler serializingCompiler = getLastCompiler();
ImmutableList<SourceFile> externFiles = collectSourceFilesFromScripts(serializingCompiler.getRoot().getFirstChild());
ImmutableList<SourceFile> codeFiles = collectSourceFilesFromScripts(serializingCompiler.getRoot().getSecondChild());
// NOTE: We need a fresh compiler instance in which to deserialize, because:
// 1. This is a better representation of what will happen in production use.
// 2. Deserializing expects to start with a compiler which has no Color-related state.
// For example, both serializing and deserializing currently need to call
// `compiler.initRuntimeLibraryTypedAsts()`, which may only be called once.
Compiler deserializingCompiler = createAndInitializeCompiler(externs, code);
DeserializedAst ast = TypedAstDeserializer.deserializeFullAst(deserializingCompiler, SourceFile.fromCode("syntheticExterns", "", StaticSourceFile.SourceKind.EXTERN), ImmutableList.<SourceFile>builder().addAll(externFiles).addAll(codeFiles).build(), serializedStream, includeTypes);
ColorRegistry registry = ast.getColorRegistry().orNull();
Node newExternsRoot = IR.root();
Node newSourceRoot = IR.root();
// actual file names.
for (Node oldExtern = serializingCompiler.getRoot().getFirstFirstChild(); oldExtern != null; oldExtern = oldExtern.getNext()) {
SourceFile extern = (SourceFile) oldExtern.getStaticSourceFile();
Node script = ast.getFilesystem().get(extern).get();
script.setInputId(new InputId(extern.getName()));
newExternsRoot.addChildToBack(script);
}
for (Node oldSource = serializingCompiler.getRoot().getSecondChild().getFirstChild(); oldSource != null; oldSource = oldSource.getNext()) {
SourceFile source = (SourceFile) oldSource.getStaticSourceFile();
Node script = ast.getFilesystem().get(source).get();
script.setInputId(new InputId(source.getName()));
newSourceRoot.addChildToBack(script);
}
Node expectedRoot = this.parseExpectedJs(expected);
assertNode(newSourceRoot).isEqualIncludingJsDocTo(expectedRoot);
new AstValidator(deserializingCompiler, /* validateScriptFeatures= */
true).validateRoot(IR.root(newExternsRoot, newSourceRoot));
consumer = null;
return new Result(ast, registry, newExternsRoot, newSourceRoot);
}
Aggregations