Skip to content

Commit 79b1008

Browse files
author
Andy
authored
getApplicableRefactors: Don't return undefined response (#16773)
1 parent efc861c commit 79b1008

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,17 @@ namespace ts {
467467
return result;
468468
}
469469

470+
export function flatMapIter<T, U>(iter: Iterator<T>, mapfn: (x: T) => U[] | undefined): U[] {
471+
const result: U[] = [];
472+
while (true) {
473+
const { value, done } = iter.next();
474+
if (done) break;
475+
const res = mapfn(value);
476+
if (res) result.push(...res);
477+
}
478+
return result;
479+
}
480+
470481
/**
471482
* Maps an array. If the mapped value is an array, it is spread into the result.
472483
* Avoids allocation if all elements map to themselves.

src/services/refactorProvider.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,9 @@ namespace ts {
3333
refactors.set(refactor.name, refactor);
3434
}
3535

36-
export function getApplicableRefactors(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
37-
let results: ApplicableRefactorInfo[];
38-
const refactorList: Refactor[] = [];
39-
refactors.forEach(refactor => {
40-
refactorList.push(refactor);
41-
});
42-
for (const refactor of refactorList) {
43-
if (context.cancellationToken && context.cancellationToken.isCancellationRequested()) {
44-
return results;
45-
}
46-
const infos = refactor.getAvailableActions(context);
47-
if (infos && infos.length) {
48-
(results || (results = [])).push(...infos);
49-
}
50-
}
51-
return results;
36+
export function getApplicableRefactors(context: RefactorContext): ApplicableRefactorInfo[] {
37+
return flatMapIter(refactors.values(), refactor =>
38+
context.cancellationToken && context.cancellationToken.isCancellationRequested() ? [] : refactor.getAvailableActions(context));
5239
}
5340

5441
export function getEditsForRefactor(context: RefactorContext, refactorName: string, actionName: string): RefactorEditInfo | undefined {

0 commit comments

Comments
 (0)