|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.convertArrowFunctionOrFunctionExpression { |
| 3 | + const refactorName = "Convert arrow function or function expression"; |
| 4 | + const refactorDescription = getLocaleSpecificMessage(Diagnostics.Convert_arrow_function_or_function_expression); |
| 5 | + |
| 6 | + const toAnonymousFunctionActionName = "Convert to anonymous function"; |
| 7 | + const toNamedFunctionActionName = "Convert to named function"; |
| 8 | + const toArrowFunctionActionName = "Convert to arrow function"; |
| 9 | + |
| 10 | + const toAnonymousFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_anonymous_function); |
| 11 | + const toNamedFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_named_function); |
| 12 | + const toArrowFunctionActionDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_arrow_function); |
| 13 | + |
| 14 | + registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); |
| 15 | + |
| 16 | + interface FunctionInfo { |
| 17 | + readonly selectedVariableDeclaration: boolean; |
| 18 | + readonly func: FunctionExpression | ArrowFunction; |
| 19 | + } |
| 20 | + |
| 21 | + interface VariableInfo { |
| 22 | + readonly variableDeclaration: VariableDeclaration; |
| 23 | + readonly variableDeclarationList: VariableDeclarationList; |
| 24 | + readonly statement: VariableStatement; |
| 25 | + readonly name: Identifier; |
| 26 | + } |
| 27 | + |
| 28 | + function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] { |
| 29 | + const { file, startPosition, program } = context; |
| 30 | + const info = getFunctionInfo(file, startPosition, program); |
| 31 | + |
| 32 | + if (!info) return emptyArray; |
| 33 | + const { selectedVariableDeclaration, func } = info; |
| 34 | + const possibleActions: RefactorActionInfo[] = []; |
| 35 | + |
| 36 | + if (selectedVariableDeclaration || (isArrowFunction(func) && isVariableDeclaration(func.parent))) { |
| 37 | + possibleActions.push({ |
| 38 | + name: toNamedFunctionActionName, |
| 39 | + description: toNamedFunctionActionDescription |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + if (!selectedVariableDeclaration && isArrowFunction(func)) { |
| 44 | + possibleActions.push({ |
| 45 | + name: toAnonymousFunctionActionName, |
| 46 | + description: toAnonymousFunctionActionDescription |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + if (isFunctionExpression(func)) { |
| 51 | + possibleActions.push({ |
| 52 | + name: toArrowFunctionActionName, |
| 53 | + description: toArrowFunctionActionDescription |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return [{ |
| 58 | + name: refactorName, |
| 59 | + description: refactorDescription, |
| 60 | + actions: possibleActions |
| 61 | + }]; |
| 62 | + } |
| 63 | + |
| 64 | + function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { |
| 65 | + const { file, startPosition, program } = context; |
| 66 | + const info = getFunctionInfo(file, startPosition, program); |
| 67 | + |
| 68 | + if (!info) return undefined; |
| 69 | + const { func } = info; |
| 70 | + const edits: FileTextChanges[] = []; |
| 71 | + |
| 72 | + switch (actionName) { |
| 73 | + case toAnonymousFunctionActionName: |
| 74 | + edits.push(...getEditInfoForConvertToAnonymousFunction(context, func)); |
| 75 | + break; |
| 76 | + |
| 77 | + case toNamedFunctionActionName: |
| 78 | + const variableInfo = getVariableInfo(func); |
| 79 | + if (!variableInfo) return undefined; |
| 80 | + |
| 81 | + edits.push(...getEditInfoForConvertToNamedFunction(context, func, variableInfo)); |
| 82 | + break; |
| 83 | + |
| 84 | + case toArrowFunctionActionName: |
| 85 | + if (!isFunctionExpression(func)) return undefined; |
| 86 | + edits.push(...getEditInfoForConvertToArrowFunction(context, func)); |
| 87 | + break; |
| 88 | + |
| 89 | + default: |
| 90 | + return Debug.fail("invalid action"); |
| 91 | + } |
| 92 | + |
| 93 | + return { renameFilename: undefined, renameLocation: undefined, edits }; |
| 94 | + } |
| 95 | + |
| 96 | + function containingThis(node: Node): boolean { |
| 97 | + let containsThis = false; |
| 98 | + node.forEachChild(function checkThis(child) { |
| 99 | + |
| 100 | + if (isThis(child)) { |
| 101 | + containsThis = true; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (!isClassLike(child) && !isFunctionDeclaration(child) && !isFunctionExpression(child)) { |
| 106 | + forEachChild(child, checkThis); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + return containsThis; |
| 111 | + } |
| 112 | + |
| 113 | + function getFunctionInfo(file: SourceFile, startPosition: number, program: Program): FunctionInfo | undefined { |
| 114 | + const token = getTokenAtPosition(file, startPosition); |
| 115 | + |
| 116 | + const arrowFunc = getArrowFunctionFromVariableDeclaration(token.parent); |
| 117 | + if (arrowFunc && !containingThis(arrowFunc.body)) return { selectedVariableDeclaration: true, func: arrowFunc }; |
| 118 | + |
| 119 | + const maybeFunc = getContainingFunction(token); |
| 120 | + const typeChecker = program.getTypeChecker(); |
| 121 | + |
| 122 | + if ( |
| 123 | + maybeFunc && |
| 124 | + (isFunctionExpression(maybeFunc) || isArrowFunction(maybeFunc)) && |
| 125 | + !rangeContainsRange(maybeFunc.body, token) && |
| 126 | + !containingThis(maybeFunc.body) |
| 127 | + ) { |
| 128 | + if ((isFunctionExpression(maybeFunc) && maybeFunc.name && FindAllReferences.Core.isSymbolReferencedInFile(maybeFunc.name, typeChecker, file))) return undefined; |
| 129 | + return { selectedVariableDeclaration: false, func: maybeFunc }; |
| 130 | + } |
| 131 | + |
| 132 | + return undefined; |
| 133 | + } |
| 134 | + |
| 135 | + function isSingleVariableDeclaration(parent: Node): parent is VariableDeclarationList { |
| 136 | + return isVariableDeclaration(parent) || (isVariableDeclarationList(parent) && parent.declarations.length === 1); |
| 137 | + } |
| 138 | + |
| 139 | + function getArrowFunctionFromVariableDeclaration(parent: Node): ArrowFunction | undefined { |
| 140 | + if (!isSingleVariableDeclaration(parent)) return undefined; |
| 141 | + const variableDeclaration = isVariableDeclaration(parent) ? parent : parent.declarations[0]; |
| 142 | + |
| 143 | + const initializer = variableDeclaration.initializer; |
| 144 | + if (!initializer || !isArrowFunction(initializer)) return undefined; |
| 145 | + return initializer; |
| 146 | + } |
| 147 | + |
| 148 | + function convertToBlock(body: ConciseBody): Block { |
| 149 | + if (isExpression(body)) { |
| 150 | + return createBlock([createReturn(body)], /* multiLine */ true); |
| 151 | + } |
| 152 | + else { |
| 153 | + return body; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + function getVariableInfo(func: FunctionExpression | ArrowFunction): VariableInfo | undefined { |
| 158 | + const variableDeclaration = func.parent; |
| 159 | + if (!isVariableDeclaration(variableDeclaration) || !isVariableDeclarationInVariableStatement(variableDeclaration)) return undefined; |
| 160 | + |
| 161 | + const variableDeclarationList = variableDeclaration.parent; |
| 162 | + const statement = variableDeclarationList.parent; |
| 163 | + if (!isVariableDeclarationList(variableDeclarationList) || !isVariableStatement(statement) || !isIdentifier(variableDeclaration.name)) return undefined; |
| 164 | + |
| 165 | + return { variableDeclaration, variableDeclarationList, statement, name: variableDeclaration.name }; |
| 166 | + } |
| 167 | + |
| 168 | + function getEditInfoForConvertToAnonymousFunction(context: RefactorContext, func: FunctionExpression | ArrowFunction): FileTextChanges[] { |
| 169 | + const { file } = context; |
| 170 | + const body = convertToBlock(func.body); |
| 171 | + const newNode = createFunctionExpression(func.modifiers, func.asteriskToken, /* name */ undefined, func.typeParameters, func.parameters, func.type, body); |
| 172 | + return textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func, newNode)); |
| 173 | + } |
| 174 | + |
| 175 | + function getEditInfoForConvertToNamedFunction(context: RefactorContext, func: FunctionExpression | ArrowFunction, variableInfo: VariableInfo): FileTextChanges[] { |
| 176 | + const { file } = context; |
| 177 | + const body = convertToBlock(func.body); |
| 178 | + |
| 179 | + const { variableDeclaration, variableDeclarationList, statement, name } = variableInfo; |
| 180 | + suppressLeadingTrivia(statement); |
| 181 | + const newNode = createFunctionDeclaration(func.decorators, statement.modifiers, func.asteriskToken, name, func.typeParameters, func.parameters, func.type, body); |
| 182 | + |
| 183 | + if (variableDeclarationList.declarations.length === 1) { |
| 184 | + return textChanges.ChangeTracker.with(context, t => t.replaceNode(file, statement, newNode)); |
| 185 | + } |
| 186 | + else { |
| 187 | + return textChanges.ChangeTracker.with(context, t => { |
| 188 | + t.delete(file, variableDeclaration); |
| 189 | + t.insertNodeAfter(file, statement, newNode); |
| 190 | + }); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + function getEditInfoForConvertToArrowFunction(context: RefactorContext, func: FunctionExpression): FileTextChanges[] { |
| 195 | + const { file } = context; |
| 196 | + const statements = func.body.statements; |
| 197 | + const head = statements[0]; |
| 198 | + let body: ConciseBody; |
| 199 | + |
| 200 | + if (canBeConvertedToExpression(func.body, head)) { |
| 201 | + body = head.expression!; |
| 202 | + suppressLeadingAndTrailingTrivia(body); |
| 203 | + copyComments(head, body); |
| 204 | + } |
| 205 | + else { |
| 206 | + body = func.body; |
| 207 | + } |
| 208 | + |
| 209 | + const newNode = createArrowFunction(func.modifiers, func.typeParameters, func.parameters, func.type, createToken(SyntaxKind.EqualsGreaterThanToken), body); |
| 210 | + return textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func, newNode)); |
| 211 | + } |
| 212 | + |
| 213 | + function canBeConvertedToExpression(body: Block, head: Statement): head is ReturnStatement { |
| 214 | + return body.statements.length === 1 && ((isReturnStatement(head) && !!head.expression)); |
| 215 | + } |
| 216 | +} |
0 commit comments