Search in sources :

Example 1 with ColorRegistry

use of com.google.javascript.jscomp.colors.ColorRegistry 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);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) ColorRegistry(com.google.javascript.jscomp.colors.ColorRegistry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DeserializedAst(com.google.javascript.jscomp.serialization.TypedAstDeserializer.DeserializedAst) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) InputId(com.google.javascript.rhino.InputId) AstValidator(com.google.javascript.jscomp.AstValidator) SourceFile(com.google.javascript.jscomp.SourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Aggregations

AstValidator (com.google.javascript.jscomp.AstValidator)1 Compiler (com.google.javascript.jscomp.Compiler)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 ColorRegistry (com.google.javascript.jscomp.colors.ColorRegistry)1 DeserializedAst (com.google.javascript.jscomp.serialization.TypedAstDeserializer.DeserializedAst)1 InputId (com.google.javascript.rhino.InputId)1 Node (com.google.javascript.rhino.Node)1 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)1 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1