forked from microsoft/TypeScript
/
convertArrowFunctionOrFunctionExpression.ts
188 lines (151 loc) · 8.4 KB
/
convertArrowFunctionOrFunctionExpression.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* @internal */
namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
const refactorName = "Convert arrow function or function expression";
const refactorDescription = getLocaleSpecificMessage(Diagnostics.Convert_arrow_function_or_function_expression);
const toAnonymousFunctionActionName = "Convert to anonymous function";
const toNamedFunctionActionName = "Convert to named function";
const toArrowFunctionActionName = "Convert to arrow function";
const toAnonymousFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_anonymous_function);
const toNamedFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_named_function);
const toArrowFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_arrow_function);
registerRefactor(refactorName, { getEditsForAction, getAvailableActions });
interface FunctionInfo {
selectedVariableDeclaration: boolean;
func: FunctionExpression | ArrowFunction;
}
interface VariableInfo {
variableDeclaration: VariableDeclaration;
variableDeclarationList: VariableDeclarationList;
statement: VariableStatement;
name: Identifier;
}
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
const { file, startPosition } = context;
const info = getFunctionInfo(file, startPosition);
if (!info) return undefined;
const { selectedVariableDeclaration, func } = info;
const possibleActions: RefactorActionInfo[] = [];
if (selectedVariableDeclaration || (isArrowFunction(func) && isVariableDeclaration(func.parent))) {
possibleActions.push({
name: toNamedFunctionActionName,
description: toNamedFunctionActionDescription
});
}
if (!selectedVariableDeclaration && isArrowFunction(func)) {
possibleActions.push({
name: toAnonymousFunctionActionName,
description: toAnonymousFunctionActionDescription
});
}
if (isFunctionExpression(func)) {
possibleActions.push({
name: toArrowFunctionActionName,
description: toArrowFunctionActionDescription
});
}
return [{
name: refactorName,
description: refactorDescription,
actions: possibleActions
}];
}
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined {
const { file, startPosition } = context;
const info = getFunctionInfo(file, startPosition);
if (!info) return undefined;
const { func } = info;
switch (actionName) {
case toAnonymousFunctionActionName:
return getEditInfoForConvertToAnonymousFunction(context, func);
case toNamedFunctionActionName:
return getEditInfoForConvertToNamedFunction(context, func);
case toArrowFunctionActionName:
return getEditInfoForConvertToArrowFunction(context, func);
default:
Debug.fail("invalid action");
break;
}
return undefined;
}
function getFunctionInfo(file: SourceFile, startPosition: number): FunctionInfo | undefined {
const token = getTokenAtPosition(file, startPosition);
let maybeFunc;
maybeFunc = getArrowFunctionFromVariableDeclaration(token.parent);
if (!!maybeFunc) return { selectedVariableDeclaration: true, func: maybeFunc };
maybeFunc = getContainingFunction(token);
if (!!maybeFunc && (isFunctionExpression(maybeFunc) || isArrowFunction(maybeFunc)) && !rangeContainsRange(maybeFunc.body, token)) {
return { selectedVariableDeclaration: false, func: maybeFunc };
}
return undefined;
}
function isSingleVariableDeclaration(parent: Node): parent is VariableDeclarationList {
return isVariableDeclarationList(parent) && parent.declarations.length === 1;
}
function getArrowFunctionFromVariableDeclaration(parent: Node): ArrowFunction | undefined {
if (!(isVariableDeclaration(parent) || isSingleVariableDeclaration(parent))) return undefined;
const variableDeclaration = isVariableDeclaration(parent) ? parent : parent.declarations[0];
const initializer = variableDeclaration.initializer;
if (!initializer || !isArrowFunction(initializer)) return undefined;
return initializer;
}
function convertToBlock(func: ArrowFunction | FunctionExpression): Block {
if (isExpression(func.body)) {
return createBlock([createReturn(func.body)], /* multiLine */ true);
}
else {
return func.body;
}
}
function getVariableInfo(func: FunctionExpression | ArrowFunction): VariableInfo | undefined {
const variableDeclaration = func.parent;
if (!isVariableDeclaration(variableDeclaration) || !isVariableDeclarationInVariableStatement(variableDeclaration)) return undefined;
const variableDeclarationList = variableDeclaration.parent;
const statement = variableDeclarationList.parent;
if (!isVariableDeclarationList(variableDeclarationList) || !isVariableStatement(statement) || !isIdentifier(variableDeclaration.name)) return undefined;
return { variableDeclaration, variableDeclarationList, statement, name: variableDeclaration.name };
}
function getEditInfoForConvertToAnonymousFunction(context: RefactorContext, func: FunctionExpression | ArrowFunction): RefactorEditInfo {
const { file } = context;
const body = convertToBlock(func);
const newNode = createFunctionExpression(func.modifiers, func.asteriskToken, /* name */ undefined, func.typeParameters, func.parameters, func.type, body);
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func, newNode));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
function getEditInfoForConvertToNamedFunction(context: RefactorContext, func: FunctionExpression | ArrowFunction): RefactorEditInfo | undefined {
const { file } = context;
const body = convertToBlock(func);
const variableInfo = getVariableInfo(func);
if (!variableInfo) return undefined;
const { variableDeclaration, variableDeclarationList, statement, name } = variableInfo;
const newNode = createFunctionDeclaration(func.decorators, statement.modifiers, func.asteriskToken, name, func.typeParameters, func.parameters, func.type, body);
let edits: FileTextChanges[];
if (variableDeclarationList.declarations.length === 1) {
edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement, newNode));
}
else {
edits = textChanges.ChangeTracker.with(context, t => {
t.delete(file, variableDeclaration);
t.insertNodeAfter(file, statement, newNode);
});
}
return { renameFilename: undefined, renameLocation: undefined, edits };
}
function getEditInfoForConvertToArrowFunction(context: RefactorContext, func: FunctionExpression | ArrowFunction): RefactorEditInfo | undefined {
const { file } = context;
if (!isFunctionExpression(func)) return undefined;
const statements = func.body.statements;
const head = statements[0];
let body: ConciseBody;
if (func.body.statements.length === 1 && ((isReturnStatement(head) && !!head.expression) || isExpressionStatement(head))) {
body = head.expression!;
suppressLeadingAndTrailingTrivia(body);
copyComments(head, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false);
}
else {
body = func.body;
}
const newNode = createArrowFunction(func.modifiers, func.typeParameters, func.parameters, func.type, /* equalsGreaterThanToken */ undefined, body);
const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func, newNode));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
}