use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class IntegrationTest method testSubstituteEs6Syntax.
@Test
public void testSubstituteEs6Syntax() {
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT_IN);
options.setLanguageOut(LanguageMode.ECMASCRIPT_NEXT);
externs = ImmutableList.of(SourceFile.fromCode("testExterns.js", new TestExternsBuilder().addConsole().build()));
// This is a regression test to confirm both that SubstituteEs6Syntax switches to object
// literal shorthand here and that it correctly reports that it has added that feature.
// IntegrationTestCase runs `ValidityCheck` to report an error if a feature usage gets added to
// the AST without that addition being recorded.
test(options, //
"console.log({console: console});", "console.log({console});");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class TypedAstIntegrationTest method removesRegExpCallsIfUnsafelyReferenced.
@Test
public void removesRegExpCallsIfUnsafelyReferenced() throws IOException {
precompileLibrary(extern(new TestExternsBuilder().addRegExp().addConsole().build()), code(//
"(/abc/gi).exec('');", "console.log(RegExp.$1);"));
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
Compiler compiler = compileTypedAstShards(options);
Node expectedRoot = parseExpectedCode("(/abc/gi).exec(''); console.log(RegExp.$1);");
assertNode(compiler.getRoot().getSecondChild()).usingSerializer(compiler::toSource).isEqualTo(expectedRoot);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder 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.testing.TestExternsBuilder in project closure-compiler by google.
the class CompilerTest method testExternsFileAsEntryPoint3.
@Test
public void testExternsFileAsEntryPoint3() throws Exception {
// Test code reference to an extern that doesn't exist,
// but the extern and source files are both entry points
List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode("/externs.js", "/** @fileoverview @externs */ /** @const {number} */ var bar = 1;"), SourceFile.fromCode("/foo.js", "console.log(nonexistentExtern);"));
List<ModuleIdentifier> entryPoints = ImmutableList.of(ModuleIdentifier.forFile("/externs.js"), ModuleIdentifier.forFile("/foo.js"));
CompilerOptions options = createNewFlagBasedOptions();
options.setDependencyOptions(DependencyOptions.pruneForEntryPoints(entryPoints));
List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("default_externs.js"));
Compiler compiler = new Compiler();
compiler.compile(externs, inputs, options);
Result result = compiler.getResult();
assertThat(result.errors).hasSize(1);
assertThat(result.errors.get(0).getType()).isEqualTo(VarCheck.UNDEFINED_VAR_ERROR);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class CompilerTest method testExternsFileAsEntryPoint2.
@Test
public void testExternsFileAsEntryPoint2() throws Exception {
// Test code reference to an extern that doesn't exist,
// but the extern is still the sole entry point.
List<SourceFile> inputs = ImmutableList.of(SourceFile.fromCode("/externs.js", "/** @fileoverview @externs */ /** @const {number} */ var bar = 1;"), SourceFile.fromCode("/foo.js", "console.log(nonexistentExtern);"));
List<ModuleIdentifier> entryPoints = ImmutableList.of(ModuleIdentifier.forFile("/externs.js"));
CompilerOptions options = createNewFlagBasedOptions();
options.setDependencyOptions(DependencyOptions.pruneForEntryPoints(entryPoints));
List<SourceFile> externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("default_externs.js"));
Compiler compiler = new Compiler();
compiler.compile(externs, inputs, options);
Result result = compiler.getResult();
assertThat(result.errors).isEmpty();
assertThat(compiler.toSource()).isEmpty();
}
Aggregations