use of com.google.javascript.jscomp.testing.TestExternsBuilder 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.TestExternsBuilder in project closure-compiler by google.
the class OptionalChainingIntegrationTest method testRemoveUnusedCode.
@Test
public void testRemoveUnusedCode() {
CompilerOptions options = createCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
test(options, lines(//
"class MyClass {", " method() {", " return 4;", " }", "}", "", "/**", " * @param {?MyClass} maybeMyClass", " */", "function maybeLog(maybeMyClass) {", // optional chaining call
" maybeMyClass?.method()", "}", "", "maybeLog(new MyClass());", ""), "");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class OptionalChainingIntegrationTest method testInline.
@Test
public void testInline() {
CompilerOptions options = createCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
test(options, lines(//
"class MyClass {", " method() {", " return 4;", " }", "}", // regular call gets devirtualized and inlined
"let x = (new MyClass()).method();", "console.log(x);", ""), "console.log(4);");
test(options, lines(//
"class MyClass {", " method() {", " return 4;", " }", "}", "let x = (new MyClass())?.method();", "console.log(x);", ""), lines(//
"class a {", " a() {", " return 4;", " }", "}", // optional call is not devirtualized and not inlined
"console.log((new a)?.a());"));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class OptionalChainingIntegrationTest method bareMinimumTranspileTest.
@Test
public void bareMinimumTranspileTest() {
CompilerOptions options = createCompilerOptions();
// transpile
options.setLanguageOut(LanguageMode.ECMASCRIPT_2019);
externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
test(options, lines(//
"class MyClass {", " method(msg) {", " console.log(msg);", " }", "}", "", "/**", " * @param {?MyClass} maybeMyClass", " */", "function maybeLog(maybeMyClass) {", " maybeMyClass?.method('log message')", "}", "", "maybeLog(null);", "maybeLog(new MyClass());", ""), "console.log('log message');");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class OptionalChainingIntegrationTest method testCheckTypes.
@Test
public void testCheckTypes() {
CompilerOptions options = createCompilerOptions();
externs = ImmutableList.of(new TestExternsBuilder().addConsole().buildExternsFile("externs"));
options.setCheckTypes(true);
// JSC_WRONG_ARGUMENT_COUNT error is reported
test(options, "var x = x || {}; x.f = function() {}; x?.f(3);", DiagnosticGroups.CHECK_TYPES);
}
Aggregations