Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLVSCODE-824 Use strict option in tsconfig #609

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cfamily/cfamily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function showMessageAndUpdateConfig(compilationDbPath: string) {
.update(FULL_PATH_TO_COMPILE_COMMANDS, pathForSettings, vscode.ConfigurationTarget.Workspace);
}

function tryRelativizeToWorkspaceFolder(filePath: string): [string, vscode.WorkspaceFolder] {
function tryRelativizeToWorkspaceFolder(filePath: string): [string, (vscode.WorkspaceFolder | undefined)] {
if (!path.isAbsolute(filePath)) {
return [filePath, undefined];
}
Expand Down
39 changes: 27 additions & 12 deletions src/connected/autobinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

import { BindingService } from './binding';
import { ConnectionSettingsService } from '../settings/connectionsettings';
import { ConnectionSettingsService, SonarCloudConnection, SonarQubeConnection } from '../settings/connectionsettings';
import {
BindingSuggestion,
FolderUriParams,
Expand All @@ -34,6 +34,13 @@ const CONFIGURE_BINDING_PROMPT_MESSAGE = `There are folders in your workspace th
Do you want to configure binding?
[Learn More](${SonarLintDocumentation.CONNECTED_MODE})`;

type ConnectionQuickPickItem = {
label : string;
description : string;
connectionId : string;
contextValue : string;
}

export class AutoBindingService implements FileSystemSubscriber {
private static _instance: AutoBindingService;
private readonly filesPerConfigScope : Map<string, FoundFileDto[]> = new Map<string, FoundFileDto[]>();
Expand Down Expand Up @@ -116,7 +123,7 @@ export class AutoBindingService implements FileSystemSubscriber {
contextValue: 'sonarcloudConnection'
};
} else {
const connectionNames = [];
const connectionNames : ConnectionQuickPickItem[] = [];
sonarQubeConnections.forEach(c => {
connectionNames.push({
label: this.computeItemLabel('SonarQube', c),
Expand All @@ -141,14 +148,14 @@ export class AutoBindingService implements FileSystemSubscriber {
return targetConnection;
}

private computeItemLabel(serverType: 'SonarQube' | 'SonarCloud', connection) {
private computeItemLabel(serverType: 'SonarQube' | 'SonarCloud', connection: (SonarCloudConnection | SonarQubeConnection)) {
if (serverType === 'SonarQube') {
return connection.connectionId ? connection.connectionId : connection.serverUrl;
return connection.connectionId ? connection.connectionId : (connection as SonarQubeConnection).serverUrl;
}
return connection.connectionId ? connection.connectionId : connection.organizationKey;
return connection.connectionId ? connection.connectionId : (connection as SonarCloudConnection).organizationKey;
}

private computeConnectionId(connection) {
private computeConnectionId(connection : (SonarCloudConnection | SonarQubeConnection)) {
return connection.connectionId ? connection.connectionId : DEFAULT_CONNECTION_ID;
}

Expand All @@ -164,7 +171,9 @@ export class AutoBindingService implements FileSystemSubscriber {
this.workspaceState.update(DO_NOT_ASK_ABOUT_AUTO_BINDING_FOR_WS_FLAG, true);
} else if (action === BIND_ACTION) {
const targetConnection = await this.getTargetConnectionForManualBinding();
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
if (targetConnection) {
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
}
}
});
}
Expand Down Expand Up @@ -195,7 +204,9 @@ export class AutoBindingService implements FileSystemSubscriber {
]);
} else if (action === BIND_ACTION) {
const targetConnection = await this.getTargetConnectionForManualBinding();
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
if (targetConnection) {
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
}
}
});
}
Expand Down Expand Up @@ -229,7 +240,9 @@ export class AutoBindingService implements FileSystemSubscriber {
break;
case CHOOSE_MANUALLY_ACTION: {
const targetConnection = await this.getTargetConnectionForManualBinding();
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
if (targetConnection) {
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
}
break;
}
case DONT_ASK_AGAIN_ACTION:
Expand Down Expand Up @@ -258,7 +271,9 @@ export class AutoBindingService implements FileSystemSubscriber {
switch (result) {
case BIND_ACTION: {
const targetConnection = await this.getTargetConnectionForManualBinding();
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
if (targetConnection) {
await this.bindingService.createOrEditBinding(targetConnection.connectionId, targetConnection.contextValue);
}
break;
}
case DONT_ASK_AGAIN_ACTION:
Expand All @@ -277,7 +292,7 @@ export class AutoBindingService implements FileSystemSubscriber {
if(folderUri && this.filesPerConfigScope.get(folderUri) === undefined) {
this.filesPerConfigScope.set(folderUri, []);
}
this.filesPerConfigScope.get(folderUri).push({ fileName, filePath: fullFileUri.fsPath, content: null });
this.filesPerConfigScope.get(folderUri)?.push({ fileName, filePath: fullFileUri.fsPath, content: undefined });
}

didRemoveWorkspaceFolder(workspaceFolderUri:vscode.Uri) {
Expand Down Expand Up @@ -322,7 +337,7 @@ export class AutoBindingService implements FileSystemSubscriber {
}

private async readJsonFiles(name: string, fullFileUri: vscode.Uri, foundFiles: Array<FoundFileDto>) {
let content: string = null;
let content: (string | undefined) = undefined;
if (name.endsWith('.json')) {
content = (await vscode.workspace.fs.readFile(fullFileUri)).toString();
}
Expand Down
98 changes: 54 additions & 44 deletions src/connected/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ const OPEN_FOLDER_ACTION = 'Open Folder';
const BIND_MANUALLY_ACTION = 'Bind Manually';
export const DO_NOT_ASK_ABOUT_AUTO_BINDING_FOR_FOLDER_FLAG = 'doNotAskAboutAutoBindingForFolder';

async function bindManuallyAction(workspaceFolder: VSCode.WorkspaceFolder) {
const existingSettings = VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.get<ProjectBinding>(BINDING_SETTINGS);
if (existingSettings.projectKey === undefined) {
await VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.update(BINDING_SETTINGS, { connectionId: '', projectKey: '' });
await this.languageClient.didCreateBinding(BindingCreationMode.MANUAL);
}
VSCode.commands.executeCommand('workbench.action.openFolderSettingsFile');
}

export class BindingService {
private static _instance: BindingService;

Expand Down Expand Up @@ -86,7 +73,7 @@ export class BindingService {
async deleteBindingsForConnection(connection: Connection | string) {
const connectionId = typeof connection === 'string' ? connection : connection.id || DEFAULT_CONNECTION_ID;
const allBindings = this.getAllBindings();
const bindingsForConnection: Map<string, BoundFolder[]> = allBindings.get(connectionId);
const bindingsForConnection: Map<string, BoundFolder[]> | undefined = allBindings.get(connectionId);
if (bindingsForConnection) {
for (const folders of bindingsForConnection.values()) {
await Promise.all(folders.map(f => this.deleteBinding(f)));
Expand All @@ -99,17 +86,17 @@ export class BindingService {
for (const folder of VSCode.workspace.workspaceFolders || []) {
const config = VSCode.workspace.getConfiguration(SONARLINT_CATEGORY, folder.uri);
const binding = config.get<ProjectBinding>(BINDING_SETTINGS);
const projectKey = binding.projectKey;
const projectKey = binding?.projectKey;
if (projectKey) {
const connectionId = binding.connectionId || binding.serverId || DEFAULT_CONNECTION_ID;
const connectionId = (binding.connectionId ?? binding.serverId) ?? DEFAULT_CONNECTION_ID;
if (!bindingsPerConnectionId.has(connectionId)) {
bindingsPerConnectionId.set(connectionId, new Map<string, BoundFolder[]>());
}
const connectionBindingsPerProjectKey = bindingsPerConnectionId.get(connectionId);
if (!connectionBindingsPerProjectKey.has(projectKey)) {
connectionBindingsPerProjectKey.set(projectKey, []);
if (!connectionBindingsPerProjectKey?.has(projectKey)) {
connectionBindingsPerProjectKey?.set(projectKey, []);
}
connectionBindingsPerProjectKey.get(projectKey).push({ folder, binding });
connectionBindingsPerProjectKey?.get(projectKey)?.push({ folder, binding });
}
}
return bindingsPerConnectionId;
Expand All @@ -120,28 +107,31 @@ export class BindingService {
for (const folder of VSCode.workspace.workspaceFolders || []) {
const config = VSCode.workspace.getConfiguration(SONARLINT_CATEGORY, folder.uri);
const binding = config.get<ProjectBinding>(BINDING_SETTINGS);
bindingStatePerFolder.set(folder.uri, binding.projectKey !== undefined);
bindingStatePerFolder.set(folder.uri, binding?.projectKey !== undefined);
}
return bindingStatePerFolder;
}

async assistBinding(params: AssistBindingParams) {
const workspaceFolders = VSCode.workspace.workspaceFolders;
if (workspaceFolders === undefined) {
return null;
}
const selectedWorkspaceFolderName = await this.showFolderSelectionQuickPickOrReturnDefaultSelection(
workspaceFolders
);
const workspaceFolder = workspaceFolders.find(f => f.name === selectedWorkspaceFolderName);
const workspaceFolder = workspaceFolders?.find(f => f.name === selectedWorkspaceFolderName);

const existingSettings = VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.get<ProjectBinding>(BINDING_SETTINGS);
if (existingSettings.projectKey === undefined) {
if (existingSettings?.projectKey === undefined) {
await VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.update(BINDING_SETTINGS, { connectionId: params.connectionId, projectKey: params.projectKey });
await this.languageClient.didCreateBinding(params.isFromSharedConfiguration ? BindingCreationMode.IMPORTED : BindingCreationMode.AUTOMATIC);
}
return { configurationScopeId: workspaceFolder.uri.toString() };
return { configurationScopeId: workspaceFolder?.uri.toString() };
}

async createOrEditBinding(
Expand Down Expand Up @@ -170,7 +160,7 @@ export class BindingService {
if (!serverType) {
serverType = contextValue === 'sonarqubeConnection' ? 'SonarQube' : 'SonarCloud';
}
let selectedFolderName;
let selectedFolderName : string | undefined;
if (workspaceFolder) {
selectedFolderName = workspaceFolder.name;
} else {
Expand All @@ -180,23 +170,23 @@ export class BindingService {
await this.pickRemoteProjectToBind(connectionId, workspaceFolder, serverType, selectedFolderName);
}

isRelatedConnectionValid(connectionId: string) : boolean {
return this.settingsService.getStatusForConnection(connectionId).success;
isRelatedConnectionValid(connectionId: string) : boolean | undefined {
return this.settingsService.getStatusForConnection(connectionId)?.success;
}

async getBaseServerUrl(connectionId: string, serverType: ServerType): Promise<string> {
const serverUrlOrOrganizationKey =
serverType === 'SonarQube'
? (await this.settingsService.loadSonarQubeConnection(connectionId)).serverUrl
: (await this.settingsService.loadSonarCloudConnection(connectionId)).organizationKey;
return buildBaseServerUrl(serverType, serverUrlOrOrganizationKey);
? (await this.settingsService.loadSonarQubeConnection(connectionId))?.serverUrl
: (await this.settingsService.loadSonarCloudConnection(connectionId))?.organizationKey;
return serverUrlOrOrganizationKey ? buildBaseServerUrl(serverType, serverUrlOrOrganizationKey) : '';
}

private async pickRemoteProjectToBind(
connectionId: string,
workspaceFolder: VSCode.WorkspaceFolder,
workspaceFolder: VSCode.WorkspaceFolder | undefined,
serverType: ServerType,
selectedFolderName
selectedFolderName : string | undefined
) {
if (!workspaceFolder) {
return;
Expand All @@ -210,8 +200,9 @@ export class BindingService {
const suggestedProjectsGroup = { label: 'Suggested Projects', kind: VSCode.QuickPickItemKind.Separator };
if (remoteProjects) {
const remoteProjectsQuickPick = VSCode.window.createQuickPick();
remoteProjectsQuickPick.title = `Select ${serverType} Project to Bind with '${selectedFolderName}/'`;
remoteProjectsQuickPick.placeholder = `Select the remote project you want to bind with '${selectedFolderName}/' folder`;
remoteProjectsQuickPick.title = `Select ${serverType} Project to Bind with '${selectedFolderName ?? workspaceFolder.name}/'`;
remoteProjectsQuickPick.placeholder =
`Select the remote project you want to bind with '${selectedFolderName ?? workspaceFolder.name}/' folder`;
remoteProjectsQuickPick.items = [
suggestedProjectsGroup,
...suggestedProjects,
Expand Down Expand Up @@ -261,6 +252,19 @@ export class BindingService {
return [];
}

async bindManuallyAction(workspaceFolder: VSCode.WorkspaceFolder) {
const existingSettings = VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.get<ProjectBinding>(BINDING_SETTINGS);
if (existingSettings?.projectKey === undefined) {
await VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.update(BINDING_SETTINGS, { connectionId: '', projectKey: '' });
await this.languageClient.didCreateBinding(BindingCreationMode.MANUAL);
}
VSCode.commands.executeCommand('workbench.action.openFolderSettingsFile');
}

async showFolderSelectionQuickPickOrReturnDefaultSelection(workspaceFolders: readonly VSCode.WorkspaceFolder[]) {
return workspaceFolders.length === 1
? workspaceFolders[0].name
Expand All @@ -273,17 +277,23 @@ export class BindingService {
);
}

async saveBinding(projectKey: string, workspaceFolder: VSCode.WorkspaceFolder, creationMode: BindingCreationMode, connectionId?: string) {
connectionId = connectionId || DEFAULT_CONNECTION_ID;
await VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.update(BINDING_SETTINGS, { connectionId, projectKey });
await this.languageClient.didCreateBinding(creationMode);
async saveBinding(projectKey: string | undefined, workspaceFolder: VSCode.WorkspaceFolder | undefined, creationMode: BindingCreationMode, connectionId?: string) {
if (!projectKey) {
VSCode.window.showWarningMessage('No project selected. Binding cannot be created.');
} else if (!workspaceFolder) {
VSCode.window.showWarningMessage('No workspace folder selected. Binding cannot be created.');
} else {
connectionId = connectionId ?? DEFAULT_CONNECTION_ID;
await VSCode.workspace
.getConfiguration(SONARLINT_CATEGORY, workspaceFolder)
.update(BINDING_SETTINGS, { connectionId, projectKey });
await this.languageClient.didCreateBinding(creationMode);

VSCode.window.showInformationMessage(`Workspace folder '${workspaceFolder.name}/' has been bound with project '${projectKey}'`);
VSCode.window.showInformationMessage(`Workspace folder '${workspaceFolder.name}/' has been bound with project '${projectKey}'`);

if (creationMode === BindingCreationMode.MANUAL) {
this.proposeSharingConfig(projectKey, workspaceFolder);
if (creationMode === BindingCreationMode.MANUAL) {
this.proposeSharingConfig(projectKey, workspaceFolder);
}
}
}

Expand Down Expand Up @@ -321,7 +331,7 @@ export class BindingService {
if (remoteProjects.size === 0) {
VSCode.window.showWarningMessage('No remote projects to display.', BIND_MANUALLY_ACTION).then(async action => {
if (action === BIND_MANUALLY_ACTION) {
bindManuallyAction(workspaceFolder);
this.bindManuallyAction(workspaceFolder);
}
});
}
Expand Down Expand Up @@ -356,7 +366,7 @@ export class BindingService {
isBound(workspaceFolder: VSCode.WorkspaceFolder) {
const config = VSCode.workspace.getConfiguration(SONARLINT_CATEGORY, workspaceFolder.uri);
const binding = config.get<ProjectBinding>(BINDING_SETTINGS);
return !!binding.projectKey;
return !!binding?.projectKey;
}

async removeBindingsForRemovedConnections(connectionIds: string[]) {
Expand Down
Loading