Search in sources :

Example 86 with Compiler

use of com.google.javascript.jscomp.Compiler in project kontraktor by RuedigerMoeller.

the class ClojureJSPostProcessor method postProcess.

@Override
public String postProcess(String currentJS, JSPostProcessorContext context) {
    try {
        Compiler compiler = new Compiler();
        CompilerOptions options = new CompilerOptions();
        options.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT5);
        CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
        // To get the complete set of externs, the logic in
        // CompilerRunner.getDefaultExterns() should be used here.
        SourceFile extern = SourceFile.fromCode("xxxx.js", "");
        // The dummy input name "input.js" is used here so that any warnings or
        // errors will cite line numbers in terms of input.js.
        SourceFile input = SourceFile.fromCode("input.js", currentJS);
        ArrayList in = new ArrayList();
        in.add(input);
        // compile() returns a Result, but it is not needed here.
        Result compile = compiler.compile(CommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.BROWSER), in, options);
        // System.out.println(compile);
        // The compiler is responsible for generating the compiled code; it is not
        // accessible via the Result.
        String s = compiler.toSource();
        // System.out.println(s);
        return s;
    } catch (Exception e) {
        Log.Warn(this, e);
    }
    return currentJS;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 87 with Compiler

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

the class TypeSyntaxTest method parse.

private Node parse(String source, String expected, LanguageMode languageIn) {
    CompilerOptions options = new CompilerOptions();
    options.setLanguageIn(languageIn);
    options.setLanguageOut(LanguageMode.ECMASCRIPT6_TYPED);
    options.setPreserveTypeAnnotations(true);
    options.setPrettyPrint(true);
    options.setLineLengthThreshold(80);
    options.setPreferSingleQuotes(true);
    Compiler compiler = new Compiler();
    compiler.setErrorManager(testErrorManager);
    compiler.initOptions(options);
    Node script = compiler.parse(SourceFile.fromCode("[test]", source));
    // Verifying that all warnings were seen
    assertTrue("Missing an error", testErrorManager.hasEncounteredAllErrors());
    assertTrue("Missing a warning", testErrorManager.hasEncounteredAllWarnings());
    if (script != null && testErrorManager.getErrorCount() == 0) {
        // if it can be parsed, it should round trip.
        String actual = new CodePrinter.Builder(script).setCompilerOptions(options).setTypeRegistry(compiler.getTypeIRegistry()).build().trim();
        assertThat(actual).isEqualTo(expected);
    }
    return script;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) CompilerOptions(com.google.javascript.jscomp.CompilerOptions)

Example 88 with Compiler

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

the class RefasterJsScannerTest method testInitialize_emptyBeforeTemplates.

@Test
public void testInitialize_emptyBeforeTemplates() throws Exception {
    try {
        Compiler compiler = createCompiler();
        compileTestCode(compiler, "", "");
        createScanner(compiler, "function before_foo() {}; function after_foo() {};");
        fail("RefasterJS templates are not allowed to be empty!.");
    } catch (IllegalStateException expected) {
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Test(org.junit.Test)

Example 89 with Compiler

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

the class RefasterJsScannerTest method testInitialize_missingAfterTemplate.

@Test
public void testInitialize_missingAfterTemplate() throws Exception {
    try {
        Compiler compiler = createCompiler();
        compileTestCode(compiler, "", "");
        createScanner(compiler, "function before_foo() {'bar'};");
        fail("An exception should have been thrown for missing the after template.");
    } catch (IllegalStateException expected) {
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Test(org.junit.Test)

Example 90 with Compiler

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

the class RefasterJsScannerTest method testInitialize_success.

@Test
public void testInitialize_success() throws Exception {
    Compiler compiler = createCompiler();
    compileTestCode(compiler, "", "");
    createScanner(compiler, "function before_foo() {'str';}; function after_foo() {};");
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) 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