Search in sources :

Example 41 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class RewriteAsyncFunctionsTest method testInnerSuperCall.

@Test
public void testInnerSuperCall() {
    test(externs(new TestExternsBuilder().addPromise().addJSCompLibraries().build()), srcs(lines("class A {", "  m() {", "    return Promise.resolve(this);", "  }", "}", "class X extends A {", "  async m() {", "    return super.m();", "  }", "}")), expected(lines("class A {", "  m() {", "    return Promise.resolve(this);", "  }", "}", "class X extends A {", "  m() {", "    const $jscomp$async$this = this;", "    const $jscomp$async$super$get$m = () => super.m;", "    return $jscomp.asyncExecutePromiseGeneratorFunction(", "        function* () {", "          return $jscomp$async$super$get$m().call($jscomp$async$this);", "        });", "  }", "}")));
    Color classAInstanceType = getGlobalInstanceColor("A");
    // type of A.prototype.m
    Color classAPropertyMType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("m").getRootNode().getColor();
    CodeSubTree classXMethodMDefinition = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m");
    // Check type information on wrapper function for `super.m`
    ImmutableList<Node> superMethodWrapperNameNodes = classXMethodMDefinition.findMatchingQNameReferences("$jscomp$async$super$get$m");
    // one declaration and one reference
    assertThat(superMethodWrapperNameNodes).hasSize(2);
    // first name node is declaration
    // const $jscomp$async$super$get$m = () => super.m;
    Node wrapperDeclarationNameNode = superMethodWrapperNameNodes.get(0);
    Node wrapperArrowFunction = wrapperDeclarationNameNode.getOnlyChild();
    // optimization colors don't track function signatures
    assertNode(wrapperArrowFunction).isArrowFunction().hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
    // wrapper function variable has type matching the function itself
    Color wrapperArrowColor = wrapperArrowFunction.getColor();
    assertNode(wrapperDeclarationNameNode).hasColorThat().isEqualTo(wrapperArrowColor);
    // get `super.m` from `() => `super.m`
    Node superDotM = wrapperArrowFunction.getLastChild();
    assertNode(superDotM).matchesQualifiedName("super.m").hasColorThat().isEqualTo(classAPropertyMType);
    Node superNode = superDotM.getFirstChild();
    assertNode(superNode).isSuper().hasColorThat().isEqualTo(classAInstanceType);
    // second name node is reference
    // return $jscomp$async$super$get$m().call($jscomp$async$this);
    Node wrapperReferenceNameNode = superMethodWrapperNameNodes.get(1);
    // optimization colors don't track function signatures
    assertNode(wrapperArrowFunction).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
    // `$jscomp$async$super$get$m()`
    Node wrapperCallNode = wrapperReferenceNameNode.getParent();
    assertNode(wrapperCallNode).isCall().hasColorThat().isEqualTo(classAPropertyMType);
    // `$jscomp$async$super$get$m().call($jscomp$async$this)`
    Node methodCallNode = wrapperCallNode.getGrandparent();
    // optimization colors don't track .call types
    assertNode(methodCallNode).isCall().hasColorThat().isEqualTo(StandardColors.UNKNOWN);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) CodeSubTree(com.google.javascript.jscomp.testing.CodeSubTree) Test(org.junit.Test)

Example 42 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class OptionalChainingIntegrationTest method testRemoveUnusedCode.

@Test
public void testRemoveUnusedCode() {
    CompilerOptions options = createCompilerOptions();
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
    test(options, lines(// 
    "class MyClass {", "  method() {", "    return 4;", "  }", "}", "", "/**", " * @param {?MyClass} maybeMyClass", " */", "function maybeLog(maybeMyClass) {", // optional chaining call
    "  maybeMyClass?.method()", "}", "", "maybeLog(new MyClass());", ""), "");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 43 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class OptionalChainingIntegrationTest method testInline.

@Test
public void testInline() {
    CompilerOptions options = createCompilerOptions();
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
    test(options, lines(// 
    "class MyClass {", "  method() {", "    return 4;", "  }", "}", // regular call gets devirtualized and inlined
    "let x = (new MyClass()).method();", "console.log(x);", ""), "console.log(4);");
    test(options, lines(// 
    "class MyClass {", "  method() {", "    return 4;", "  }", "}", "let x = (new MyClass())?.method();", "console.log(x);", ""), lines(// 
    "class a {", "  a() {", "    return 4;", "  }", "}", // optional call is not devirtualized and not inlined
    "console.log((new a)?.a());"));
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 44 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class OptionalChainingIntegrationTest method bareMinimumTranspileTest.

@Test
public void bareMinimumTranspileTest() {
    CompilerOptions options = createCompilerOptions();
    // transpile
    options.setLanguageOut(LanguageMode.ECMASCRIPT_2019);
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
    test(options, lines(// 
    "class MyClass {", "  method(msg) {", "    console.log(msg);", "  }", "}", "", "/**", " * @param {?MyClass} maybeMyClass", " */", "function maybeLog(maybeMyClass) {", "  maybeMyClass?.method('log message')", "}", "", "maybeLog(null);", "maybeLog(new MyClass());", ""), "console.log('log message');");
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 45 with TestExternsBuilder

use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.

the class OptionalChainingIntegrationTest method testCheckTypes.

@Test
public void testCheckTypes() {
    CompilerOptions options = createCompilerOptions();
    externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
    options.setCheckTypes(true);
    // JSC_WRONG_ARGUMENT_COUNT error is reported
    test(options, "var x = x || {}; x.f = function() {}; x?.f(3);", DiagnosticGroups.CHECK_TYPES);
}
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