@@ -10,7 +10,8 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
1010
1111 interface Info {
1212 func : ArrowFunction ;
13- expression : Expression ;
13+ expression : Expression | undefined ;
14+ returnStatement ?: ReturnStatement ;
1415 addBraces : boolean ;
1516 }
1617
@@ -40,23 +41,23 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
4041 const info = getConvertibleArrowFunctionAtPosition ( file , startPosition ) ;
4142 if ( ! info ) return undefined ;
4243
43- const { expression, func } = info ;
44+ const { expression, returnStatement , func } = info ;
4445
4546 let body : ConciseBody ;
4647 if ( actionName === addBracesActionName ) {
4748 const returnStatement = createReturn ( expression ) ;
4849 body = createBlock ( [ returnStatement ] , /* multiLine */ true ) ;
4950 suppressLeadingAndTrailingTrivia ( body ) ;
50- copyComments ( expression , returnStatement , file , SyntaxKind . MultiLineCommentTrivia , true , true ) ;
51+ copyComments ( expression ! , returnStatement , file , SyntaxKind . MultiLineCommentTrivia , /* explicitHtnl */ true ) ;
5152 }
52- else if ( actionName === removeBracesActionName ) {
53- const returnStatement = < ReturnStatement > expression . parent ;
54- body = needsParentheses ( expression ) ? createParen ( expression ) : expression ;
53+ else if ( actionName === removeBracesActionName && returnStatement ) {
54+ const actualExpression = expression || createVoidZero ( ) ;
55+ body = needsParentheses ( actualExpression ) ? createParen ( actualExpression ) : actualExpression ;
5556 suppressLeadingAndTrailingTrivia ( body ) ;
56- copyComments ( returnStatement , body , file , SyntaxKind . MultiLineCommentTrivia , false ) ;
57+ copyComments ( returnStatement , body , file , SyntaxKind . MultiLineCommentTrivia , /* explicitHtnl */ false ) ;
5758 }
5859 else {
59- Debug . fail ( ' invalid action' ) ;
60+ Debug . fail ( " invalid action" ) ;
6061 }
6162
6263 const edits = textChanges . ChangeTracker . with ( context , t => updateBody ( t , file , func , body ) ) ;
@@ -89,7 +90,8 @@ namespace ts.refactor.addOrRemoveBracesToArrowFunction {
8990 return {
9091 func,
9192 addBraces : false ,
92- expression : firstStatement . expression
93+ expression : firstStatement . expression ,
94+ returnStatement : firstStatement
9395 } ;
9496 }
9597 }
0 commit comments