|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.addOrRemoveBracesToArrowFunction { |
| 3 | + const refactorName = "Add or remove braces in an arrow function"; |
| 4 | + const refactorDescription = Diagnostics.Add_or_remove_braces_in_an_arrow_function.message; |
| 5 | + const addBracesActionName = "Add braces to arrow function"; |
| 6 | + const removeBracesActionName = "Remove braces from arrow function"; |
| 7 | + const addBracesActionDescription = Diagnostics.Add_braces_to_arrow_function.message; |
| 8 | + const removeBracesActionDescription = Diagnostics.Remove_braces_from_arrow_function.message; |
| 9 | + registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); |
| 10 | + |
| 11 | + interface Info { |
| 12 | + func: ArrowFunction; |
| 13 | + expression: Expression | undefined; |
| 14 | + returnStatement?: ReturnStatement; |
| 15 | + addBraces: boolean; |
| 16 | + } |
| 17 | + |
| 18 | + function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { |
| 19 | + const { file, startPosition } = context; |
| 20 | + const info = getConvertibleArrowFunctionAtPosition(file, startPosition); |
| 21 | + if (!info) return undefined; |
| 22 | + |
| 23 | + return [{ |
| 24 | + name: refactorName, |
| 25 | + description: refactorDescription, |
| 26 | + actions: [ |
| 27 | + info.addBraces ? |
| 28 | + { |
| 29 | + name: addBracesActionName, |
| 30 | + description: addBracesActionDescription |
| 31 | + } : { |
| 32 | + name: removeBracesActionName, |
| 33 | + description: removeBracesActionDescription |
| 34 | + } |
| 35 | + ] |
| 36 | + }]; |
| 37 | + } |
| 38 | + |
| 39 | + function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { |
| 40 | + const { file, startPosition } = context; |
| 41 | + const info = getConvertibleArrowFunctionAtPosition(file, startPosition); |
| 42 | + if (!info) return undefined; |
| 43 | + |
| 44 | + const { expression, returnStatement, func } = info; |
| 45 | + |
| 46 | + let body: ConciseBody; |
| 47 | + if (actionName === addBracesActionName) { |
| 48 | + const returnStatement = createReturn(expression); |
| 49 | + body = createBlock([returnStatement], /* multiLine */ true); |
| 50 | + suppressLeadingAndTrailingTrivia(body); |
| 51 | + copyComments(expression!, returnStatement, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ true); |
| 52 | + } |
| 53 | + else if (actionName === removeBracesActionName && returnStatement) { |
| 54 | + const actualExpression = expression || createVoidZero(); |
| 55 | + body = needsParentheses(actualExpression) ? createParen(actualExpression) : actualExpression; |
| 56 | + suppressLeadingAndTrailingTrivia(body); |
| 57 | + copyComments(returnStatement, body, file, SyntaxKind.MultiLineCommentTrivia, /* hasTrailingNewLine */ false); |
| 58 | + } |
| 59 | + else { |
| 60 | + Debug.fail("invalid action"); |
| 61 | + } |
| 62 | + |
| 63 | + const edits = textChanges.ChangeTracker.with(context, t => t.replaceNode(file, func.body, body)); |
| 64 | + return { renameFilename: undefined, renameLocation: undefined, edits }; |
| 65 | + } |
| 66 | + |
| 67 | + function needsParentheses(expression: Expression) { |
| 68 | + return isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken || isObjectLiteralExpression(expression); |
| 69 | + } |
| 70 | + |
| 71 | + function getConvertibleArrowFunctionAtPosition(file: SourceFile, startPosition: number): Info | undefined { |
| 72 | + const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); |
| 73 | + const func = getContainingFunction(node); |
| 74 | + if (!func || !isArrowFunction(func) || (!rangeContainsRange(func, node) || rangeContainsRange(func.body, node))) return undefined; |
| 75 | + |
| 76 | + if (isExpression(func.body)) { |
| 77 | + return { |
| 78 | + func, |
| 79 | + addBraces: true, |
| 80 | + expression: func.body |
| 81 | + }; |
| 82 | + } |
| 83 | + else if (func.body.statements.length === 1) { |
| 84 | + const firstStatement = first(func.body.statements); |
| 85 | + if (isReturnStatement(firstStatement)) { |
| 86 | + return { |
| 87 | + func, |
| 88 | + addBraces: false, |
| 89 | + expression: firstStatement.expression, |
| 90 | + returnStatement: firstStatement |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + return undefined; |
| 95 | + } |
| 96 | +} |
0 commit comments