Skip to content

Commit cb0dc76

Browse files
author
Benjamin Pasero
committed
debt - more strict init (microsoft#78168)
1 parent 66eea49 commit cb0dc76

29 files changed

Lines changed: 285 additions & 245 deletions

File tree

src/vs/base/browser/ui/dropdown/dropdown.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ export interface IDropdownMenuOptions extends IBaseDropdownOptions {
208208

209209
export class DropdownMenu extends BaseDropdown {
210210
private _contextMenuProvider: IContextMenuProvider;
211-
private _menuOptions: IMenuOptions;
212-
private _actions: ReadonlyArray<IAction>;
211+
private _menuOptions: IMenuOptions | undefined;
212+
private _actions: ReadonlyArray<IAction> = [];
213213
private actionProvider?: IActionProvider;
214214
private menuClassName: string;
215215

@@ -222,11 +222,11 @@ export class DropdownMenu extends BaseDropdown {
222222
this.menuClassName = options.menuClassName || '';
223223
}
224224

225-
set menuOptions(options: IMenuOptions) {
225+
set menuOptions(options: IMenuOptions | undefined) {
226226
this._menuOptions = options;
227227
}
228228

229-
get menuOptions(): IMenuOptions {
229+
get menuOptions(): IMenuOptions | undefined {
230230
return this._menuOptions;
231231
}
232232

@@ -256,7 +256,7 @@ export class DropdownMenu extends BaseDropdown {
256256
getMenuClassName: () => this.menuClassName,
257257
onHide: () => this.onHide(),
258258
actionRunner: this.menuOptions ? this.menuOptions.actionRunner : undefined,
259-
anchorAlignment: this.menuOptions.anchorAlignment
259+
anchorAlignment: this.menuOptions ? this.menuOptions.anchorAlignment : AnchorAlignment.LEFT
260260
});
261261
}
262262

@@ -345,7 +345,11 @@ export class DropdownMenuActionViewItem extends BaseActionViewItem {
345345
super.setActionContext(newContext);
346346

347347
if (this.dropdownMenu) {
348-
this.dropdownMenu.menuOptions.context = newContext;
348+
if (this.dropdownMenu.menuOptions) {
349+
this.dropdownMenu.menuOptions.context = newContext;
350+
} else {
351+
this.dropdownMenu.menuOptions = { context: newContext };
352+
}
349353
}
350354
}
351355

src/vs/platform/backup/electron-main/backupMainService.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export class BackupMainService implements IBackupMainService {
2828
protected backupHome: string;
2929
protected workspacesJsonPath: string;
3030

31-
private rootWorkspaces: IWorkspaceBackupInfo[];
32-
private folderWorkspaces: URI[];
33-
private emptyWorkspaces: IEmptyWindowBackupInfo[];
31+
private rootWorkspaces: IWorkspaceBackupInfo[] = [];
32+
private folderWorkspaces: URI[] = [];
33+
private emptyWorkspaces: IEmptyWindowBackupInfo[] = [];
3434

3535
constructor(
3636
@IEnvironmentService environmentService: IEnvironmentService,
@@ -55,8 +55,6 @@ export class BackupMainService implements IBackupMainService {
5555
} else if (Array.isArray(backups.emptyWorkspaces)) {
5656
// read legacy entries
5757
this.emptyWorkspaces = await this.validateEmptyWorkspaces(backups.emptyWorkspaces.map(backupFolder => ({ backupFolder })));
58-
} else {
59-
this.emptyWorkspaces = [];
6058
}
6159

6260
// read workspace backups
@@ -70,6 +68,7 @@ export class BackupMainService implements IBackupMainService {
7068
} catch (e) {
7169
// ignore URI parsing exceptions
7270
}
71+
7372
this.rootWorkspaces = await this.validateWorkspaces(rootWorkspaces);
7473

7574
// read folder backups

src/vs/platform/files/node/watcher/unix/chokidarWatcherService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class ChokidarWatcherService implements IWatcherService {
3232
private static readonly FS_EVENT_DELAY = 50; // aggregate and only emit events when changes have stopped for this duration (in ms)
3333
private static readonly EVENT_SPAM_WARNING_THRESHOLD = 60 * 1000; // warn after certain time span of event spam
3434

35-
private _watchers: { [watchPath: string]: IWatcher };
36-
private _watcherCount: number;
35+
private _watchers: { [watchPath: string]: IWatcher } = Object.create(null);
36+
private _watcherCount = 0;
3737

3838
private _pollingInterval?: number;
3939
private _usePolling?: boolean;

src/vs/platform/lifecycle/common/lifecycleService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export abstract class AbstractLifecycleService extends Disposable implements ILi
2323
protected readonly _onShutdown = this._register(new Emitter<void>());
2424
readonly onShutdown: Event<void> = this._onShutdown.event;
2525

26-
protected _startupKind: StartupKind;
26+
protected _startupKind: StartupKind = StartupKind.NewWindow;
2727
get startupKind(): StartupKind { return this._startupKind; }
2828

2929
private _phase: LifecyclePhase = LifecyclePhase.Starting;

src/vs/platform/storage/browser/storageService.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { joinPath } from 'vs/base/common/resources';
1515
import { runWhenIdle, RunOnceScheduler } from 'vs/base/common/async';
1616
import { serializableToMap, mapToSerializable } from 'vs/base/common/map';
1717
import { VSBuffer } from 'vs/base/common/buffer';
18+
import { assertIsDefined, assertAllDefined } from 'vs/base/common/types';
1819

1920
export class BrowserStorageService extends Disposable implements IStorageService {
2021

@@ -26,20 +27,20 @@ export class BrowserStorageService extends Disposable implements IStorageService
2627
private readonly _onWillSaveState: Emitter<IWillSaveStateEvent> = this._register(new Emitter<IWillSaveStateEvent>());
2728
readonly onWillSaveState: Event<IWillSaveStateEvent> = this._onWillSaveState.event;
2829

29-
private globalStorage: IStorage;
30-
private workspaceStorage: IStorage;
30+
private globalStorage: IStorage | undefined;
31+
private workspaceStorage: IStorage | undefined;
3132

32-
private globalStorageDatabase: FileStorageDatabase;
33-
private workspaceStorageDatabase: FileStorageDatabase;
33+
private globalStorageDatabase: FileStorageDatabase | undefined;
34+
private workspaceStorageDatabase: FileStorageDatabase | undefined;
3435

35-
private globalStorageFile: URI;
36-
private workspaceStorageFile: URI;
36+
private globalStorageFile: URI | undefined;
37+
private workspaceStorageFile: URI | undefined;
3738

38-
private initializePromise: Promise<void>;
39+
private initializePromise: Promise<void> | undefined;
3940
private periodicSaveScheduler = this._register(new RunOnceScheduler(() => this.collectState(), 5000));
4041

4142
get hasPendingUpdate(): boolean {
42-
return this.globalStorageDatabase.hasPendingUpdate || this.workspaceStorageDatabase.hasPendingUpdate;
43+
return (!!this.globalStorageDatabase && this.globalStorageDatabase.hasPendingUpdate) || (!!this.workspaceStorageDatabase && this.workspaceStorageDatabase.hasPendingUpdate);
4344
}
4445

4546
constructor(
@@ -137,16 +138,18 @@ export class BrowserStorageService extends Disposable implements IStorageService
137138
}
138139

139140
private getStorage(scope: StorageScope): IStorage {
140-
return scope === StorageScope.GLOBAL ? this.globalStorage : this.workspaceStorage;
141+
return assertIsDefined(scope === StorageScope.GLOBAL ? this.globalStorage : this.workspaceStorage);
141142
}
142143

143144
async logStorage(): Promise<void> {
145+
const [globalStorage, workspaceStorage, globalStorageFile, workspaceStorageFile] = assertAllDefined(this.globalStorage, this.workspaceStorage, this.globalStorageFile, this.workspaceStorageFile);
146+
144147
const result = await Promise.all([
145-
this.globalStorage.items,
146-
this.workspaceStorage.items
148+
globalStorage.items,
149+
workspaceStorage.items
147150
]);
148151

149-
return logStorage(result[0], result[1], this.globalStorageFile.toString(), this.workspaceStorageFile.toString());
152+
return logStorage(result[0], result[1], globalStorageFile.toString(), workspaceStorageFile.toString());
150153
}
151154

152155
async migrate(toWorkspace: IWorkspaceInitializationPayload): Promise<void> {

src/vs/platform/storage/node/storageService.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { copy, exists, mkdirp, writeFile } from 'vs/base/node/pfs';
1515
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1616
import { IWorkspaceInitializationPayload, isWorkspaceIdentifier, isSingleFolderWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
1717
import { onUnexpectedError } from 'vs/base/common/errors';
18+
import { assertIsDefined, assertAllDefined } from 'vs/base/common/types';
1819

1920
export class NativeStorageService extends Disposable implements IStorageService {
2021

@@ -31,11 +32,11 @@ export class NativeStorageService extends Disposable implements IStorageService
3132

3233
private globalStorage: IStorage;
3334

34-
private workspaceStoragePath: string;
35-
private workspaceStorage: IStorage;
36-
private workspaceStorageListener: IDisposable;
35+
private workspaceStoragePath: string | undefined;
36+
private workspaceStorage: IStorage | undefined;
37+
private workspaceStorageListener: IDisposable | undefined;
3738

38-
private initializePromise: Promise<void>;
39+
private initializePromise: Promise<void> | undefined;
3940

4041
constructor(
4142
globalStorageDatabase: IStorageDatabase,
@@ -191,22 +192,24 @@ export class NativeStorageService extends Disposable implements IStorageService
191192

192193
// Do it
193194
await Promise.all([
194-
this.globalStorage.close(),
195-
this.workspaceStorage.close()
195+
this.getStorage(StorageScope.GLOBAL).close(),
196+
this.getStorage(StorageScope.WORKSPACE).close()
196197
]);
197198
}
198199

199200
private getStorage(scope: StorageScope): IStorage {
200-
return scope === StorageScope.GLOBAL ? this.globalStorage : this.workspaceStorage;
201+
return assertIsDefined(scope === StorageScope.GLOBAL ? this.globalStorage : this.workspaceStorage);
201202
}
202203

203204
async logStorage(): Promise<void> {
205+
const [workspaceStorage, workspaceStoragePath] = assertAllDefined(this.workspaceStorage, this.workspaceStoragePath);
206+
204207
const result = await Promise.all([
205208
this.globalStorage.items,
206-
this.workspaceStorage.items
209+
workspaceStorage.items
207210
]);
208211

209-
logStorage(result[0], result[1], this.environmentService.globalStorageHome, this.workspaceStoragePath);
212+
logStorage(result[0], result[1], this.environmentService.globalStorageHome, workspaceStoragePath);
210213
}
211214

212215
async migrate(toWorkspace: IWorkspaceInitializationPayload): Promise<void> {
@@ -215,15 +218,15 @@ export class NativeStorageService extends Disposable implements IStorageService
215218
}
216219

217220
// Close workspace DB to be able to copy
218-
await this.workspaceStorage.close();
221+
await this.getStorage(StorageScope.WORKSPACE).close();
219222

220223
// Prepare new workspace storage folder
221224
const result = await this.prepareWorkspaceStorageFolder(toWorkspace);
222225

223226
const newWorkspaceStoragePath = join(result.path, NativeStorageService.WORKSPACE_STORAGE_NAME);
224227

225228
// Copy current storage over to new workspace storage
226-
await copy(this.workspaceStoragePath, newWorkspaceStoragePath);
229+
await copy(assertIsDefined(this.workspaceStoragePath), newWorkspaceStoragePath);
227230

228231
// Recreate and init workspace storage
229232
return this.createWorkspaceStorage(newWorkspaceStoragePath).init();

src/vs/platform/telemetry/common/telemetryUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const NullTelemetryService = new class implements ITelemetryService {
2020
return this.publicLog(eventName, data as ITelemetryData);
2121
}
2222
setEnabled() { }
23-
isOptedIn: true;
23+
isOptedIn = true;
2424
getTelemetryInfo(): Promise<ITelemetryInfo> {
2525
return Promise.resolve({
2626
instanceId: 'someValue.instanceId',

src/vs/workbench/browser/composite.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { IConstructorSignature0, IInstantiationService } from 'vs/platform/insta
1414
import { trackFocus, Dimension } from 'vs/base/browser/dom';
1515
import { IStorageService } from 'vs/platform/storage/common/storage';
1616
import { Disposable } from 'vs/base/common/lifecycle';
17+
import { assertIsDefined } from 'vs/base/common/types';
1718

1819
/**
1920
* Composites are layed out in the sidebar and panel part of the workbench. At a time only one composite
@@ -35,10 +36,10 @@ export abstract class Composite extends Component implements IComposite {
3536
private readonly _onDidChangeVisibility: Emitter<boolean> = this._register(new Emitter<boolean>());
3637
readonly onDidChangeVisibility: Event<boolean> = this._onDidChangeVisibility.event;
3738

38-
private _onDidFocus: Emitter<void>;
39+
private _onDidFocus: Emitter<void> | undefined;
3940
get onDidFocus(): Event<void> {
4041
if (!this._onDidFocus) {
41-
this.registerFocusTrackEvents();
42+
this._onDidFocus = this.registerFocusTrackEvents().onDidFocus;
4243
}
4344

4445
return this._onDidFocus.event;
@@ -50,28 +51,32 @@ export abstract class Composite extends Component implements IComposite {
5051
}
5152
}
5253

53-
private _onDidBlur: Emitter<void>;
54+
private _onDidBlur: Emitter<void> | undefined;
5455
get onDidBlur(): Event<void> {
5556
if (!this._onDidBlur) {
56-
this.registerFocusTrackEvents();
57+
this._onDidBlur = this.registerFocusTrackEvents().onDidBlur;
5758
}
5859

5960
return this._onDidBlur.event;
6061
}
6162

62-
private registerFocusTrackEvents(): void {
63-
this._onDidFocus = this._register(new Emitter<void>());
64-
this._onDidBlur = this._register(new Emitter<void>());
63+
private registerFocusTrackEvents(): { onDidFocus: Emitter<void>, onDidBlur: Emitter<void> } {
64+
const container = assertIsDefined(this.getContainer());
65+
const focusTracker = this._register(trackFocus(container));
6566

66-
const focusTracker = this._register(trackFocus(this.getContainer()));
67-
this._register(focusTracker.onDidFocus(() => this._onDidFocus.fire()));
68-
this._register(focusTracker.onDidBlur(() => this._onDidBlur.fire()));
67+
const onDidFocus = this._onDidFocus = this._register(new Emitter<void>());
68+
this._register(focusTracker.onDidFocus(() => onDidFocus.fire()));
69+
70+
const onDidBlur = this._onDidBlur = this._register(new Emitter<void>());
71+
this._register(focusTracker.onDidBlur(() => onDidBlur.fire()));
72+
73+
return { onDidFocus, onDidBlur };
6974
}
7075

7176
protected actionRunner: IActionRunner | undefined;
7277

7378
private visible: boolean;
74-
private parent: HTMLElement;
79+
private parent: HTMLElement | undefined;
7580

7681
constructor(
7782
id: string,
@@ -112,7 +117,7 @@ export abstract class Composite extends Component implements IComposite {
112117
/**
113118
* Returns the container this composite is being build in.
114119
*/
115-
getContainer(): HTMLElement {
120+
getContainer(): HTMLElement | undefined {
116121
return this.parent;
117122
}
118123

src/vs/workbench/browser/layout.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { IActivityBarService } from 'vs/workbench/services/activityBar/browser/a
3636
import { IFileService } from 'vs/platform/files/common/files';
3737
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
3838
import { coalesce } from 'vs/base/common/arrays';
39+
import { assertIsDefined } from 'vs/base/common/types';
3940

4041
enum Settings {
4142
MENUBAR_VISIBLE = 'window.menuBarVisibility',
@@ -340,10 +341,12 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
340341
this.state.sideBar.position = position;
341342

342343
// Adjust CSS
343-
removeClass(activityBar.getContainer(), oldPositionValue);
344-
removeClass(sideBar.getContainer(), oldPositionValue);
345-
addClass(activityBar.getContainer(), newPositionValue);
346-
addClass(sideBar.getContainer(), newPositionValue);
344+
const activityBarContainer = assertIsDefined(activityBar.getContainer());
345+
const sideBarContainer = assertIsDefined(sideBar.getContainer());
346+
removeClass(activityBarContainer, oldPositionValue);
347+
removeClass(sideBarContainer, oldPositionValue);
348+
addClass(activityBarContainer, newPositionValue);
349+
addClass(sideBarContainer, newPositionValue);
347350

348351
// Update Styles
349352
activityBar.updateStyles();
@@ -528,10 +531,10 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
528531

529532
const container = this.getContainer(part);
530533

531-
return isAncestor(activeElement, container);
534+
return !!container && isAncestor(activeElement, container);
532535
}
533536

534-
getContainer(part: Parts): HTMLElement {
537+
getContainer(part: Parts): HTMLElement | undefined {
535538
switch (part) {
536539
case Parts.TITLEBAR_PART:
537540
return this.getPart(Parts.TITLEBAR_PART).getContainer();
@@ -579,7 +582,7 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
579582
return true; // any other part cannot be hidden
580583
}
581584

582-
getDimension(part: Parts): Dimension {
585+
getDimension(part: Parts): Dimension | undefined {
583586
return this.getPart(part).dimension;
584587
}
585588

@@ -1121,8 +1124,9 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
11211124
this.storageService.store(Storage.PANEL_POSITION, positionToString(this.state.panel.position), StorageScope.WORKSPACE);
11221125

11231126
// Adjust CSS
1124-
removeClass(panelPart.getContainer(), oldPositionValue);
1125-
addClass(panelPart.getContainer(), newPositionValue);
1127+
const panelContainer = assertIsDefined(panelPart.getContainer());
1128+
removeClass(panelContainer, oldPositionValue);
1129+
addClass(panelContainer, newPositionValue);
11261130

11271131
// Update Styles
11281132
panelPart.updateStyles();

src/vs/workbench/browser/panel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ export abstract class TogglePanelAction extends Action {
112112

113113
private isPanelFocused(): boolean {
114114
const activeElement = document.activeElement;
115+
const panelPart = this.layoutService.getContainer(Parts.PANEL_PART);
115116

116-
return !!(this.isPanelActive() && activeElement && isAncestor(activeElement, this.layoutService.getContainer(Parts.PANEL_PART)));
117+
return !!(this.isPanelActive() && activeElement && panelPart && isAncestor(activeElement, panelPart));
117118
}
118119
}
119120

0 commit comments

Comments
 (0)