@@ -95,18 +95,18 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
95
95
96
96
maybeFunc = getContainingFunction ( token ) ;
97
97
if ( ! ! maybeFunc && ( isFunctionExpression ( maybeFunc ) || isArrowFunction ( maybeFunc ) ) && ! rangeContainsRange ( maybeFunc . body , token ) ) {
98
- return { selectedVariableDeclaration : false , func : maybeFunc } ;
98
+ return { selectedVariableDeclaration : false , func : maybeFunc } ;
99
99
}
100
100
101
101
return undefined ;
102
102
}
103
103
104
104
function isSingleVariableDeclaration ( parent : Node ) : parent is VariableDeclarationList {
105
- return isVariableDeclarationList ( parent ) && parent . declarations . length === 1 ;
105
+ return isVariableDeclaration ( parent ) || ( isVariableDeclarationList ( parent ) && parent . declarations . length === 1 ) ;
106
106
}
107
107
108
108
function getArrowFunctionFromVariableDeclaration ( parent : Node ) : ArrowFunction | undefined {
109
- if ( ! ( isVariableDeclaration ( parent ) || isSingleVariableDeclaration ( parent ) ) ) return undefined ;
109
+ if ( ! isSingleVariableDeclaration ( parent ) ) return undefined ;
110
110
const variableDeclaration = isVariableDeclaration ( parent ) ? parent : parent . declarations [ 0 ] ;
111
111
112
112
const initializer = variableDeclaration . initializer ;
@@ -172,7 +172,7 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
172
172
const head = statements [ 0 ] ;
173
173
let body : ConciseBody ;
174
174
175
- if ( func . body . statements . length === 1 && ( ( isReturnStatement ( head ) && ! ! head . expression ) || isExpressionStatement ( head ) ) ) {
175
+ if ( canBeConvertedToExpression ( func . body , head ) ) {
176
176
body = head . expression ! ;
177
177
suppressLeadingAndTrailingTrivia ( body ) ;
178
178
copyComments ( head , body , file , SyntaxKind . MultiLineCommentTrivia , /* hasTrailingNewLine */ false ) ;
@@ -185,4 +185,8 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression {
185
185
const edits = textChanges . ChangeTracker . with ( context , t => t . replaceNode ( file , func , newNode ) ) ;
186
186
return { renameFilename : undefined , renameLocation : undefined , edits } ;
187
187
}
188
+
189
+ function canBeConvertedToExpression ( body : Block , head : Statement ) : head is ReturnStatement | ExpressionStatement {
190
+ return body . statements . length === 1 && ( ( isReturnStatement ( head ) && ! ! head . expression ) || isExpressionStatement ( head ) ) ;
191
+ }
188
192
}
0 commit comments