com.google.javascript.jscomp.SourceFile Java Examples

The following examples show how to use com.google.javascript.jscomp.SourceFile. 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: TestUtil.java    From jsinterop-generator with Apache License 2.0 6 votes vote down vote up
private static Options createOptions(SourceFile sourceFile) {
  return Options.builder()
      .sources(ImmutableList.of(sourceFile))
      .outputJarFile("unused")
      .outputDependencyFile("unused")
      .globalScopeClassName("unused")
      .extensionTypePrefix("unused")
      .debugEnabled(false)
      .strict(true)
      .dependencyMappingFiles(ImmutableList.of())
      .dependencies(ImmutableList.of())
      .nameMappingFiles(ImmutableList.of())
      .integerEntitiesFiles(ImmutableList.of())
      .wildcardTypesFiles(ImmutableList.of())
      .customPreprocessingPasses(ImmutableList.of())
      .build();
}
 
Example #2
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 #3
Source File: DossierCompiler.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends SourceFile> void initModules(
    List<T> externs, List<JSModule> modules, CompilerOptions options) {
  checkArgument(modules.size() == 1, "only expecting 1 module, but got %s", modules.size());
  List<? extends SourceFile> externList = externs;
  if (!modulePaths.isEmpty()) {
    try {
      externList = concat(externs, nodeLibrary.getExternFiles());

      JSModule module = modules.iterator().next();
      nodeLibrary.getExternModules().forEach(module::add);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  super.initModules(externList, modules, options);
}
 
Example #4
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 #5
Source File: NodeModulePassTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void sortsSingleModuleDep() {
  CompilerUtil compiler = createCompiler(path("foo/leaf.js"), path("foo/root.js"));

  SourceFile root = createSourceFile(path("foo/root.js"), "");
  SourceFile leaf = createSourceFile(path("foo/leaf.js"), "require('./root');");

  compiler.compile(leaf, root); // Should reorder since leaf depends on root.

  assertThat(compiler.toSource())
      .startsWith(
          lines(
              "var module$exports$module$foo$root = {};",
              "var module$exports$module$foo$leaf = {};"));
  assertIsNodeModule("module$exports$module$foo$leaf", "foo/leaf.js");
  assertIsNodeModule("module$exports$module$foo$root", "foo/root.js");
}
 
Example #6
Source File: NodeModulePassTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void sortsWithTwoModuleDeps() {
  CompilerUtil compiler =
      createCompiler(path("foo/one.js"), path("foo/two.js"), path("foo/three.js"));

  SourceFile one = createSourceFile(path("foo/one.js"), "");
  SourceFile two =
      createSourceFile(path("foo/two.js"), "require('./one');", "require('./three');");
  SourceFile three = createSourceFile(path("foo/three.js"));

  compiler.compile(two, one, three); // Should properly reorder inputs.

  assertThat(compiler.toSource())
      .startsWith(
          lines(
              "var module$exports$module$foo$one = {};",
              "var module$exports$module$foo$three = {};",
              "var module$exports$module$foo$two = {};"));
  assertIsNodeModule("module$exports$module$foo$one", "foo/one.js");
  assertIsNodeModule("module$exports$module$foo$two", "foo/two.js");
  assertIsNodeModule("module$exports$module$foo$three", "foo/three.js");
}
 
Example #7
Source File: Runner.java    From jsinterop-generator with Apache License 2.0 6 votes vote down vote up
private void run() {
  Options options =
      Options.builder()
          .outputJarFile(output)
          .outputDependencyFile(outputDependencyPath)
          .packagePrefix(packagePrefix)
          .extensionTypePrefix(extensionTypePrefix)
          .globalScopeClassName(globalScopeClassName)
          .debugEnabled(debugEnabled)
          .strict(strict)
          .dependencyMappingFiles(dependencyMappingFilePaths)
          .nameMappingFiles(nameMappingFilePaths)
          .integerEntitiesFiles(integerEntitiesFiles)
          .wildcardTypesFiles(wildcardTypesFiles)
          .dependencies(
              dependencyFilePaths.stream().map(SourceFile::fromFile).collect(toImmutableList()))
          .sources(sourceFilePaths.stream().map(SourceFile::fromFile).collect(toImmutableList()))
          .customPreprocessingPasses(customPreprocessingPasses)
          .build();
  try {
    new ClosureJsInteropGenerator(options).convert();
  } catch (AbortError error) {
    System.exit(1);
  }
}
 
Example #8
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
String generateDeclarations(
    List<SourceFile> sourceFiles, List<SourceFile> externs, Depgraph depgraph)
    throws AssertionError {
  // Compile should always be first here, because it sets internal state.
  compiler.compile(externs, sourceFiles, opts.getCompilerOptions());
  importRenameMap =
      new ImportRenameMapBuilder()
          .build(compiler.getParsedInputs(), opts.depgraph.getGoogProvides());
  aliasMap =
      new AliasMapBuilder().build(compiler.getParsedInputs(), opts.depgraph.getGoogProvides());
  legacyNamespaceReexportMap =
      new LegacyNamespaceReexportMapBuilder()
          .build(compiler.getParsedInputs(), opts.depgraph.getGoogProvides());

  unknownType = compiler.getTypeRegistry().getNativeType(JSTypeNative.UNKNOWN_TYPE);
  numberType = compiler.getTypeRegistry().getNativeType(JSTypeNative.NUMBER_TYPE);
  stringType = compiler.getTypeRegistry().getNativeType(JSTypeNative.STRING_TYPE);

  iteratorIterableType = compiler.getTypeRegistry().getGlobalType("IteratorIterable");

  arrayType = compiler.getTypeRegistry().getGlobalType("Array");
  // TODO(rado): replace with null and do not emit file when errors.
  String dts = "";
  // If there is an error top scope is null.
  if (compiler.getTopScope() != null) {
    precomputeChildLists();
    collectTypedefs();
    dts = produceDts();
  }
  errorManager.doGenerateReport();
  return dts;
}
 
Example #9
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 #10
Source File: IRFactoryTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node newParse(String string, TestErrorReporter errorReporter) {
  CompilerEnvirons environment = new CompilerEnvirons();

  environment.setRecordingComments(true);
  environment.setRecordingLocalJsDocComments(true);

  Parser p = new Parser(environment);
  AstRoot script = p.parse(string, null, 1);

  Config config = ParserRunner.createConfig(true, mode, false);
  Node root = IRFactory.transformTree(
      script, SourceFile.fromCode("FileName.js", string), string, config, errorReporter);

  return root;
}
 
Example #11
Source File: SourceExtractor.java    From clutz with MIT License 5 votes vote down vote up
public SourceExtractor(Iterable<SourceFile> sourceFiles) {
  ImmutableMap.Builder<String, SourceFile> builder = ImmutableMap.builder();
  for (SourceFile sf : sourceFiles) {
    builder.put(sf.getName(), sf);
  }
  this.sourceFiles = builder.build();
}
 
Example #12
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
static List<SourceFile> getDefaultExterns(Options opts) {
  try {
    return AbstractCommandLineRunner.getBuiltinExterns(
        opts.getCompilerOptions().getEnvironment());
  } catch (IOException e) {
    throw new RuntimeException("Could not locate builtin externs", e);
  }
}
 
Example #13
Source File: TypeScriptGenerator.java    From clutz with MIT License 5 votes vote down vote up
/** Returns a list of source files from a list of file names. */
private static List<SourceFile> getFiles(Collection<String> fileNames) throws IOException {
  List<SourceFile> files = new ArrayList<>(fileNames.size());
  for (String fileName : fileNames) {
    if (!fileName.endsWith(".zip")) {
      files.add(SourceFile.fromFile(fileName, UTF_8));
      continue;
    }

    getJsEntryPathsFromZip(fileName).stream()
        .map(p -> SourceFile.fromPath(p, UTF_8))
        .forEach(files::add);
  }
  return files;
}
 
Example #14
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 #15
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 #16
Source File: CompileTask.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates an Ant file list into the file format that the compiler
 * expects.
 */
private List<SourceFile> findJavaScriptFiles(FileList fileList) {
  List<SourceFile> files = Lists.newLinkedList();
  File baseDir = fileList.getDir(getProject());

  for (String included : fileList.getFiles(getProject())) {
    files.add(SourceFile.fromFile(new File(baseDir, included),
        Charset.forName(encoding)));
  }

  return files;
}
 
Example #17
Source File: CompileTask.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates an Ant Path into the file list format that the compiler
 * expects.
 */
private List<SourceFile> findJavaScriptFiles(Path path) {
  List<SourceFile> files = Lists.newArrayList();

  for (String included : path.list()) {
    files.add(SourceFile.fromFile(new File(included),
        Charset.forName(encoding)));
  }

  return files;
}
 
Example #18
Source File: CompileTask.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the default externs set.
 *
 * Adapted from {@link CommandLineRunner}.
 */
private List<SourceFile> getDefaultExterns() {
  try {
    return CommandLineRunner.getDefaultExterns();
  } catch (IOException e) {
    throw new BuildException(e);
  }
}
 
Example #19
Source File: CompileTask.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private List<SourceFile> findExternFiles() {
  List<SourceFile> files = Lists.newLinkedList();
  if (!this.customExternsOnly) {
    files.addAll(getDefaultExterns());
  }

  for (FileList list : this.externFileLists) {
    files.addAll(findJavaScriptFiles(list));
  }

  return files;
}
 
Example #20
Source File: DepsGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new DepsGenerator.
 */
public DepsGenerator(
    Collection<SourceFile> deps,
    Collection<SourceFile> srcs,
    InclusionStrategy mergeStrategy,
    String closurePathAbs,
    ErrorManager errorManager) {
  this.deps = deps;
  this.srcs = srcs;
  this.mergeStrategy = mergeStrategy;
  this.closurePathAbs = closurePathAbs;
  this.errorManager = errorManager;
}
 
Example #21
Source File: DepsGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static List<SourceFile> createSourceFilesFromPaths(
    Collection<String> paths) {
  List<SourceFile> files = Lists.newArrayList();
  for (String path : paths) {
    files.add(SourceFile.fromFile(path));
  }
  return files;
}
 
Example #22
Source File: DepsGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses all source files for dependency information.
 * @param preparsedFiles A set of closure-relative paths.
 *     Files in this set are not parsed if they are encountered in srcs.
 * @return Returns a map of closure-relative paths -> DependencyInfo for the
 *     newly parsed files.
 * @throws IOException Occurs upon an IO error.
 */
private Map<String, DependencyInfo> parseSources(
    Set<String> preparsedFiles) throws IOException {
  Map<String, DependencyInfo> parsedFiles = Maps.newHashMap();
  JsFileParser jsParser = new JsFileParser(errorManager);

  for (SourceFile file : srcs) {
    String closureRelativePath =
        PathUtil.makeRelative(
            closurePathAbs, PathUtil.makeAbsolute(file.getName()));
    logger.fine("Closure-relative path: " + closureRelativePath);

    if (InclusionStrategy.WHEN_IN_SRCS == mergeStrategy ||
        !preparsedFiles.contains(closureRelativePath)) {
      DependencyInfo depInfo =
          jsParser.parseFile(
              file.getName(), closureRelativePath,
              file.getCode());

      // Kick the source out of memory.
      file.clearCachedSource();
      parsedFiles.put(closureRelativePath, depInfo);
    }
  }

  return parsedFiles;
}
 
Example #23
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 #24
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 #25
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 #26
Source File: DossierCommandLineRunner.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Override
protected List<SourceFile> createInputs(
    List<FlagEntry<JsSourceType>> files,
    List<JsonFileSpec> jsonFiles,
    boolean allowStdIn,
    List<JsModuleSpec> jsModuleSpecs)
    throws IOException {
  // Compiler defaults to reading from stdin if no files specified, but we don't support that.
  if (files.size() == 1 && "-".equals(files.get(0).getValue())) {
    return ImmutableList.of();
  }

  ImmutableList.Builder<SourceFile> inputs = ImmutableList.builder();
  for (FlagEntry<JsSourceType> flagEntry : files) {
    checkArgument(
        flagEntry.getFlag() != JsSourceType.JS_ZIP,
        "Zip file inputs are not supported: %s",
        flagEntry.getValue());
    checkArgument(!"-".equals(flagEntry.getValue()), "Reading from stdin is not supported");

    Path path = inputFileSystem.getPath(flagEntry.getValue());
    try (InputStream inputStream = newInputStream(path)) {
      SourceFile file = SourceFile.fromInputStream(path.toString(), inputStream, UTF_8);
      inputs.add(file);
    }
  }
  return inputs.build();
}
 
Example #27
Source File: TypeExpressionParserTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void parseExpression_externalEnumReference() {
  ImmutableList<SourceFile> externs =
      ImmutableList.of(
          createSourceFile(
              fs.getPath("externs.js"), "/** @enum {string} */", "var Data = {ONE: 'one'};"));

  ImmutableList<SourceFile> sources =
      ImmutableList.of(
          createSourceFile(
              fs.getPath("one.js"),
              "/**",
              " * @param {Data} x .",
              " * @constructor",
              " */",
              "function Widget(x) {}"));
  util.compile(externs, sources);

  NominalType type = typeRegistry.getType("Widget");
  JSTypeExpression jsTypeExpression = type.getJsDoc().getParameter("x").getType();
  JSType jsType = util.evaluate(jsTypeExpression);
  TypeExpression expression = parserFactory.create(linkFactoryBuilder.create(type)).parse(jsType);
  assertThat(expression)
      .isEqualTo(
          TypeExpression.newBuilder()
              .setNamedType(NamedType.newBuilder().setName("Data"))
              .build());
}
 
Example #28
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 #29
Source File: CompilerUtil.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static ImmutableListMultimap<Environment, SourceFile> loadBuiltinExterns() {
  ImmutableListMultimap.Builder<Environment, SourceFile> externs =
      ImmutableListMultimap.builder();
  for (Environment environment : Environment.values()) {
    try {
      externs.putAll(environment, AbstractCommandLineRunner.getBuiltinExterns(environment));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  return externs.build();
}
 
Example #30
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));
}