fix(42714): do not show inferFunctionReturnType QF on function body (#42737)

This commit is contained in:
Oleksandr T 2021-02-11 20:46:48 +02:00 committed by GitHub
parent d18b728e0b
commit b7922147d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 182 additions and 5 deletions

View File

@ -59,7 +59,9 @@ namespace ts.refactor.inferFunctionReturnType {
if (isInJSFile(context.file) || !refactorKindBeginsWith(inferReturnTypeAction.kind, context.kind)) return;
const token = getTokenAtPosition(context.file, context.startPosition);
const declaration = findAncestor(token, isConvertibleDeclaration);
const declaration = findAncestor(token, n =>
isBlock(n) || n.parent && isArrowFunction(n.parent) && (n.kind === SyntaxKind.EqualsGreaterThanToken || n.parent.body === n) ? "quit" :
isConvertibleDeclaration(n)) as ConvertibleDeclaration | undefined;
if (!declaration || !declaration.body || declaration.type) {
return { error: getLocaleSpecificMessage(Diagnostics.Return_type_must_be_inferred_from_a_function) };
}
@ -68,7 +70,7 @@ namespace ts.refactor.inferFunctionReturnType {
const returnType = tryGetReturnType(typeChecker, declaration);
if (!returnType) {
return { error: getLocaleSpecificMessage(Diagnostics.Could_not_determine_function_return_type) };
};
}
const returnTypeNode = typeChecker.typeToTypeNode(returnType, declaration, NodeBuilderFlags.NoTruncation);
if (returnTypeNode) {

View File

@ -0,0 +1,32 @@
/// <reference path='fourslash.ts' />
////function /*a*//*b*/f1() {
//// return { x: 1, y: 1 };
////}
////func/*c*//*d*/tion f2() {
//// return { x: 1, y: 1 };
////}
////function f3(/*e*//*f*/) {
//// return { x: 1, y: 1 };
////}
////function f4() {/*g*//*h*/
//// return { x: 1, y: 1 };
////}
////function f5() {
//// return { x: 1, y: 1 /*i*//*j*/};
////}
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.refactorAvailable("Infer function return type");
goTo.select("g", "h");
verify.not.refactorAvailable("Infer function return type");
goTo.select("i", "j");
verify.not.refactorAvailable("Infer function return type");

View File

@ -0,0 +1,42 @@
/// <reference path='fourslash.ts' />
////class F1 {
//// /*a*//*b*/method() {
//// return { x: 1, y: 1 };
//// }
////}
////class F2 {
//// met/*c*//*d*/hod(){
//// return { x: 1, y: 1 };
//// }
////}
////class F3 {
//// method(/*e*//*f*/) {
//// return { x: 1, y: 1 };
//// }
////}
////class F4 {
//// method() {
//// return { x: 1, y: 1 /*g*//*h*/};
//// }
////}
////class F5 {
//// method() {
//// return { x: 1, y: 1 /*i*//*j*/};
//// }
////}
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.refactorAvailable("Infer function return type");
goTo.select("g", "h");
verify.not.refactorAvailable("Infer function return type");
goTo.select("i", "j");
verify.not.refactorAvailable("Infer function return type");

View File

@ -0,0 +1,32 @@
/// <reference path='fourslash.ts' />
////const f1 = /*a*//*b*/function () {
//// return { x: 1, y: 1 };
////}
////const f2 = func/*c*//*d*/tion() {
//// return { x: 1, y: 1 };
////}
////const f3 = function (/*e*//*f*/) {
//// return { x: 1, y: 1 };
////}
////const f4 = function () {/*g*//*h*/
//// return { x: 1, y: 1 };
////}
////const f5 = function () {
//// return { x: 1, y: 1 /*i*//*j*/};
////}
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.refactorAvailable("Infer function return type");
goTo.select("g", "h");
verify.not.refactorAvailable("Infer function return type");
goTo.select("i", "j");
verify.not.refactorAvailable("Infer function return type");

View File

@ -0,0 +1,32 @@
/// <reference path='fourslash.ts' />
////const f1 = /*a*//*b*/() =>{
//// return { x: 1, y: 1 };
////}
////const f2 = (/*c*//*d*/) => {
//// return { x: 1, y: 1 };
////}
////const f3 = () => /*e*//*f*/{
//// return { x: 1, y: 1 };
////}
////const f4 = () => {/*g*//*h*/
//// return { x: 1, y: 1 };
////}
////const f5 = () => {
//// return { x: 1, y: 1/*i*//*j*/ };
////}
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.not.refactorAvailable("Infer function return type");
goTo.select("g", "h");
verify.not.refactorAvailable("Infer function return type");
goTo.select("i", "j");
verify.not.refactorAvailable("Infer function return type");

View File

@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
////function f1(/*a*//*b*/x, { y }) {
//// return { x, y: 1 };
////}
////function f2(x, { /*c*//*d*/y }) {
//// return { x, y: 1 };
////}
////function f3(x, /*e*//*f*/{ y }) {
//// return { x, y: 1 };
////}
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.refactorAvailable("Infer function return type");

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
////const f1 = /*a*//*b*/(x: number) => x;
////const f2 = (x: number) /*c*//*d*/=> x;
////const f3 = (x: number) => /*e*//*f*/x
////const f4= (x: number) => x/*g*//*h*/
goTo.select("a", "b");
verify.refactorAvailable("Infer function return type");
goTo.select("c", "d");
verify.not.refactorAvailable("Infer function return type");
goTo.select("e", "f");
verify.not.refactorAvailable("Infer function return type");
goTo.select("g", "h");
verify.not.refactorAvailable("Infer function return type");

View File

@ -1,10 +1,9 @@
/// <reference path='fourslash.ts' />
//// const arrow = () /*a*/=>/*b*/ 1;
//// const arrow = /*a*//*b*/() => 1;
goTo.select("a", "b");
verify.refactorKindAvailable("refactor.rewrite",
[
verify.refactorKindAvailable("refactor.rewrite", [
"refactor.rewrite.arrow.braces.add",
"refactor.rewrite.function.named",
"refactor.rewrite.function.anonymous",