Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some unnecessary undefined checks in extractSymbol #19256

Merged
1 commit merged into from
Oct 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ namespace ts.refactor.extractSymbol {
* you may be able to extract into a class method *or* local closure *or* namespace function,
* depending on what's in the extracted body.
*/
function collectEnclosingScopes(range: TargetRange): Scope[] | undefined {
function collectEnclosingScopes(range: TargetRange): Scope[] {
let current: Node = isReadonlyArray(range.range) ? first(range.range) : range.range;
if (range.facts & RangeFacts.UsesThis) {
// 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
Expand All @@ -483,30 +483,27 @@ namespace ts.refactor.extractSymbol {
}
}

const start = current;
const scopes: Scope[] = [];
while (true) {
current = current.parent;
// A function parameter's initializer is actually in the outer scope, not the function declaration
if (current.kind === SyntaxKind.Parameter) {
// Skip all the way to the outer scope of the function that declared this parameter
current = findAncestor(current, parent => isFunctionLikeDeclaration(parent)).parent;
}

let scopes: Scope[] | undefined = undefined;
while (current) {
// We want to find the nearest parent where we can place an "equivalent" sibling to the node we're extracting out of.
// Walk up to the closest parent of a place where we can logically put a sibling:
// * Function declaration
// * Class declaration or expression
// * Module/namespace or source file
if (current !== start && isScope(current)) {
(scopes = scopes || []).push(current);
}

// A function parameter's initializer is actually in the outer scope, not the function declaration
if (current && current.parent && current.parent.kind === SyntaxKind.Parameter) {
// Skip all the way to the outer scope of the function that declared this parameter
current = findAncestor(current, parent => isFunctionLikeDeclaration(parent)).parent;
}
else {
current = current.parent;
if (isScope(current)) {
scopes.push(current);
if (current.kind === SyntaxKind.SourceFile) {
return scopes;
}
}

}
return scopes;
}

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

if (targetRange === undefined) {
return undefined;
}

const scopes = collectEnclosingScopes(targetRange);
if (scopes === undefined) {
return undefined;
}

const enclosingTextRange = getEnclosingTextRange(targetRange, sourceFile);
const readsAndWrites = collectReadsAndWrites(
targetRange,
Expand Down