Search in sources :

Example 66 with TestExternsBuilder

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();"), "");
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 67 with TestExternsBuilder

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)");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 68 with TestExternsBuilder

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);", ""));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 69 with TestExternsBuilder

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');");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 70 with TestExternsBuilder

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);");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Aggregations

TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)107 Test (org.junit.Test)106 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)58 Node (com.google.javascript.rhino.Node)17 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)17 Compiler (com.google.javascript.jscomp.Compiler)8 SourceFile (com.google.javascript.jscomp.SourceFile)7 Color (com.google.javascript.jscomp.colors.Color)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)1 ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)1 SemanticReverseAbstractInterpreter (com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 File (java.io.File)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1