forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinferFunctionReturnType.ts
More file actions
161 lines (146 loc) · 5.91 KB
/
Copy pathinferFunctionReturnType.ts
File metadata and controls
161 lines (146 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
ApplicableRefactorInfo,
ArrowFunction,
Diagnostics,
emptyArray,
factory,
findAncestor,
findChildOfKind,
first,
FunctionDeclaration,
FunctionExpression,
getLocaleSpecificMessage,
getTouchingPropertyName,
InternalNodeBuilderFlags,
isArrowFunction,
isBlock,
isInJSFile,
mapDefined,
MethodDeclaration,
Node,
NodeBuilderFlags,
RefactorContext,
RefactorEditInfo,
SourceFile,
SyntaxKind,
textChanges,
Type,
TypeNode,
} from "../_namespaces/ts.js";
import {
isRefactorErrorInfo,
RefactorErrorInfo,
refactorKindBeginsWith,
registerRefactor,
} from "../_namespaces/ts.refactor.js";
const refactorName = "Infer function return type";
const refactorDescription = getLocaleSpecificMessage(Diagnostics.Infer_function_return_type);
const inferReturnTypeAction = {
name: refactorName,
description: refactorDescription,
kind: "refactor.rewrite.function.returnType",
};
registerRefactor(refactorName, {
kinds: [inferReturnTypeAction.kind],
getEditsForAction: getRefactorEditsToInferReturnType,
getAvailableActions: getRefactorActionsToInferReturnType,
});
function getRefactorEditsToInferReturnType(context: RefactorContext): RefactorEditInfo | undefined {
const info = getInfo(context);
if (info && !isRefactorErrorInfo(info)) {
const edits = textChanges.ChangeTracker.with(context, t => doChange(context.file, t, info.declaration, info.returnTypeNode));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
return undefined;
}
function getRefactorActionsToInferReturnType(context: RefactorContext): readonly ApplicableRefactorInfo[] {
const info = getInfo(context);
if (!info) return emptyArray;
if (!isRefactorErrorInfo(info)) {
return [{
name: refactorName,
description: refactorDescription,
actions: [inferReturnTypeAction],
}];
}
if (context.preferences.provideRefactorNotApplicableReason) {
return [{
name: refactorName,
description: refactorDescription,
actions: [{ ...inferReturnTypeAction, notApplicableReason: info.error }],
}];
}
return emptyArray;
}
type ConvertibleDeclaration =
| FunctionDeclaration
| FunctionExpression
| ArrowFunction
| MethodDeclaration;
interface FunctionInfo {
declaration: ConvertibleDeclaration;
returnTypeNode: TypeNode;
}
function doChange(sourceFile: SourceFile, changes: textChanges.ChangeTracker, declaration: ConvertibleDeclaration, typeNode: TypeNode) {
const closeParen = findChildOfKind(declaration, SyntaxKind.CloseParenToken, sourceFile);
const needParens = isArrowFunction(declaration) && closeParen === undefined;
const endNode = needParens ? first(declaration.parameters) : closeParen;
if (endNode) {
if (needParens) {
changes.insertNodeBefore(sourceFile, endNode, factory.createToken(SyntaxKind.OpenParenToken));
changes.insertNodeAfter(sourceFile, endNode, factory.createToken(SyntaxKind.CloseParenToken));
}
changes.insertNodeAt(sourceFile, endNode.end, typeNode, { prefix: ": " });
}
}
function getInfo(context: RefactorContext): FunctionInfo | RefactorErrorInfo | undefined {
if (isInJSFile(context.file) || !refactorKindBeginsWith(inferReturnTypeAction.kind, context.kind)) return;
const token = getTouchingPropertyName(context.file, context.startPosition);
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) };
}
const typeChecker = context.program.getTypeChecker();
let returnType: Type | undefined;
if (typeChecker.isImplementationOfOverload(declaration)) {
const signatures = typeChecker.getTypeAtLocation(declaration).getCallSignatures();
if (signatures.length > 1) {
returnType = typeChecker.getUnionType(mapDefined(signatures, s => s.getReturnType()));
}
}
if (!returnType) {
const signature = typeChecker.getSignatureFromDeclaration(declaration);
if (signature) {
const typePredicate = typeChecker.getTypePredicateOfSignature(signature);
if (typePredicate && typePredicate.type) {
const typePredicateTypeNode = typeChecker.typePredicateToTypePredicateNode(typePredicate, declaration, NodeBuilderFlags.NoTruncation, InternalNodeBuilderFlags.AllowUnresolvedNames);
if (typePredicateTypeNode) {
return { declaration, returnTypeNode: typePredicateTypeNode };
}
}
else {
returnType = typeChecker.getReturnTypeOfSignature(signature);
}
}
}
if (!returnType) {
return { error: getLocaleSpecificMessage(Diagnostics.Could_not_determine_function_return_type) };
}
const returnTypeNode = typeChecker.typeToTypeNode(returnType, declaration, NodeBuilderFlags.NoTruncation, InternalNodeBuilderFlags.AllowUnresolvedNames);
if (returnTypeNode) {
return { declaration, returnTypeNode };
}
}
function isConvertibleDeclaration(node: Node): node is ConvertibleDeclaration {
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
case SyntaxKind.MethodDeclaration:
return true;
default:
return false;
}
}