use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.
the class TypedAstIntegrationTest method disambiguatesAndDeletesMethodsAcrossLibraries.
@Test
public void disambiguatesAndDeletesMethodsAcrossLibraries() throws IOException {
SourceFile lib1 = code("class Lib1 { m() { return 'lib1'; } n() { return 'delete me'; } }");
SourceFile lib2 = code("class Lib2 { m() { return 'delete me'; } n() { return 'lib2'; } }");
precompileLibrary(lib1);
precompileLibrary(lib2);
precompileLibrary(extern(new TestExternsBuilder().addAlert().build()), typeSummary(lib1), typeSummary(lib2), code("alert(new Lib1().m()); alert(new Lib2().n());"));
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setDisambiguateProperties(true);
Compiler compiler = compileTypedAstShards(options);
Node expectedRoot = parseExpectedCode("", "", "alert('lib1'); alert('lib2')");
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.
the class TypedAstIntegrationTest method precompileLibrary.
private void precompileLibrary(SourceFile... files) throws IOException {
Path typedAstPath = Files.createTempFile("", ".typedast");
CompilerOptions options = new CompilerOptions();
options.setChecksOnly(true);
WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
options.setProtectHiddenSideEffects(true);
options.setTypedAstOutputFile(typedAstPath);
ImmutableList.Builder<SourceFile> externs = ImmutableList.builder();
ImmutableList.Builder<SourceFile> sources = ImmutableList.builder();
for (SourceFile file : files) {
if (file.isExtern()) {
externs.add(file);
} else {
sources.add(file);
}
}
Compiler compiler = new Compiler();
compiler.init(externs.build(), sources.build(), options);
compiler.parse();
checkUnexpectedErrorsOrWarnings(compiler, 0);
// serializes a TypedAST into typedAstPath
compiler.stage1Passes();
checkUnexpectedErrorsOrWarnings(compiler, 0);
this.shards.add(typedAstPath);
}
use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.
the class TypedAstIntegrationTest method testDefineCheck.
@Test
public void testDefineCheck() throws IOException {
precompileLibrary(code(""));
CompilerOptions options = new CompilerOptions();
options.setDefineReplacements(ImmutableMap.of("FOOBAR", 1));
Compiler compiler = compileTypedAstShardsWithoutErrorChecks(options);
assertThat(compiler.getWarnings()).comparingElementsUsing(JSCompCorrespondences.OWNING_DIAGNOSTIC_GROUP).containsExactly(DiagnosticGroups.UNKNOWN_DEFINES);
assertThat(compiler.getErrors()).isEmpty();
}
use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.
the class TypedAstIntegrationTest method typeSummary.
/**
* Runs the type summary generator on the given source code, and returns a summary file
*/
private SourceFile typeSummary(SourceFile original) {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.setIncrementalChecks(IncrementalCheckMode.GENERATE_IJS);
compiler.init(ImmutableList.of(), ImmutableList.of(original), options);
compiler.parse();
checkUnexpectedErrorsOrWarnings(compiler, 0);
compiler.stage1Passes();
checkUnexpectedErrorsOrWarnings(compiler, 0);
return SourceFile.fromCode(original.getName(), compiler.toSource());
}
use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.
the class TypedAstIntegrationTest method protectsHiddenSideEffects.
@Test
public void protectsHiddenSideEffects() throws IOException {
precompileLibrary(extern("const foo = {}; foo.bar;"), code("/** @fileoverview @suppress {uselessCode} */ foo.bar;"));
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
Compiler compiler = compileTypedAstShards(options);
Node expectedRoot = parseExpectedCode("foo.bar");
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
Aggregations