diff --git a/news/3 Code Health/611.md b/news/3 Code Health/611.md new file mode 100644 index 000000000000..e5ceb375fa5b --- /dev/null +++ b/news/3 Code Health/611.md @@ -0,0 +1 @@ +Fix all typescript errors when compiled in strict mode diff --git a/src/client/activation/activationService.ts b/src/client/activation/activationService.ts index b8f4cbf30219..c48bd17e5034 100644 --- a/src/client/activation/activationService.ts +++ b/src/client/activation/activationService.ts @@ -106,8 +106,8 @@ export class LanguageServerExtensionActivationService implements IExtensionActiv const activatedWkspcFoldersRemoved = activatedWkspcKeys.filter(item => workspaceKeys.indexOf(item) < 0); if (activatedWkspcFoldersRemoved.length > 0) { for (const folder of activatedWkspcFoldersRemoved) { - this.lsActivatedWorkspaces.get(folder).dispose(); - this.lsActivatedWorkspaces.delete(folder); + this.lsActivatedWorkspaces.get(folder)!.dispose(); + this.lsActivatedWorkspaces!.delete(folder); } } } diff --git a/src/client/activation/languageServer/languageServerExtension.ts b/src/client/activation/languageServer/languageServerExtension.ts index 5e5a886dda5a..0a89069d5bd1 100644 --- a/src/client/activation/languageServer/languageServerExtension.ts +++ b/src/client/activation/languageServer/languageServerExtension.ts @@ -23,7 +23,7 @@ export class LanguageServerExtension implements ILanguageServerExtension { this.disposable.dispose(); } } - public register(): Promise { + public async register(): Promise { if (this.disposable) { return; } diff --git a/src/client/common/application/types.ts b/src/client/common/application/types.ts index b766712a217c..4eae68cebda5 100644 --- a/src/client/common/application/types.ts +++ b/src/client/common/application/types.ts @@ -47,7 +47,7 @@ import { } from 'vscode'; import * as vsls from 'vsls/vscode'; -import { IAsyncDisposable } from '../types'; +import { IAsyncDisposable, Resource } from '../types'; // tslint:disable:no-any unified-signatures @@ -581,7 +581,7 @@ export interface IWorkspaceService { * @param uri An uri. * @return A workspace folder or `undefined` */ - getWorkspaceFolder(uri: Uri): WorkspaceFolder | undefined; + getWorkspaceFolder(uri: Resource): WorkspaceFolder | undefined; /** * Generate a key that's unique to the workspace folder (could be fsPath). diff --git a/src/client/common/variables/environment.ts b/src/client/common/variables/environment.ts index f1ae49ee739c..2a32ff9e5e53 100644 --- a/src/client/common/variables/environment.ts +++ b/src/client/common/variables/environment.ts @@ -68,7 +68,7 @@ export function parseEnvFile( baseVars?: EnvironmentVariables ): EnvironmentVariables { const globalVars = baseVars ? baseVars : {}; - const vars = {}; + const vars : EnvironmentVariables = {}; lines.toString().split('\n').forEach((line, idx) => { const [name, value] = parseEnvLine(line); if (name === '') { diff --git a/src/client/datascience/cellMatcher.ts b/src/client/datascience/cellMatcher.ts index cddfaeec9f11..7892a662269c 100644 --- a/src/client/datascience/cellMatcher.ts +++ b/src/client/datascience/cellMatcher.ts @@ -33,7 +33,7 @@ export class CellMatcher { } public exec(code: string) : string | undefined { - let result: RegExpExecArray; + let result: RegExpExecArray | null = null; if (this.codeMatchRegEx.test(code)) { this.codeExecRegEx.lastIndex = -1; result = this.codeExecRegEx.exec(code); diff --git a/src/client/datascience/historyMessageListener.ts b/src/client/datascience/historyMessageListener.ts index 398c0f164c9a..faa97d431ce2 100644 --- a/src/client/datascience/historyMessageListener.ts +++ b/src/client/datascience/historyMessageListener.ts @@ -50,6 +50,6 @@ export class HistoryMessageListener implements IWebPanelMessageListener { } private getHistoryMessages() : string [] { - return Object.keys(HistoryMessages).map(k => HistoryMessages[k].toString()); + return Object.keys(HistoryMessages).map(k => (HistoryMessages as any)[k].toString()); } } diff --git a/src/client/datascience/liveshare/postOffice.ts b/src/client/datascience/liveshare/postOffice.ts index 4edf0909b05c..69d5fe580e41 100644 --- a/src/client/datascience/liveshare/postOffice.ts +++ b/src/client/datascience/liveshare/postOffice.ts @@ -223,7 +223,7 @@ export class PostOffice implements IAsyncDisposable { const jsonArray = JSON.parse(a.args) as JSONArray; if (jsonArray !== null && jsonArray.length >= 2) { const firstArg = jsonArray[0]; // More stupid hygiene problems. - const command = firstArg !== null ? firstArg.toString() : ''; + const command = firstArg !== null ? firstArg!.toString() : ''; this.postCommand(command, ...jsonArray.slice(1)).ignoreErrors(); } } diff --git a/src/client/debugger/extension/configuration/resolvers/base.ts b/src/client/debugger/extension/configuration/resolvers/base.ts index ccd650932bd9..3f6f2b070264 100644 --- a/src/client/debugger/extension/configuration/resolvers/base.ts +++ b/src/client/debugger/extension/configuration/resolvers/base.ts @@ -20,7 +20,7 @@ import { IDebugConfigurationResolver } from '../types'; @injectable() export abstract class BaseConfigurationResolver implements IDebugConfigurationResolver { - protected pythonPathSource: PythonPathSource; + protected pythonPathSource: PythonPathSource = PythonPathSource.launchJson; constructor(protected readonly workspaceService: IWorkspaceService, protected readonly documentManager: IDocumentManager, protected readonly configurationService: IConfigurationService) { } diff --git a/src/client/providers/jediProxy.ts b/src/client/providers/jediProxy.ts index c3d0c0d2e21b..2235fd9cbdba 100644 --- a/src/client/providers/jediProxy.ts +++ b/src/client/providers/jediProxy.ts @@ -173,7 +173,7 @@ export class JediProxy implements Disposable { } private static getProperty(o: object, name: string): T { - return o[name]; + return (o as any)[name]; } public dispose() { diff --git a/src/client/providers/symbolProvider.ts b/src/client/providers/symbolProvider.ts index 9de9d0108016..b3c21b2eb379 100644 --- a/src/client/providers/symbolProvider.ts +++ b/src/client/providers/symbolProvider.ts @@ -30,7 +30,8 @@ function flattenSymbolTree(tree: DocumentSymbol, uri: Uri, containerName: string tree.name, // Type coercion is a bit fuzzy when it comes to enums, so we // play it safe by explicitly converting. - SymbolKind[SymbolKind[kind]], + // tslint:disable-next-line:no-any + (SymbolKind as any)[(SymbolKind as any)[kind]], containerName, new Location(uri, range) ); diff --git a/src/client/terminals/codeExecution/djangoContext.ts b/src/client/terminals/codeExecution/djangoContext.ts index d6f8755d447b..04b0a6a2592d 100644 --- a/src/client/terminals/codeExecution/djangoContext.ts +++ b/src/client/terminals/codeExecution/djangoContext.ts @@ -11,9 +11,9 @@ import { IFileSystem } from '../../common/platform/types'; @injectable() export class DjangoContextInitializer implements Disposable { private readonly isDjangoProject: ContextKey; - private monitoringActiveTextEditor: boolean; + private monitoringActiveTextEditor: boolean = false; private workspaceContextKeyValues = new Map(); - private lastCheckedWorkspace: string; + private lastCheckedWorkspace: string = ''; private disposables: Disposable[] = []; constructor(private documentManager: IDocumentManager, diff --git a/src/client/typeFormatters/contracts.ts b/src/client/typeFormatters/contracts.ts index efd376b75b69..ed7ba30b41d0 100644 --- a/src/client/typeFormatters/contracts.ts +++ b/src/client/typeFormatters/contracts.ts @@ -1,5 +1,5 @@ export class BlockRegEx { - constructor(private regEx: RegExp, public startWord) { + constructor(private regEx: RegExp, public startWord: String) { } public test(value: string): boolean { diff --git a/src/client/unittests/common/services/unitTestDiagnosticService.ts b/src/client/unittests/common/services/unitTestDiagnosticService.ts index d83775d7d150..6c51dc0ab7dc 100644 --- a/src/client/unittests/common/services/unitTestDiagnosticService.ts +++ b/src/client/unittests/common/services/unitTestDiagnosticService.ts @@ -12,7 +12,7 @@ import { TestStatus } from '../types'; @injectable() export class UnitTestDiagnosticService implements IUnitTestDiagnosticService { private MessageTypes = new Map(); - private MessageSeverities = new Map(); + private MessageSeverities = new Map(); private MessagePrefixes = new Map(); constructor() { @@ -23,16 +23,17 @@ export class UnitTestDiagnosticService implements IUnitTestDiagnosticService { this.MessageSeverities.set(PythonUnitTestMessageSeverity.Error, DiagnosticSeverity.Error); this.MessageSeverities.set(PythonUnitTestMessageSeverity.Failure, DiagnosticSeverity.Error); this.MessageSeverities.set(PythonUnitTestMessageSeverity.Skip, DiagnosticSeverity.Information); - this.MessageSeverities.set(PythonUnitTestMessageSeverity.Pass, null); + this.MessageSeverities.set(PythonUnitTestMessageSeverity.Pass, undefined); this.MessagePrefixes.set(DiagnosticMessageType.Error, localize.UnitTests.testErrorDiagnosticMessage()); this.MessagePrefixes.set(DiagnosticMessageType.Fail, localize.UnitTests.testFailDiagnosticMessage()); this.MessagePrefixes.set(DiagnosticMessageType.Skipped, localize.UnitTests.testSkippedDiagnosticMessage()); this.MessagePrefixes.set(DiagnosticMessageType.Pass, ''); } - public getMessagePrefix(status: TestStatus): string { - return this.MessagePrefixes.get(this.MessageTypes.get(status)); + public getMessagePrefix(status: TestStatus): string | undefined { + const msgType = this.MessageTypes.get(status); + return msgType !== undefined ? this.MessagePrefixes.get(msgType!) : undefined; } - public getSeverity(unitTestSeverity: PythonUnitTestMessageSeverity): DiagnosticSeverity { + public getSeverity(unitTestSeverity: PythonUnitTestMessageSeverity): DiagnosticSeverity | undefined { return this.MessageSeverities.get(unitTestSeverity); } } diff --git a/src/client/unittests/configuration.ts b/src/client/unittests/configuration.ts index 69a82978fc2d..ad481559de1a 100644 --- a/src/client/unittests/configuration.ts +++ b/src/client/unittests/configuration.ts @@ -77,7 +77,7 @@ export class UnitTestConfigurationService implements IUnitTestConfigurationServi // tslint:disable-next-line:prefer-type-cast return selectedTestRunner ? selectedTestRunner.product as UnitTestProduct : undefined; } - public enableTest(wkspace: Uri, product: UnitTestProduct) { + public async enableTest(wkspace: Uri, product: UnitTestProduct): Promise { const factory = this.serviceContainer.get(ITestConfigurationManagerFactory); const configMgr = factory.create(wkspace, product); const pythonConfig = this.workspaceService.getConfiguration('python', wkspace); diff --git a/src/client/unittests/types.ts b/src/client/unittests/types.ts index 7fbc873f8dd1..1c9c1aba2d41 100644 --- a/src/client/unittests/types.ts +++ b/src/client/unittests/types.ts @@ -12,8 +12,8 @@ export const IUnitTestConfigurationService = Symbol('IUnitTestConfigurationServi export interface IUnitTestConfigurationService { displayTestFrameworkError(wkspace: Uri): Promise; selectTestRunner(placeHolderMessage: string): Promise; - enableTest(wkspace: Uri, product: UnitTestProduct); - promptToEnableAndConfigureTestFramework(wkspace: Uri); + enableTest(wkspace: Uri, product: UnitTestProduct): Promise; + promptToEnableAndConfigureTestFramework(wkspace: Uri): Promise; } export const ITestResultDisplay = Symbol('ITestResultDisplay'); @@ -112,8 +112,8 @@ export interface IUnitTestHelper { export const IUnitTestDiagnosticService = Symbol('IUnitTestDiagnosticService'); export interface IUnitTestDiagnosticService { - getMessagePrefix(status: TestStatus): string; - getSeverity(unitTestSeverity: PythonUnitTestMessageSeverity): DiagnosticSeverity; + getMessagePrefix(status: TestStatus): string | undefined; + getSeverity(unitTestSeverity: PythonUnitTestMessageSeverity): DiagnosticSeverity | undefined; } export interface IPythonUnitTestMessage { diff --git a/src/client/workspaceSymbols/parser.ts b/src/client/workspaceSymbols/parser.ts index 162108e8ee1f..f065821d2ed0 100644 --- a/src/client/workspaceSymbols/parser.ts +++ b/src/client/workspaceSymbols/parser.ts @@ -4,6 +4,7 @@ import { fsExistsAsync } from '../common/utils/fs'; import { Tag } from './contracts'; // tslint:disable:no-require-imports no-var-requires no-suspicious-comment +// tslint:disable:no-any // TODO: Turn these into imports. const LineByLineReader = require('line-by-line'); const NamedRegexp = require('named-js-regexp'); @@ -20,7 +21,7 @@ export interface IRegexGroup { line: number; } -export function matchNamedRegEx(data, regex): IRegexGroup | null { +export function matchNamedRegEx(data: String, regex: String): IRegexGroup | null { const compiledRegexp = NamedRegexp(regex, 'g'); const rawMatch = compiledRegexp.exec(data); if (rawMatch !== null) { @@ -96,10 +97,10 @@ CTagKinMapping.set('_singleton methods', vscode.SymbolKind.Method); const newValuesAndKeys = {}; CTagKinMapping.forEach((value, key) => { - newValuesAndKeys[key.substring(1)] = value; + (newValuesAndKeys as any)[key.substring(1)] = value; }); Object.keys(newValuesAndKeys).forEach(key => { - CTagKinMapping.set(key, newValuesAndKeys[key]); + CTagKinMapping.set(key, (newValuesAndKeys as any)[key]); }); export function parseTags( @@ -119,11 +120,11 @@ export function parseTags( let lineNumber = 0; const tags: Tag[] = []; - lr.on('error', (err) => { + lr.on('error', (err: Error) => { reject(err); }); - lr.on('line', (line) => { + lr.on('line', (line: string) => { lineNumber = lineNumber + 1; if (token.isCancellationRequested) { lr.close(); diff --git a/src/test/activation/activationService.unit.test.ts b/src/test/activation/activationService.unit.test.ts index b39d1023a672..db9943b3c221 100644 --- a/src/test/activation/activationService.unit.test.ts +++ b/src/test/activation/activationService.unit.test.ts @@ -18,7 +18,7 @@ import { LanguageServerActivator } from '../../client/activation/types'; import { LSNotSupportedDiagnosticServiceId } from '../../client/application/diagnostics/checks/lsNotSupported'; -import { IDiagnosticsService } from '../../client/application/diagnostics/types'; +import { IDiagnostic, IDiagnosticsService } from '../../client/application/diagnostics/types'; import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../client/common/application/types'; import { IPlatformService } from '../../client/common/platform/types'; import { IConfigurationService, IDisposable, IDisposableRegistry, IOutputChannel, IPythonSettings, Resource } from '../../client/common/types'; @@ -101,7 +101,7 @@ suite('Activation - ActivationService', () => { if (lsSupported && !jediIsEnabled) { activatorName = LanguageServerActivator.DotNet; } - let diagnostics; + let diagnostics : IDiagnostic[]; if (!lsSupported && !jediIsEnabled) { diagnostics = [TypeMoq.It.isAny()]; } else { @@ -155,7 +155,6 @@ suite('Activation - ActivationService', () => { activator .setup(a => a.dispose()) - .returns(() => Promise.resolve()) .verifiable(TypeMoq.Times.once()); activationService.dispose(); @@ -311,7 +310,7 @@ suite('Activation - ActivationService', () => { const activatorDotNet = TypeMoq.Mock.ofType(); const activatorJedi = TypeMoq.Mock.ofType(); const activationService = new LanguageServerExtensionActivationService(serviceContainer.object); - const diagnostics = []; + const diagnostics: IDiagnostic[] = []; lsNotSupportedDiagnosticService .setup(l => l.diagnose(undefined)) .returns(() => Promise.resolve(diagnostics)); @@ -372,7 +371,7 @@ suite('Activation - ActivationService', () => { .verifiable(TypeMoq.Times.atLeastOnce()); workspaceService .setup(w => w.getWorkspaceFolderIdentifier(resource, '')) - .returns(() => resource.fsPath) + .returns(() => resource!.fsPath) .verifiable(TypeMoq.Times.atLeastOnce()); await activationService.activate(resource); diff --git a/src/test/application/diagnostics/checks/macPythonInterpreter.unit.test.ts b/src/test/application/diagnostics/checks/macPythonInterpreter.unit.test.ts index 9de963821519..a7650d44fb1a 100644 --- a/src/test/application/diagnostics/checks/macPythonInterpreter.unit.test.ts +++ b/src/test/application/diagnostics/checks/macPythonInterpreter.unit.test.ts @@ -309,7 +309,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { test('Event Handler is registered and invoked', async () => { let invoked = false; let callbackHandler!: (e: ConfigurationChangeEvent) => Promise; - const workspaceService = { onDidChangeConfiguration: cb => callbackHandler = cb } as any; + const workspaceService = { onDidChangeConfiguration: (cb : (e: ConfigurationChangeEvent) => Promise) => callbackHandler = cb } as any; const serviceContainerObject = createContainer(); serviceContainer.setup(s => s.get(typemoq.It.isValue(IWorkspaceService))) .returns(() => workspaceService); @@ -348,16 +348,16 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { serviceContainer.setup(s => s.get(typemoq.It.isValue(IWorkspaceService))) .returns(() => workspaceService.object); const diagnosticSvc = new class extends InvalidMacPythonInterpreterService { - constructor(arg1, arg2, arg3, arg4) { + constructor(arg1: IServiceContainer, arg2: IInterpreterService, arg3: IPlatformService, arg4: IInterpreterHelper) { super(arg1, arg2, arg3, arg4); this.changeThrottleTimeout = 1; } - public onDidChangeConfigurationEx = e => super.onDidChangeConfiguration(e); + public onDidChangeConfigurationEx = (e: ConfigurationChangeEvent) => super.onDidChangeConfiguration(e); public diagnose(): Promise { diagnoseInvocationCount += 1; return Promise.resolve(); } - }(serviceContainerObject, undefined, undefined, undefined); + }(serviceContainerObject, typemoq.Mock.ofType().object, typemoq.Mock.ofType().object, typemoq.Mock.ofType().object); event .setup(e => e.affectsConfiguration(typemoq.It.isValue('python.pythonPath'), typemoq.It.isAny())) @@ -389,16 +389,16 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { serviceContainer.setup(s => s.get(typemoq.It.isValue(IWorkspaceService))) .returns(() => workspaceService.object); const diagnosticSvc = new class extends InvalidMacPythonInterpreterService { - constructor(arg1, arg2, arg3, arg4) { + constructor(arg1: IServiceContainer, arg2: IInterpreterService, arg3: IPlatformService, arg4: IInterpreterHelper) { super(arg1, arg2, arg3, arg4); this.changeThrottleTimeout = 100; } - public onDidChangeConfigurationEx = e => super.onDidChangeConfiguration(e); + public onDidChangeConfigurationEx = (e: ConfigurationChangeEvent) => super.onDidChangeConfiguration(e); public diagnose(): Promise { diagnoseInvocationCount += 1; return Promise.resolve(); } - }(serviceContainerObject, undefined, undefined, undefined); + }(serviceContainerObject, typemoq.Mock.ofType().object, typemoq.Mock.ofType().object, typemoq.Mock.ofType().object); event .setup(e => e.affectsConfiguration(typemoq.It.isValue('python.pythonPath'), typemoq.It.isAny()))