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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations