From cc480f0eb127481ccdebfafd60b28ee2c17b63ca Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 12 Feb 2019 11:29:28 -0800 Subject: [PATCH] Strict null work on preferencesModel --- src/vs/workbench/common/editor.ts | 2 +- .../preferences/common/preferences.ts | 6 +-- .../preferences/common/preferencesModels.ts | 38 +++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index bd9740edb23..027f4d17d21 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -678,7 +678,7 @@ export class EditorOptions implements IEditorOptions { /** * Helper to create EditorOptions inline. */ - static create(settings: IEditorOptions): EditorOptions { + static create(settings: IEditorOptions): EditorOptions | null { const options = new EditorOptions(); options.preserveFocus = settings.preserveFocus; diff --git a/src/vs/workbench/services/preferences/common/preferences.ts b/src/vs/workbench/services/preferences/common/preferences.ts index 472c1d06fc3..01a55393bc5 100644 --- a/src/vs/workbench/services/preferences/common/preferences.ts +++ b/src/vs/workbench/services/preferences/common/preferences.ts @@ -135,7 +135,7 @@ export interface IFilterMetadata { export interface IPreferencesEditorModel { uri?: URI; - getPreference(key: string): T; + getPreference(key: string): T | null; dispose(): void; } @@ -147,7 +147,7 @@ export interface ISettingsEditorModel extends IPreferencesEditorModel settingsGroups: ISettingsGroup[]; filterSettings(filter: string, groupFilter: IGroupFilter, settingMatcher: ISettingMatcher): ISettingMatch[]; findValueMatches(filter: string, setting: ISetting): IRange[]; - updateResultGroup(id: string, resultGroup: ISearchResultGroup): IFilterResult; + updateResultGroup(id: string, resultGroup: ISearchResultGroup): IFilterResult | null; } export interface ISettingsEditorOptions extends IEditorOptions { @@ -165,7 +165,7 @@ export class SettingsEditorOptions extends EditorOptions implements ISettingsEdi folderUri?: URI; query?: string; - static create(settings: ISettingsEditorOptions): SettingsEditorOptions { + static create(settings: ISettingsEditorOptions): SettingsEditorOptions | null { if (!settings) { return null; } diff --git a/src/vs/workbench/services/preferences/common/preferencesModels.ts b/src/vs/workbench/services/preferences/common/preferencesModels.ts index fab9317ee52..f61324585e4 100644 --- a/src/vs/workbench/services/preferences/common/preferencesModels.ts +++ b/src/vs/workbench/services/preferences/common/preferencesModels.ts @@ -27,7 +27,7 @@ export abstract class AbstractSettingsModel extends EditorModel { protected _currentResultGroups = new Map(); - updateResultGroup(id: string, resultGroup: ISearchResultGroup): IFilterResult { + updateResultGroup(id: string, resultGroup: ISearchResultGroup): IFilterResult | null { if (resultGroup) { this._currentResultGroups.set(id, resultGroup); } else { @@ -44,9 +44,9 @@ export abstract class AbstractSettingsModel extends EditorModel { private removeDuplicateResults(): void { const settingKeys = new Set(); map.keys(this._currentResultGroups) - .sort((a, b) => this._currentResultGroups.get(a).order - this._currentResultGroups.get(b).order) + .sort((a, b) => this._currentResultGroups.get(a)!.order - this._currentResultGroups.get(b)!.order) .forEach(groupId => { - const group = this._currentResultGroups.get(groupId); + const group = this._currentResultGroups.get(groupId)!; group.result.filterMatches = group.result.filterMatches.filter(s => !settingKeys.has(s.setting.key)); group.result.filterMatches.forEach(s => settingKeys.add(s.setting.key)); }); @@ -76,7 +76,7 @@ export abstract class AbstractSettingsModel extends EditorModel { return filterMatches.sort((a, b) => b.score - a.score); } - getPreference(key: string): ISetting { + getPreference(key: string): ISetting | null { for (const group of this.settingsGroups) { for (const section of group.sections) { for (const setting of section.settings) { @@ -111,12 +111,12 @@ export abstract class AbstractSettingsModel extends EditorModel { abstract findValueMatches(filter: string, setting: ISetting): IRange[]; - protected abstract update(): IFilterResult; + protected abstract update(): IFilterResult | null; } export class SettingsEditorModel extends AbstractSettingsModel implements ISettingsEditorModel { - private _settingsGroups: ISettingsGroup[]; + private _settingsGroups: ISettingsGroup[] | null; protected settingsModel: ITextModel; private readonly _onDidChangeGroups: Emitter = this._register(new Emitter()); @@ -144,7 +144,7 @@ export class SettingsEditorModel extends AbstractSettingsModel implements ISetti if (!this._settingsGroups) { this.parse(); } - return this._settingsGroups; + return this._settingsGroups!; } get content(): string { @@ -163,7 +163,7 @@ export class SettingsEditorModel extends AbstractSettingsModel implements ISetti this._settingsGroups = parse(this.settingsModel, (property: string, previousParents: string[]): boolean => this.isSettingsProperty(property, previousParents)); } - protected update(): IFilterResult { + protected update(): IFilterResult | null { const resultGroups = map.values(this._currentResultGroups); if (!resultGroups.length) { return null; @@ -179,7 +179,7 @@ export class SettingsEditorModel extends AbstractSettingsModel implements ISetti }); }); - let filteredGroup: ISettingsGroup; + let filteredGroup: ISettingsGroup | undefined; const modelGroup = this.settingsGroups[0]; // Editable model has one or zero groups if (modelGroup) { filteredGroup = { @@ -836,7 +836,7 @@ export class DefaultSettingsEditorModel extends AbstractSettingsModel implements return []; } - getPreference(key: string): ISetting { + getPreference(key: string): ISetting | null { for (const group of this.settingsGroups) { for (const section of group.sections) { for (const setting of section.settings) { @@ -1003,7 +1003,7 @@ class SettingsContentBuilder { } } -export function createValidator(prop: IConfigurationPropertySchema): ((value: any) => string) | null { +export function createValidator(prop: IConfigurationPropertySchema): (value: any) => (string | null) { return value => { let exclusiveMax: number | undefined; let exclusiveMin: number | undefined; @@ -1037,28 +1037,28 @@ export function createValidator(prop: IConfigurationPropertySchema): ((value: an const numericValidations: Validator[] = isNumeric ? [ { enabled: exclusiveMax !== undefined && (prop.maximum === undefined || exclusiveMax <= prop.maximum), - isValid: (value => value < exclusiveMax), + isValid: (value => value < exclusiveMax!), message: nls.localize('validations.exclusiveMax', "Value must be strictly less than {0}.", exclusiveMax) }, { enabled: exclusiveMin !== undefined && (prop.minimum === undefined || exclusiveMin >= prop.minimum), - isValid: (value => value > exclusiveMin), + isValid: (value => value > exclusiveMin!), message: nls.localize('validations.exclusiveMin', "Value must be strictly greater than {0}.", exclusiveMin) }, { enabled: prop.maximum !== undefined && (exclusiveMax === undefined || exclusiveMax > prop.maximum), - isValid: (value => value <= prop.maximum), + isValid: (value => value <= prop.maximum!), message: nls.localize('validations.max', "Value must be less than or equal to {0}.", prop.maximum) }, { enabled: prop.minimum !== undefined && (exclusiveMin === undefined || exclusiveMin < prop.minimum), - isValid: (value => value >= prop.minimum), + isValid: (value => value >= prop.minimum!), message: nls.localize('validations.min', "Value must be greater than or equal to {0}.", prop.minimum) }, { enabled: prop.multipleOf !== undefined, - isValid: (value => value % prop.multipleOf === 0), + isValid: (value => value % prop.multipleOf! === 0), message: nls.localize('validations.multipleOf', "Value must be a multiple of {0}.", prop.multipleOf) }, { @@ -1071,17 +1071,17 @@ export function createValidator(prop: IConfigurationPropertySchema): ((value: an const stringValidations: Validator[] = [ { enabled: prop.maxLength !== undefined, - isValid: (value => value.length <= prop.maxLength), + isValid: (value => value.length <= prop.maxLength!), message: nls.localize('validations.maxLength', "Value must be {0} or fewer characters long.", prop.maxLength) }, { enabled: prop.minLength !== undefined, - isValid: (value => value.length >= prop.minLength), + isValid: (value => value.length >= prop.minLength!), message: nls.localize('validations.minLength', "Value must be {0} or more characters long.", prop.minLength) }, { enabled: patternRegex !== undefined, - isValid: (value => patternRegex.test(value)), + isValid: (value => patternRegex!.test(value)), message: prop.patternErrorMessage || nls.localize('validations.regex', "Value must match regex `{0}`.", prop.pattern) }, ].filter(validation => validation.enabled);