Search in sources :

Example 26 with Color

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

the class Es6ConvertSuperTest method testSynthesizingConstructorOfBaseClassInSource.

// Constructor synthesis
@Test
public void testSynthesizingConstructorOfBaseClassInSource() {
    test(externs(""), srcs(lines(// Force wrapping.
    "class A { }", "", "class B extends A {", "  constructor() { super(); }", "}")), expected(lines("class A {", "  constructor() { }", "}", "", "class B extends A {", "  constructor() { super(); }", "}")));
    // 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(0).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(0).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 27 with Color

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

the class Es6ConvertSuperTest method testAccessingSuperStaticElement.

@Test
public void testAccessingSuperStaticElement() {
    test(externs(lines("/** @dict */", "class A {", "  constructor() { }", "", "  /** @param {number} x */", "  static ['g'](x) { };", "}")), srcs(lines("/** @dict */", "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();
    // 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(27);
    // 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").hasLineno(// position and length of `super`
    5).hasCharno(27).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 28 with Color

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

the class Es6ConvertSuperTest method testCallingSuperStaticProperty.

// Static `super` resolution
@Test
public void testCallingSuperStaticProperty() {
    test(externs(lines("class A {", "  constructor() { }", "", "  /** @param {number} x @return {string} */", "  static g(x) { }", "}")), srcs(lines("class B extends A {", "  constructor() { super(); }", "", "  static f() { super.g(3); }", "}")), expected(lines("class B extends A {", "  constructor() { super(); }", "", "  static f() { A.g.call(this, 3); }", "}")));
    // get types we need to check
    Color classAType = findClassDefinition(getLastCompiler(), "A").getRootNode().getColor();
    Color aDotGMethodType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("g").getRootNode().getFirstChild().getColor();
    // colors do not track ".call" type
    Color aDotGDotCallType = StandardColors.UNKNOWN;
    Color classBType = findClassDefinition(getLastCompiler(), "B").getRootNode().getColor();
    // A.g.call(this, 3)
    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)`
    4).hasCharno(15);
    assertNode(callNode).hasColorThat().isEqualTo(StandardColors.STRING);
    // A.g.call
    Node callee = callNode.getFirstChild();
    assertNode(callee).matchesQualifiedName("A.g.call").hasLineno(// position and length of `super.g`
    4).hasCharno(21);
    assertNode(callee).hasColorThat().isEqualTo(aDotGDotCallType);
    // A.g
    Node superDotGReplacement = callee.getFirstChild();
    assertNode(superDotGReplacement).matchesQualifiedName("A.g").hasLineno(// position and length of `super.g`
    4).hasCharno(21);
    assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
    // A
    Node superReplacement = superDotGReplacement.getFirstChild();
    assertNode(superReplacement).matchesQualifiedName("A").hasLineno(// position and length of `super`
    4).hasCharno(15).hasOriginalName("super");
    assertNode(superReplacement).hasColorThat().isEqualTo(classAType);
    Node thisNode = callee.getNext();
    assertNode(thisNode).hasType(Token.THIS);
    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 29 with Color

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

the class Es6ConvertSuperTest method testSynthesizingConstructorOfDerivedClassInSource.

@Test
public void testSynthesizingConstructorOfDerivedClassInSource() {
    test(externs(new TestExternsBuilder().addArguments().build()), srcs(lines(// Force wrapping.
    "class A {", "  constructor() { }", "}", "", "class B extends A { }")), expected(lines("class A {", "  constructor() { }", "}", "", "class B extends A {", "  constructor() { super(...arguments); }", "}")));
    // class A { ... }
    Node classANode = findClassDefinition(getLastCompiler(), "A").getRootNode();
    Color classAConstructorType = classANode.getColor();
    // class B extends A { ... }
    Node classBNode = findClassDefinition(getLastCompiler(), "B").getRootNode();
    Color classBConstructorType = classBNode.getColor();
    Color classBInstanceType = Color.createUnion(classBConstructorType.getInstanceColors());
    // constructor() { }
    Node constructorMemberFunctionDefForB = classBNode.getLastChild().getFirstChild();
    assertNode(constructorMemberFunctionDefForB).isMemberFunctionDef("constructor").hasLineno(// synthetic constructor gets position and length of original class definition
    5).hasCharno(0).isIndexable(false);
    assertNode(constructorMemberFunctionDefForB).hasColorThat().isEqualTo(classBConstructorType);
    Node constructorFunctionForB = constructorMemberFunctionDefForB.getOnlyChild();
    assertNode(constructorFunctionForB).hasToken(Token.FUNCTION).hasLineno(// synthetic constructor gets position and length of original class definition
    5).hasCharno(0).isIndexable(false);
    assertNode(constructorFunctionForB).hasColorThat().isEqualTo(classBConstructorType);
    // super(...arguments)
    Node superConstructorCall = constructorFunctionForB.getLastChild().getFirstChild().getOnlyChild();
    assertNode(superConstructorCall).hasToken(Token.CALL).hasLineno(// synthetic constructor gets position and length of original class definition
    5).hasCharno(0).isIndexable(false);
    assertNode(superConstructorCall).hasColorThat().isEqualTo(classBInstanceType);
    Node superNode = superConstructorCall.getFirstChild();
    assertNode(superNode).hasToken(Token.SUPER).hasLineno(// synthetic constructor gets position and length of original class definition
    5).hasCharno(0).isIndexable(false);
    assertNode(superNode).hasColorThat().isEqualTo(classAConstructorType);
    Node argumentsNode = superNode.getNext().getOnlyChild();
    assertNode(argumentsNode).isName("arguments").hasLineno(// synthetic constructor gets position and length of original class definition
    5).hasCharno(0).isIndexable(false);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 30 with Color

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

the class ColorGraphBuilderTest method disambiguationSupertypes_createConnection.

@Test
public void disambiguationSupertypes_createConnection() {
    // Given
    Color parent = colorWithName("Parent").build();
    Color child = colorWithName("Child").build();
    ColorGraphBuilder builder = this.createBuilder(null, ColorRegistry.builder().setDefaultNativeColorsForTesting().addDisambiguationEdge(child, parent).build());
    builder.add(graphNodeFactory.createNode(child));
    // When
    this.result = builder.build();
    // Then
    this.assertThatResultAsTable().containsCell("Parent", "Child", CAN_HOLD);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) 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