Search in sources :

Example 16 with Compiler

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

the class GwtRunner method compile.

/**
 * Public compiler call. Exposed in {@link #exportCompile}.
 */
public static ModuleOutput compile(Flags flags) {
    String[] unhandled = updateFlags(flags, defaultFlags);
    if (unhandled.length > 0) {
        throw new RuntimeException("Unhandled flag: " + unhandled[0]);
    }
    List<SourceFile> jsCode = fromFileArray(flags.jsCode, "Input_");
    ImmutableMap<String, SourceMapInput> sourceMaps = buildSourceMaps(flags.jsCode, "Input_");
    CompilerOptions options = new CompilerOptions();
    applyDefaultOptions(options);
    applyOptionsFromFlags(options, flags);
    options.setInputSourceMaps(sourceMaps);
    disableUnsupportedOptions(options);
    List<SourceFile> externs = fromFileArray(flags.externs, "Extern_");
    externs.addAll(createExterns(options.getEnvironment()));
    NodeErrorManager errorManager = new NodeErrorManager();
    Compiler compiler = new Compiler(new NodePrintStream());
    compiler.setErrorManager(errorManager);
    compiler.compile(externs, jsCode, options);
    ModuleOutput output = new ModuleOutput();
    output.compiledCode = writeOutput(compiler, flags.outputWrapper);
    output.errors = toNativeErrorArray(errorManager.errors);
    output.warnings = toNativeErrorArray(errorManager.warnings);
    if (flags.createSourceMap) {
        StringBuilder b = new StringBuilder();
        try {
            compiler.getSourceMap().appendTo(b, "");
        } catch (IOException e) {
        // ignore
        }
        output.sourceMap = b.toString();
    }
    return output;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) IOException(java.io.IOException) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) SourceMapInput(com.google.javascript.jscomp.SourceMapInput)

Example 17 with Compiler

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

the class CompileTask method execute.

@Override
public void execute() {
    if (this.outputFile == null) {
        throw new BuildException("outputFile attribute must be set");
    }
    Compiler.setLoggingLevel(Level.OFF);
    CompilerOptions options = createCompilerOptions();
    Compiler compiler = createCompiler(options);
    List<SourceFile> externs = findExternFiles(options);
    List<SourceFile> sources = findSourceFiles();
    if (isStale() || forceRecompile) {
        log("Compiling " + sources.size() + " file(s) with " + externs.size() + " extern(s)");
        Result result = compiler.compile(externs, sources, options);
        if (result.success) {
            StringBuilder source = new StringBuilder(compiler.toSource());
            if (this.outputWrapperFile != null) {
                try {
                    this.outputWrapper = Files.asCharSource(this.outputWrapperFile, UTF_8).read();
                } catch (Exception e) {
                    throw new BuildException("Invalid output_wrapper_file specified.");
                }
            }
            if (this.outputWrapper != null) {
                int pos = this.outputWrapper.indexOf(CommandLineRunner.OUTPUT_MARKER);
                if (pos > -1) {
                    String prefix = this.outputWrapper.substring(0, pos);
                    source.insert(0, prefix);
                    // end of outputWrapper
                    int suffixStart = pos + CommandLineRunner.OUTPUT_MARKER.length();
                    String suffix = this.outputWrapper.substring(suffixStart);
                    source.append(suffix);
                } else {
                    throw new BuildException("Invalid output_wrapper specified. " + "Missing '" + CommandLineRunner.OUTPUT_MARKER + "'.");
                }
            }
            if (result.sourceMap != null) {
                flushSourceMap(result.sourceMap);
            }
            writeResult(source.toString());
        } else {
            throw new BuildException("Compilation failed.");
        }
    } else {
        log("None of the files changed. Compilation skipped.");
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) BuildException(org.apache.tools.ant.BuildException) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Result(com.google.javascript.jscomp.Result)

Example 18 with Compiler

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

the class DebuggerGwtMain method doCompile.

private void doCompile() {
    SourceFile externFile = SourceFile.fromCode("externs", externs.getValue());
    SourceFile srcFile = SourceFile.fromCode("input0", input0.getValue());
    Compiler compiler = new Compiler();
    try {
        Result result = compiler.compile(externFile, srcFile, options);
        updateUi(compiler, result);
    } catch (Exception e) {
        updateUiException(e);
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) SourceFile(com.google.javascript.jscomp.SourceFile) Result(com.google.javascript.jscomp.Result)

Example 19 with Compiler

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

the class SuggestedFixTest method testAddRequireModuleDifferentNameUnchanged.

@Test
public void testAddRequireModuleDifferentNameUnchanged() {
    String input = Joiner.on('\n').join("goog.module('js.Foo');", "const googSafe = goog.require('goog.safe');", "", "/** @private */", "function foo_() {};");
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
    SuggestedFix.Builder fixBuilder = new SuggestedFix.Builder().addGoogRequire(match, "goog.safe");
    assertThat(fixBuilder.getRequireName(match, "goog.safe")).isEqualTo("googSafe");
    assertThat(fixBuilder.build().getReplacements()).isEmpty();
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 20 with Compiler

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

the class SuggestedFixTest method testInsertArguments.

@Test
public void testInsertArguments() {
    String before = "goog.dom.classes.add(";
    String after = "foo, bar);";
    Compiler compiler = getCompiler(before + after);
    Node root = compileToScriptRoot(compiler);
    SuggestedFix fix = new SuggestedFix.Builder().insertArguments(root.getFirstFirstChild(), 0, "baz").build();
    CodeReplacement replacement = CodeReplacement.create(before.length(), 0, "baz, ");
    assertReplacement(fix, replacement);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)107 Test (org.junit.Test)83 Node (com.google.javascript.rhino.Node)80 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)15 SourceFile (com.google.javascript.jscomp.SourceFile)11 IOException (java.io.IOException)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Result (com.google.javascript.jscomp.Result)3 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSModule (com.google.javascript.jscomp.JSModule)2 JSSourceFile (com.google.javascript.jscomp.JSSourceFile)2 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)2 AbstractJSProject (net.vtst.ow.closure.compiler.deps.AbstractJSProject)2 JSProject (net.vtst.ow.closure.compiler.deps.JSProject)2 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)1