Skip to content

Commit 30b2a5f

Browse files
committed
Replace !isFalsyOrEmpty with isNonEmptyArray
This new call works as a type guard and can help avoid writing double negations Avoid either call in cases where we were iterating over the possibly undefined value. Use `|| []` for these
1 parent 8392c36 commit 30b2a5f

16 files changed

Lines changed: 41 additions & 51 deletions

File tree

src/vs/editor/common/services/modelServiceImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as nls from 'vs/nls';
7-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
7+
import { isNonEmptyArray } from 'vs/base/common/arrays';
88
import { Emitter, Event } from 'vs/base/common/event';
99
import { MarkdownString } from 'vs/base/common/htmlContent';
1010
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
@@ -212,9 +212,9 @@ class ModelMarkerHandler {
212212

213213
hoverMessage = new MarkdownString().appendCodeblock('_', message);
214214

215-
if (!isFalsyOrEmpty(relatedInformation)) {
215+
if (isNonEmptyArray(relatedInformation)) {
216216
hoverMessage.appendMarkdown('\n');
217-
for (const { message, resource, startLineNumber, startColumn } of relatedInformation!) {
217+
for (const { message, resource, startLineNumber, startColumn } of relatedInformation) {
218218
hoverMessage.appendMarkdown(
219219
`* [${basename(resource.path)}(${startLineNumber}, ${startColumn})](${resource.toString(false)}#${startLineNumber},${startColumn}): `
220220
);

src/vs/editor/contrib/documentSymbols/outlineModel.ts

Lines changed: 3 additions & 5 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 { binarySearch, isFalsyOrEmpty, coalesceInPlace } from 'vs/base/common/arrays';
6+
import { binarySearch, coalesceInPlace } from 'vs/base/common/arrays';
77
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
88
import { first, forEach, size } from 'vs/base/common/collections';
99
import { onUnexpectedExternalError } from 'vs/base/common/errors';
@@ -301,10 +301,8 @@ export class OutlineModel extends TreeElement {
301301
let group = new OutlineGroup(id, result, provider, index);
302302

303303
return Promise.resolve(provider.provideDocumentSymbols(result.textModel, token)).then(result => {
304-
if (!isFalsyOrEmpty(result)) {
305-
for (const info of result) {
306-
OutlineModel._makeOutlineElement(info, group);
307-
}
304+
for (const info of result || []) {
305+
OutlineModel._makeOutlineElement(info, group);
308306
}
309307
return group;
310308
}, err => {

src/vs/editor/contrib/format/format.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { illegalArgument, onUnexpectedExternalError } from 'vs/base/common/errors';
77
import { URI } from 'vs/base/common/uri';
8-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
8+
import { isNonEmptyArray } from 'vs/base/common/arrays';
99
import { Range } from 'vs/editor/common/core/range';
1010
import { ITextModel } from 'vs/editor/common/model';
1111
import { registerDefaultLanguageCommand, registerLanguageCommand } from 'vs/editor/browser/editorExtensions';
@@ -39,7 +39,7 @@ export function getDocumentRangeFormattingEdits(model: ITextModel, range: Range,
3939
return first(providers.map(provider => () => {
4040
return Promise.resolve(provider.provideDocumentRangeFormattingEdits(model, range, options, token))
4141
.then(undefined, onUnexpectedExternalError);
42-
}), result => !isFalsyOrEmpty(result));
42+
}), isNonEmptyArray);
4343
}
4444

4545
export function getDocumentFormattingEdits(model: ITextModel, options: FormattingOptions, token: CancellationToken): Promise<TextEdit[] | null | undefined> {
@@ -53,7 +53,7 @@ export function getDocumentFormattingEdits(model: ITextModel, options: Formattin
5353
return first(providers.map(provider => () => {
5454
return Promise.resolve(provider.provideDocumentFormattingEdits(model, options, token))
5555
.then(undefined, onUnexpectedExternalError);
56-
}), result => !isFalsyOrEmpty(result));
56+
}), isNonEmptyArray);
5757
}
5858

5959
export function getOnTypeFormattingEdits(model: ITextModel, position: Position, ch: string, options: FormattingOptions): Promise<TextEdit[] | null | undefined> {

src/vs/editor/contrib/gotoError/gotoErrorWidget.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElemen
2121
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
2222
import { ScrollType } from 'vs/editor/common/editorCommon';
2323
import { getBaseLabel, getPathLabel } from 'vs/base/common/labels';
24-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
24+
import { isNonEmptyArray } from 'vs/base/common/arrays';
2525
import { Event, Emitter } from 'vs/base/common/event';
2626

2727
class MessageWidget {
@@ -94,11 +94,11 @@ class MessageWidget {
9494

9595
dom.clearNode(this._relatedBlock);
9696

97-
if (!isFalsyOrEmpty(relatedInformation)) {
97+
if (isNonEmptyArray(relatedInformation)) {
9898
this._relatedBlock.style.paddingTop = `${Math.floor(this._editor.getConfiguration().lineHeight * .66)}px`;
9999
this._lines += 1;
100100

101-
for (const related of relatedInformation || []) {
101+
for (const related of relatedInformation) {
102102

103103
let container = document.createElement('div');
104104

src/vs/editor/contrib/suggest/suggest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { first } from 'vs/base/common/async';
7-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
7+
import { isNonEmptyArray } from 'vs/base/common/arrays';
88
import { assign } from 'vs/base/common/objects';
99
import { onUnexpectedExternalError, canceled } from 'vs/base/common/errors';
1010
import { IEditorContribution } from 'vs/editor/common/editorCommon';
@@ -80,16 +80,16 @@ export function provideSuggestionItems(
8080
// for each support in the group ask for suggestions
8181
return Promise.all(supports.map(support => {
8282

83-
if (!isFalsyOrEmpty(onlyFrom) && onlyFrom!.indexOf(support) < 0) {
83+
if (isNonEmptyArray(onlyFrom) && onlyFrom.indexOf(support) < 0) {
8484
return undefined;
8585
}
8686

8787
return Promise.resolve(support.provideCompletionItems(model, position, suggestConext, token)).then(container => {
8888

8989
const len = allSuggestions.length;
9090

91-
if (container && !isFalsyOrEmpty(container.suggestions)) {
92-
for (let suggestion of container.suggestions) {
91+
if (container) {
92+
for (let suggestion of container.suggestions || []) {
9393
if (acceptSuggestion(suggestion)) {
9494

9595
// fill in default range when missing

src/vs/editor/contrib/suggest/suggestModel.ts

Lines changed: 3 additions & 6 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 { isFalsyOrEmpty } from 'vs/base/common/arrays';
6+
import { isNonEmptyArray } from 'vs/base/common/arrays';
77
import { TimeoutTimer } from 'vs/base/common/async';
88
import { onUnexpectedError } from 'vs/base/common/errors';
99
import { Emitter, Event } from 'vs/base/common/event';
@@ -193,10 +193,7 @@ export class SuggestModel implements IDisposable {
193193

194194
const supportsByTriggerCharacter: { [ch: string]: Set<CompletionItemProvider> } = Object.create(null);
195195
for (const support of CompletionProviderRegistry.all(this._editor.getModel())) {
196-
if (isFalsyOrEmpty(support.triggerCharacters)) {
197-
continue;
198-
}
199-
for (const ch of support.triggerCharacters) {
196+
for (const ch of support.triggerCharacters || []) {
200197
let set = supportsByTriggerCharacter[ch];
201198
if (!set) {
202199
set = supportsByTriggerCharacter[ch] = new Set();
@@ -411,7 +408,7 @@ export class SuggestModel implements IDisposable {
411408
return;
412409
}
413410

414-
if (!isFalsyOrEmpty(existingItems)) {
411+
if (isNonEmptyArray(existingItems)) {
415412
const cmpFn = getSuggestionComparator(this._editor.getConfiguration().contribInfo.suggest.snippets);
416413
items = items.concat(existingItems).sort(cmpFn);
417414
}

src/vs/platform/keybinding/common/keybindingResolver.ts

Lines changed: 2 additions & 2 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 { isFalsyOrEmpty } from 'vs/base/common/arrays';
6+
import { isNonEmptyArray } from 'vs/base/common/arrays';
77
import { MenuRegistry } from 'vs/platform/actions/common/actions';
88
import { CommandsRegistry, ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
99
import { ContextKeyAndExpr, ContextKeyExpr, IContext } from 'vs/platform/contextkey/common/contextkey';
@@ -322,7 +322,7 @@ export class KeybindingResolver {
322322
}
323323
const command = CommandsRegistry.getCommand(id);
324324
if (command && typeof command.description === 'object'
325-
&& !isFalsyOrEmpty((<ICommandHandlerDescription>command.description).args)) { // command with args
325+
&& isNonEmptyArray((<ICommandHandlerDescription>command.description).args)) { // command with args
326326
return;
327327
}
328328
unboundCommands.push(id);

src/vs/platform/markers/common/markerService.ts

Lines changed: 4 additions & 6 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 { isFalsyOrEmpty } from 'vs/base/common/arrays';
6+
import { isFalsyOrEmpty, isNonEmptyArray } from 'vs/base/common/arrays';
77
import { Schemas } from 'vs/base/common/network';
88
import { IDisposable } from 'vs/base/common/lifecycle';
99
import { isEmptyObject } from 'vs/base/common/types';
@@ -146,10 +146,8 @@ export class MarkerService implements IMarkerService {
146146
}
147147

148148
remove(owner: string, resources: URI[]): void {
149-
if (!isFalsyOrEmpty(resources)) {
150-
for (const resource of resources) {
151-
this.changeOne(owner, resource, []);
152-
}
149+
for (const resource of resources || []) {
150+
this.changeOne(owner, resource, []);
153151
}
154152
}
155153

@@ -238,7 +236,7 @@ export class MarkerService implements IMarkerService {
238236
}
239237

240238
// add new markers
241-
if (!isFalsyOrEmpty(data)) {
239+
if (isNonEmptyArray(data)) {
242240

243241
// group by resource
244242
const groups: { [resource: string]: IMarker[] } = Object.create(null);

src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerServ
2424
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
2525
import { IProgressService2, ProgressLocation } from 'vs/platform/progress/common/progress';
2626
import { localize } from 'vs/nls';
27-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
27+
import { isNonEmptyArray } from 'vs/base/common/arrays';
2828
import { ILogService } from 'vs/platform/log/common/log';
2929
import { shouldSynchronizeModel } from 'vs/editor/common/services/modelService';
3030
import { SnippetController2 } from 'vs/editor/contrib/snippet/snippetController2';
@@ -251,7 +251,7 @@ class FormatOnSaveParticipant implements ISaveParticipantParticipant {
251251
});
252252

253253
}).then(edits => {
254-
if (!isFalsyOrEmpty(edits) && versionNow === model.getVersionId()) {
254+
if (isNonEmptyArray(edits) && versionNow === model.getVersionId()) {
255255
const editor = findEditor(model, this._editorService);
256256
if (editor) {
257257
this._editsWithEditor(editor, edits);

src/vs/workbench/api/node/extHostCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverte
1010
import { cloneAndChange } from 'vs/base/common/objects';
1111
import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ObjectIdentifier, IMainContext } from './extHost.protocol';
1212
import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService';
13-
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
13+
import { isNonEmptyArray } from 'vs/base/common/arrays';
1414
import * as modes from 'vs/editor/common/modes';
1515
import * as vscode from 'vscode';
1616
import { ILogService } from 'vs/platform/log/common/log';
@@ -189,7 +189,7 @@ export class CommandsConverter {
189189
title: command.title
190190
};
191191

192-
if (command.command && !isFalsyOrEmpty(command.arguments)) {
192+
if (command.command && isNonEmptyArray(command.arguments)) {
193193
// we have a contributed command with arguments. that
194194
// means we don't want to send the arguments around
195195

0 commit comments

Comments
 (0)