use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testSpreadArgumentsPassedToSuperDoesNotPreventRemoval.
@Test
public void testSpreadArgumentsPassedToSuperDoesNotPreventRemoval() {
CompilerOptions options = createCompilerOptions();
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
// Create a noninjecting compiler avoid comparing all the polyfill code.
useNoninjectingCompiler = true;
// include externs definitions for the stuff that would have been injected
ImmutableList.Builder<SourceFile> externsList = ImmutableList.builder();
externsList.add(SourceFile.fromCode("extraExterns", new TestExternsBuilder().addExtra("/** @type {!Global} */ var globalThis;").addJSCompLibraries().build()));
externs = externsList.build();
test(options, lines(// preserve newlines
"", "class A {", " constructor(a, b) {", " this.a = a;", " this.b = b;", " }", "}", "class B extends A {", " constructor() {", " super(...arguments);", " }", "}", "var b = new B();"), "");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testBigInt.
@Test
public void testBigInt() {
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
// Confirm that the native BigInt type has been implemented, so the BigInt externs don't cause
// errors.
externs = ImmutableList.of(new TestExternsBuilder().addBigInt().buildExternsFile("externs.js"));
testSame(options, "BigInt(1)");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testObjectLiteralPropertyNamesUsedInDestructuringAssignment.
@Test
public void testObjectLiteralPropertyNamesUsedInDestructuringAssignment() {
CompilerOptions options = createCompilerOptions();
options.setLanguageOut(LanguageMode.ECMASCRIPT_2017);
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
externs = ImmutableList.of(new TestExternsBuilder().addMath().addConsole().buildExternsFile("externs.js"));
test(options, lines(//
"const X = { a: 1, b: 2 };", "const { a, b } = X;", "console.log(a, b);", ""), "console.log(1,2);");
test(options, lines(//
"const X = { a: 1, b: 2 };", "let { a, b } = X;", "const Y = { a: 4, b: 5 };", "({ a, b } = Y);", "console.log(a, b);", ""), lines(//
"", "let {a, b} = {a:1, b:2};", "({a, b} = {a:4, b:5});", "console.log(a, b);"));
// Demonstrates https://github.com/google/closure-compiler/issues/3671
test(options, lines(//
"const X = { a: 1, b: 2 };", "const Y = { a: 1, b: 2 };", // the properties on X and Y.
"const { a, b } = Math.random() ? X : Y;", "console.log(a, b);", ""), lines(//
"const a = { a: 1, b: 2},", " b = { a: 1, b: 2},", " {a:c, b:d} = Math.random() ? a : b;", "console.log(c, d);", ""));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testDisambiguationOfForwardReferenedTemplatizedAliasedInterface.
@Test
public void testDisambiguationOfForwardReferenedTemplatizedAliasedInterface() {
// This test case is nearly identical to the one above.
// The only difference here is the interfaces are templatized to makes sure
// resolving the templates doesn't interfere with handling the aliasing
// of the interface.
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setLanguageOut(LanguageMode.NO_TRANSPILE);
options.setDisambiguateProperties(true);
options.setPrettyPrint(true);
options.setPreserveTypeAnnotations(true);
externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs.js"));
test(options, lines("", // but it does have a matching `foo()` method.
"class MyBaseClass {", " /** @return {string} */", " foo() {", " console.log('I should exist');", " return 'return value';", " }", "}", "", // Note that `AliasedInterface` isn't defined yet.
"/** @implements {AliasedInterface<string>} */", "class MyClass extends MyBaseClass {", "}", "", // code doesn't appear to do this.
"/**", " * @record", " * @template T", " */", "function OriginalInterface() { }", "/** @return {T} */", "OriginalInterface.prototype.foo = function () { };", "", // error.
"/** @const */", "const AliasedInterface = OriginalInterface;", "", // `MyClass.prototype.foo`.
"/** @return {!AliasedInterface<string>} */", "function magicFactory() {", " return new MyClass();", "}", "", "magicFactory().foo();"), // up inlined as the only output statement.
"console.log('I should exist');");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AdvancedOptimizationsIntegrationTest method testUndefinedNameReferencedInCodeAndExterns.
@Test
public void testUndefinedNameReferencedInCodeAndExterns() {
// NOTE(lharker): I added this test as a regression test for existing compiler behavior.
CompilerOptions options = createCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addAlert().addExtra("/** @fileoverview @suppress {externsValidation} */ foobar").buildExternsFile("externs.js"));
// with just variable renaming on, the code is unchanged becasue of the 'foobar' externs ref.
options.setVariableRenaming(VariableRenamingPolicy.ALL);
test(options, "foobar = {x: 1}; alert(foobar.x);", "foobar = {x:1}; alert(foobar.x);");
// with inlining + other advanced optimizations, foobar.x is renamed
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
test(options, "foobar = {x: 1}; alert(foobar.x);", "foobar = {a: 1}; alert(foobar.a);");
}
Aggregations