Skip to content

fix(42714): "Infer function return type" is annoyingly suggested in function bodies #42737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/services/refactors/inferFunctionReturnType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand All @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType16.ts
Original file line number Diff line number Diff line change
@@ -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");
42 changes: 42 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType17.ts
Original file line number Diff line number Diff line change
@@ -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");
32 changes: 32 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType18.ts
Original file line number Diff line number Diff line change
@@ -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");
32 changes: 32 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType19.ts
Original file line number Diff line number Diff line change
@@ -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");
20 changes: 20 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType20.ts
Original file line number Diff line number Diff line change
@@ -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");
18 changes: 18 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType21.ts
Original file line number Diff line number Diff line change
@@ -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");
5 changes: 2 additions & 3 deletions tests/cases/fourslash/refactorKind_rewriteFunction.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down