use of com.google.javascript.jscomp.testing.CodeSubTree 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.testing.CodeSubTree in project closure-compiler by google.
the class RewriteAsyncIterationTest method testAsyncGenerator.
@Test
public void testAsyncGenerator() {
test("async function* baz() { foo() }", lines("function baz() {", " return new $jscomp.AsyncGeneratorWrapper((function*() {", " foo();", " })());", "}"));
CodeSubTree bazSubTree = findFunctionDefinition(getLastCompiler(), "baz");
Node wrapper = bazSubTree.findMatchingQNameReferences("$jscomp.AsyncGeneratorWrapper").get(0);
Node newExpr = wrapper.getParent();
Node innerGeneratorCall = newExpr.getSecondChild();
assertNode(wrapper).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperClassColor());
assertNode(newExpr).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperInstanceColor());
assertNode(innerGeneratorCall).hasColorThat().isEqualTo(getGlobalColor(StandardColors.GENERATOR_ID));
}
use of com.google.javascript.jscomp.testing.CodeSubTree in project closure-compiler by google.
the class RewriteAsyncFunctionsTest method testInnerSuperReference.
@Test
public void testInnerSuperReference() {
test(externs(new TestExternsBuilder().addFunction().addJSCompLibraries().build()), srcs(lines("class A {", " m() {", " return this;", " }", "}", "class X extends A {", " async m() {", " const tmp = super.m;", " return tmp.call(null);", " }", "}")), expected(lines("class A {", " m() {", " return this;", " }", "}", "class X extends A {", " m() {", " const $jscomp$async$super$get$m = () => super.m;", " return $jscomp.asyncExecutePromiseGeneratorFunction(", " function* () {", " const tmp = $jscomp$async$super$get$m();", // type of tmp will indicate it requires `this` be provided, but will allow null.
" return tmp.call(null);", " });", " }", "}")));
// type of A.prototype.m
Color classAPropertyMType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("m").getRootNode().getColor();
Color classAInstanceType = getGlobalInstanceColor("A");
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);
// arrow function has a Color representing a object
Node wrapperArrowFunction = wrapperDeclarationNameNode.getOnlyChild();
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).hasColorThat().isEqualTo(classAInstanceType);
// second name node is reference
// const tmp = $jscomp$async$super$get$m();
Node wrapperReferenceNameNode = superMethodWrapperNameNodes.get(1);
// optimization colors don't track function signatures
assertNode(wrapperReferenceNameNode).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
// `$jscomp$async$super$get$m()`
Node wrapperCallNode = wrapperReferenceNameNode.getParent();
assertNode(wrapperCallNode).isCall().hasColorThat().isEqualTo(classAPropertyMType);
}
use of com.google.javascript.jscomp.testing.CodeSubTree in project closure-compiler by google.
the class RewriteAsyncIterationTest method testYieldInAsyncGenerator.
@Test
public void testYieldInAsyncGenerator() {
test(lines(//
"/** @return {!AsyncGenerator<number>} */", "async function* baz() { yield 2+2 }"), lines("function baz() {", " return new $jscomp.AsyncGeneratorWrapper((function*() {", " yield new $jscomp.AsyncGeneratorWrapper$ActionRecord(", " $jscomp.AsyncGeneratorWrapper$ActionEnum.YIELD_VALUE, 2+2);", " })());", "}"));
CodeSubTree bazSubTree = findFunctionDefinition(getLastCompiler(), "baz");
Node wrapper = bazSubTree.findMatchingQNameReferences("$jscomp.AsyncGeneratorWrapper").get(0);
Node newExpr = wrapper.getParent();
Node innerGeneratorCall = newExpr.getSecondChild();
assertNode(wrapper).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperClassColor());
assertNode(newExpr).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperInstanceColor());
assertNode(innerGeneratorCall).hasColorThat().isEqualTo(getGlobalColor(StandardColors.GENERATOR_ID));
test(lines(//
"/** @return {!AsyncGenerator<number>} */", "async function* baz() { bar = yield 2+2 }"), lines(//
"function baz() {", " return new $jscomp.AsyncGeneratorWrapper((function*() {", " bar = yield new $jscomp.AsyncGeneratorWrapper$ActionRecord(", " $jscomp.AsyncGeneratorWrapper$ActionEnum.YIELD_VALUE, 2+2);", " })());", "}"));
Node bar = findFunctionDefinition(getLastCompiler(), "baz").findMatchingQNameReferences("bar").get(0);
// The generator yields numbers but yield expressions should always be "?" as next accepts "?"
assertNode(bar).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
}
use of com.google.javascript.jscomp.testing.CodeSubTree in project closure-compiler by google.
the class RewriteAsyncIterationTest method testAwaitInAsyncGenerator.
@Test
public void testAwaitInAsyncGenerator() {
test(lines("let /** function(): !Promise<number> */ foo;", "/** @return {!AsyncGenerator<undefined>} */", "async function* baz() { await foo() }"), lines("let foo;", "function baz() {", " return new $jscomp.AsyncGeneratorWrapper((function*() {", " yield new $jscomp.AsyncGeneratorWrapper$ActionRecord(", " $jscomp.AsyncGeneratorWrapper$ActionEnum.AWAIT_VALUE, foo());", " })());", "}"));
CodeSubTree bazSubTree = findFunctionDefinition(getLastCompiler(), "baz");
Node wrapper = bazSubTree.findMatchingQNameReferences("$jscomp.AsyncGeneratorWrapper").get(0);
Node newExpr = wrapper.getParent();
Node innerGeneratorCall = newExpr.getSecondChild();
assertNode(wrapper).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperClassColor());
assertNode(newExpr).hasColorThat().isEqualTo(getJSCompAsyncGeneratorWrapperInstanceColor());
assertNode(innerGeneratorCall).hasColorThat().isEqualTo(getGlobalColor(StandardColors.GENERATOR_ID));
test(lines("let /** function(): !Promise<number> */ foo;", "async function* baz() { bar = await foo() }"), lines("let foo;", "function baz() {", " return new $jscomp.AsyncGeneratorWrapper((function*() {", " bar = yield new $jscomp.AsyncGeneratorWrapper$ActionRecord(", " $jscomp.AsyncGeneratorWrapper$ActionEnum.AWAIT_VALUE, foo());", " })());", "}"));
Node bar = findFunctionDefinition(getLastCompiler(), "baz").findMatchingQNameReferences("bar").get(0);
assertNode(bar).hasColorThat().isEqualTo(StandardColors.NUMBER);
}
Aggregations