Skip to content

Commit 8392c36

Browse files
committed
Introduce isNonEmptyArray
This negation of `isFalsyOrEmpty` works better for type guards. After the check, you know that you have an array with at least one element
1 parent f205b5a commit 8392c36

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/vs/base/common/arrays.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,14 @@ export function move(array: any[], from: number, to: number): void {
334334
* and not empty.
335335
*/
336336
export function isFalsyOrEmpty(obj: any): boolean {
337-
return !Array.isArray(obj) || (<Array<any>>obj).length === 0;
337+
return !Array.isArray(obj) || obj.length === 0;
338+
}
339+
340+
/**
341+
* @returns {{true}} if the provided object is an array and has at least one element.
342+
*/
343+
export function isNonEmptyArray<T>(obj: ReadonlyArray<T> | undefined | null): obj is Array<T> {
344+
return Array.isArray(obj) && obj.length > 0;
338345
}
339346

340347
/**

src/vs/editor/contrib/codeAction/codeAction.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { flatten, isFalsyOrEmpty, mergeSort } from 'vs/base/common/arrays';
6+
import { flatten, mergeSort, isNonEmptyArray } from 'vs/base/common/arrays';
77
import { CancellationToken } from 'vs/base/common/cancellation';
88
import { illegalArgument, isPromiseCanceledError, onUnexpectedExternalError } from 'vs/base/common/errors';
99
import { URI } from 'vs/base/common/uri';
@@ -83,15 +83,13 @@ function isValidActionKind(filter: CodeActionFilter | undefined, kind: string |
8383
}
8484

8585
function codeActionsComparator(a: CodeAction, b: CodeAction): number {
86-
const aHasDiags = !isFalsyOrEmpty(a.diagnostics);
87-
const bHasDiags = !isFalsyOrEmpty(b.diagnostics);
88-
if (aHasDiags) {
89-
if (bHasDiags) {
90-
return a.diagnostics![0].message.localeCompare(b.diagnostics![0].message);
86+
if (isNonEmptyArray(a.diagnostics)) {
87+
if (isNonEmptyArray(b.diagnostics)) {
88+
return a.diagnostics[0].message.localeCompare(b.diagnostics[0].message);
9189
} else {
9290
return -1;
9391
}
94-
} else if (bHasDiags) {
92+
} else if (isNonEmptyArray(b.diagnostics)) {
9593
return 1;
9694
} else {
9795
return 0; // both have no diagnostics

src/vs/editor/contrib/wordHighlighter/wordHighlighter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getOccurrencesAtPosition(model: ITextModel, position: Position,
4747
return first<DocumentHighlight[] | null | undefined>(orderedByScore.map(provider => () => {
4848
return Promise.resolve(provider.provideDocumentHighlights(model, position, token))
4949
.then(undefined, onUnexpectedExternalError);
50-
}), result => !arrays.isFalsyOrEmpty(result));
50+
}), arrays.isNonEmptyArray);
5151
}
5252

5353
interface IOccurenceAtPositionRequest {

0 commit comments

Comments
 (0)