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