@@ -41,49 +41,34 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
41
41
if ( ! info ) return undefined ;
42
42
43
43
const { expression, func } = info ;
44
- const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
45
44
46
45
let body : ConciseBody ;
47
46
if ( actionName === addBracesActionName ) {
48
47
const returnStatement = createReturn ( expression ) ;
49
48
body = createBlock ( [ returnStatement ] , /* multiLine */ true ) ;
50
- suppressLeadingAndTrailingTrivia ( expression ) ;
49
+ suppressLeadingAndTrailingTrivia ( body ) ;
51
50
copyComments ( expression , returnStatement , file , SyntaxKind . MultiLineCommentTrivia , true , true ) ;
52
51
}
53
52
else if ( actionName === removeBracesActionName ) {
54
53
const returnStatement = < ReturnStatement > expression . parent ;
55
54
body = needsParentheses ( expression ) ? createParen ( expression ) : expression ;
56
- suppressLeadingAndTrailingTrivia ( returnStatement ) ;
55
+ suppressLeadingAndTrailingTrivia ( body ) ;
57
56
copyComments ( returnStatement , body , file , SyntaxKind . MultiLineCommentTrivia , false ) ;
58
57
}
59
58
else {
60
59
Debug . fail ( 'invalid action' ) ;
61
60
}
62
61
63
- updateBody ( changeTracker , file , func , body ) ;
64
-
65
- return {
66
- renameFilename : undefined ,
67
- renameLocation : undefined ,
68
- edits : changeTracker . getChanges ( )
69
- } ;
62
+ const edits = textChanges . ChangeTracker . with ( context , t => updateBody ( t , file , func , body ) ) ;
63
+ return { renameFilename : undefined , renameLocation : undefined , edits } ;
70
64
}
71
65
72
66
function needsParentheses ( expression : Expression ) {
73
- if ( isBinaryExpression ( expression ) && expression . operatorToken . kind === SyntaxKind . CommaToken ) return true ;
74
- if ( isObjectLiteralExpression ( expression ) ) return true ;
75
- return false ;
67
+ return isBinaryExpression ( expression ) && expression . operatorToken . kind === SyntaxKind . CommaToken || isObjectLiteralExpression ( expression ) ;
76
68
}
77
69
78
70
function updateBody ( changeTracker : textChanges . ChangeTracker , file : SourceFile , container : ArrowFunction , body : ConciseBody ) {
79
- const arrowFunction = updateArrowFunction (
80
- container ,
81
- container . modifiers ,
82
- container . typeParameters ,
83
- container . parameters ,
84
- container . type ,
85
- body ) ;
86
- changeTracker . replaceNode ( file , container , arrowFunction ) ;
71
+ changeTracker . replaceNode ( file , container . body , body ) ;
87
72
}
88
73
89
74
function getConvertibleArrowFunctionAtPosition ( file : SourceFile , startPosition : number ) : Info | undefined {
0 commit comments