Fix errors resulting from strictNullCheck
diff --git a/src/diffUtils.ts b/src/diffUtils.ts
index 5310b8b..3da3bc8 100644
--- a/src/diffUtils.ts
+++ b/src/diffUtils.ts
@@ -4,14 +4,17 @@
*--------------------------------------------------------*/
import jsDiff = require('diff');
-import { Position, Range, TextEdit, TextEditorEdit, Uri, WorkspaceEdit } from 'vscode';
+import { Position, Range, TextEditorEdit, Uri, WorkspaceEdit } from 'vscode';
import { getBinPathFromEnvVar } from './goPath';
-let diffToolAvailable: boolean = null;
+let diffToolAvailable: boolean | null = null;
export function isDiffToolAvailable(): boolean {
if (diffToolAvailable == null) {
const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
+ if (!envPath) {
+ return false;
+ }
diffToolAvailable = getBinPathFromEnvVar('diff', envPath, false) != null;
}
return diffToolAvailable;
@@ -35,20 +38,6 @@
this.text = '';
}
- // Creates TextEdit for current Edit
- public apply(): TextEdit {
- switch (this.action) {
- case EditTypes.EDIT_INSERT:
- return TextEdit.insert(this.start, this.text);
-
- case EditTypes.EDIT_DELETE:
- return TextEdit.delete(new Range(this.start, this.end));
-
- case EditTypes.EDIT_REPLACE:
- return TextEdit.replace(new Range(this.start, this.end), this.text);
- }
- }
-
// Applies Edit using given TextEditorEdit
public applyUsingTextEditorEdit(editBuilder: TextEditorEdit): void {
switch (this.action) {
@@ -99,7 +88,7 @@
function parseUniDiffs(diffOutput: jsDiff.IUniDiff[]): FilePatch[] {
const filePatches: FilePatch[] = [];
diffOutput.forEach((uniDiff: jsDiff.IUniDiff) => {
- let edit: Edit = null;
+ let edit: Edit;
const edits: Edit[] = [];
uniDiff.hunks.forEach((hunk: jsDiff.IHunk) => {
let startLine = hunk.oldStart;
diff --git a/src/goBrowsePackage.ts b/src/goBrowsePackage.ts
index 755e448..15f4079 100644
--- a/src/goBrowsePackage.ts
+++ b/src/goBrowsePackage.ts
@@ -14,11 +14,10 @@
export function browsePackages() {
let workDir = '';
- let currentUri: vscode.Uri = null;
let selectedText = '';
const editor = vscode.window.activeTextEditor;
if (editor) {
- currentUri = vscode.window.activeTextEditor.document.uri;
+ const currentUri = editor.document.uri;
workDir = path.dirname(currentUri.fsPath);
const selection = editor.selection;
if (!selection.isEmpty) {
@@ -30,7 +29,7 @@
}
selectedText = getImportPath(selectedText) || selectedText.trim();
} else if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1) {
- currentUri = vscode.workspace.workspaceFolders[0].uri;
+ const currentUri = vscode.workspace.workspaceFolders[0].uri;
workDir = currentUri.fsPath;
}
diff --git a/src/goCover.ts b/src/goCover.ts
index 45b22d4..2ad8e54 100644
--- a/src/goCover.ts
+++ b/src/goCover.ts
@@ -54,22 +54,28 @@
// Update the coverageDecorator in User config, if they are using the old style.
const goConfig = getGoConfig();
const inspectResult = goConfig.inspect('coverageDecorator');
- if (typeof inspectResult.globalValue === 'string') {
- goConfig.update('coverageDecorator', { type: inspectResult.globalValue }, vscode.ConfigurationTarget.Global);
- }
- if (typeof inspectResult.workspaceValue === 'string') {
- goConfig.update(
- 'coverageDecorator',
- { type: inspectResult.workspaceValue },
- vscode.ConfigurationTarget.Workspace
- );
- }
- if (typeof inspectResult.workspaceFolderValue === 'string') {
- goConfig.update(
- 'coverageDecorator',
- { type: inspectResult.workspaceValue },
- vscode.ConfigurationTarget.WorkspaceFolder
- );
+ if (inspectResult) {
+ if (typeof inspectResult.globalValue === 'string') {
+ goConfig.update(
+ 'coverageDecorator',
+ { type: inspectResult.globalValue },
+ vscode.ConfigurationTarget.Global
+ );
+ }
+ if (typeof inspectResult.workspaceValue === 'string') {
+ goConfig.update(
+ 'coverageDecorator',
+ { type: inspectResult.workspaceValue },
+ vscode.ConfigurationTarget.Workspace
+ );
+ }
+ if (typeof inspectResult.workspaceFolderValue === 'string') {
+ goConfig.update(
+ 'coverageDecorator',
+ { type: inspectResult.workspaceValue },
+ vscode.ConfigurationTarget.WorkspaceFolder
+ );
+ }
}
// Update the decorators
diff --git a/src/goDebugConfiguration.ts b/src/goDebugConfiguration.ts
index 97fd0e3..a9b50a5 100644
--- a/src/goDebugConfiguration.ts
+++ b/src/goDebugConfiguration.ts
@@ -58,7 +58,7 @@
debugConfiguration['packagePathToGoModPathMap'] = packagePathToGoModPathMap;
- const gopath = getCurrentGoPath(folder ? folder.uri : null);
+ const gopath = getCurrentGoPath(folder ? folder.uri : undefined);
if (!debugConfiguration['env']) {
debugConfiguration['env'] = { GOPATH: gopath };
} else if (!debugConfiguration['env']['GOPATH']) {
@@ -73,7 +73,7 @@
}
});
- const dlvConfig: { [key: string]: any } = goConfig.get('delveConfig');
+ const dlvConfig = goConfig.get<any>('delveConfig');
let useApiV1 = false;
if (debugConfiguration.hasOwnProperty('useApiV1')) {
useApiV1 = debugConfiguration['useApiV1'] === true;
diff --git a/src/goExtraInfo.ts b/src/goExtraInfo.ts
index d34da6d..8775c66 100644
--- a/src/goExtraInfo.ts
+++ b/src/goExtraInfo.ts
@@ -11,7 +11,7 @@
import { getGoConfig } from './util';
export class GoHoverProvider implements HoverProvider {
- private goConfig: WorkspaceConfiguration = null;
+ private goConfig: WorkspaceConfiguration | undefined;
constructor(goConfig?: WorkspaceConfiguration) {
this.goConfig = goConfig;
diff --git a/src/goFillStruct.ts b/src/goFillStruct.ts
index ec68eaf..dddc3eb 100644
--- a/src/goFillStruct.ts
+++ b/src/goFillStruct.ts
@@ -26,7 +26,7 @@
return execFillStruct(editor, args);
}
-function getCommonArgs(editor: vscode.TextEditor): string[] {
+function getCommonArgs(editor: vscode.TextEditor): string[] | undefined {
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
@@ -50,7 +50,7 @@
function getTabsCount(editor: vscode.TextEditor): number {
const startline = editor.selection.start.line;
const tabs = editor.document.lineAt(startline).text.match('^\t*');
- return tabs.length;
+ return tabs ? tabs.length : 0;
}
function execFillStruct(editor: vscode.TextEditor, args: string[]): Promise<void> {
diff --git a/src/goGenerateTests.ts b/src/goGenerateTests.ts
index 9fcc111..97cf776 100644
--- a/src/goGenerateTests.ts
+++ b/src/goGenerateTests.ts
@@ -19,7 +19,7 @@
/**
* If current active editor has a Go file, returns the editor.
*/
-function checkActiveEditor(): vscode.TextEditor {
+function checkActiveEditor(): vscode.TextEditor | undefined {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('Cannot generate unit tests. No editor selected.');
diff --git a/src/goImpl.ts b/src/goImpl.ts
index f0d0d29..fb743e1 100644
--- a/src/goImpl.ts
+++ b/src/goImpl.ts
@@ -15,7 +15,12 @@
const inputRegex = /^(\w+\ \*?\w+\ )?([\w./]+)$/;
export function implCursor() {
- const cursor = vscode.window.activeTextEditor.selection;
+ const editor = vscode.window.activeTextEditor;
+ if (!editor) {
+ vscode.window.showErrorMessage('No active editor found.');
+ return;
+ }
+ const cursor = editor.selection;
return vscode.window
.showInputBox({
placeHolder: 'f *File io.Closer',
@@ -35,16 +40,16 @@
// if matches[1] is undefined then detect receiver type
// take first character and use as receiver name
- runGoImpl([matches[1], matches[2]], cursor.start);
+ runGoImpl([matches[1], matches[2]], cursor.start, editor);
});
}
-function runGoImpl(args: string[], insertPos: vscode.Position) {
+function runGoImpl(args: string[], insertPos: vscode.Position, editor: vscode.TextEditor) {
const goimpl = getBinPath('impl');
const p = cp.execFile(
goimpl,
args,
- { env: getToolsEnvVars(), cwd: dirname(vscode.window.activeTextEditor.document.fileName) },
+ { env: getToolsEnvVars(), cwd: dirname(editor.document.fileName) },
(err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('impl');
@@ -56,7 +61,7 @@
return;
}
- vscode.window.activeTextEditor.edit((editBuilder) => {
+ editor.edit((editBuilder) => {
editBuilder.insert(insertPos, stdout);
});
}
diff --git a/src/goImport.ts b/src/goImport.ts
index 7fa2408..82627d6 100644
--- a/src/goImport.ts
+++ b/src/goImport.ts
@@ -60,7 +60,7 @@
return imports;
}
-async function askUserForImport(): Promise<string> {
+async function askUserForImport(): Promise<string | undefined> {
try {
const packages = await listPackages(true);
return vscode.window.showQuickPick(packages);
@@ -120,13 +120,21 @@
}
export function addImport(arg: { importPath: string; from: string }) {
+ const editor = vscode.window.activeTextEditor;
+ if (!editor) {
+ vscode.window.showErrorMessage('No active editor found to add imports.');
+ return;
+ }
const p = arg && arg.importPath ? Promise.resolve(arg.importPath) : askUserForImport();
p.then((imp) => {
+ if (!imp) {
+ return;
+ }
sendTelemetryEventForAddImportCmd(arg);
const edits = getTextEditForAddImport(imp);
if (edits && edits.length > 0) {
const edit = new vscode.WorkspaceEdit();
- edit.set(vscode.window.activeTextEditor.document.uri, edits);
+ edit.set(editor.document.uri, edits);
vscode.workspace.applyEdit(edit);
}
});
@@ -134,6 +142,10 @@
export function addImportToWorkspace() {
const editor = vscode.window.activeTextEditor;
+ if (!editor) {
+ vscode.window.showErrorMessage('No active editor found to determine current package.');
+ return;
+ }
const selection = editor.selection;
let importPath = '';
diff --git a/src/goReferencesCodelens.ts b/src/goReferencesCodelens.ts
index a24ad55..546280b 100644
--- a/src/goReferencesCodelens.ts
+++ b/src/goReferencesCodelens.ts
@@ -26,7 +26,7 @@
if (!this.enabled) {
return [];
}
- const codeLensConfig: { [key: string]: any } = getGoConfig(document.uri).get('enableCodeLens');
+ const codeLensConfig = getGoConfig(document.uri).get<{ [key: string]: any }>('enableCodeLens');
const codelensEnabled = codeLensConfig ? codeLensConfig['references'] : false;
if (!codelensEnabled) {
return Promise.resolve([]);
diff --git a/src/goRename.ts b/src/goRename.ts
index 518e269..3bb0400 100644
--- a/src/goRename.ts
+++ b/src/goRename.ts
@@ -56,7 +56,7 @@
try {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('gorename');
- return resolve(null);
+ return reject('Could not find gorename tool.');
}
if (err) {
const errMsg = stderr ? 'Rename failed: ' + stderr.replace(/\n/g, ' ') : 'Rename failed';
diff --git a/src/goRunTestCodelens.ts b/src/goRunTestCodelens.ts
index 749a72d..e79ab60 100644
--- a/src/goRunTestCodelens.ts
+++ b/src/goRunTestCodelens.ts
@@ -29,7 +29,7 @@
return [];
}
const config = getGoConfig(document.uri);
- const codeLensConfig: { [key: string]: any } = config.get('enableCodeLens');
+ const codeLensConfig = config.get<{ [key: string]: any }>('enableCodeLens');
const codelensEnabled = codeLensConfig ? codeLensConfig['runtest'] : false;
if (!codelensEnabled || !document.fileName.endsWith('_test.go')) {
return [];
@@ -55,7 +55,7 @@
const symbols = await documentSymbolProvider.provideDocumentSymbols(document, token);
const pkg = symbols[0];
if (!pkg) {
- return;
+ return [];
}
const range = pkg.range;
const packageCodeLens = [
diff --git a/src/telemetry.ts b/src/telemetry.ts
index 14bcb97..5f789aa 100644
--- a/src/telemetry.ts
+++ b/src/telemetry.ts
@@ -7,7 +7,8 @@
import TelemetryReporter from 'vscode-extension-telemetry';
export const extensionId: string = 'ms-vscode.Go';
-const extensionVersion: string = vscode.extensions.getExtension(extensionId).packageJSON.version;
+const extension = vscode.extensions.getExtension(extensionId);
+const extensionVersion: string = extension ? extension.packageJSON.version : '';
const aiKey: string = 'AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217';
export function sendTelemetryEventForModulesUsage() {