Java Code Examples for com.google.javascript.jscomp.SourceFile#fromCode()

The following examples show how to use com.google.javascript.jscomp.SourceFile#fromCode() . 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: UndefinedTypesTest.java    From jsinterop-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void undefinedTypes_throwException() {
  SourceFile source =
      SourceFile.fromCode(
          "undefinedtypes.js",
          "/** @externs */\n/** @return {UndefinedType} */ foo = function() {};");

  try {
    runClosureJsInteropGenerator(source);
    fail("test should have throw an AbortException");
  } catch (AbortError ignore) {
    // test succeed
  } catch (Throwable throwable) {
    throw new AssertionError("Unexpected error.", throwable);
  }
}
 
Example 2
Source File: ClosureCompiler.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String compile(String code) {
	Compiler compiler = new Compiler();
	compiler.disableThreads();
	
	SourceFile extern = SourceFile.fromCode("externs.js","function alert(x) {}");
	SourceFile input = SourceFile.fromCode("input.js", code);
	
	compiler.compile(extern, input, options);
	return compiler.toSource();
}
 
Example 3
Source File: JsOptimizerUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
/**
	 * @param code
	 *            JavaScript source code to compile.
	 * @return The compiled version of the code.
	 */
	public static String compile(String code) {
		
//		System.out.println("------------------------");
//		System.out.println(code);
//		System.out.println("------------------------");
		
		Compiler compiler = new Compiler();

		CompilerOptions options = new CompilerOptions();
		// Advanced mode is used here, but additional options could be set, too.
		CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

		// To get the complete set of externs, the logic in
		// CompilerRunner.getDefaultExterns() should be used here.
		SourceFile extern = SourceFile.fromCode("externs.js", "");

		// The dummy input name "input.js" is used here so that any warnings or
		// errors will cite line numbers in terms of input.js.
		SourceFile input = SourceFile.fromCode("input.js", code);

		// compile() returns a Result, but it is not needed here.
		compiler.compile(extern, input, options);

		// The compiler is responsible for generating the compiled code; it is
		// not
		// accessible via the Result.
		return compiler.toSource();
	}
 
Example 4
Source File: Config.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static Function<Path, SourceFile> toSourceFile() {
  return input -> {
    try {
      String content = new String(readAllBytes(input), UTF_8);
      return SourceFile.fromCode(input.toString(), content);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  };
}
 
Example 5
Source File: CompilerUtil.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
public static SourceFile createSourceFile(Path path, String... lines) {
  if (!exists(path)) {
    try {
      if (path.getParent() != null) {
        createDirectories(path.getParent());
      }
      createFile(path);
    } catch (IOException e) {
      throw new AssertionError("unexpected IO error", e);
    }
  }
  return SourceFile.fromCode(path.toString(), Joiner.on("\n").join(lines));
}
 
Example 6
Source File: CompilerTest.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Fuzz
public void testWithString(@From(AsciiStringGenerator.class) String code) {
    SourceFile input = SourceFile.fromCode("input", code);
    doCompile(input);
}
 
Example 7
Source File: SimpleSourceExcerptProvider.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public SimpleSourceExcerptProvider(String source) {
  sourceFile = SourceFile.fromCode("input", source);
}
 
Example 8
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;
}
 
Example 9
Source File: MinerrPass.java    From ng-closure-runner with MIT License 4 votes vote down vote up
private Node createSubstituteMinerrDefinition() {
  SourceFile source = SourceFile.fromCode("MINERR_ASSET", minerrDefSource);
  JsAst ast = new JsAst(source);
  return ast.getAstRoot(compiler).getFirstChild().detachFromParent();
}
 
Example 10
Source File: NodeLibraryImpl.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private static SourceFile loadSource(Path path, boolean isInternal) throws IOException {
  String sourceName = isInternal ? (FILE_NAME_PREFIX + path.getFileName()) : path.toString();
  String content = new String(readAllBytes(path), UTF_8);
  return SourceFile.fromCode(sourceName, content);
}
 
Example 11
Source File: DossierFileSystemTest.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static SourceFile createSourceFile(Path path, String... lines) {
  return SourceFile.fromCode(path.toString(), Joiner.on("\n").join(lines));
}