com.google.javascript.jscomp.Result Java Examples

The following examples show how to use com.google.javascript.jscomp.Result. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SecureCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void compile(JsonML source) {
  if (report != null) {
    throw new IllegalStateException(COMPILATION_ALREADY_COMPLETED_MSG);
  }

  sourceAst = new JsonMLAst(source);

  CompilerInput input = new CompilerInput(
      sourceAst, "[[jsonmlsource]]", false);

  JSModule module = new JSModule("[[jsonmlmodule]]");
  module.add(input);

  Result result = compiler.compileModules(
      ImmutableList.<SourceFile>of(),
      ImmutableList.of(module),
      options);

  report = generateReport(result);
}
 
Example #2
Source File: SecureCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
Report generateReport(Result result) {
  // a report may be generated only after actual compilation is complete
  if (result == null) {
    return null;
  }

  ArrayList<JsonMLError> errors = Lists.newArrayList();
  for (JSError error : result.errors) {
    errors.add(JsonMLError.make(error, sourceAst));
  }

  ArrayList<JsonMLError> warnings = Lists.newArrayList();
  for (JSError warning : result.warnings) {
    warnings.add(JsonMLError.make(warning, sourceAst));
  }

  return new Report(
      errors.toArray(new JsonMLError[0]),
      warnings.toArray(new JsonMLError[0]));
}
 
Example #3
Source File: CheckRequiresForConstructorsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testRequiresAreCaughtBeforeProcessed() {
  String js = "var foo = {}; var bar = new foo.bar.goo();";
  SourceFile input = SourceFile.fromCode("foo.js", js);
  Compiler compiler = new Compiler();
  CompilerOptions opts = new CompilerOptions();
  opts.checkRequires = CheckLevel.WARNING;
  opts.closurePass = true;

  Result result = compiler.compile(ImmutableList.<SourceFile>of(),
      ImmutableList.of(input), opts);
  JSError[] warnings = result.warnings;
  assertNotNull(warnings);
  assertTrue(warnings.length > 0);

  String expectation = "'foo.bar.goo' used but not goog.require'd";

  for (JSError warning : warnings) {
    if (expectation.equals(warning.description)) {
      return;
    }
  }

  fail("Could not find the following warning:" + expectation);
}
 
Example #4
Source File: ClosureJsInteropGenerator.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
private void checkJavascriptCompilationResults(Result compilationResult) {
  for (JSError error : compilationResult.errors) {
    problems.error("Javascript compilation error: %s", error.getDescription());
  }

  problems.report();
}
 
Example #5
Source File: InitialParseRetainingCompiler.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Copied verbatim from com.google.javascript.jscomp.Compiler, except using getter methods instead
 * of private fields and running cloneParsedInputs() at the appropriate time.
 */
@Override
public <T1 extends SourceFile, T2 extends SourceFile> Result compile(
    List<T1> externs, List<T2> inputs, CompilerOptions options) {
  // The compile method should only be called once.
  checkState(getRoot() == null);

  try {
    init(externs, inputs, options);
    if (!hasErrors()) {
      parseForCompilation();
      cloneParsedInputs();
    }
    if (!hasErrors()) {
      if (options.getInstrumentForCoverageOnly()) {
        // TODO(bradfordcsmith): The option to instrument for coverage only should belong to the
        //     runner, not the compiler.
        instrumentForCoverage();
      } else {
        stage1Passes();
        if (!hasErrors()) {
          stage2Passes();
        }
      }
      performPostCompilationTasks();
    }
  } finally {
    generateReport();
  }
  return getResult();
}
 
Example #6
Source File: ClosureCompiler.java    From caja with Apache License 2.0 5 votes vote down vote up
public static String build(Task task, List<File> inputs) {
  List<SourceFile> externs;
  try {
    externs = CommandLineRunner.getDefaultExterns();
  } catch (IOException e) {
    throw new BuildException(e);
  }

  List<SourceFile> jsInputs = new ArrayList<SourceFile>();
  for (File f : inputs) {
    jsInputs.add(SourceFile.fromFile(f));
  }

  CompilerOptions options = new CompilerOptions();
  CompilationLevel.ADVANCED_OPTIMIZATIONS
      .setOptionsForCompilationLevel(options);
  WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
  for (DiagnosticGroup dg : diagnosticGroups) {
    options.setWarningLevel(dg, CheckLevel.ERROR);
  }

  options.setCodingConvention(new GoogleCodingConvention());

  Compiler compiler = new Compiler();
  MessageFormatter formatter =
      options.errorFormat.toFormatter(compiler, false);
  AntErrorManager errorManager = new AntErrorManager(formatter, task);
  compiler.setErrorManager(errorManager);

  Result r = compiler.compile(externs, jsInputs, options);
  if (!r.success) {
    return null;
  }

  String wrapped = "(function(){" + compiler.toSource() + "})();\n";
  return wrapped;
}
 
Example #7
Source File: CompileTask.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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();
  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) {
      writeResult(compiler.toSource());
    } else {
      throw new BuildException("Compilation failed.");
    }
  } else {
    log("None of the files changed. Compilation skipped.");
  }
}
 
Example #8
Source File: SourceMapTestCase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected RunResult compile(
    String js1, String fileName1, String js2, String fileName2) {
  Compiler compiler = new Compiler();
  CompilerOptions options = getCompilerOptions();

  // Turn on IDE mode to get rid of optimizations.
  options.ideMode = 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;
}
 
Example #9
Source File: CompilerUtil.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
public void compile(List<SourceFile> externs, List<SourceFile> inputs) {
  externs =
      ImmutableList.<SourceFile>builder()
          .addAll(externs)
          .addAll(getBuiltInExterns(options.getEnvironment()))
          .build();

  Result result = compiler.compile(externs, inputs, options);
  assertCompiled(result);

  typeRegistry.computeTypeRelationships(compiler.getTopScope(), compiler.getTypeRegistry());
}
 
Example #10
Source File: CompilerTest.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void doCompile(SourceFile input) {
    Result result = compiler.compile(externs, input, options);
    Assume.assumeTrue(result.success);
}
 
Example #11
Source File: ClosureJavaScriptCompressor.java    From htmlcompressor with Apache License 2.0 4 votes vote down vote up
@Override
public String compress(String source) {
	
	StringWriter writer = new StringWriter();
	
	//prepare source
	List<JSSourceFile> input = new ArrayList<JSSourceFile>();
	input.add(JSSourceFile.fromCode("source.js", source));
	
	//prepare externs
	List<JSSourceFile> externsList = new ArrayList<JSSourceFile>();
	if(compilationLevel.equals(CompilationLevel.ADVANCED_OPTIMIZATIONS)) {
		//default externs
		if(!customExternsOnly) {
			try {
				externsList = getDefaultExterns();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//add user defined externs
		if(externs != null) {
			for(JSSourceFile extern : externs) {
				externsList.add(extern);
			}
		}
		//add empty externs
		if(externsList.size() == 0) {
			externsList.add(JSSourceFile.fromCode("externs.js", ""));
		}
	} else {
		//empty externs
		externsList.add(JSSourceFile.fromCode("externs.js", ""));
	}
	
	Compiler.setLoggingLevel(loggingLevel);
	
	Compiler compiler = new Compiler();
	compiler.disableThreads();
	
	compilationLevel.setOptionsForCompilationLevel(compilerOptions);
	warningLevel.setOptionsForWarningLevel(compilerOptions);
	
	Result result = compiler.compile(externsList, input, compilerOptions);
	
	if (result.success) {
		writer.write(compiler.toSource());
	} else {
		writer.write(source);
	}

	return writer.toString();

}
 
Example #12
Source File: CompileEachLineOfProgramOutput.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public static Result compile(String program, int num) {
  SourceFile input = SourceFile.fromCode(""+num, program);
  Compiler compiler = new Compiler();
  Result result = compiler.compile(extern, input, options);
  return result;
}