Search in sources :

Example 21 with Color

use of com.google.javascript.jscomp.colors.Color 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 22 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class Es6ConvertSuperTest method testAccessingSuperInstanceElement.

@Test
public void testAccessingSuperInstanceElement() {
    test(externs(lines("/** @dict */", "class A {", "  constructor() { }", "", "  /** @param {number} x */", "  ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", "  constructor() { super(); }", "", "  ['f']() { var t = super['g']; }", "}")), expected(lines("class B extends A {", "  constructor() {", "    super();", "  }", "", "  ['f']() { var t = A.prototype['g']; }", "}")));
    // get types we need to check
    Color classAPrototypeType = Color.createUnion(findClassDefinition(getLastCompiler(), "A").getRootNode().getColor().getPrototypes());
    // A.prototype['g']
    Node superGetelemReplacement = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getSecondChild().getLastChild().getFirstChild().getFirstChild().getOnlyChild();
    assertNode(superGetelemReplacement).hasToken(Token.GETELEM).hasLineno(// position and length of `super['g']`
    5).hasCharno(20);
    // getelem prevents us doing any better than unknown type
    assertNode(superGetelemReplacement).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A.prototype
    Node superReplacement = superGetelemReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
    5).hasCharno(20).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAPrototypeType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 23 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class Es6ConvertSuperTest method testCallingSuperInstanceElement.

@Test
public void testCallingSuperInstanceElement() {
    test(externs(lines("/** @dict */", "class A {", "  constructor() { }", "", "  /** @param {number} x */", "  ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", "  constructor() { super(); }", "", "  ['f']() { super['g'](4); }", "}")), expected(lines("class B extends A {", "  constructor() {", "    super();", "  }", "", "  ['f']() { A.prototype['g'].call(this, 4); }", "}")));
    // get types we need to check
    Color classAPrototypeType = Color.createUnion(findClassDefinition(getLastCompiler(), "A").getRootNode().getColor().getPrototypes());
    Color classBInstanceType = Color.createUnion(findClassDefinition(getLastCompiler(), "B").getRootNode().getColor().getInstanceColors());
    // A.prototype['g'].call(this, 4)
    Node callNode = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getSecondChild().getLastChild().getFirstChild().getOnlyChild();
    assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super['g'](4)`
    5).hasCharno(12);
    // computed property prevents doing any better than StandardColors.UNKNOWN
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A.prototype['g'].call
    Node callee = callNode.getFirstChild();
    assertNode(callee).hasToken(Token.GETPROP).hasLineno(// position and length of `super['g']`
    5).hasCharno(12);
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A.prototype['g']
    Node superGetelemReplacement = callee.getFirstChild();
    assertNode(superGetelemReplacement).hasToken(Token.GETELEM).hasLineno(// position and length of `super['g']`
    5).hasCharno(12);
    // computed property prevents doing any better than StandardColors.UNKNOWN
    assertNode(superGetelemReplacement).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A.prototype
    Node superReplacement = superGetelemReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
    5).hasCharno(12).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAPrototypeType);
    // `this` node from `A.prototype['g'].call(this, 3)`
    Node thisNode = callee.getNext();
    assertNode(thisNode).hasToken(Token.THIS).hasLineno(// position and length of `super['g']`
    5).hasCharno(12).isIndexable(// there's no direct correlation with text in the original source
    false);
    assertNode(thisNode).hasColorThat().isEqualTo(classBInstanceType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 24 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class Es6ConvertSuperTest method testCallingSuperStaticElement.

@Test
public void testCallingSuperStaticElement() {
    test(externs(lines("/** @dict */", "class A {", "  constructor() { }", "", "  /** @param {number} x */", "  static ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", "  constructor() { super(); }", "", "  static ['f']() { super['g'](4); }", "}")), expected(lines("class B extends A {", "  constructor() {", "    super();", "  }", "", "  static ['f']() { A['g'].call(this, 4); }", "}")));
    // get types we need to check
    Color classAType = findClassDefinition(getLastCompiler(), "A").getRootNode().getColor();
    Color classBType = findClassDefinition(getLastCompiler(), "B").getRootNode().getColor();
    // A['g'].call(this, 4)
    Node callNode = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getSecondChild().getLastChild().getFirstChild().getOnlyChild();
    assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super['g'](4)`
    5).hasCharno(19);
    // computed property prevents doing any better than StandardColors.UNKNOWN
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A['g'].call
    Node callee = callNode.getFirstChild();
    assertNode(callee).hasToken(Token.GETPROP).hasLineno(// position and length of `super['g']`
    5).hasCharno(19);
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A['g']
    Node superGetelemReplacement = callee.getFirstChild();
    assertNode(superGetelemReplacement).hasToken(Token.GETELEM).hasLineno(// position and length of `super['g']`
    5).hasCharno(19);
    // computed property prevents doing any better than StandardColors.UNKNOWN
    assertNode(superGetelemReplacement).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A
    Node superReplacement = superGetelemReplacement.getFirstChild();
    assertNode(superReplacement).matchesName("A").hasLineno(// position and length of `super`
    5).hasCharno(19).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAType);
    // `this` node from `A['g'].call(this, 3)`
    Node thisNode = callee.getNext();
    assertNode(thisNode).hasToken(Token.THIS).hasLineno(// position and length of `super['g']`
    5).hasCharno(19).isIndexable(// there's no direct correlation with text in the original source
    false);
    assertNode(thisNode).hasColorThat().isEqualTo(classBType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 25 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class Es6ConvertSuperTest method testAccessingSuperInstanceProperty.

@Test
public void testAccessingSuperInstanceProperty() {
    test(externs(lines("class A {", "  constructor() { }", "", "  /** @param {number} x */", "  g(x) { }", "}")), srcs(lines("class B extends A {", "  constructor() { super(); }", "", "  f() { var t = super.g; }", "}")), expected(lines("class B extends A {", "  constructor() { super(); }", "", "  f() { var t = A.prototype.g; }", "}")));
    // get types we need to check
    Color classAPrototypeType = Color.createUnion(findClassDefinition(getLastCompiler(), "A").getRootNode().getColor().getPrototypes());
    Color aDotGMethodType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("g").getRootNode().getFirstChild().getColor();
    // A.prototype.g
    Node superDotGReplacement = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getOnlyChild().getLastChild().getFirstChild().getFirstChild().getOnlyChild();
    assertNode(superDotGReplacement).matchesQualifiedName("A.prototype.g").hasLineno(// position and length of `super.g`
    4).hasCharno(22);
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
    // A.prototype
    Node superReplacement = superDotGReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
    4).hasCharno(16).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAPrototypeType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Aggregations

Color (com.google.javascript.jscomp.colors.Color)57 Node (com.google.javascript.rhino.Node)37 Test (org.junit.Test)37 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)29 JSType (com.google.javascript.rhino.jstype.JSType)6 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)5 ImmutableSet (com.google.common.collect.ImmutableSet)2 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CompilerPass (com.google.javascript.jscomp.CompilerPass)1 DefaultNameGenerator (com.google.javascript.jscomp.DefaultNameGenerator)1 GatherGetterAndSetterProperties (com.google.javascript.jscomp.GatherGetterAndSetterProperties)1 NameGenerator (com.google.javascript.jscomp.NameGenerator)1 NodeTraversal (com.google.javascript.jscomp.NodeTraversal)1 AbstractPostOrderCallback (com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback)1