@@ -470,7 +470,7 @@ namespace ts.refactor.extractSymbol {
470
470
* you may be able to extract into a class method *or* local closure *or* namespace function,
471
471
* depending on what's in the extracted body.
472
472
*/
473
- function collectEnclosingScopes ( range : TargetRange ) : Scope [ ] | undefined {
473
+ function collectEnclosingScopes ( range : TargetRange ) : Scope [ ] {
474
474
let current : Node = isReadonlyArray ( range . range ) ? first ( range . range ) : range . range ;
475
475
if ( range . facts & RangeFacts . UsesThis ) {
476
476
// if range uses this as keyword or as type inside the class then it can only be extracted to a method of the containing class
@@ -483,30 +483,27 @@ namespace ts.refactor.extractSymbol {
483
483
}
484
484
}
485
485
486
- const start = current ;
486
+ const scopes : Scope [ ] = [ ] ;
487
+ while ( true ) {
488
+ current = current . parent ;
489
+ // A function parameter's initializer is actually in the outer scope, not the function declaration
490
+ if ( current . kind === SyntaxKind . Parameter ) {
491
+ // Skip all the way to the outer scope of the function that declared this parameter
492
+ current = findAncestor ( current , parent => isFunctionLikeDeclaration ( parent ) ) . parent ;
493
+ }
487
494
488
- let scopes : Scope [ ] | undefined = undefined ;
489
- while ( current ) {
490
495
// We want to find the nearest parent where we can place an "equivalent" sibling to the node we're extracting out of.
491
496
// Walk up to the closest parent of a place where we can logically put a sibling:
492
497
// * Function declaration
493
498
// * Class declaration or expression
494
499
// * Module/namespace or source file
495
- if ( current !== start && isScope ( current ) ) {
496
- ( scopes = scopes || [ ] ) . push ( current ) ;
497
- }
498
-
499
- // A function parameter's initializer is actually in the outer scope, not the function declaration
500
- if ( current && current . parent && current . parent . kind === SyntaxKind . Parameter ) {
501
- // Skip all the way to the outer scope of the function that declared this parameter
502
- current = findAncestor ( current , parent => isFunctionLikeDeclaration ( parent ) ) . parent ;
503
- }
504
- else {
505
- current = current . parent ;
500
+ if ( isScope ( current ) ) {
501
+ scopes . push ( current ) ;
502
+ if ( current . kind === SyntaxKind . SourceFile ) {
503
+ return scopes ;
504
+ }
506
505
}
507
-
508
506
}
509
- return scopes ;
510
507
}
511
508
512
509
function getFunctionExtractionAtIndex ( targetRange : TargetRange , context : RefactorContext , requestedChangesIndex : number ) : RefactorEditInfo {
@@ -592,15 +589,7 @@ namespace ts.refactor.extractSymbol {
592
589
function getPossibleExtractionsWorker ( targetRange : TargetRange , context : RefactorContext ) : { readonly scopes : Scope [ ] , readonly readsAndWrites : ReadsAndWrites } {
593
590
const { file : sourceFile } = context ;
594
591
595
- if ( targetRange === undefined ) {
596
- return undefined ;
597
- }
598
-
599
592
const scopes = collectEnclosingScopes ( targetRange ) ;
600
- if ( scopes === undefined ) {
601
- return undefined ;
602
- }
603
-
604
593
const enclosingTextRange = getEnclosingTextRange ( targetRange , sourceFile ) ;
605
594
const readsAndWrites = collectReadsAndWrites (
606
595
targetRange ,
0 commit comments