Search in sources :

Example 36 with Color

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

the class Es6RewriteClassTest method testSuperCall.

@Test
public void testSuperCall() {
    test(lines(// 
    "class D {}", "class C extends D {", "  constructor() { SUPER: super(); } ", "}"), lines("/** @constructor */", "let D = function() {};", "/** @constructor */", "let C = function() {", "  SUPER: D.call(this);", "}", "$jscomp.inherits(C, D);"));
    Color dType = getNodeWithName(getLastCompiler().getJsRoot(), "D").getColor();
    Color cType = getNodeWithName(getLastCompiler().getJsRoot(), "C").getColor();
    // D.call(this);
    Node callNode = getNodeMatchingLabel(getLastCompiler().getJsRoot(), "SUPER").getOnlyChild();
    assertNode(callNode).hasToken(Token.CALL);
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.NULL_OR_VOID);
    // D.call
    Node callee = callNode.getFirstChild();
    assertNode(callee).matchesQualifiedName("D.call");
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
    // D
    Node superDotGReplacement = callee.getFirstChild();
    assertNode(superDotGReplacement).matchesQualifiedName("D");
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(dType);
    // `this` node from `D.call(this)`
    Node thisNode = callee.getNext();
    assertNode(thisNode).hasToken(Token.THIS);
    assertThat(cType.getInstanceColors()).containsExactly(thisNode.getColor());
}
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 37 with Color

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

the class Es6ConvertSuperTest method testSynthesizingConstructorOfBaseInterface.

@Test
public void testSynthesizingConstructorOfBaseInterface() {
    test(externs(""), srcs("/** @interface */ class A { }"), expected("/** @interface */ class A { constructor() { } }"));
    // class A { ... }
    Node classANode = findClassDefinition(getLastCompiler(), "A").getRootNode();
    Color classAConstructorType = classANode.getColor();
    // constructor() { }
    Node constructorMemberFunctionDefForA = classANode.getLastChild().getFirstChild();
    assertNode(constructorMemberFunctionDefForA).isMemberFunctionDef("constructor").hasLineno(// synthetic constructor gets position and length of original class definition
    1).hasCharno(18).isIndexable(false);
    assertNode(constructorMemberFunctionDefForA).hasColorThat().isEqualTo(classAConstructorType);
    Node constructorFunctionForA = constructorMemberFunctionDefForA.getOnlyChild();
    assertNode(constructorFunctionForA).hasToken(Token.FUNCTION).hasLineno(// synthetic constructor gets position and length of original class definition
    1).hasCharno(18).isIndexable(false);
    assertNode(constructorFunctionForA).hasColorThat().isEqualTo(classAConstructorType);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 38 with Color

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

the class Es6ConvertSuperTest method testResolvingSuperInSetter.

@Test
public void testResolvingSuperInSetter() {
    test(externs(lines("class A {", "  constructor() { }", "", "  /** @param {number} x @return {string} */", "  g(x) { }", "}")), srcs(lines("class B extends A {", "  constructor() { super(); }", "", "  /** @param {number} x */", "  set f(x) { super.g(x); }", "}")), expected(lines("class B extends A {", "  constructor() { super(); }", "", "  set f(x) { A.prototype.g.call(this, x); }", "}")));
    // 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();
    // colors do not track ".call" type
    Color aDotGDotCallType = StandardColors.UNKNOWN;
    Color classBInstanceType = Color.createUnion(findClassDefinition(getLastCompiler(), "B").getRootNode().getColor().getInstanceColors());
    // A.prototype.g.call(this, x)
    Node callNode = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getOnlyChild().getLastChild().getFirstChild().getOnlyChild();
    assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super.g(3)`
    5).hasCharno(13);
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.STRING);
    // A.prototype.g.call
    Node callee = callNode.getFirstChild();
    assertNode(callee).matchesQualifiedName("A.prototype.g.call").hasLineno(// position and length of `super.g`
    5).hasCharno(19);
    assertNode(callee).hasColorThat().isEqualTo(aDotGDotCallType);
    // A.prototype.g
    Node superDotGReplacement = callee.getFirstChild();
    assertNode(superDotGReplacement).matchesQualifiedName("A.prototype.g").hasLineno(// position and length of `super.g`
    5).hasCharno(19);
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
    // A.prototype
    Node superReplacement = superDotGReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
    5).hasCharno(13).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(19).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 39 with Color

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

the class Es6ConvertSuperTest method testAccessingSuperStaticProperty.

@Test
public void testAccessingSuperStaticProperty() {
    test(externs(lines("class A {", "  constructor() { }", "", "  /** @param {number} x */", "  static g(x) { }", "}")), srcs(lines("class B extends A {", "  constructor() { super(); }", "", "  static f() { var t = super.g; }", "}")), expected(lines("class B extends A {", "  constructor() { super(); }", "", "  static f() { var t = A.g; }", "}")));
    // get types we need to check
    Color classAType = findClassDefinition(getLastCompiler(), "A").getRootNode().getColor();
    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.g").hasLineno(// position and length of `super.g`
    4).hasCharno(29);
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
    // A.prototype
    Node superReplacement = superDotGReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A").hasLineno(// position and length of `super`
    4).hasCharno(23).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAType);
}
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 40 with Color

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

the class Es6ConvertSuperTest method testCallingSuperInstanceProperty.

// Instance `super` resolution
@Test
public void testCallingSuperInstanceProperty() {
    test(externs(lines("class A {", "  constructor() { }", "", "  /** @param {number} x @return {string} */", "  g(x) { }", "}")), srcs(lines("class B extends A {", "  constructor() { super(); }", "", "  f() { super.g(3); }", "}")), expected(lines("class B extends A {", "  constructor() { super(); }", "", "  f() { A.prototype.g.call(this, 3); }", "}")));
    // 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();
    Color classBInstanceType = Color.createUnion(findClassDefinition(getLastCompiler(), "B").getRootNode().getColor().getInstanceColors());
    // A.prototype.g.call(this, 3)
    Node callNode = findClassDefinition(getLastCompiler(), "B").findMethodDefinition("f").findMatchingQNameReferences("A.prototype.g.call").get(// GETPROP node for A.prototype.g.call
    0).getParent();
    assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super.g(3)`
    4).hasCharno(8);
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.STRING);
    // A.prototype.g.call
    Node callee = callNode.getFirstChild();
    assertNode(callee).matchesQualifiedName("A.prototype.g.call").hasLineno(// position and length of `super.g`
    4).hasCharno(14);
    assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
    // A.prototype.g
    Node superDotGReplacement = callee.getFirstChild();
    assertNode(superDotGReplacement).matchesQualifiedName("A.prototype.g").hasLineno(// position and length of `super.g`
    4).hasCharno(14);
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
    // A.prototype
    Node superReplacement = superDotGReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
    4).hasCharno(8).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`
    4).hasCharno(14).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)

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