use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class Es6ConvertSuperTest method testResolvingSuperInGetter.
// Getters and setters
@Test
public void testResolvingSuperInGetter() {
test(externs(lines("class A {", " constructor() { }", "", " /** @param {number} x @return {number} */", " g(x) { }", "}")), srcs(lines("class B extends A {", " constructor() { super(); }", "", " get f() { super.g(3); }", "}")), expected(lines("class B extends A {", " constructor() { super(); }", "", " get 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();
// 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, 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(12);
assertNode(callNode).hasColorThat().isEqualTo(StandardColors.NUMBER);
// A.prototype.g.call
Node callee = callNode.getFirstChild();
assertNode(callee).matchesQualifiedName("A.prototype.g.call").hasLineno(// position and length of `super.g`
4).hasCharno(18);
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`
4).hasCharno(18);
assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
// A.prototype
Node superReplacement = superDotGReplacement.getFirstChild();
assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
4).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`
4).hasCharno(18).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 DevirtualizeMethodsTest method checkColorOfRewrittenMethods.
private void checkColorOfRewrittenMethods() {
Color fooType = getLabelledExpression("FOO_RESULT").getFirstChild().getColor();
Color barType = getLabelledExpression("BAR_RESULT").getFirstChild().getColor();
Color bazType = getLabelledExpression("BAZ_RESULT").getFirstChild().getColor();
Color fooResultType = getLabelledExpression("FOO_RESULT").getColor();
Color barResultType = getLabelledExpression("BAR_RESULT").getColor();
Color bazResultType = getLabelledExpression("BAZ_RESULT").getColor();
assertThat(fooResultType).isEqualTo(StandardColors.NUMBER);
assertThat(barResultType).isEqualTo(StandardColors.NUMBER);
assertThat(bazResultType).isEqualTo(StandardColors.NULL_OR_VOID);
assertThat(fooType).isNotEqualTo(StandardColors.UNKNOWN);
assertThat(barType).isNotEqualTo(StandardColors.UNKNOWN);
assertThat(bazType).isNotEqualTo(StandardColors.UNKNOWN);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class Es6RewriteDestructuringTest method testObjectDestructuring_getsCorrectTypes.
@Test
public void testObjectDestructuring_getsCorrectTypes() {
test(lines(//
"const obj = {a: 3, b: 'string', c: null};", "const {a} = obj;"), lines("const obj = {a: 3, b: 'string', c: null};", "/** @const */ var $jscomp$destructuring$var0=obj;", "const a = $jscomp$destructuring$var0.a;"));
Node jsRoot = getLastCompiler().getJsRoot();
Node aName = getNodeMatchingQName(jsRoot, "a");
assertNode(aName).hasColorThat().isEqualTo(StandardColors.NUMBER);
Color objType = getNodeMatchingQName(jsRoot, "obj").getColor();
// `$jscomp$destructuring$var0` has the same type as `obj`
Node jscompDestructuringVar0Name = getNodeMatchingQName(jsRoot, "$jscomp$destructuring$var0");
assertNode(jscompDestructuringVar0Name).hasColorThat().isEqualTo(objType);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class ColorGraphNodeFactoryTest method colorGraphNodes_areCached.
@Test
public void colorGraphNodes_areCached() {
// Given
ColorGraphNodeFactory factory = ColorGraphNodeFactory.createFactory(this.colorRegistry);
Color fooType = Color.singleBuilder().setId(fromAscii("Foo")).build();
ColorGraphNode flatFoo = factory.createNode(fooType);
// When
ColorGraphNode flatFooAgain = factory.createNode(fooType);
// Then
assertThat(flatFooAgain).isSameInstanceAs(flatFoo);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class AstFactoryTest method testCreateObjectLit_empty_withColor.
@Test
public void testCreateObjectLit_empty_withColor() {
AstFactory astFactory = createTestAstFactoryWithColors();
Color objColor = Color.singleBuilder().setId(ColorId.fromAscii("1")).build();
Node objectLit = astFactory.createObjectLit(type(JSTypeNative.UNKNOWN_TYPE, objColor));
assertNode(objectLit).hasColorThat().isEqualTo(objColor);
assertNode(objectLit).hasToken(Token.OBJECTLIT);
assertNode(objectLit).hasChildren(false);
}
Aggregations