Skip to content

Commit a53d3bb

Browse files
author
Andy
authored
Remove some unnecessary undefined checks in extractSymbol (#19256)
1 parent 8212c96 commit a53d3bb

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

Diff for: src/services/refactors/extractSymbol.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ namespace ts.refactor.extractSymbol {
470470
* you may be able to extract into a class method *or* local closure *or* namespace function,
471471
* depending on what's in the extracted body.
472472
*/
473-
function collectEnclosingScopes(range: TargetRange): Scope[] | undefined {
473+
function collectEnclosingScopes(range: TargetRange): Scope[] {
474474
let current: Node = isReadonlyArray(range.range) ? first(range.range) : range.range;
475475
if (range.facts & RangeFacts.UsesThis) {
476476
// 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 {
483483
}
484484
}
485485

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+
}
487494

488-
let scopes: Scope[] | undefined = undefined;
489-
while (current) {
490495
// We want to find the nearest parent where we can place an "equivalent" sibling to the node we're extracting out of.
491496
// Walk up to the closest parent of a place where we can logically put a sibling:
492497
// * Function declaration
493498
// * Class declaration or expression
494499
// * 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+
}
506505
}
507-
508506
}
509-
return scopes;
510507
}
511508

512509
function getFunctionExtractionAtIndex(targetRange: TargetRange, context: RefactorContext, requestedChangesIndex: number): RefactorEditInfo {
@@ -592,15 +589,7 @@ namespace ts.refactor.extractSymbol {
592589
function getPossibleExtractionsWorker(targetRange: TargetRange, context: RefactorContext): { readonly scopes: Scope[], readonly readsAndWrites: ReadsAndWrites } {
593590
const { file: sourceFile } = context;
594591

595-
if (targetRange === undefined) {
596-
return undefined;
597-
}
598-
599592
const scopes = collectEnclosingScopes(targetRange);
600-
if (scopes === undefined) {
601-
return undefined;
602-
}
603-
604593
const enclosingTextRange = getEnclosingTextRange(targetRange, sourceFile);
605594
const readsAndWrites = collectReadsAndWrites(
606595
targetRange,

0 commit comments

Comments
 (0)