* Computes possible places we could extract the function into. For example, * you may be able to extract into a class method *or* local closure *or* namespace function, * depending on what's in the extracted body.
(range)
| 160533 | * depending on what's in the extracted body. |
| 160534 | */ |
| 160535 | function collectEnclosingScopes(range) { |
| 160536 | var current = isReadonlyArray(range.range) ? ts.first(range.range) : range.range; |
| 160537 | if (range.facts & RangeFacts.UsesThis && !(range.facts & RangeFacts.UsesThisInFunction)) { |
| 160538 | // 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 |
| 160539 | var containingClass = ts.getContainingClass(current); |
| 160540 | if (containingClass) { |
| 160541 | var containingFunction = ts.findAncestor(current, ts.isFunctionLikeDeclaration); |
| 160542 | return containingFunction |
| 160543 | ? [containingFunction, containingClass] |
| 160544 | : [containingClass]; |
| 160545 | } |
| 160546 | } |
| 160547 | var scopes = []; |
| 160548 | while (true) { |
| 160549 | current = current.parent; |
| 160550 | // A function parameter's initializer is actually in the outer scope, not the function declaration |
| 160551 | if (current.kind === 164 /* SyntaxKind.Parameter */) { |
| 160552 | // Skip all the way to the outer scope of the function that declared this parameter |
| 160553 | current = ts.findAncestor(current, function (parent) { return ts.isFunctionLikeDeclaration(parent); }).parent; |
| 160554 | } |
| 160555 | // We want to find the nearest parent where we can place an "equivalent" sibling to the node we're extracting out of. |
| 160556 | // Walk up to the closest parent of a place where we can logically put a sibling: |
| 160557 | // * Function declaration |
| 160558 | // * Class declaration or expression |
| 160559 | // * Module/namespace or source file |
| 160560 | if (isScope(current)) { |
| 160561 | scopes.push(current); |
| 160562 | if (current.kind === 305 /* SyntaxKind.SourceFile */) { |
| 160563 | return scopes; |
| 160564 | } |
| 160565 | } |
| 160566 | } |
| 160567 | } |
| 160568 | function getFunctionExtractionAtIndex(targetRange, context, requestedChangesIndex) { |
| 160569 | var _a = getPossibleExtractionsWorker(targetRange, context), scopes = _a.scopes, _b = _a.readsAndWrites, target = _b.target, usagesPerScope = _b.usagesPerScope, functionErrorsPerScope = _b.functionErrorsPerScope, exposedVariableDeclarations = _b.exposedVariableDeclarations; |
| 160570 | ts.Debug.assert(!functionErrorsPerScope[requestedChangesIndex].length, "The extraction went missing? How?"); |
no test coverage detected