Search in sources :

Example 6 with SourceFile

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

the class SourceMapTestCase method compile.

protected RunResult compile(String js1, String fileName1, String js2, String fileName2) {
    Compiler compiler = new Compiler();
    CompilerOptions options = getCompilerOptions();
    options.setChecksOnly(true);
    List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode(fileName1, js1));
    if (js2 != null && fileName2 != null) {
        inputs = ImmutableList.of(SourceFile.fromCode(fileName1, js1), SourceFile.fromCode(fileName2, js2));
    }
    Result result = compiler.compile(EXTERNS, inputs, options);
    assertTrue("compilation failed", result.success);
    String source = compiler.toSource();
    StringBuilder sb = new StringBuilder();
    try {
        result.sourceMap.validate(true);
        result.sourceMap.appendTo(sb, "testcode");
    } catch (IOException e) {
        throw new RuntimeException("unexpected exception", e);
    }
    RunResult rr = new RunResult();
    rr.generatedSource = source;
    rr.sourceMap = result.sourceMap;
    rr.sourceMapFileContent = sb.toString();
    return rr;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) Result(com.google.javascript.jscomp.Result)

Example 7 with SourceFile

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

the class GwtRunner method fromFileArray.

private static List<SourceFile> fromFileArray(File[] src, String unknownPrefix) {
    List<SourceFile> out = new ArrayList<>();
    if (src != null) {
        for (int i = 0; i < src.length; ++i) {
            File file = src[i];
            String path = file.path;
            if (path == null) {
                path = unknownPrefix + i;
            }
            out.add(SourceFile.fromCode(path, nullToEmpty(file.src)));
        }
    }
    return out;
}
Also used : ArrayList(java.util.ArrayList) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) EntryPoint(com.google.gwt.core.client.EntryPoint)

Example 8 with SourceFile

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

the class JsfileParser method parse.

/**
 * Internal implementation to produce the {@link FileInfo} object.
 */
private static FileInfo parse(String code, String filename, @Nullable Reporter reporter) {
    ErrorReporter errorReporter = new DelegatingReporter(reporter);
    Compiler compiler = new Compiler();
    compiler.init(ImmutableList.<SourceFile>of(), ImmutableList.<SourceFile>of(), new CompilerOptions());
    Config config = ParserRunner.createConfig(// TODO(sdh): ES8 STRICT, with a non-strict fallback - then give warnings.
    Config.LanguageMode.ECMASCRIPT8, Config.JsDocParsing.INCLUDE_DESCRIPTIONS_NO_WHITESPACE, Config.RunMode.KEEP_GOING, /* extraAnnotationNames */
    ImmutableSet.<String>of(), /* parseInlineSourceMaps */
    true, Config.StrictMode.SLOPPY);
    SourceFile source = SourceFile.fromCode(filename, code);
    FileInfo info = new FileInfo(errorReporter);
    ParserRunner.ParseResult parsed = ParserRunner.parse(source, code, config, errorReporter);
    parsed.ast.setInputId(new InputId(filename));
    String version = parsed.features.version();
    if (!version.equals("es3")) {
        info.loadFlags.add(JsArray.of("lang", version));
    }
    for (Comment comment : parsed.comments) {
        if (comment.type == Comment.Type.JSDOC) {
            parseComment(comment, info);
        }
    }
    NodeTraversal.traverseEs6(compiler, parsed.ast, new Traverser(info));
    return info;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ParserRunner(com.google.javascript.jscomp.parsing.ParserRunner) Config(com.google.javascript.jscomp.parsing.Config) ErrorReporter(com.google.javascript.rhino.ErrorReporter) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) InputId(com.google.javascript.rhino.InputId) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 9 with SourceFile

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

the class GwtRunner method buildSourceMaps.

private static ImmutableMap<String, SourceMapInput> buildSourceMaps(File[] src, String unknownPrefix) {
    ImmutableMap.Builder<String, SourceMapInput> inputSourceMaps = new ImmutableMap.Builder<>();
    if (src != null) {
        for (int i = 0; i < src.length; ++i) {
            File file = src[i];
            if (isNullOrEmpty(file.sourceMap)) {
                continue;
            }
            String path = file.path;
            if (path == null) {
                path = unknownPrefix + i;
            }
            path += ".map";
            SourceFile sf = SourceFile.fromCode(path, file.sourceMap);
            inputSourceMaps.put(path, new SourceMapInput(sf));
        }
    }
    return inputSourceMaps.build();
}
Also used : SourceFile(com.google.javascript.jscomp.SourceFile) SourceMapInput(com.google.javascript.jscomp.SourceMapInput) SourceFile(com.google.javascript.jscomp.SourceFile) ImmutableMap(com.google.common.collect.ImmutableMap) EntryPoint(com.google.gwt.core.client.EntryPoint)

Example 10 with SourceFile

use of com.google.javascript.jscomp.SourceFile 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)

Aggregations

SourceFile (com.google.javascript.jscomp.SourceFile)32 Compiler (com.google.javascript.jscomp.Compiler)9 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Result (com.google.javascript.jscomp.Result)3 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)3 EntryPoint (com.google.gwt.core.client.EntryPoint)2 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)2 SourceMapInput (com.google.javascript.jscomp.SourceMapInput)2 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)2 Node (com.google.javascript.rhino.Node)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JSExtern (net.vtst.ow.closure.compiler.deps.JSExtern)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 Config (com.google.javascript.jscomp.parsing.Config)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1