From 2fc484337dad5d94b6fde307f62a372d0ce355a0 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Fri, 30 Nov 2018 12:02:11 +0100 Subject: [PATCH] Strict null check for simpleServices --- src/tsconfig.strictNullChecks.json | 1 + src/vs/base/common/labels.ts | 2 +- .../standalone/browser/simpleServices.ts | 62 +++++++++++-------- .../configuration/common/configuration.ts | 2 +- .../common/configurationModels.ts | 14 ++--- src/vs/platform/workspace/common/workspace.ts | 2 +- .../parts/search/common/queryBuilder.ts | 2 +- 7 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/tsconfig.strictNullChecks.json b/src/tsconfig.strictNullChecks.json index ccb5ca5df8e..596895d1aac 100644 --- a/src/tsconfig.strictNullChecks.json +++ b/src/tsconfig.strictNullChecks.json @@ -352,6 +352,7 @@ "./vs/editor/standalone/browser/colorizer.ts", "./vs/editor/standalone/browser/iPadShowKeyboard/iPadShowKeyboard.ts", "./vs/editor/standalone/browser/inspectTokens/inspectTokens.ts", + "./vs/editor/standalone/browser/simpleServices.ts", "./vs/editor/standalone/browser/standaloneCodeServiceImpl.ts", "./vs/editor/standalone/browser/standaloneThemeServiceImpl.ts", "./vs/editor/standalone/browser/toggleHighContrast/toggleHighContrast.ts", diff --git a/src/vs/base/common/labels.ts b/src/vs/base/common/labels.ts index 94f776bb64b..bcba54a4316 100644 --- a/src/vs/base/common/labels.ts +++ b/src/vs/base/common/labels.ts @@ -11,7 +11,7 @@ import { isLinux, isWindows, isMacintosh } from 'vs/base/common/platform'; import { isEqual } from 'vs/base/common/resources'; export interface IWorkspaceFolderProvider { - getWorkspaceFolder(resource: URI): { uri: URI, name?: string }; + getWorkspaceFolder(resource: URI): { uri: URI, name?: string } | null; getWorkspace(): { folders: { uri: URI, name?: string }[]; }; diff --git a/src/vs/editor/standalone/browser/simpleServices.ts b/src/vs/editor/standalone/browser/simpleServices.ts index 6cfb3f16e0e..d37e49b266e 100644 --- a/src/vs/editor/standalone/browser/simpleServices.ts +++ b/src/vs/editor/standalone/browser/simpleServices.ts @@ -98,15 +98,13 @@ export class SimpleEditorModelResolverService implements ITextModelService { } public createModelReference(resource: URI): Promise> { - let model: ITextModel; - - model = withTypedEditor(this.editor, + let model: ITextModel | null = withTypedEditor(this.editor, (editor) => this.findModel(editor, resource), (diffEditor) => this.findModel(diffEditor.getOriginalEditor(), resource) || this.findModel(diffEditor.getModifiedEditor(), resource) ); if (!model) { - return Promise.resolve(new ImmortalReference(null)); + return Promise.reject(new Error(`Model not found`)); } return Promise.resolve(new ImmortalReference(new SimpleModel(model))); @@ -144,7 +142,7 @@ export class SimpleProgressService implements IProgressService { } showWhile(promise: Thenable, delay?: number): Thenable { - return null; + return Promise.resolve(void 0); } } @@ -277,11 +275,16 @@ export class StandaloneKeybindingService extends AbstractKeybindingService { })); } - public addDynamicKeybinding(commandId: string, keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | null): IDisposable { + public addDynamicKeybinding(commandId: string, _keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | null): IDisposable { + const keybinding = createKeybinding(_keybinding, OS); + if (!keybinding) { + throw new Error(`Invalid keybinding`); + } + let toDispose: IDisposable[] = []; this._dynamicKeybindings.push({ - keybinding: createKeybinding(keybinding, OS), + keybinding: keybinding, command: commandId, when: when, weight1: 1000, @@ -426,10 +429,10 @@ export class SimpleConfigurationService implements IConfigurationService { } public reloadConfiguration(): Promise { - return Promise.resolve(null); + return Promise.resolve(void 0); } - public getConfigurationData(): IConfigurationData { + public getConfigurationData(): IConfigurationData | null { return null; } } @@ -450,8 +453,11 @@ export class SimpleResourceConfigurationService implements ITextResourceConfigur getValue(resource: URI, section?: string): T; getValue(resource: URI, position?: IPosition, section?: string): T; getValue(resource: any, arg2?: any, arg3?: any) { - const position: IPosition = Pos.isIPosition(arg2) ? arg2 : null; - const section: string = position ? (typeof arg3 === 'string' ? arg3 : void 0) : (typeof arg2 === 'string' ? arg2 : void 0); + const position: IPosition | null = Pos.isIPosition(arg2) ? arg2 : null; + const section: string | undefined = position ? (typeof arg3 === 'string' ? arg3 : void 0) : (typeof arg2 === 'string' ? arg2 : void 0); + if (typeof section === 'undefined') { + return this.configurationService.getValue(); + } return this.configurationService.getValue(section); } } @@ -482,11 +488,11 @@ export class StandaloneTelemetryService implements ITelemetryService { public isOptedIn = false; public publicLog(eventName: string, data?: any): Promise { - return Promise.resolve(null); + return Promise.resolve(void 0); } public getTelemetryInfo(): Promise { - return null; + throw new Error(`Not available`); } } @@ -526,8 +532,8 @@ export class SimpleWorkspaceContextService implements IWorkspaceContextService { return WorkbenchState.EMPTY; } - public getWorkspaceFolder(resource: URI): IWorkspaceFolder { - return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME ? this.workspace.folders[0] : void 0; + public getWorkspaceFolder(resource: URI): IWorkspaceFolder | null { + return resource && resource.scheme === SimpleWorkspaceContextService.SCHEME ? this.workspace.folders[0] : null; } public isInsideWorkspace(resource: URI): boolean { @@ -567,19 +573,21 @@ export class SimpleBulkEditService implements IBulkEditService { let edits = new Map(); - for (let edit of workspaceEdit.edits) { - if (!isResourceTextEdit(edit)) { - return Promise.reject(new Error('bad edit - only text edits are supported')); + if (workspaceEdit.edits) { + for (let edit of workspaceEdit.edits) { + if (!isResourceTextEdit(edit)) { + return Promise.reject(new Error('bad edit - only text edits are supported')); + } + let model = this._modelService.getModel(edit.resource); + if (!model) { + return Promise.reject(new Error('bad edit - model not found')); + } + let array = edits.get(model); + if (!array) { + array = []; + } + edits.set(model, array.concat(edit.edits)); } - let model = this._modelService.getModel(edit.resource); - if (!model) { - return Promise.reject(new Error('bad edit - model not found')); - } - let array = edits.get(model); - if (!array) { - array = []; - } - edits.set(model, array.concat(edit.edits)); } let totalEdits = 0; diff --git a/src/vs/platform/configuration/common/configuration.ts b/src/vs/platform/configuration/common/configuration.ts index 2bd8874037b..340fbea2a9a 100644 --- a/src/vs/platform/configuration/common/configuration.ts +++ b/src/vs/platform/configuration/common/configuration.ts @@ -63,7 +63,7 @@ export interface IConfigurationService { onDidChangeConfiguration: Event; - getConfigurationData(): IConfigurationData; + getConfigurationData(): IConfigurationData | null; /** * Fetches the value of the section for the given overrides. diff --git a/src/vs/platform/configuration/common/configurationModels.ts b/src/vs/platform/configuration/common/configurationModels.ts index b332c2b18b3..e22cf70f63b 100644 --- a/src/vs/platform/configuration/common/configurationModels.ts +++ b/src/vs/platform/configuration/common/configurationModels.ts @@ -36,7 +36,7 @@ export class ConfigurationModel implements IConfigurationModel { return this.checkAndFreeze(this._keys); } - getValue(section: string): V { + getValue(section: string | undefined): V { return section ? getConfigurationValue(this.contents, section) : this.contents; } @@ -289,7 +289,7 @@ export class Configuration { private _freeze: boolean = true) { } - getValue(section: string, overrides: IConfigurationOverrides, workspace: Workspace): any { + getValue(section: string | undefined, overrides: IConfigurationOverrides, workspace: Workspace | null): any { const consolidateConfigurationModel = this.getConsolidateConfigurationModel(overrides, workspace); return consolidateConfigurationModel.getValue(section); } @@ -317,7 +317,7 @@ export class Configuration { } } - inspect(key: string, overrides: IConfigurationOverrides, workspace: Workspace): { + inspect(key: string, overrides: IConfigurationOverrides, workspace: Workspace | null): { default: C, user: C, workspace?: C, @@ -338,7 +338,7 @@ export class Configuration { }; } - keys(workspace: Workspace): { + keys(workspace: Workspace | null): { default: string[]; user: string[]; workspace: string[]; @@ -397,12 +397,12 @@ export class Configuration { return this._folderConfigurations; } - private getConsolidateConfigurationModel(overrides: IConfigurationOverrides, workspace: Workspace): ConfigurationModel { + private getConsolidateConfigurationModel(overrides: IConfigurationOverrides, workspace: Workspace | null): ConfigurationModel { let configurationModel = this.getConsolidatedConfigurationModelForResource(overrides, workspace); return overrides.overrideIdentifier ? configurationModel.override(overrides.overrideIdentifier) : configurationModel; } - private getConsolidatedConfigurationModelForResource({ resource }: IConfigurationOverrides, workspace: Workspace): ConfigurationModel { + private getConsolidatedConfigurationModelForResource({ resource }: IConfigurationOverrides, workspace: Workspace | null): ConfigurationModel { let consolidateConfiguration = this.getWorkspaceConsolidatedConfiguration(); if (workspace && resource) { @@ -447,7 +447,7 @@ export class Configuration { return folderConsolidatedConfiguration; } - private getFolderConfigurationModelForResource(resource: URI | undefined, workspace: Workspace): ConfigurationModel | null { + private getFolderConfigurationModelForResource(resource: URI | undefined, workspace: Workspace | null): ConfigurationModel | null { if (workspace && resource) { const root = workspace.getFolder(resource); if (root) { diff --git a/src/vs/platform/workspace/common/workspace.ts b/src/vs/platform/workspace/common/workspace.ts index 321c66641fa..8ea645c21b0 100644 --- a/src/vs/platform/workspace/common/workspace.ts +++ b/src/vs/platform/workspace/common/workspace.ts @@ -63,7 +63,7 @@ export interface IWorkspaceContextService { * Returns the folder for the given resource from the workspace. * Can be null if there is no workspace or the resource is not inside the workspace. */ - getWorkspaceFolder(resource: URI): IWorkspaceFolder; + getWorkspaceFolder(resource: URI): IWorkspaceFolder | null; /** * Return `true` if the current workspace has the given identifier otherwise `false`. diff --git a/src/vs/workbench/parts/search/common/queryBuilder.ts b/src/vs/workbench/parts/search/common/queryBuilder.ts index 1c19646e1c1..2eb59fc8117 100644 --- a/src/vs/workbench/parts/search/common/queryBuilder.ts +++ b/src/vs/workbench/parts/search/common/queryBuilder.ts @@ -334,11 +334,11 @@ export class QueryBuilder { private getFolderQueryForSearchPath(searchPath: ISearchPathPattern, options: ICommonQueryBuilderOptions): IFolderQuery { const searchPathWorkspaceFolder = this.workspaceContextService.getWorkspaceFolder(searchPath.searchPath); - const searchPathRelativePath = searchPathWorkspaceFolder && searchPath.searchPath.path.substr(searchPathWorkspaceFolder.uri.path.length + 1); const rootConfig = this.getFolderQueryForRoot(searchPath.searchPath, options); let resolvedExcludes: glob.IExpression = {}; if (searchPathWorkspaceFolder && rootConfig.excludePattern) { + const searchPathRelativePath = searchPath.searchPath.path.substr(searchPathWorkspaceFolder.uri.path.length + 1); // Resolve excludes relative to the search path for (let excludePattern in rootConfig.excludePattern) { const { pathPortion, globPortion } = splitSimpleGlob(excludePattern);