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