Skip to content

Commit 2932ed7

Browse files
committed
fix(42714): do not show inferFunctionReturnType QF on function body
1 parent 55cd98e commit 2932ed7

8 files changed

+100
-3
lines changed

src/services/refactors/inferFunctionReturnType.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ namespace ts.refactor.inferFunctionReturnType {
5959
if (isInJSFile(context.file) || !refactorKindBeginsWith(inferReturnTypeAction.kind, context.kind)) return;
6060

6161
const token = getTokenAtPosition(context.file, context.startPosition);
62-
const declaration = findAncestor(token, isConvertibleDeclaration);
62+
const declaration = findAncestor(token, n =>
63+
isBlock(n) || isParameter(n) || n.parent && isArrowFunction(n.parent) && (n.kind === SyntaxKind.EqualsGreaterThanToken || n.parent.body === n) ? "quit" :
64+
isConvertibleDeclaration(n)) as ConvertibleDeclaration | undefined;
6365
if (!declaration || !declaration.body || declaration.type) {
6466
return { error: getLocaleSpecificMessage(Diagnostics.Return_type_must_be_inferred_from_a_function) };
6567
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function /*a*/f/*c*/()/*d*//*b*/ /*e*/{
4+
//// /*g*/return { x: 1, y: 1 };/*h*/
5+
////}/*f*/
6+
7+
goTo.select("a", "b");
8+
verify.refactorAvailable("Infer function return type");
9+
10+
goTo.select("c", "d");
11+
verify.refactorAvailable("Infer function return type");
12+
13+
goTo.select("e", "f");
14+
verify.not.refactorAvailable("Infer function return type");
15+
16+
goTo.select("g", "h");
17+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class Foo {
4+
//// /*a*/method/*c*/()/*d*//*b*/ /*e*/{
5+
//// /*g*/return { x: 1, y: 1 };/*h*/
6+
//// }/*f*/
7+
////}
8+
9+
goTo.select("a", "b");
10+
verify.refactorAvailable("Infer function return type");
11+
12+
goTo.select("c", "d");
13+
verify.refactorAvailable("Infer function return type");
14+
15+
goTo.select("e", "f");
16+
verify.not.refactorAvailable("Infer function return type");
17+
18+
goTo.select("g", "h");
19+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const foo = /*a*/function/*c*/()/*d*//*b*/ /*e*/{
4+
//// /*g*/return { x: 1, y: 1 };/*h*/
5+
////}/*f*/
6+
7+
goTo.select("a", "b");
8+
verify.refactorAvailable("Infer function return type");
9+
10+
goTo.select("c", "d");
11+
verify.refactorAvailable("Infer function return type");
12+
13+
goTo.select("e", "f");
14+
verify.not.refactorAvailable("Infer function return type");
15+
16+
goTo.select("g", "h");
17+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const foo = /*c*//*a*/()/*b*/ =>/*d*/ /*e*/{
4+
//// /*g*/return { x: 1, y: 1 };/*h*/
5+
////}/*f*/
6+
7+
goTo.select("a", "b");
8+
verify.refactorAvailable("Infer function return type");
9+
10+
goTo.select("c", "d");
11+
verify.refactorAvailable("Infer function return type");
12+
13+
goTo.select("e", "f");
14+
verify.not.refactorAvailable("Infer function return type");
15+
16+
goTo.select("g", "h");
17+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function f(/*a*/x/*b*/, /*e*/{ /*c*/y/*d*/ }/*f*/) {
4+
//// return { x, y: 1 };
5+
////}
6+
7+
goTo.select("a", "b");
8+
verify.not.refactorAvailable("Infer function return type");
9+
10+
goTo.select("c", "d");
11+
verify.not.refactorAvailable("Infer function return type");
12+
13+
goTo.select("e", "f");
14+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const fn = /*a*/(x)/*b*/ /*c*/=>/*d*/ /*e*/x/*f*/
4+
5+
goTo.select("a", "b");
6+
verify.refactorAvailable("Infer function return type");
7+
8+
goTo.select("c", "d");
9+
verify.not.refactorAvailable("Infer function return type");
10+
11+
goTo.select("e", "f");
12+
verify.not.refactorAvailable("Infer function return type");

tests/cases/fourslash/refactorKind_rewriteFunction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ verify.refactorKindAvailable("refactor.rewrite",
77
[
88
"refactor.rewrite.arrow.braces.add",
99
"refactor.rewrite.function.named",
10-
"refactor.rewrite.function.anonymous",
11-
"refactor.rewrite.function.returnType"
10+
"refactor.rewrite.function.anonymous"
1211
]);

0 commit comments

Comments
 (0)