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;
}
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;
}
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;
}
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();
}
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;
}
Aggregations