use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class RewriteAsyncFunctionsTest method testInnerSuperCall.
@Test
public void testInnerSuperCall() {
test(externs(new TestExternsBuilder().addPromise().addJSCompLibraries().build()), srcs(lines("class A {", " m() {", " return Promise.resolve(this);", " }", "}", "class X extends A {", " async m() {", " return super.m();", " }", "}")), expected(lines("class A {", " m() {", " return Promise.resolve(this);", " }", "}", "class X extends A {", " m() {", " const $jscomp$async$this = this;", " const $jscomp$async$super$get$m = () => super.m;", " return $jscomp.asyncExecutePromiseGeneratorFunction(", " function* () {", " return $jscomp$async$super$get$m().call($jscomp$async$this);", " });", " }", "}")));
Color classAInstanceType = getGlobalInstanceColor("A");
// type of A.prototype.m
Color classAPropertyMType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("m").getRootNode().getColor();
CodeSubTree classXMethodMDefinition = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m");
// Check type information on wrapper function for `super.m`
ImmutableList<Node> superMethodWrapperNameNodes = classXMethodMDefinition.findMatchingQNameReferences("$jscomp$async$super$get$m");
// one declaration and one reference
assertThat(superMethodWrapperNameNodes).hasSize(2);
// first name node is declaration
// const $jscomp$async$super$get$m = () => super.m;
Node wrapperDeclarationNameNode = superMethodWrapperNameNodes.get(0);
Node wrapperArrowFunction = wrapperDeclarationNameNode.getOnlyChild();
// optimization colors don't track function signatures
assertNode(wrapperArrowFunction).isArrowFunction().hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
// wrapper function variable has type matching the function itself
Color wrapperArrowColor = wrapperArrowFunction.getColor();
assertNode(wrapperDeclarationNameNode).hasColorThat().isEqualTo(wrapperArrowColor);
// get `super.m` from `() => `super.m`
Node superDotM = wrapperArrowFunction.getLastChild();
assertNode(superDotM).matchesQualifiedName("super.m").hasColorThat().isEqualTo(classAPropertyMType);
Node superNode = superDotM.getFirstChild();
assertNode(superNode).isSuper().hasColorThat().isEqualTo(classAInstanceType);
// second name node is reference
// return $jscomp$async$super$get$m().call($jscomp$async$this);
Node wrapperReferenceNameNode = superMethodWrapperNameNodes.get(1);
// optimization colors don't track function signatures
assertNode(wrapperArrowFunction).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
// `$jscomp$async$super$get$m()`
Node wrapperCallNode = wrapperReferenceNameNode.getParent();
assertNode(wrapperCallNode).isCall().hasColorThat().isEqualTo(classAPropertyMType);
// `$jscomp$async$super$get$m().call($jscomp$async$this)`
Node methodCallNode = wrapperCallNode.getGrandparent();
// optimization colors don't track .call types
assertNode(methodCallNode).isCall().hasColorThat().isEqualTo(StandardColors.UNKNOWN);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class Es6ConvertSuperTest method testAccessingSuperInstanceElement.
@Test
public void testAccessingSuperInstanceElement() {
test(externs(lines("/** @dict */", "class A {", " constructor() { }", "", " /** @param {number} x */", " ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", " constructor() { super(); }", "", " ['f']() { var t = super['g']; }", "}")), expected(lines("class B extends A {", " constructor() {", " super();", " }", "", " ['f']() { var t = A.prototype['g']; }", "}")));
// get types we need to check
Color classAPrototypeType = Color.createUnion(findClassDefinition(getLastCompiler(), "A").getRootNode().getColor().getPrototypes());
// 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(20);
// getelem prevents us doing any better than unknown type
assertNode(superGetelemReplacement).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A.prototype
Node superReplacement = superGetelemReplacement.getFirstChild();
assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
5).hasCharno(20).hasOriginalName("super");
assertNode(superReplacement).hasColorThat().isEqualTo(classAPrototypeType);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class Es6ConvertSuperTest method testCallingSuperInstanceElement.
@Test
public void testCallingSuperInstanceElement() {
test(externs(lines("/** @dict */", "class A {", " constructor() { }", "", " /** @param {number} x */", " ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", " constructor() { super(); }", "", " ['f']() { super['g'](4); }", "}")), expected(lines("class B extends A {", " constructor() {", " super();", " }", "", " ['f']() { A.prototype['g'].call(this, 4); }", "}")));
// get types we need to check
Color classAPrototypeType = Color.createUnion(findClassDefinition(getLastCompiler(), "A").getRootNode().getColor().getPrototypes());
Color classBInstanceType = Color.createUnion(findClassDefinition(getLastCompiler(), "B").getRootNode().getColor().getInstanceColors());
// A.prototype['g'].call(this, 4)
Node callNode = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getSecondChild().getLastChild().getFirstChild().getOnlyChild();
assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super['g'](4)`
5).hasCharno(12);
// computed property prevents doing any better than StandardColors.UNKNOWN
assertNode(callNode).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A.prototype['g'].call
Node callee = callNode.getFirstChild();
assertNode(callee).hasToken(Token.GETPROP).hasLineno(// position and length of `super['g']`
5).hasCharno(12);
assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A.prototype['g']
Node superGetelemReplacement = callee.getFirstChild();
assertNode(superGetelemReplacement).hasToken(Token.GETELEM).hasLineno(// position and length of `super['g']`
5).hasCharno(12);
// 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.prototype").hasLineno(// position and length of `super`
5).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']`
5).hasCharno(12).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 testCallingSuperStaticElement.
@Test
public void testCallingSuperStaticElement() {
test(externs(lines("/** @dict */", "class A {", " constructor() { }", "", " /** @param {number} x */", " static ['g'](x) { };", "}")), srcs(lines("/** @dict */", "class B extends A {", " constructor() { super(); }", "", " static ['f']() { super['g'](4); }", "}")), expected(lines("class B extends A {", " constructor() {", " super();", " }", "", " static ['f']() { A['g'].call(this, 4); }", "}")));
// get types we need to check
Color classAType = findClassDefinition(getLastCompiler(), "A").getRootNode().getColor();
Color classBType = findClassDefinition(getLastCompiler(), "B").getRootNode().getColor();
// A['g'].call(this, 4)
Node callNode = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getSecondChild().getLastChild().getFirstChild().getOnlyChild();
assertNode(callNode).hasToken(Token.CALL).hasLineno(// position and length of `super['g'](4)`
5).hasCharno(19);
// computed property prevents doing any better than StandardColors.UNKNOWN
assertNode(callNode).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A['g'].call
Node callee = callNode.getFirstChild();
assertNode(callee).hasToken(Token.GETPROP).hasLineno(// position and length of `super['g']`
5).hasCharno(19);
assertNode(callee).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A['g']
Node superGetelemReplacement = callee.getFirstChild();
assertNode(superGetelemReplacement).hasToken(Token.GETELEM).hasLineno(// position and length of `super['g']`
5).hasCharno(19);
// computed property prevents doing any better than StandardColors.UNKNOWN
assertNode(superGetelemReplacement).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
// A
Node superReplacement = superGetelemReplacement.getFirstChild();
assertNode(superReplacement).matchesName("A").hasLineno(// position and length of `super`
5).hasCharno(19).hasOriginalName("super");
assertNode(superReplacement).hasColorThat().isEqualTo(classAType);
// `this` node from `A['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(classBType);
}
use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.
the class Es6ConvertSuperTest method testAccessingSuperInstanceProperty.
@Test
public void testAccessingSuperInstanceProperty() {
test(externs(lines("class A {", " constructor() { }", "", " /** @param {number} x */", " g(x) { }", "}")), srcs(lines("class B extends A {", " constructor() { super(); }", "", " f() { var t = super.g; }", "}")), expected(lines("class B extends A {", " constructor() { super(); }", "", " f() { var t = A.prototype.g; }", "}")));
// 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();
// A.prototype.g
Node superDotGReplacement = getLastCompiler().getJsRoot().getFirstChild().getFirstChild().getLastChild().getLastChild().getOnlyChild().getLastChild().getFirstChild().getFirstChild().getOnlyChild();
assertNode(superDotGReplacement).matchesQualifiedName("A.prototype.g").hasLineno(// position and length of `super.g`
4).hasCharno(22);
assertNode(superDotGReplacement).hasColorThat().isEqualTo(aDotGMethodType);
// A.prototype
Node superReplacement = superDotGReplacement.getFirstChild();
assertNode(superReplacement).matchesQualifiedName("A.prototype").hasLineno(// position and length of `super`
4).hasCharno(16).hasOriginalName("super");
assertNode(superReplacement).hasColorThat().isEqualTo(classAPrototypeType);
}
Aggregations