Skip to content

Commit 200786f

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

8 files changed

+148
-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) || 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function /*a*/f1()/*b*/ {
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////function f2/*c*/()/*d*/ {
7+
//// return { x: 1, y: 1 };
8+
////}
9+
////function f3() /*e*/{
10+
//// return { x: 1, y: 1 };
11+
////}/*f*/
12+
////function f4() {
13+
//// /*g*/return { x: 1, y: 1 };/*h*/
14+
////}
15+
16+
goTo.select("a", "b");
17+
verify.refactorAvailable("Infer function return type");
18+
19+
goTo.select("c", "d");
20+
verify.refactorAvailable("Infer function return type");
21+
22+
goTo.select("e", "f");
23+
verify.not.refactorAvailable("Infer function return type");
24+
25+
goTo.select("g", "h");
26+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class F1 {
4+
//// /*a*/method()/*b*/ {
5+
//// return { x: 1, y: 1 };
6+
//// }
7+
////}
8+
////class F2 {
9+
//// method/*c*/()/*d*/{
10+
//// return { x: 1, y: 1 };
11+
//// }
12+
////}
13+
////class F3 {
14+
//// method() /*e*/{
15+
//// return { x: 1, y: 1 };
16+
//// }/*f*/
17+
////}
18+
////class F4 {
19+
//// method() {
20+
//// /*g*/return { x: 1, y: 1 };/*h*/
21+
//// }
22+
////}
23+
24+
goTo.select("a", "b");
25+
verify.refactorAvailable("Infer function return type");
26+
27+
goTo.select("c", "d");
28+
verify.refactorAvailable("Infer function return type");
29+
30+
goTo.select("e", "f");
31+
verify.not.refactorAvailable("Infer function return type");
32+
33+
goTo.select("g", "h");
34+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f1 = /*a*/function()/*b*/ {
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////const f2 = function/*c*/()/*d*/{
7+
//// return { x: 1, y: 1 };
8+
////}
9+
////const f3 = function () /*e*/{
10+
//// return { x: 1, y: 1 };
11+
////}/*f*/
12+
////const f4 = function () {
13+
//// /*g*/return { x: 1, y: 1 };/*h*/
14+
////}
15+
16+
goTo.select("a", "b");
17+
verify.refactorAvailable("Infer function return type");
18+
19+
goTo.select("c", "d");
20+
verify.refactorAvailable("Infer function return type");
21+
22+
goTo.select("e", "f");
23+
verify.not.refactorAvailable("Infer function return type");
24+
25+
goTo.select("g", "h");
26+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f1 = /*a*/()/*b*/ =>{
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////const f2 = () => /*c*/{
7+
//// return { x: 1, y: 1 };
8+
////}/*d*/
9+
////const f3 = () => {
10+
//// /*e*/return { x: 1, y: 1 };/*f*/
11+
////}
12+
13+
goTo.select("a", "b");
14+
verify.refactorAvailable("Infer function return type");
15+
16+
goTo.select("c", "d");
17+
verify.not.refactorAvailable("Infer function return type");
18+
19+
goTo.select("e", "f");
20+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function f1(/*a*/x/*b*/, { y }) {
4+
//// return { x, y: 1 };
5+
////}
6+
////function f2(x, { /*c*/y/*d*/ }) {
7+
//// return { x, y: 1 };
8+
////}
9+
////function f3(x, /*e*/{ y }/*f*/) {
10+
//// return { x, y: 1 };
11+
////}
12+
13+
goTo.select("a", "b");
14+
verify.refactorAvailable("Infer function return type");
15+
16+
goTo.select("c", "d");
17+
verify.refactorAvailable("Infer function return type");
18+
19+
goTo.select("e", "f");
20+
verify.refactorAvailable("Infer function return type");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f1 = /*a*/(x)/*b*/ => x;
4+
////const f2 = (x) /*c*/=>/*d*/ x;
5+
////const f3 = (x) => /*e*/x/*f*/
6+
////const f4= (x) => /*g*/x/*h*/
7+
8+
goTo.select("a", "b");
9+
verify.refactorAvailable("Infer function return type");
10+
11+
goTo.select("c", "d");
12+
verify.not.refactorAvailable("Infer function return type");
13+
14+
goTo.select("e", "f");
15+
verify.not.refactorAvailable("Infer function return type");
16+
17+
goTo.select("g", "h");
18+
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)