From 59ad5728001314cacfda0f3a56756123e2c1bdc4 Mon Sep 17 00:00:00 2001 From: Alan Ren Date: Thu, 23 Mar 2023 15:19:23 -0700 Subject: [PATCH] fix strict null issues (#22430) --- extensions/mssql/src/mssql.d.ts | 4 +- .../mssql/src/objectManagement/commands.ts | 44 +++++++++---------- .../objectManagementService.ts | 3 +- .../src/objectManagement/ui/loginDialog.ts | 8 ++-- .../ui/objectManagementDialogBase.ts | 8 ++-- .../src/objectManagement/ui/userDialog.ts | 6 +-- .../mssql/src/objectManagement/utils.ts | 8 ++-- .../mssql/src/tableDesigner/tableDesigner.ts | 28 ++++++------ 8 files changed, 56 insertions(+), 53 deletions(-) diff --git a/extensions/mssql/src/mssql.d.ts b/extensions/mssql/src/mssql.d.ts index de7f86e7be..7a51fcb08c 100644 --- a/extensions/mssql/src/mssql.d.ts +++ b/extensions/mssql/src/mssql.d.ts @@ -1104,11 +1104,11 @@ declare module 'mssql' { /** * Schemas owned by the user. */ - ownedSchemas: string[] | undefined; + ownedSchemas: string[]; /** * Database roles that the user belongs to. */ - databaseRoles: string[] | undefined; + databaseRoles: string[]; /** * The name of the server login associated with the user. * Only applicable when the user type is 'WithLogin'. diff --git a/extensions/mssql/src/objectManagement/commands.ts b/extensions/mssql/src/objectManagement/commands.ts index 0e64ad99bf..701968eff6 100644 --- a/extensions/mssql/src/objectManagement/commands.ts +++ b/extensions/mssql/src/objectManagement/commands.ts @@ -69,7 +69,7 @@ async function handleNewUserDialogCommand(context: azdata.ObjectExplorerContext, return; } try { - const dialog = new UserDialog(service, connectionUri, context.connectionProfile.databaseName, true, undefined, context); + const dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, true, undefined, context); await dialog.open(); } catch (err) { @@ -85,15 +85,15 @@ async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplore if (!connectionUri) { return; } - const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo.nodeType); + const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType); try { let dialog; - switch (context.nodeInfo.nodeType) { + switch (context.nodeInfo!.nodeType) { case NodeType.Login: - dialog = new LoginDialog(service, connectionUri, false, context.nodeInfo.label); + dialog = new LoginDialog(service, connectionUri, false, context.nodeInfo!.label); break; case NodeType.User: - dialog = new UserDialog(service, connectionUri, context.connectionProfile.databaseName, false, context.nodeInfo.label); + dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, false, context.nodeInfo!.label); break; default: break; @@ -104,9 +104,9 @@ async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplore } catch (err) { TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenPropertiesDialog, err).withAdditionalProperties({ - objectType: context.nodeInfo.nodeType + objectType: context.nodeInfo!.nodeType }).send(); - await vscode.window.showErrorMessage(localizedConstants.OpenObjectPropertiesDialogError(nodeTypeDisplayName, context.nodeInfo.label, getErrorMessage(err))); + await vscode.window.showErrorMessage(localizedConstants.OpenObjectPropertiesDialogError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err))); } } @@ -115,16 +115,16 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext, if (!connectionUri) { return; } - let additionalConfirmationMessage: string; - switch (context.nodeInfo.nodeType) { + let additionalConfirmationMessage: string | undefined = undefined; + switch (context.nodeInfo!.nodeType) { case NodeType.Login: additionalConfirmationMessage = localizedConstants.DeleteLoginConfirmationText; break; default: break; } - const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo.nodeType); - let confirmMessage = localizedConstants.DeleteObjectConfirmationText(nodeTypeDisplayName, context.nodeInfo.label); + const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType); + let confirmMessage = localizedConstants.DeleteObjectConfirmationText(nodeTypeDisplayName, context.nodeInfo!.label); if (additionalConfirmationMessage) { confirmMessage = `${additionalConfirmationMessage} ${confirmMessage}`; } @@ -133,23 +133,23 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext, return; } azdata.tasks.startBackgroundOperation({ - displayName: localizedConstants.DeleteObjectOperationDisplayName(nodeTypeDisplayName, context.nodeInfo.label), + displayName: localizedConstants.DeleteObjectOperationDisplayName(nodeTypeDisplayName, context.nodeInfo!.label), description: '', isCancelable: false, operation: async (operation) => { try { const startTime = Date.now(); - await service.drop(connectionUri, context.nodeInfo.metadata.urn); + await service.drop(connectionUri, context.nodeInfo!.metadata!.urn); TelemetryReporter.sendTelemetryEvent(TelemetryActions.DeleteObject, { - objectType: context.nodeInfo.nodeType + objectType: context.nodeInfo!.nodeType }, { elapsedTimeMs: Date.now() - startTime }); } catch (err) { - operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.DeleteObjectError(nodeTypeDisplayName, context.nodeInfo.label, getErrorMessage(err))); + operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.DeleteObjectError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err))); TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.DeleteObject, err).withAdditionalProperties({ - objectType: context.nodeInfo.nodeType + objectType: context.nodeInfo!.nodeType }).send(); return; } @@ -164,8 +164,8 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext, if (!connectionUri) { return; } - const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo.nodeType); - const originalName = context.nodeInfo.metadata.name; + const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType); + const originalName = context.nodeInfo!.metadata!.name; const newName = await vscode.window.showInputBox({ title: localizedConstants.RenameObjectDialogTitle, value: originalName, @@ -191,9 +191,9 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext, operation: async (operation) => { try { const startTime = Date.now(); - await service.rename(connectionUri, context.nodeInfo.metadata.urn, newName); + await service.rename(connectionUri, context.nodeInfo!.metadata!.urn, newName); TelemetryReporter.sendTelemetryEvent(TelemetryActions.RenameObject, { - objectType: context.nodeInfo.nodeType + objectType: context.nodeInfo!.nodeType }, { elapsedTimeMs: Date.now() - startTime }); @@ -201,7 +201,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext, catch (err) { operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.RenameObjectError(nodeTypeDisplayName, originalName, newName, getErrorMessage(err))); TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.RenameObject, err).withAdditionalProperties({ - objectType: context.nodeInfo.nodeType + objectType: context.nodeInfo!.nodeType }).send(); return; } @@ -212,7 +212,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext, } async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise { - const connectionUri = await azdata.connection.getUriForConnection(context.connectionProfile.id); + const connectionUri = await azdata.connection.getUriForConnection(context.connectionProfile!.id); if (!connectionUri) { await vscode.window.showErrorMessage(localizedConstants.FailedToRetrieveConnectionInfoErrorMessage, { modal: true }); } diff --git a/extensions/mssql/src/objectManagement/objectManagementService.ts b/extensions/mssql/src/objectManagement/objectManagementService.ts index 25027fd581..59b8749b9c 100644 --- a/extensions/mssql/src/objectManagement/objectManagementService.ts +++ b/extensions/mssql/src/objectManagement/objectManagementService.ts @@ -236,7 +236,8 @@ export class TestObjectManagementService implements IObjectManagementService { authenticationType: AuthenticationType.Sql, loginName: 'sa', ownedSchemas: [], - databaseRoles: [] + databaseRoles: [], + password: '' }, languages: languages, schemas: schemas, diff --git a/extensions/mssql/src/objectManagement/ui/loginDialog.ts b/extensions/mssql/src/objectManagement/ui/loginDialog.ts index 907f61d381..e89ec71504 100644 --- a/extensions/mssql/src/objectManagement/ui/loginDialog.ts +++ b/extensions/mssql/src/objectManagement/ui/loginDialog.ts @@ -122,7 +122,7 @@ export class LoginDialog extends ObjectManagementDialogBase { - this.objectInfo.name = this.nameInput.value; + this.objectInfo.name = this.nameInput.value!; this.onObjectValueChange(); await this.runValidation(false); })); @@ -149,7 +149,7 @@ export class LoginDialog extends ObjectManagementDialogBase { - this.objectInfo.isEnabled = this.enabledCheckbox.checked; + this.objectInfo.isEnabled = this.enabledCheckbox.checked!; this.onObjectValueChange(); })); this.generalSection = this.createGroup(localizedConstants.GeneralSectionHeader, [nameContainer, authTypeContainer, this.enabledCheckbox], false); @@ -222,7 +222,7 @@ export class LoginDialog extends ObjectManagementDialogBase { - this.objectInfo.isLockedOut = this.lockedOutCheckbox.checked; + this.objectInfo.isLockedOut = this.lockedOutCheckbox.checked!; this.onObjectValueChange(); })); } @@ -250,7 +250,7 @@ export class LoginDialog extends ObjectManagementDialogBase { - this.objectInfo.connectPermission = this.connectPermissionCheckbox.checked; + this.objectInfo.connectPermission = this.connectPermissionCheckbox.checked!; this.onObjectValueChange(); })); items.push(defaultDatabaseContainer, defaultLanguageContainer, this.connectPermissionCheckbox); diff --git a/extensions/mssql/src/objectManagement/ui/objectManagementDialogBase.ts b/extensions/mssql/src/objectManagement/ui/objectManagementDialogBase.ts index 1cbf1f5c11..f27b9d037f 100644 --- a/extensions/mssql/src/objectManagement/ui/objectManagementDialogBase.ts +++ b/extensions/mssql/src/objectManagement/ui/objectManagementDialogBase.ts @@ -53,7 +53,7 @@ export abstract class ObjectManagementDialogBase { + this.disposables.push(table.onCellAction!((arg: azdata.ICheckboxCellActionEventArgs) => { const name = listValues[arg.row]; const idx = selectedValues.indexOf(name); if (arg.checked && idx === -1) { @@ -283,7 +283,7 @@ export abstract class ObjectManagementDialogBase { - this.objectInfo.name = this.nameInput.value; + this.objectInfo.name = this.nameInput.value!; this.onObjectValueChange(); await this.runValidation(false); })); const nameContainer = this.createLabelInputContainer(localizedConstants.NameText, this.nameInput); - this.defaultSchemaDropdown = this.createDropdown(localizedConstants.DefaultSchemaText, this.viewInfo.schemas, this.objectInfo.defaultSchema); + this.defaultSchemaDropdown = this.createDropdown(localizedConstants.DefaultSchemaText, this.viewInfo.schemas, this.objectInfo.defaultSchema!); this.defaultSchemaContainer = this.createLabelInputContainer(localizedConstants.DefaultSchemaText, this.defaultSchemaDropdown); this.disposables.push(this.defaultSchemaDropdown.onValueChanged(() => { this.objectInfo.defaultSchema = this.defaultSchemaDropdown.value; diff --git a/extensions/mssql/src/objectManagement/utils.ts b/extensions/mssql/src/objectManagement/utils.ts index 9c1f234b5f..256ed9bc08 100644 --- a/extensions/mssql/src/objectManagement/utils.ts +++ b/extensions/mssql/src/objectManagement/utils.ts @@ -31,7 +31,7 @@ export function deepClone(obj: T): T { export async function refreshParentNode(context: azdata.ObjectExplorerContext): Promise { if (context) { try { - const node = await azdata.objectexplorer.getNode(context.connectionProfile.id, context.nodeInfo.nodePath); + const node = await azdata.objectexplorer.getNode(context.connectionProfile!.id, context.nodeInfo!.nodePath); const parentNode = await node?.getParent(); await parentNode?.refresh(); } @@ -44,7 +44,7 @@ export async function refreshParentNode(context: azdata.ObjectExplorerContext): export async function refreshNode(context: azdata.ObjectExplorerContext): Promise { if (context) { try { - const node = await azdata.objectexplorer.getNode(context.connectionProfile.id, context.nodeInfo.nodePath); + const node = await azdata.objectexplorer.getNode(context.connectionProfile!.id, context.nodeInfo!.nodePath); await node?.refresh(); } catch (err) { @@ -72,7 +72,9 @@ export function getNodeTypeDisplayName(type: string, inTitle: boolean = false): } } -export function getAuthenticationTypeDisplayName(authType: AuthenticationType): string { +export function getAuthenticationTypeDisplayName(authType: AuthenticationType | undefined): string | undefined { + if (authType === undefined) { return undefined; } + switch (authType) { case AuthenticationType.Windows: return WindowsAuthenticationTypeDisplayText; diff --git a/extensions/mssql/src/tableDesigner/tableDesigner.ts b/extensions/mssql/src/tableDesigner/tableDesigner.ts index 6299fda82f..6304e03134 100644 --- a/extensions/mssql/src/tableDesigner/tableDesigner.ts +++ b/extensions/mssql/src/tableDesigner/tableDesigner.ts @@ -20,30 +20,30 @@ const DidInformUserKey: string = 'tableDesigner.DidInformUser'; export function registerTableDesignerCommands(appContext: AppContext) { appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newTable', async (context: azdata.ObjectExplorerContext) => { void showPreloadDbModelSettingPrompt(appContext); - const connectionString = await azdata.connection.getConnectionString(context.connectionProfile.id, true); - const tableIcon = context.nodeInfo.nodeSubType as azdata.designers.TableIcon; + const connectionString = await azdata.connection.getConnectionString(context.connectionProfile!.id, true); + const tableIcon = context.nodeInfo!.nodeSubType as azdata.designers.TableIcon; const telemetryInfo = await getTelemetryInfo(context, tableIcon); await azdata.designers.openTableDesigner(sqlProviderName, { title: NewTableText, - tooltip: `${context.connectionProfile.serverName} - ${context.connectionProfile.databaseName} - ${NewTableText}`, - server: context.connectionProfile.serverName, - database: context.connectionProfile.databaseName, + tooltip: `${context.connectionProfile!.serverName} - ${context.connectionProfile!.databaseName} - ${NewTableText}`, + server: context.connectionProfile!.serverName, + database: context.connectionProfile!.databaseName, isNewTable: true, id: generateUuid(), connectionString: connectionString, - accessToken: context.connectionProfile.options.azureAccountToken, + accessToken: context.connectionProfile!.options.azureAccountToken, tableIcon: tableIcon }, telemetryInfo); })); appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.designTable', async (context: azdata.ObjectExplorerContext) => { void showPreloadDbModelSettingPrompt(appContext); - const server = context.connectionProfile.serverName; - const database = context.connectionProfile.databaseName; - const schema = context.nodeInfo.metadata.schema; - const name = context.nodeInfo.metadata.name; - const connectionString = await azdata.connection.getConnectionString(context.connectionProfile.id, true); - const tableIcon = context.nodeInfo.nodeSubType as azdata.designers.TableIcon; + const server = context.connectionProfile!.serverName; + const database = context.connectionProfile!.databaseName; + const schema = context.nodeInfo!.metadata!.schema; + const name = context.nodeInfo!.metadata!.name; + const connectionString = await azdata.connection.getConnectionString(context.connectionProfile!.id, true); + const tableIcon = context.nodeInfo!.nodeSubType as azdata.designers.TableIcon; const telemetryInfo = await getTelemetryInfo(context, tableIcon); await azdata.designers.openTableDesigner(sqlProviderName, { title: `${schema}.${name}`, @@ -55,14 +55,14 @@ export function registerTableDesignerCommands(appContext: AppContext) { schema: schema, id: `${sqlProviderName}|${server}|${database}|${schema}|${name}`, connectionString: connectionString, - accessToken: context.connectionProfile.options.azureAccountToken, + accessToken: context.connectionProfile!.options.azureAccountToken, tableIcon: tableIcon }, telemetryInfo); })); } async function getTelemetryInfo(context: azdata.ObjectExplorerContext, tableType: string): Promise { - const serverInfo = await azdata.connection.getServerInfo(context.connectionProfile.id); + const serverInfo = await azdata.connection.getServerInfo(context.connectionProfile!.id); const telemetryInfo: telemetry.TelemetryEventProperties = { tableType };