Search in sources :

Example 71 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class TypedAstIntegrationTest method disambiguatesAndDeletesMethodsAcrossLibraries.

@Test
public void disambiguatesAndDeletesMethodsAcrossLibraries() throws IOException {
    SourceFile lib1 = code("class Lib1 { m() { return 'lib1'; } n() { return 'delete me'; } }");
    SourceFile lib2 = code("class Lib2 { m() { return 'delete me'; } n() { return 'lib2'; } }");
    precompileLibrary(lib1);
    precompileLibrary(lib2);
    precompileLibrary(extern(new TestExternsBuilder().addAlert().build()), typeSummary(lib1), typeSummary(lib2), code("alert(new Lib1().m()); alert(new Lib2().n());"));
    CompilerOptions options = new CompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    options.setDisambiguateProperties(true);
    Compiler compiler = compileTypedAstShards(options);
    Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
    assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 72 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class TypedAstIntegrationTest method precompileLibrary.

private void precompileLibrary(SourceFile... files) throws IOException {
    Path typedAstPath = Files.createTempFile("", ".typedast");
    CompilerOptions options = new CompilerOptions();
    options.setChecksOnly(true);
    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
    options.setProtectHiddenSideEffects(true);
    options.setTypedAstOutputFile(typedAstPath);
    ImmutableList.Builder<SourceFile> externs = ImmutableList.builder();
    ImmutableList.Builder<SourceFile> sources = ImmutableList.builder();
    for (SourceFile file : files) {
        if (file.isExtern()) {
            externs.add(file);
        } else {
            sources.add(file);
        }
    }
    Compiler compiler = new Compiler();
    compiler.init(externs.build(), sources.build(), options);
    compiler.parse();
    checkUnexpectedErrorsOrWarnings(compiler, 0);
    // serializes a TypedAST into typedAstPath
    compiler.stage1Passes();
    checkUnexpectedErrorsOrWarnings(compiler, 0);
    this.shards.add(typedAstPath);
}
Also used : Path(java.nio.file.Path) Compiler(com.google.javascript.jscomp.Compiler) ImmutableList(com.google.common.collect.ImmutableList) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 73 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class TypedAstIntegrationTest method testDefineCheck.

@Test
public void testDefineCheck() throws IOException {
    precompileLibrary(code(""));
    CompilerOptions options = new CompilerOptions();
    options.setDefineReplacements(ImmutableMap.of("FOOBAR", 1));
    Compiler compiler = compileTypedAstShardsWithoutErrorChecks(options);
    assertThat(compiler.getWarnings()).comparingElementsUsing(JSCompCorrespondences.OWNING_DIAGNOSTIC_GROUP).containsExactly(DiagnosticGroups.UNKNOWN_DEFINES);
    assertThat(compiler.getErrors()).isEmpty();
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Example 74 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class TypedAstIntegrationTest method typeSummary.

/**
 * Runs the type summary generator on the given source code, and returns a summary file
 */
private SourceFile typeSummary(SourceFile original) {
    Compiler compiler = new Compiler();
    CompilerOptions options = new CompilerOptions();
    options.setIncrementalChecks(IncrementalCheckMode.GENERATE_IJS);
    compiler.init(ImmutableList.of(), ImmutableList.of(original), options);
    compiler.parse();
    checkUnexpectedErrorsOrWarnings(compiler, 0);
    compiler.stage1Passes();
    checkUnexpectedErrorsOrWarnings(compiler, 0);
    return SourceFile.fromCode(original.getName(), compiler.toSource());
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions)

Example 75 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class TypedAstIntegrationTest method protectsHiddenSideEffects.

@Test
public void protectsHiddenSideEffects() throws IOException {
    precompileLibrary(extern("const foo = {}; foo.bar;"), code("/** @fileoverview @suppress {uselessCode} */ foo.bar;"));
    CompilerOptions options = new CompilerOptions();
    CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    Compiler compiler = compileTypedAstShards(options);
    Node expectedRoot = parseExpectedCode("foo.bar");
    assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) Test(org.junit.Test)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)172 Test (org.junit.Test)132 Node (com.google.javascript.rhino.Node)116 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)50 SourceFile (com.google.javascript.jscomp.SourceFile)22 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)16 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)9 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)8 Result (com.google.javascript.jscomp.Result)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 File (java.io.File)4 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)3 JSError (com.google.javascript.jscomp.JSError)3 InputId (com.google.javascript.rhino.InputId)3 GwtIncompatible (com.google.common.annotations.GwtIncompatible)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSChunk (com.google.javascript.jscomp.JSChunk)2 JSModule (com.google.javascript.jscomp.JSModule)2