@@ -232,17 +232,7 @@ namespace ts.refactor.extractMethod {
232
232
return { errors } ;
233
233
}
234
234
235
- // If our selection is the expression in an ExpressionStatement, expand
236
- // the selection to include the enclosing Statement (this stops us
237
- // from trying to care about the return value of the extracted function
238
- // and eliminates double semicolon insertion in certain scenarios)
239
- const range = isStatement ( start )
240
- ? [ start ]
241
- : start . parent && start . parent . kind === SyntaxKind . ExpressionStatement
242
- ? [ start . parent as Statement ]
243
- : start as Expression ;
244
-
245
- return { targetRange : { range, facts : rangeFacts , declarations } } ;
235
+ return { targetRange : { range : getStatementOrExpressionRange ( start ) , facts : rangeFacts , declarations } } ;
246
236
}
247
237
248
238
function createErrorResult ( sourceFile : SourceFile , start : number , length : number , message : DiagnosticMessage ) : RangeToExtract {
@@ -459,6 +449,20 @@ namespace ts.refactor.extractMethod {
459
449
}
460
450
}
461
451
452
+ function getStatementOrExpressionRange ( node : Node ) : Statement [ ] | Expression {
453
+ if ( isStatement ( node ) ) {
454
+ return [ node ] ;
455
+ }
456
+ else if ( isPartOfExpression ( node ) ) {
457
+ // If our selection is the expression in an ExpressionStatement, expand
458
+ // the selection to include the enclosing Statement (this stops us
459
+ // from trying to care about the return value of the extracted function
460
+ // and eliminates double semicolon insertion in certain scenarios)
461
+ return isExpressionStatement ( node . parent ) ? [ node . parent ] : node as Expression ;
462
+ }
463
+ return undefined ;
464
+ }
465
+
462
466
function isValidExtractionTarget ( node : Node ) : node is Scope {
463
467
// Note that we don't use isFunctionLike because we don't want to put the extracted closure *inside* a method
464
468
return ( node . kind === SyntaxKind . FunctionDeclaration ) || isSourceFile ( node ) || isModuleBlock ( node ) || isClassLike ( node ) ;
0 commit comments