Skip to content

Commit b792214

Browse files
authored
fix(42714): do not show inferFunctionReturnType QF on function body (microsoft#42737)
1 parent d18b728 commit b792214

8 files changed

+182
-5
lines changed

src/services/refactors/inferFunctionReturnType.ts

Lines changed: 4 additions & 2 deletions
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
}
@@ -68,7 +70,7 @@ namespace ts.refactor.inferFunctionReturnType {
6870
const returnType = tryGetReturnType(typeChecker, declaration);
6971
if (!returnType) {
7072
return { error: getLocaleSpecificMessage(Diagnostics.Could_not_determine_function_return_type) };
71-
};
73+
}
7274

7375
const returnTypeNode = typeChecker.typeToTypeNode(returnType, declaration, NodeBuilderFlags.NoTruncation);
7476
if (returnTypeNode) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function /*a*//*b*/f1() {
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////func/*c*//*d*/tion f2() {
7+
//// return { x: 1, y: 1 };
8+
////}
9+
////function f3(/*e*//*f*/) {
10+
//// return { x: 1, y: 1 };
11+
////}
12+
////function f4() {/*g*//*h*/
13+
//// return { x: 1, y: 1 };
14+
////}
15+
////function f5() {
16+
//// return { x: 1, y: 1 /*i*//*j*/};
17+
////}
18+
19+
goTo.select("a", "b");
20+
verify.refactorAvailable("Infer function return type");
21+
22+
goTo.select("c", "d");
23+
verify.refactorAvailable("Infer function return type");
24+
25+
goTo.select("e", "f");
26+
verify.refactorAvailable("Infer function return type");
27+
28+
goTo.select("g", "h");
29+
verify.not.refactorAvailable("Infer function return type");
30+
31+
goTo.select("i", "j");
32+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////class F1 {
4+
//// /*a*//*b*/method() {
5+
//// return { x: 1, y: 1 };
6+
//// }
7+
////}
8+
////class F2 {
9+
//// met/*c*//*d*/hod(){
10+
//// return { x: 1, y: 1 };
11+
//// }
12+
////}
13+
////class F3 {
14+
//// method(/*e*//*f*/) {
15+
//// return { x: 1, y: 1 };
16+
//// }
17+
////}
18+
////class F4 {
19+
//// method() {
20+
//// return { x: 1, y: 1 /*g*//*h*/};
21+
//// }
22+
////}
23+
////class F5 {
24+
//// method() {
25+
//// return { x: 1, y: 1 /*i*//*j*/};
26+
//// }
27+
////}
28+
29+
goTo.select("a", "b");
30+
verify.refactorAvailable("Infer function return type");
31+
32+
goTo.select("c", "d");
33+
verify.refactorAvailable("Infer function return type");
34+
35+
goTo.select("e", "f");
36+
verify.refactorAvailable("Infer function return type");
37+
38+
goTo.select("g", "h");
39+
verify.not.refactorAvailable("Infer function return type");
40+
41+
goTo.select("i", "j");
42+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f1 = /*a*//*b*/function () {
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////const f2 = func/*c*//*d*/tion() {
7+
//// return { x: 1, y: 1 };
8+
////}
9+
////const f3 = function (/*e*//*f*/) {
10+
//// return { x: 1, y: 1 };
11+
////}
12+
////const f4 = function () {/*g*//*h*/
13+
//// return { x: 1, y: 1 };
14+
////}
15+
////const f5 = function () {
16+
//// return { x: 1, y: 1 /*i*//*j*/};
17+
////}
18+
19+
goTo.select("a", "b");
20+
verify.refactorAvailable("Infer function return type");
21+
22+
goTo.select("c", "d");
23+
verify.refactorAvailable("Infer function return type");
24+
25+
goTo.select("e", "f");
26+
verify.refactorAvailable("Infer function return type");
27+
28+
goTo.select("g", "h");
29+
verify.not.refactorAvailable("Infer function return type");
30+
31+
goTo.select("i", "j");
32+
verify.not.refactorAvailable("Infer function return type");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f1 = /*a*//*b*/() =>{
4+
//// return { x: 1, y: 1 };
5+
////}
6+
////const f2 = (/*c*//*d*/) => {
7+
//// return { x: 1, y: 1 };
8+
////}
9+
////const f3 = () => /*e*//*f*/{
10+
//// return { x: 1, y: 1 };
11+
////}
12+
////const f4 = () => {/*g*//*h*/
13+
//// return { x: 1, y: 1 };
14+
////}
15+
////const f5 = () => {
16+
//// return { x: 1, y: 1/*i*//*j*/ };
17+
////}
18+
19+
goTo.select("a", "b");
20+
verify.refactorAvailable("Infer function return type");
21+
22+
goTo.select("c", "d");
23+
verify.refactorAvailable("Infer function return type");
24+
25+
goTo.select("e", "f");
26+
verify.not.refactorAvailable("Infer function return type");
27+
28+
goTo.select("g", "h");
29+
verify.not.refactorAvailable("Infer function return type");
30+
31+
goTo.select("i", "j");
32+
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*//*b*/x, { y }) {
4+
//// return { x, y: 1 };
5+
////}
6+
////function f2(x, { /*c*//*d*/y }) {
7+
//// return { x, y: 1 };
8+
////}
9+
////function f3(x, /*e*//*f*/{ y }) {
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*//*b*/(x: number) => x;
4+
////const f2 = (x: number) /*c*//*d*/=> x;
5+
////const f3 = (x: number) => /*e*//*f*/x
6+
////const f4= (x: number) => x/*g*//*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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/// <reference path='fourslash.ts' />
22

3-
//// const arrow = () /*a*/=>/*b*/ 1;
3+
//// const arrow = /*a*//*b*/() => 1;
44

55
goTo.select("a", "b");
6-
verify.refactorKindAvailable("refactor.rewrite",
7-
[
6+
verify.refactorKindAvailable("refactor.rewrite", [
87
"refactor.rewrite.arrow.braces.add",
98
"refactor.rewrite.function.named",
109
"refactor.rewrite.function.anonymous",

0 commit comments

Comments
 (0)