Skip to content

Commit cd1a6b4

Browse files
author
Rachel Macfarlane
committed
Add strict null checks in issue and launch services, microsoft#60565
1 parent 8784755 commit cd1a6b4

3 files changed

Lines changed: 37 additions & 26 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@
429429
"./vs/platform/integrity/common/integrity.ts",
430430
"./vs/platform/integrity/node/integrityServiceImpl.ts",
431431
"./vs/platform/issue/common/issue.ts",
432+
"./vs/platform/issue/electron-main/issueService.ts",
432433
"./vs/platform/issue/node/issueIpc.ts",
433434
"./vs/platform/jsonschemas/common/jsonContributionRegistry.ts",
434435
"./vs/platform/keybinding/common/abstractKeybindingService.ts",
@@ -440,6 +441,7 @@
440441
"./vs/platform/keybinding/test/common/mockKeybindingService.ts",
441442
"./vs/platform/label/common/label.ts",
442443
"./vs/platform/label/electron-browser/label.contribution.ts",
444+
"./vs/platform/launch/electron-main/launchService.ts",
443445
"./vs/platform/lifecycle/common/lifecycle.ts",
444446
"./vs/platform/lifecycle/electron-browser/lifecycleService.ts",
445447
"./vs/platform/lifecycle/electron-main/lifecycleMain.ts",

src/vs/platform/issue/electron-main/issueService.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment'
1515
import { isMacintosh, IProcessEnvironment } from 'vs/base/common/platform';
1616
import { ILogService } from 'vs/platform/log/common/log';
1717
import { IWindowsService } from 'vs/platform/windows/common/windows';
18+
import { IWindowState } from 'vs/platform/windows/electron-main/windows';
1819

1920
const DEFAULT_BACKGROUND_COLOR = '#1E1E1E';
2021

2122
export class IssueService implements IIssueService {
2223
_serviceBrand: any;
23-
_issueWindow: BrowserWindow;
24+
_issueWindow: BrowserWindow | null;
2425
_issueParentWindow: BrowserWindow;
25-
_processExplorerWindow: BrowserWindow;
26+
_processExplorerWindow: BrowserWindow | null;
2627

2728
constructor(
2829
private machineId: string,
@@ -108,7 +109,7 @@ export class IssueService implements IIssueService {
108109

109110
this._issueWindow.focus();
110111

111-
return TPromise.as(null);
112+
return TPromise.as(undefined);
112113
}
113114

114115
openProcessExplorer(data: ProcessExplorerData): TPromise<void> {
@@ -150,7 +151,7 @@ export class IssueService implements IIssueService {
150151

151152
this._processExplorerWindow.loadURL(`${require.toUrl('vs/code/electron-browser/processExplorer/processExplorer.html')}?config=${encodeURIComponent(JSON.stringify(config))}`);
152153

153-
this._processExplorerWindow.on('close', () => this._processExplorerWindow = void 0);
154+
this._processExplorerWindow.on('close', () => this._processExplorerWindow = null);
154155

155156
parentWindow.on('close', () => {
156157
if (this._processExplorerWindow) {
@@ -163,12 +164,12 @@ export class IssueService implements IIssueService {
163164
// Focus
164165
this._processExplorerWindow.focus();
165166

166-
return TPromise.as(null);
167+
return TPromise.as(undefined);
167168
}
168169

169-
private getWindowPosition(parentWindow: BrowserWindow, defaultWidth: number, defaultHeight: number) {
170+
private getWindowPosition(parentWindow: BrowserWindow, defaultWidth: number, defaultHeight: number): IWindowState {
170171
// We want the new window to open on the same display that the parent is in
171-
let displayToUse: Electron.Display;
172+
let displayToUse: Electron.Display | undefined;
172173
const displays = screen.getAllDisplays();
173174

174175
// Single Display
@@ -196,16 +197,14 @@ export class IssueService implements IIssueService {
196197
}
197198
}
198199

199-
let state = {
200+
const state: IWindowState = {
200201
width: defaultWidth,
201-
height: defaultHeight,
202-
x: undefined,
203-
y: undefined
202+
height: defaultHeight
204203
};
205204

206205
const displayBounds = displayToUse.bounds;
207-
state.x = displayBounds.x + (displayBounds.width / 2) - (state.width / 2);
208-
state.y = displayBounds.y + (displayBounds.height / 2) - (state.height / 2);
206+
state.x = displayBounds.x + (displayBounds.width / 2) - (state.width! / 2);
207+
state.y = displayBounds.y + (displayBounds.height / 2) - (state.height! / 2);
209208

210209
if (displayBounds.width > 0 && displayBounds.height > 0 /* Linux X11 sessions sometimes report wrong display bounds */) {
211210
if (state.x < displayBounds.x) {
@@ -224,11 +223,11 @@ export class IssueService implements IIssueService {
224223
state.y = displayBounds.y; // prevent window from falling out of the screen to the bottom
225224
}
226225

227-
if (state.width > displayBounds.width) {
226+
if (state.width! > displayBounds.width) {
228227
state.width = displayBounds.width; // prevent window from exceeding display bounds width
229228
}
230229

231-
if (state.height > displayBounds.height) {
230+
if (state.height! > displayBounds.height) {
232231
state.height = displayBounds.height; // prevent window from exceeding display bounds height
233232
}
234233
}
@@ -259,7 +258,11 @@ export class IssueService implements IIssueService {
259258
});
260259
}
261260

262-
private getIssueReporterPath(data: IssueReporterData, features: IssueReporterFeatures) {
261+
private getIssueReporterPath(data: IssueReporterData, features: IssueReporterFeatures): string {
262+
if (!this._issueWindow) {
263+
throw new Error('Issue window has been disposed');
264+
}
265+
263266
const windowConfiguration = {
264267
appRoot: this.environmentService.appRoot,
265268
nodeCachedDataDir: this.environmentService.nodeCachedDataDir,

src/vs/platform/launch/electron-main/launchService.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export interface IMainProcessInfo {
4141
windows: IWindowInfo[];
4242
}
4343

44+
function isUri(uri: URI | null): uri is URI {
45+
return !!uri;
46+
}
47+
4448
function parseOpenUrl(args: ParsedArgs): URI[] {
4549
if (args['open-url'] && args._urls && args._urls.length > 0) {
4650
// --open-url must contain -- followed by the url(s)
@@ -53,7 +57,7 @@ function parseOpenUrl(args: ParsedArgs): URI[] {
5357
return null;
5458
}
5559
})
56-
.filter(uri => !!uri);
60+
.filter(isUri);
5761
}
5862

5963
return [];
@@ -99,7 +103,7 @@ export class LaunchChannel implements ILaunchChannel {
99103
return this.service.getLogsPath();
100104
}
101105

102-
return undefined;
106+
throw new Error(`Command '${command}' not found`);
103107
}
104108
}
105109

@@ -161,7 +165,7 @@ export class LaunchService implements ILaunchService {
161165
}
162166
});
163167

164-
return TPromise.as(null);
168+
return TPromise.as(undefined);
165169
}
166170

167171
// Otherwise handle in windows service
@@ -170,7 +174,7 @@ export class LaunchService implements ILaunchService {
170174

171175
private startOpenWindow(args: ParsedArgs, userEnv: IProcessEnvironment): TPromise<void> {
172176
const context = !!userEnv['VSCODE_CLI'] ? OpenContext.CLI : OpenContext.DESKTOP;
173-
let usedWindows: ICodeWindow[];
177+
let usedWindows: ICodeWindow[] | undefined;
174178

175179
// Special case extension development
176180
if (!!args.extensionDevelopmentPath) {
@@ -231,14 +235,14 @@ export class LaunchService implements ILaunchService {
231235
// If the other instance is waiting to be killed, we hook up a window listener if one window
232236
// is being used and only then resolve the startup promise which will kill this second instance.
233237
// In addition, we poll for the wait marker file to be deleted to return.
234-
if (args.wait && args.waitMarkerFilePath && usedWindows.length === 1 && usedWindows[0]) {
238+
if (args.wait && args.waitMarkerFilePath && usedWindows && usedWindows.length === 1 && usedWindows[0]) {
235239
return Promise.race([
236240
this.windowsMainService.waitForWindowCloseOrLoad(usedWindows[0].id),
237241
whenDeleted(args.waitMarkerFilePath)
238242
]).then(() => void 0, () => void 0);
239243
}
240244

241-
return TPromise.as(null);
245+
return TPromise.as(undefined);
242246
}
243247

244248
getMainProcessId(): TPromise<number> {
@@ -279,10 +283,12 @@ export class LaunchService implements ILaunchService {
279283
if (window.openedFolderUri) {
280284
folderURIs.push(window.openedFolderUri);
281285
} else if (window.openedWorkspace) {
282-
const rootFolders = this.workspacesMainService.resolveWorkspaceSync(window.openedWorkspace.configPath).folders;
283-
rootFolders.forEach(root => {
284-
folderURIs.push(root.uri);
285-
});
286+
const workspace = this.workspacesMainService.resolveWorkspaceSync(window.openedWorkspace.configPath);
287+
if (workspace) {
288+
workspace.folders.forEach(root => {
289+
folderURIs.push(root.uri);
290+
});
291+
}
286292
}
287293

288294
return this.browserWindowToInfo(window.win, folderURIs);

0 commit comments

Comments
 (0)