Skip to content

Commit b41dabe

Browse files
authored
Enable strict null checks in tsconfig (#918)
* Enable strict null checks in tsconfig * Use default value instead of null assertion * Simplify expressions
1 parent 029b22f commit b41dabe

13 files changed

+51
-45
lines changed

src/configurationProvider.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
7373
try {
7474
// See https://github.com/microsoft/vscode-java-debug/issues/778
7575
// Merge the platform specific properties to the global config to simplify the subsequent resolving logic.
76-
this.mergePlatformProperties(folder, config);
76+
this.mergePlatformProperties(config, folder);
7777
return this.resolveAndValidateDebugConfiguration(folder, config);
7878
} catch (ex) {
7979
utility.showErrorMessage({
@@ -110,7 +110,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
110110
const launchConfigs = mainClasses.map((item) => {
111111
return {
112112
...defaultLaunchConfig,
113-
name: this.constructLaunchConfigName(item.mainClass, item.projectName, cache),
113+
name: this.constructLaunchConfigName(item.mainClass, cache, item.projectName),
114114
mainClass: item.mainClass,
115115
projectName: item.projectName,
116116
};
@@ -127,7 +127,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
127127
});
128128
}
129129

130-
private mergePlatformProperties(_folder: vscode.WorkspaceFolder, config: vscode.DebugConfiguration) {
130+
private mergePlatformProperties(config: vscode.DebugConfiguration, _folder?: vscode.WorkspaceFolder) {
131131
if (config && platformName && config[platformName]) {
132132
try {
133133
for (const key of Object.keys(config[platformName])) {
@@ -140,7 +140,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
140140
}
141141
}
142142

143-
private constructLaunchConfigName(mainClass: string, projectName: string, cache: {[key: string]: any}) {
143+
private constructLaunchConfigName(mainClass: string, cache: {[key: string]: any}, projectName?: string) {
144144
const prefix = "Debug (Launch)-";
145145
let name = prefix + mainClass.substr(mainClass.lastIndexOf(".") + 1);
146146
if (projectName !== undefined) {
@@ -333,7 +333,8 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
333333
return Object.keys(config).filter((key: string) => key !== "noDebug").length === 0;
334334
}
335335

336-
private async resolveLaunchConfig(folder: vscode.Uri | undefined, config: vscode.DebugConfiguration): Promise<lsPlugin.IMainClassOption> {
336+
private async resolveLaunchConfig(folder: vscode.Uri | undefined,
337+
config: vscode.DebugConfiguration): Promise<lsPlugin.IMainClassOption | undefined> {
337338
if (!config.mainClass || this.isFile(config.mainClass)) {
338339
const currentFile = config.mainClass || _.get(vscode.window.activeTextEditor, "document.uri.fsPath");
339340
if (currentFile) {
@@ -350,7 +351,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
350351
}
351352

352353
const containsExternalClasspaths = !_.isEmpty(config.classPaths) || !_.isEmpty(config.modulePaths);
353-
const validationResponse = await lsPlugin.validateLaunchConfig(folder, config.mainClass, config.projectName, containsExternalClasspaths);
354+
const validationResponse = await lsPlugin.validateLaunchConfig(config.mainClass, config.projectName, containsExternalClasspaths, folder);
354355
if (!validationResponse.mainClass.isValid || !validationResponse.projectName.isValid) {
355356
return this.fixMainClass(folder, config, validationResponse);
356357
}
@@ -391,7 +392,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
391392
const selectedFix = await mainClassPicker.showQuickPick(validationResponse.proposals,
392393
"Please select main class<project name>.", false);
393394
if (selectedFix) {
394-
sendInfo(null, {
395+
sendInfo("", {
395396
fix: "yes",
396397
fixMessage: errors.join(os.EOL),
397398
});

src/debugCodeLensProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ function debugJavaProgram(mainClass: string, projectName: string, uri: vscode.Ur
134134
return startDebugging(mainClass, projectName, uri, false);
135135
}
136136

137-
async function constructDebugConfig(mainClass: string, projectName: string, workspace: vscode.Uri): Promise<vscode.DebugConfiguration> {
137+
async function constructDebugConfig(mainClass: string, projectName: string, workspace?: vscode.Uri): Promise<vscode.DebugConfiguration> {
138138
const launchConfigurations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("launch", workspace);
139139
const rawConfigs: vscode.DebugConfiguration[] = launchConfigurations.configurations;
140140

141-
let debugConfig: vscode.DebugConfiguration = _.find(rawConfigs, (config) => {
141+
let debugConfig: vscode.DebugConfiguration | undefined = _.find(rawConfigs, (config) => {
142142
return config.mainClass === mainClass && _.toString(config.projectName) === _.toString(projectName);
143143
});
144144

@@ -174,7 +174,7 @@ async function constructDebugConfig(mainClass: string, projectName: string, work
174174
return _.cloneDeep(debugConfig);
175175
}
176176

177-
async function launchJsonExists(workspace: vscode.Uri): Promise<boolean> {
177+
async function launchJsonExists(workspace?: vscode.Uri): Promise<boolean> {
178178
if (!workspace) {
179179
return false;
180180
}
@@ -185,8 +185,8 @@ async function launchJsonExists(workspace: vscode.Uri): Promise<boolean> {
185185
}
186186

187187
export async function startDebugging(mainClass: string, projectName: string, uri: vscode.Uri, noDebug: boolean): Promise<boolean> {
188-
const workspaceFolder: vscode.WorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri);
189-
const workspaceUri: vscode.Uri = workspaceFolder ? workspaceFolder.uri : undefined;
188+
const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(uri);
189+
const workspaceUri: vscode.Uri | undefined = workspaceFolder ? workspaceFolder.uri : undefined;
190190
const onClasspath = await isOnClasspath(uri.toString());
191191
if (workspaceUri && onClasspath === false && !(await addToClasspath(uri))) {
192192
return false;

src/extension.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function specifyProgramArguments(context: vscode.ExtensionContext): Thenable<str
165165
}
166166

167167
async function applyHCR(hcrStatusBar: NotificationBar) {
168-
const debugSession: vscode.DebugSession = vscode.debug.activeDebugSession;
168+
const debugSession: vscode.DebugSession | undefined = vscode.debug.activeDebugSession;
169169
if (!debugSession) {
170170
return;
171171
}
@@ -246,7 +246,7 @@ async function runJavaFile(uri: vscode.Uri, noDebug: boolean) {
246246
throw ex;
247247
}
248248

249-
const activeEditor: vscode.TextEditor = vscode.window.activeTextEditor;
249+
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
250250
if (!uri && activeEditor && _.endsWith(path.basename(activeEditor.document.fileName), ".java")) {
251251
uri = activeEditor.document.uri;
252252
}
@@ -279,7 +279,7 @@ async function runJavaFile(uri: vscode.Uri, noDebug: boolean) {
279279
} else {
280280
const launchMainChoice: string = "main() method";
281281
const launchTestChoice: string = "unit tests";
282-
const choice: string = await vscode.window.showQuickPick(
282+
const choice: string | undefined = await vscode.window.showQuickPick(
283283
[launchMainChoice, launchTestChoice],
284284
{ placeHolder: "Please select which kind of task you would like to launch" },
285285
);
@@ -317,7 +317,7 @@ async function launchMain(mainMethods: IMainClassOption[], uri: vscode.Uri, noDe
317317
return;
318318
}
319319

320-
await startDebugging(pick.mainClass, pick.projectName, uri, noDebug);
320+
await startDebugging(pick.mainClass, pick.projectName || "", uri, noDebug);
321321
}
322322

323323
async function runJavaProject(node: any, noDebug: boolean) {
@@ -342,13 +342,13 @@ async function runJavaProject(node: any, noDebug: boolean) {
342342
return;
343343
}
344344

345-
const projectName: string = pick.projectName;
345+
const projectName: string | undefined = pick.projectName;
346346
const mainClass: string = pick.mainClass;
347-
const filePath: string = pick.filePath;
348-
const workspaceFolder: vscode.WorkspaceFolder = filePath ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(filePath)) : undefined;
347+
const filePath: string | undefined = pick.filePath;
348+
const workspaceFolder: vscode.WorkspaceFolder | undefined = filePath ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(filePath)) : undefined;
349349
const launchConfigurations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("launch", workspaceFolder);
350350
const existingConfigs: vscode.DebugConfiguration[] = launchConfigurations.configurations;
351-
const existConfig: vscode.DebugConfiguration = _.find(existingConfigs, (config) => {
351+
const existConfig: vscode.DebugConfiguration | undefined = _.find(existingConfigs, (config) => {
352352
return config.mainClass === mainClass && _.toString(config.projectName) === _.toString(projectName);
353353
});
354354
const debugConfig = existConfig || {

src/javaDebugAdapterDescriptorFactory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { Type } from "./logger";
88
import { convertErrorToMessage, showErrorMessageWithTroubleshooting } from "./utility";
99

1010
export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
11-
public async createDebugAdapterDescriptor(_session: DebugSession, _executable: DebugAdapterExecutable): Promise<DebugAdapterDescriptor> {
12-
let error: Error;
11+
public async createDebugAdapterDescriptor(_session: DebugSession,
12+
_executable: DebugAdapterExecutable): Promise<DebugAdapterDescriptor | undefined> {
13+
let error: Error| undefined;
1314
try {
1415
const debugServerPort = <number> (await startDebugSession());
1516
if (debugServerPort) {

src/languageServerPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ export function resolveClasspath(mainClass: string, projectName: string) {
4949
return commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName);
5050
}
5151

52-
export function resolveMainClass(workspaceUri: vscode.Uri): Promise<IMainClassOption[]> {
52+
export function resolveMainClass(workspaceUri?: vscode.Uri): Promise<IMainClassOption[]> {
5353
if (workspaceUri) {
5454
return <Promise<IMainClassOption[]>>commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_MAINCLASS, workspaceUri.toString());
5555
}
5656
return <Promise<IMainClassOption[]>>commands.executeJavaLanguageServerCommand(commands.JAVA_RESOLVE_MAINCLASS);
5757
}
5858

59-
export function validateLaunchConfig(workspaceUri: vscode.Uri, mainClass: string, projectName: string, containsExternalClasspaths: boolean):
59+
export function validateLaunchConfig(mainClass: string, projectName: string, containsExternalClasspaths: boolean, workspaceUri?: vscode.Uri):
6060
Promise<ILaunchValidationResponse> {
6161
return <Promise<ILaunchValidationResponse>>commands.executeJavaLanguageServerCommand(commands.JAVA_VALIDATE_LAUNCHCONFIG,
6262
workspaceUri ? workspaceUri.toString() : undefined, mainClass, projectName, containsExternalClasspaths);

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const SENSITIVE_PROPS = ["message", "stacktrace", "detailmessage"];
1616

1717
// Deprecate
1818
class Logger implements vscode.Disposable {
19-
private reporter: TelemetryReporter = null;
19+
private reporter: TelemetryReporter | null = null;
2020

2121
public initialize(context: vscode.ExtensionContext, firstParty?: boolean): void {
2222
if (this.reporter) {

src/mainClassPicker.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,19 @@ class MainClassPicker {
8888
return this.getMRUTimestamp(b) - this.getMRUTimestamp(a);
8989
});
9090

91-
const mostRecentlyUsedOption: IMainClassOption = (options.length && this.contains(options[0])) ? options[0] : undefined;
91+
const mostRecentlyUsedOption: IMainClassOption | undefined = (options.length && this.contains(options[0])) ? options[0] : undefined;
9292
const isMostRecentlyUsed = (option: IMainClassOption): boolean => {
93-
return mostRecentlyUsedOption
93+
return !!mostRecentlyUsedOption
9494
&& mostRecentlyUsedOption.mainClass === option.mainClass
9595
&& mostRecentlyUsedOption.projectName === option.projectName;
9696
};
9797
const isFromActiveEditor = (option: IMainClassOption): boolean => {
98-
const activeEditor: TextEditor = window.activeTextEditor;
98+
const activeEditor: TextEditor | undefined = window.activeTextEditor;
9999
const currentActiveFile: string = _.get(activeEditor, "document.uri.fsPath");
100-
return option.filePath && currentActiveFile && path.relative(option.filePath, currentActiveFile) === "";
100+
if (option.filePath && currentActiveFile) {
101+
return path.relative(option.filePath, currentActiveFile) === "";
102+
}
103+
return false;
101104
};
102105
const isPrivileged = (option: IMainClassOption): boolean => {
103106
return isMostRecentlyUsed(option) || isFromActiveEditor(option);
@@ -124,7 +127,7 @@ class MainClassPicker {
124127
adjustedDetail.push("$(clock) recently used");
125128
}
126129

127-
if (isFromActiveEditor(option)) {
130+
if (isFromActiveEditor(option) && option.filePath) {
128131
adjustedDetail.push(`$(file-text) active editor (${path.basename(option.filePath)})`);
129132
}
130133

src/processPicker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function convertToJavaProcess(pid: number, command: string, args: string): IJava
4646
return undefined;
4747
}
4848

49-
export async function pickJavaProcess(): Promise<IJavaProcess> {
49+
export async function pickJavaProcess(): Promise<IJavaProcess | undefined> {
5050
const javaProcesses: IJavaProcess[] = [];
5151
try {
5252
await getProcesses((pid: number, _ppid: number, command: string, args: string, _date: number) => {

src/terminalLinkProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class JavaTerminalLinkProvder implements TerminalLinkProvider<IJavaTermin
2020
}
2121

2222
const regex = new RegExp("(\\sat\\s+)([\\w$\\.]+\\/)?(([\\w$]+\\.)+[<\\w$>]+)\\(([\\w-$]+\\.java:\\d+)\\)");
23-
const result: RegExpExecArray = regex.exec(context.line);
23+
const result: RegExpExecArray | null = regex.exec(context.line);
2424
if (result && result.length) {
2525
const stackTrace = `${result[2] || ""}${result[3]}(${result[5]})`;
2626
const sourceLineNumber = Number(result[5].split(":")[1]);

src/threadOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function initializeThreadOperations(context: vscode.ExtensionContext) {
2323
}
2424

2525
async function operateThread(request: string, threadId: any): Promise<void> {
26-
const debugSession: vscode.DebugSession = vscode.debug.activeDebugSession;
26+
const debugSession: vscode.DebugSession | undefined = vscode.debug.activeDebugSession;
2727
if (!debugSession) {
2828
return;
2929
}

0 commit comments

Comments
 (0)