Skip to content

Commit 65bb1d6

Browse files
authored
assertIsDefined vs assertDefined (fix microsoft#248362) (microsoft#250844)
1 parent af623e0 commit 65bb1d6

File tree

72 files changed

+268
-268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+268
-268
lines changed

src/vs/base/common/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function assertType(condition: unknown, type?: string): asserts condition
105105
*
106106
* @see {@link assertDefined} for a similar utility that leverages TS assertion functions to narrow down the type of `arg` to be non-nullable.
107107
*/
108-
export function assertIsDefined<T>(arg: T | null | undefined): NonNullable<T> {
108+
export function assertReturnsDefined<T>(arg: T | null | undefined): NonNullable<T> {
109109
assert(
110110
arg !== null && arg !== undefined,
111111
'Argument is `undefined` or `null`.',
@@ -137,7 +137,7 @@ export function assertIsDefined<T>(arg: T | null | undefined): NonNullable<T> {
137137
* console.log(someValue.length); // now type of `someValue` is `string`
138138
* ```
139139
*
140-
* @see {@link assertIsDefined} for a similar utility but without assertion.
140+
* @see {@link assertReturnsDefined} for a similar utility but without assertion.
141141
* @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions typescript-3-7.html#assertion-functions}
142142
*/
143143
export function assertDefined<T>(value: T, error: string | NonNullable<Error>): asserts value is NonNullable<T> {
@@ -151,10 +151,10 @@ export function assertDefined<T>(value: T, error: string | NonNullable<Error>):
151151
/**
152152
* Asserts that each argument passed in is neither undefined nor null.
153153
*/
154-
export function assertAllDefined<T1, T2>(t1: T1 | null | undefined, t2: T2 | null | undefined): [T1, T2];
155-
export function assertAllDefined<T1, T2, T3>(t1: T1 | null | undefined, t2: T2 | null | undefined, t3: T3 | null | undefined): [T1, T2, T3];
156-
export function assertAllDefined<T1, T2, T3, T4>(t1: T1 | null | undefined, t2: T2 | null | undefined, t3: T3 | null | undefined, t4: T4 | null | undefined): [T1, T2, T3, T4];
157-
export function assertAllDefined(...args: (unknown | null | undefined)[]): unknown[] {
154+
export function assertReturnsAllDefined<T1, T2>(t1: T1 | null | undefined, t2: T2 | null | undefined): [T1, T2];
155+
export function assertReturnsAllDefined<T1, T2, T3>(t1: T1 | null | undefined, t2: T2 | null | undefined, t3: T3 | null | undefined): [T1, T2, T3];
156+
export function assertReturnsAllDefined<T1, T2, T3, T4>(t1: T1 | null | undefined, t2: T2 | null | undefined, t3: T3 | null | undefined, t4: T4 | null | undefined): [T1, T2, T3, T4];
157+
export function assertReturnsAllDefined(...args: (unknown | null | undefined)[]): unknown[] {
158158
const result = [];
159159

160160
for (let i = 0; i < args.length; i++) {

src/vs/base/node/zip.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Readable } from 'stream';
88
import { createCancelablePromise, Sequencer } from '../common/async.js';
99
import { CancellationToken } from '../common/cancellation.js';
1010
import * as path from '../common/path.js';
11-
import { assertIsDefined } from '../common/types.js';
11+
import { assertReturnsDefined } from '../common/types.js';
1212
import { Promises } from './pfs.js';
1313
import * as nls from '../../nls.js';
1414
import type { Entry, ZipFile } from 'yauzl';
@@ -168,7 +168,7 @@ async function openZip(zipFile: string, lazy: boolean = false): Promise<ZipFile>
168168
if (error) {
169169
reject(toExtractError(error));
170170
} else {
171-
resolve(assertIsDefined(zipfile));
171+
resolve(assertReturnsDefined(zipfile));
172172
}
173173
});
174174
});
@@ -180,7 +180,7 @@ function openZipStream(zipFile: ZipFile, entry: Entry): Promise<Readable> {
180180
if (error) {
181181
reject(toExtractError(error));
182182
} else {
183-
resolve(assertIsDefined(stream));
183+
resolve(assertReturnsDefined(stream));
184184
}
185185
});
186186
});

src/vs/base/test/common/types.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,18 @@ suite('Types', () => {
158158
});
159159

160160
test('assertIsDefined / assertAreDefined', () => {
161-
assert.throws(() => types.assertIsDefined(undefined));
162-
assert.throws(() => types.assertIsDefined(null));
163-
assert.throws(() => types.assertAllDefined(null, undefined));
164-
assert.throws(() => types.assertAllDefined(true, undefined));
165-
assert.throws(() => types.assertAllDefined(undefined, false));
166-
167-
assert.strictEqual(types.assertIsDefined(true), true);
168-
assert.strictEqual(types.assertIsDefined(false), false);
169-
assert.strictEqual(types.assertIsDefined('Hello'), 'Hello');
170-
assert.strictEqual(types.assertIsDefined(''), '');
171-
172-
const res = types.assertAllDefined(1, true, 'Hello');
161+
assert.throws(() => types.assertReturnsDefined(undefined));
162+
assert.throws(() => types.assertReturnsDefined(null));
163+
assert.throws(() => types.assertReturnsAllDefined(null, undefined));
164+
assert.throws(() => types.assertReturnsAllDefined(true, undefined));
165+
assert.throws(() => types.assertReturnsAllDefined(undefined, false));
166+
167+
assert.strictEqual(types.assertReturnsDefined(true), true);
168+
assert.strictEqual(types.assertReturnsDefined(false), false);
169+
assert.strictEqual(types.assertReturnsDefined('Hello'), 'Hello');
170+
assert.strictEqual(types.assertReturnsDefined(''), '');
171+
172+
const res = types.assertReturnsAllDefined(1, true, 'Hello');
173173
assert.strictEqual(res[0], 1);
174174
assert.strictEqual(res[1], true);
175175
assert.strictEqual(res[2], 'Hello');

src/vs/editor/browser/view/domLineBreaksComputer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { createTrustedTypesPolicy } from '../../../base/browser/trustedTypes.js';
77
import { CharCode } from '../../../base/common/charCode.js';
88
import * as strings from '../../../base/common/strings.js';
9-
import { assertIsDefined } from '../../../base/common/types.js';
9+
import { assertReturnsDefined } from '../../../base/common/types.js';
1010
import { applyFontInfo } from '../config/domFontInfo.js';
1111
import { WrappingIndent } from '../../common/config/editorOptions.js';
1212
import { FontInfo } from '../../common/config/fontInfo.js';
@@ -35,7 +35,7 @@ export class DOMLineBreaksComputerFactory implements ILineBreaksComputerFactory
3535
injectedTexts.push(injectedText);
3636
},
3737
finalize: () => {
38-
return createLineBreaks(assertIsDefined(this.targetWindow.deref()), requests, fontInfo, tabSize, wrappingColumn, wrappingIndent, wordBreak, injectedTexts);
38+
return createLineBreaks(assertReturnsDefined(this.targetWindow.deref()), requests, fontInfo, tabSize, wrappingColumn, wrappingIndent, wordBreak, injectedTexts);
3939
}
4040
};
4141
}

src/vs/editor/browser/widget/diffEditor/components/diffEditorViewZones/diffEditorViewZones.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Codicon } from '../../../../../../base/common/codicons.js';
1010
import { Disposable, DisposableStore } from '../../../../../../base/common/lifecycle.js';
1111
import { IObservable, autorun, derived, observableFromEvent, observableValue } from '../../../../../../base/common/observable.js';
1212
import { ThemeIcon } from '../../../../../../base/common/themables.js';
13-
import { assertIsDefined } from '../../../../../../base/common/types.js';
13+
import { assertReturnsDefined } from '../../../../../../base/common/types.js';
1414
import { applyFontInfo } from '../../../../config/domFontInfo.js';
1515
import { CodeEditorWidget } from '../../../codeEditor/codeEditorWidget.js';
1616
import { diffDeleteDecoration, diffRemoveIcon } from '../../registrations.contribution.js';
@@ -239,7 +239,7 @@ export class DiffEditorViewZones extends Disposable {
239239
let zoneId: string | undefined = undefined;
240240
alignmentViewZonesDisposables.add(
241241
new InlineDiffDeletedCodeMargin(
242-
() => assertIsDefined(zoneId),
242+
() => assertReturnsDefined(zoneId),
243243
marginDomNode,
244244
this._editors.modified,
245245
a.diff,

src/vs/editor/contrib/find/browser/findWidget.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { registerIcon, widgetClose } from '../../../../platform/theme/common/ico
3838
import { registerThemingParticipant } from '../../../../platform/theme/common/themeService.js';
3939
import { ThemeIcon } from '../../../../base/common/themables.js';
4040
import { isHighContrast } from '../../../../platform/theme/common/theme.js';
41-
import { assertIsDefined } from '../../../../base/common/types.js';
41+
import { assertReturnsDefined } from '../../../../base/common/types.js';
4242
import { defaultInputBoxStyles, defaultToggleStyles } from '../../../../platform/theme/browser/defaultStyles.js';
4343
import { Selection } from '../../../common/core/selection.js';
4444
import { createInstantHoverDelegate, getDefaultHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegateFactory.js';
@@ -1008,7 +1008,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IVerticalSashL
10081008
icon: findPreviousMatchIcon,
10091009
hoverDelegate,
10101010
onTrigger: () => {
1011-
assertIsDefined(this._codeEditor.getAction(FIND_IDS.PreviousMatchFindAction)).run().then(undefined, onUnexpectedError);
1011+
assertReturnsDefined(this._codeEditor.getAction(FIND_IDS.PreviousMatchFindAction)).run().then(undefined, onUnexpectedError);
10121012
}
10131013
}, this._hoverService));
10141014

@@ -1018,7 +1018,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IVerticalSashL
10181018
icon: findNextMatchIcon,
10191019
hoverDelegate,
10201020
onTrigger: () => {
1021-
assertIsDefined(this._codeEditor.getAction(FIND_IDS.NextMatchFindAction)).run().then(undefined, onUnexpectedError);
1021+
assertReturnsDefined(this._codeEditor.getAction(FIND_IDS.NextMatchFindAction)).run().then(undefined, onUnexpectedError);
10221022
}
10231023
}, this._hoverService));
10241024

src/vs/editor/contrib/parameterHints/browser/parameterHintsWidget.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Event } from '../../../../base/common/event.js';
1111
import { IMarkdownString } from '../../../../base/common/htmlContent.js';
1212
import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';
1313
import { escapeRegExpCharacters } from '../../../../base/common/strings.js';
14-
import { assertIsDefined } from '../../../../base/common/types.js';
14+
import { assertReturnsDefined } from '../../../../base/common/types.js';
1515
import './parameterHints.css';
1616
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from '../../../browser/editorBrowser.js';
1717
import { EDITOR_FONT_DEFAULTS, EditorOption } from '../../../common/config/editorOptions.js';
@@ -282,16 +282,16 @@ export class ParameterHintsWidget extends Disposable implements IContentWidget {
282282
}
283283

284284
private hasDocs(signature: languages.SignatureInformation, activeParameter: languages.ParameterInformation | undefined): boolean {
285-
if (activeParameter && typeof activeParameter.documentation === 'string' && assertIsDefined(activeParameter.documentation).length > 0) {
285+
if (activeParameter && typeof activeParameter.documentation === 'string' && assertReturnsDefined(activeParameter.documentation).length > 0) {
286286
return true;
287287
}
288-
if (activeParameter && typeof activeParameter.documentation === 'object' && assertIsDefined(activeParameter.documentation).value.length > 0) {
288+
if (activeParameter && typeof activeParameter.documentation === 'object' && assertReturnsDefined(activeParameter.documentation).value.length > 0) {
289289
return true;
290290
}
291-
if (signature.documentation && typeof signature.documentation === 'string' && assertIsDefined(signature.documentation).length > 0) {
291+
if (signature.documentation && typeof signature.documentation === 'string' && assertReturnsDefined(signature.documentation).length > 0) {
292292
return true;
293293
}
294-
if (signature.documentation && typeof signature.documentation === 'object' && assertIsDefined(signature.documentation.value).length > 0) {
294+
if (signature.documentation && typeof signature.documentation === 'object' && assertReturnsDefined(signature.documentation.value).length > 0) {
295295
return true;
296296
}
297297
return false;

src/vs/platform/diagnostics/electron-main/diagnosticsMainService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ICodeWindow } from '../../window/electron-main/window.js';
1313
import { getAllWindowsExcludingOffscreen, IWindowsMainService } from '../../windows/electron-main/windows.js';
1414
import { isSingleFolderWorkspaceIdentifier, isWorkspaceIdentifier } from '../../workspace/common/workspace.js';
1515
import { IWorkspacesManagementMainService } from '../../workspaces/electron-main/workspacesManagementMainService.js';
16-
import { assertIsDefined } from '../../../base/common/types.js';
16+
import { assertReturnsDefined } from '../../../base/common/types.js';
1717
import { ILogService } from '../../log/common/log.js';
1818
import { UtilityProcess } from '../../utilityProcess/electron-main/utilityProcess.js';
1919

@@ -106,7 +106,7 @@ export class DiagnosticsMainService implements IDiagnosticsMainService {
106106

107107
private async codeWindowToInfo(window: ICodeWindow): Promise<IWindowDiagnostics> {
108108
const folderURIs = await this.getFolderURIs(window);
109-
const win = assertIsDefined(window.win);
109+
const win = assertReturnsDefined(window.win);
110110

111111
return this.browserWindowToInfo(win, folderURIs, window.remoteAuthority);
112112
}

src/vs/platform/lifecycle/electron-main/lifecycleMainService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Emitter, Event } from '../../../base/common/event.js';
1010
import { Disposable, DisposableStore } from '../../../base/common/lifecycle.js';
1111
import { isMacintosh, isWindows } from '../../../base/common/platform.js';
1212
import { cwd } from '../../../base/common/process.js';
13-
import { assertIsDefined } from '../../../base/common/types.js';
13+
import { assertReturnsDefined } from '../../../base/common/types.js';
1414
import { NativeParsedArgs } from '../../environment/common/argv.js';
1515
import { createDecorator } from '../../instantiation/common/instantiation.js';
1616
import { ILogService } from '../../log/common/log.js';
@@ -428,7 +428,7 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
428428
windowListeners.add(window.onWillLoad(e => this._onWillLoadWindow.fire({ window, workspace: e.workspace, reason: e.reason })));
429429

430430
// Window Before Closing: Main -> Renderer
431-
const win = assertIsDefined(window.win);
431+
const win = assertReturnsDefined(window.win);
432432
windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'close')(e => {
433433

434434
// The window already acknowledged to be closed
@@ -478,7 +478,7 @@ export class LifecycleMainService extends Disposable implements ILifecycleMainSe
478478
}
479479

480480
registerAuxWindow(auxWindow: IAuxiliaryWindow): void {
481-
const win = assertIsDefined(auxWindow.win);
481+
const win = assertReturnsDefined(auxWindow.win);
482482

483483
const windowListeners = new DisposableStore();
484484
windowListeners.add(Event.fromNodeEventEmitter<electron.Event>(win, 'close')(e => {

src/vs/platform/sharedProcess/electron-main/sharedProcess.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ILoggerMainService } from '../../log/electron-main/loggerService.js';
1717
import { UtilityProcess } from '../../utilityProcess/electron-main/utilityProcess.js';
1818
import { NullTelemetryService } from '../../telemetry/common/telemetryUtils.js';
1919
import { parseSharedProcessDebugPort } from '../../environment/node/environmentService.js';
20-
import { assertIsDefined } from '../../../base/common/types.js';
20+
import { assertReturnsDefined } from '../../../base/common/types.js';
2121
import { SharedProcessChannelConnection, SharedProcessRawConnection, SharedProcessLifecycle } from '../common/sharedProcess.js';
2222
import { Emitter } from '../../../base/common/event.js';
2323

@@ -202,7 +202,7 @@ export class SharedProcess extends Disposable {
202202
await this.whenIpcReady;
203203

204204
// Connect and return message port
205-
const utilityProcess = assertIsDefined(this.utilityProcess);
205+
const utilityProcess = assertReturnsDefined(this.utilityProcess);
206206
return utilityProcess.connect(payload);
207207
}
208208
}

0 commit comments

Comments
 (0)