Skip to content

Commit

Permalink
refactor: remove unused exports/variables (#446)
Browse files Browse the repository at this point in the history
* refactor: remove unused exports/variables

* chore: remove empty file

---------

Co-authored-by: Bastian Doetsch <bastian.doetsch@snyk.io>
  • Loading branch information
carlos-snyk and bastiandoetsch authored Apr 15, 2024
1 parent c4a5987 commit 89cbe8f
Show file tree
Hide file tree
Showing 44 changed files with 29 additions and 1,190 deletions.
4 changes: 0 additions & 4 deletions src/snyk/base/messages/loginMessages.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/snyk/base/modules/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ export interface IExtension extends IBaseSnykModule, ISnykLib {
activate(context: VSCodeExtensionContext): void;
restartLanguageServer(): Promise<void>;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type errorType = Error | any;
99 changes: 0 additions & 99 deletions src/snyk/cli/process.ts

This file was deleted.

161 changes: 0 additions & 161 deletions src/snyk/cli/services/cliService.ts
Original file line number Diff line number Diff line change
@@ -1,164 +1,3 @@
import { firstValueFrom } from 'rxjs';
import parseArgsStringToArgv from 'string-argv';
import { AnalysisStatusProvider } from '../../common/analysis/statusProvider';
import { IConfiguration } from '../../common/configuration/configuration';
import { IWorkspaceTrust } from '../../common/configuration/trustedFolders';
import { ErrorHandler } from '../../common/error/errorHandler';
import { ILanguageServer } from '../../common/languageServer/languageServer';
import { ILog } from '../../common/logger/interfaces';
import { messages as analysisMessages } from '../../common/messages/analysisMessages';
import { DownloadService } from '../../common/services/downloadService';
import { ExtensionContext } from '../../common/vscode/extensionContext';
import { IVSCodeWorkspace } from '../../common/vscode/workspace';
import { CliExecutable } from '../cliExecutable';
import { CliProcess } from '../process';

export class CliError {
constructor(public error: string | Error | unknown, public path?: string, public isCancellation = false) {}

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (ubuntu-latest)

'unknown' overrides all other types in this union type

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (windows-latest)

'unknown' overrides all other types in this union type

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (macos-latest)

'unknown' overrides all other types in this union type

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (ubuntu-latest)

'unknown' overrides all other types in this union type

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (macos-latest)

'unknown' overrides all other types in this union type

Check warning on line 2 in src/snyk/cli/services/cliService.ts

View workflow job for this annotation

GitHub Actions / build / Build and Test (windows-latest)

'unknown' overrides all other types in this union type
}

export abstract class CliService<CliResult> extends AnalysisStatusProvider {
protected abstract readonly command: string[];
protected result: CliResult | CliError | undefined;

private cliProcess?: CliProcess;
private _isCliReady: boolean;
private _isAnyWorkspaceFolderTrusted = true;

constructor(
protected readonly extensionContext: ExtensionContext,
protected readonly logger: ILog,
protected readonly config: IConfiguration,
protected readonly workspace: IVSCodeWorkspace,
protected readonly downloadService: DownloadService,
protected readonly languageServer: ILanguageServer,
protected readonly workspaceTrust: IWorkspaceTrust,
) {
super();
}

get isCliReady(): boolean {
return this._isCliReady;
}

get isAnyWorkspaceFolderTrusted(): boolean {
return this._isAnyWorkspaceFolderTrusted;
}

async test(manualTrigger: boolean, reportTriggeredEvent: boolean): Promise<CliResult | CliError | void> {
this.ensureDependencies();

const currentCliPath = CliExecutable.getPath(this.extensionContext.extensionPath, this.config.getCliPath());
const currentCliPathExists = await CliExecutable.exists(
this.extensionContext.extensionPath,
this.config.getCliPath(),
);
await this.synchronizeCliPathIfNeeded(currentCliPath, currentCliPathExists);
if (currentCliPathExists) {
const cliPath = this.config.getCliPath();
if (!cliPath) {
throw new Error('CLI path is not set, probably failed migration.');
}

this.logger.info(`Using CLI path ${cliPath}`);
this.languageServer.cliReady$.next(cliPath);
}

// Prevent from CLI scan until Language Server downloads the CLI.
const cliPath = await firstValueFrom(this.languageServer.cliReady$);
this._isCliReady = true;

const workspaceFolders = this.workspace.getWorkspaceFolders();
if (workspaceFolders.length == 0) {
throw new Error('No workspace was opened.');
}

const foldersToTest = this.workspaceTrust.getTrustedFolders(this.config, workspaceFolders);
if (foldersToTest.length == 0) {
this.handleNoTrustedFolders();
this.logger.info(`Skipping Open Source scan. ${analysisMessages.noWorkspaceTrustDescription}`);
return;
}
this._isAnyWorkspaceFolderTrusted = true;

// Start test
this.analysisStarted();
this.beforeTest(manualTrigger, reportTriggeredEvent);
this.result = undefined;

if (this.cliProcess) {
const killed = this.cliProcess.kill();
if (!killed) this.logger.error('Failed to kill an already running CLI instance.');
}

this.cliProcess = new CliProcess(this.logger, this.config, this.workspace);
const args = this.buildArguments(foldersToTest);

let output: string;
try {
output = await this.cliProcess.spawn(cliPath, foldersToTest[0], args);
} catch (spawnError) {
if (spawnError instanceof CliError) {
return spawnError;
}

const result = new CliError(spawnError, '');
this.finalizeTest(result);
return result;
}

const mappedResult = this.mapToResultType(output);
this.finalizeTest(mappedResult);

return mappedResult;
}

// Synchronizes user configuration with CLI path passed to the Snyk LS.
// TODO: Remove in VS Code + Language Server feature cleanup.
private async synchronizeCliPathIfNeeded(cliPath: string, cliPathExists: boolean) {
if (!this.config.getCliPath() && cliPathExists) {
this.logger.info("Synchronising extension's CLI path with Language Server");
try {
await this.config.setCliPath(cliPath);
} catch (e) {
ErrorHandler.handle(e, this.logger, "Failed to synchronize extension's CLI path with Language Server");
}
}

return cliPath;
}

protected abstract mapToResultType(rawCliResult: string): CliResult;

protected abstract ensureDependencies(): void;

protected abstract beforeTest(manualTrigger: boolean, reportTriggeredEvent: boolean): void;
protected abstract afterTest(result: CliResult | CliError): void;

handleNoTrustedFolders() {
this._isAnyWorkspaceFolderTrusted = false;
}

private buildArguments(foldersToTest: ReadonlyArray<string>): string[] {
const args = [];

args.push(...this.command);
args.push(...foldersToTest);
args.push('--json');

const additionalParams = this.config.getAdditionalCliParameters();
if (additionalParams) {
args.push(...parseArgsStringToArgv(additionalParams.trim()));
}

return args;
}

// To be called to finalise the analysis
public finalizeTest(result: CliResult | CliError): void {
this.result = result;

this.analysisFinished();
this.afterTest(result);
}
}
6 changes: 1 addition & 5 deletions src/snyk/cli/supportedPlatforms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
export const SupportedCliPlatformsList = ['linux', 'win32', 'darwin'] as const;
const SupportedCliPlatformsList = ['linux', 'win32', 'darwin'] as const;
export type CliSupportedPlatform = typeof SupportedCliPlatformsList[number];

export function isPlatformSupported(platform: NodeJS.Platform): boolean {
return SupportedCliPlatformsList.find(p => p === platform) !== undefined;
}
5 changes: 0 additions & 5 deletions src/snyk/common/api/headers.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/snyk/common/commands/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { completeFileSuggestionType } from '../../snykCode/interfaces';
import { CodeIssueCommandArg } from '../../snykCode/views/interfaces';
import { IacIssueCommandArg } from '../../snykIac/views/interfaces';
import { OssIssueCommandArg } from '../../snykOss/interfaces';
import { CodeIssueData, Issue } from '../languageServer/types';

export enum OpenCommandIssueType {
CodeIssue,
Expand All @@ -14,10 +12,3 @@ export type OpenIssueCommandArg = {
issue: CodeIssueCommandArg | IacIssueCommandArg | OssIssueCommandArg;
issueType: OpenCommandIssueType;
};

export const isCodeIssue = (
_issue: completeFileSuggestionType | Issue<CodeIssueData> | OssIssueCommandArg,
issueType: OpenCommandIssueType,
): _issue is Issue<CodeIssueData> => {
return issueType === OpenCommandIssueType.CodeIssue;
};
1 change: 0 additions & 1 deletion src/snyk/common/constants/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export const VSCODE_GO_TO_SETTINGS_COMMAND = 'workbench.action.openSettings';
export const VSCODE_VIEW_CONTAINER_COMMAND = 'workbench.view.extension.snyk';
export const VSCODE_ADD_COMMENT_COMMAND = 'editor.action.addCommentLine';
export const VSCODE_VIEW_OSS_VIEW_COMMAND = 'snyk.views.analysis.oss.focus';

// custom Snyk commands
export const SNYK_START_COMMAND = 'snyk.start';
Expand Down
10 changes: 1 addition & 9 deletions src/snyk/common/constants/general.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// Changing this requires changing display name in package.json.
export const SNYK_NAME = 'Snyk Security';
export const SNYK_TOKEN_KEY = 'snyk.token';
export const SNYK_UNIQUE_EXTENSION_NAME = 'Snyk Vulnerability Scanner';
const SNYK_UNIQUE_EXTENSION_NAME = 'Snyk Vulnerability Scanner';
export const SNYK_PUBLISHER = 'snyk-security';
export const SNYK_NAME_EXTENSION = SNYK_UNIQUE_EXTENSION_NAME.toLowerCase().replace(/[()]/g, '').replace(/\s/g, '-');
export const MAX_CONNECTION_RETRIES = 5; // max number of automatic retries before showing an error
export const IDE_NAME = 'Visual Studio Code';
export const IDE_NAME_SHORT = 'vscode';
export const COMMAND_DEBOUNCE_INTERVAL = 200; // 200 milliseconds
export const DEFAULT_SCAN_DEBOUNCE_INTERVAL = 1000; // 1 second
export const DEFAULT_LS_DEBOUNCE_INTERVAL = 1000; // 1 second
export const EXECUTION_THROTTLING_INTERVAL = 1000 * 10; // * 60 * 30; // 30 minutes
export const EXECUTION_PAUSE_INTERVAL = 1000 * 60 * 30; // 30 minutes
export const REFRESH_VIEW_DEBOUNCE_INTERVAL = 200; // 200 milliseconds
// If CONNECTION_ERROR_RETRY_INTERVAL is smaller than EXECUTION_DEBOUNCE_INTERVAL it might get swallowed by the debouncer
export const CONNECTION_ERROR_RETRY_INTERVAL = DEFAULT_SCAN_DEBOUNCE_INTERVAL * 2 + 1000 * 3;

export const SNYK_LEARN_API_CACHE_DURATION_IN_MS = 1000 * 60 * 60 * 24; // 1 day
1 change: 0 additions & 1 deletion src/snyk/common/constants/globalState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const MEMENTO_FIRST_INSTALL_DATE_KEY = 'snyk.firstInstallDate';
export const MEMENTO_ANONYMOUS_ID = 'snyk.anonymousId';
export const MEMENTO_LS_LAST_UPDATE_DATE = 'snyk.lsLastUpdateDate';
export const MEMENTO_LS_PROTOCOL_VERSION = 'snyk.lsProtocolVersion';
Expand Down
2 changes: 0 additions & 2 deletions src/snyk/common/constants/languageConsts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ export const JAVASCRIPT = 'javascript';
export const JAVASCRIPT_REACT = 'javascriptreact';
export const HTML = 'html';
export const PJSON = 'json';

export const SupportedLanguageIds = [TYPESCRIPT, TYPESCRIPT_REACT, JAVASCRIPT, JAVASCRIPT_REACT, HTML, PJSON];
1 change: 0 additions & 1 deletion src/snyk/common/constants/views.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const SNYK_VIEW_WELCOME = 'snyk.views.welcome';
export const SNYK_VIEW_FEATURES = 'snyk.views.features';
export const SNYK_VIEW_ANALYSIS_CODE_ENABLEMENT = 'snyk.views.analysis.code.enablement';
export const SNYK_VIEW_ANALYSIS_CODE_SECURITY = 'snyk.views.analysis.code.security';
export const SNYK_VIEW_ANALYSIS_CODE_QUALITY = 'snyk.views.analysis.code.quality';
Expand Down
2 changes: 1 addition & 1 deletion src/snyk/common/languageServer/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from '../vscode/types';
import { LanguageServerSettings, ServerSettings } from './settings';

export type LanguageClientWorkspaceMiddleware = Partial<WorkspaceMiddleware> & {
type LanguageClientWorkspaceMiddleware = Partial<WorkspaceMiddleware> & {
configuration: (
params: ConfigurationParams,
token: CancellationToken,
Expand Down
13 changes: 0 additions & 13 deletions src/snyk/common/languageServer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ export type ServerSettings = {
deviceId?: string;
};

/**
* Transforms a boolean or undefined value into a string representation.
* It guarantees that undefined values are represented as 'true'.
* This utility is used to ensure feature flags are enabled by default
* when not explicitly set to false.
*
* @param {boolean | undefined} value - The value to transform.
* @returns {string} - The string 'true' if the value is undefined or truthy, 'false' if the value is false.
*/
export const defaultToTrue = (value: boolean | undefined): string => {
return `${value !== undefined ? value : true}`;
};

export class LanguageServerSettings {
static async fromConfiguration(configuration: IConfiguration, user: User): Promise<ServerSettings> {
const featuresConfiguration = configuration.getFeaturesConfiguration();
Expand Down
4 changes: 0 additions & 4 deletions src/snyk/common/languageServer/supportedPlatforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ export const SupportedLsPlatformsList = [
'windowsAmd64',
] as const;

export function isPlatformSupported(platform: string): boolean {
return SupportedLsPlatformsList.find(p => p === platform) !== undefined;
}

export type LsSupportedPlatform = typeof SupportedLsPlatformsList[number];
Loading

0 comments on commit 89cbe8f

Please sign in to comment.