From 0ac3c72ecb021f8d9a1ade8745e34265a59d5f48 Mon Sep 17 00:00:00 2001 From: peternhale Date: Tue, 9 Jan 2024 09:27:02 -0700 Subject: [PATCH] chore: refactor vscode apex replay to use arrow (#5304) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @W-14564471@ Refactor vscode apex replay denugger to use arrow functions Co-authored-by: Cristina CaƱizales <113132642+CristiCanizales@users.noreply.github.com> --- .../fix-these-rules.json | 1 + .../src/activation/getDialogStartingPath.ts | 20 +++---- .../src/breakpoints/checkpointService.ts | 38 +++++++------- .../src/commands/launchFromLogFile.ts | 6 +-- .../src/commands/quickLaunch.ts | 6 +-- .../src/index.ts | 52 +++++++++---------- .../src/messages/index.ts | 4 +- .../src/utils/settings.ts | 4 +- .../adapter/MockApexExtension.ts | 8 +-- .../checkpoints/checkpointService.test.ts | 16 +++--- .../apexExecutionOverlayActionCommand.test.ts | 6 +-- 11 files changed, 80 insertions(+), 81 deletions(-) diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/fix-these-rules.json b/packages/salesforcedx-vscode-apex-replay-debugger/fix-these-rules.json index dadddc6afe..35a79d62e3 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/fix-these-rules.json +++ b/packages/salesforcedx-vscode-apex-replay-debugger/fix-these-rules.json @@ -1,5 +1,6 @@ { "rules": { + "prefer-arrow/prefer-arrow-functions": ["error", {}], "@typescript-eslint/await-thenable": "warn", "@typescript-eslint/no-floating-promises": "warn", "@typescript-eslint/no-unsafe-argument": "warn", diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/activation/getDialogStartingPath.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/activation/getDialogStartingPath.ts index d966d1f34e..dedd607ced 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/activation/getDialogStartingPath.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/activation/getDialogStartingPath.ts @@ -13,9 +13,9 @@ import { import { existsSync } from 'fs'; import * as vscode from 'vscode'; -export function getDialogStartingPath( +export const getDialogStartingPath = ( extContext: vscode.ExtensionContext -): vscode.Uri | undefined { +): vscode.Uri | undefined => { if (workspaceUtils.hasRootWorkspace()) { // If the user has already selected a document through getLogFileName then // use that path if it still exists. @@ -37,21 +37,21 @@ export function getDialogStartingPath( const stateFolderUri = getUriFor(pathToStateFolder); return stateFolderUri; } -} +}; -function getLastOpenedLogFolder( +const getLastOpenedLogFolder = ( extContext: vscode.ExtensionContext -): string | undefined { +): string | undefined => { const pathToLastOpenedLogFolder = extContext.workspaceState.get( LAST_OPENED_LOG_FOLDER_KEY ); return pathToLastOpenedLogFolder; -} +}; -function folderExists(path: string): boolean { +const folderExists = (path: string): boolean => { return existsSync(path); -} +}; -function getUriFor(path: string): vscode.Uri { +const getUriFor = (path: string): vscode.Uri => { return vscode.Uri.file(path); -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/breakpoints/checkpointService.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/breakpoints/checkpointService.ts index 7de1e7dcf7..4d658c7d8c 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/breakpoints/checkpointService.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/breakpoints/checkpointService.ts @@ -804,9 +804,9 @@ const lock = new AsyncLock(); // This is the function registered for vscode.debug.onDidChangeBreakpoints. This // particular event fires breakpoint events without an active debug session which // allows us to manipulate checkpoints prior to the debug session. -export async function processBreakpointChangedForCheckpoints( +export const processBreakpointChangedForCheckpoints = async ( breakpointsChangedEvent: vscode.BreakpointsChangeEvent -): Promise { +): Promise => { for (const bp of breakpointsChangedEvent.removed) { if (bp.condition && bp.condition.toLowerCase().indexOf(CHECKPOINT) >= 0) { await lock.acquire(CHECKPOINTS_LOCK_STRING, async () => { @@ -878,11 +878,11 @@ export async function processBreakpointChangedForCheckpoints( }); } } -} +}; -export function parseCheckpointInfoFromBreakpoint( +export const parseCheckpointInfoFromBreakpoint = ( breakpoint: vscode.SourceBreakpoint -): ApexExecutionOverlayAction { +): ApexExecutionOverlayAction => { // declare the overlayAction with defaults const checkpointOverlayAction: ApexExecutionOverlayAction = { ActionScript: '', @@ -914,9 +914,9 @@ export function parseCheckpointInfoFromBreakpoint( checkpointOverlayAction.ActionScript = logMessage; } return checkpointOverlayAction; -} +}; -function setTypeRefsForEnabledCheckpoints(): boolean { +const setTypeRefsForEnabledCheckpoints = (): boolean => { let everythingSet = true; for (const cpNode of checkpointService.getChildren() as CheckpointNode[]) { if (cpNode.isCheckpointEnabled()) { @@ -942,7 +942,7 @@ function setTypeRefsForEnabledCheckpoints(): boolean { } } return everythingSet; -} +}; // The order of operations here should be to // 1. Get the source/line information @@ -965,7 +965,7 @@ let creatingCheckpoints = false; // that may be on the checkpoint are the condition (which needs to get set to Checkpoint) // and the logMessage. The logMessage is scrapped since this ends up being taken over by // checkpoints for user input SOQL or Apex. -export async function sfdxToggleCheckpoint() { +export const sfdxToggleCheckpoint = async() => { if (creatingCheckpoints) { writeToDebuggerOutputWindow( nls.localize('checkpoint_upload_in_progress'), @@ -1017,29 +1017,29 @@ export async function sfdxToggleCheckpoint() { await vscode.debug.addBreakpoints(bpAdd); } return; -} +}; // This methods was broken out of sfdxToggleCheckpoint for testing purposes. -function fetchActiveEditorUri(): vscode.Uri | undefined { +const fetchActiveEditorUri = (): vscode.Uri | undefined => { const editor = vscode.window.activeTextEditor; if (editor) { return editor.document.uri; } -} +}; // This methods was broken out of sfdxToggleCheckpoint for testing purposes. -function fetchActiveSelectionLineNumber(): number | undefined { +const fetchActiveSelectionLineNumber = (): number | undefined => { const editor = vscode.window.activeTextEditor; if (editor && editor.selection) { return editor.selection.start.line; } return undefined; -} +}; -function fetchExistingBreakpointForUriAndLineNumber( +const fetchExistingBreakpointForUriAndLineNumber = ( uriInput: vscode.Uri, lineInput: number -): vscode.Breakpoint | undefined { +): vscode.Breakpoint | undefined => { for (const bp of vscode.debug.breakpoints) { if (bp instanceof vscode.SourceBreakpoint) { // Uri comparison doesn't work even if they're contain the same @@ -1053,10 +1053,10 @@ function fetchExistingBreakpointForUriAndLineNumber( } } return undefined; -} +}; // See https://github.com/Microsoft/vscode-languageserver-node/issues/105 -function code2ProtocolConverter(value: vscode.Uri) { +const code2ProtocolConverter = (value: vscode.Uri) => { if (/^win32/.test(process.platform)) { // The *first* : is also being encoded which is not the standard for URI on Windows // Here we transform it back to the standard way @@ -1064,7 +1064,7 @@ function code2ProtocolConverter(value: vscode.Uri) { } else { return value.toString(); } -} +}; export const checkpointUtils = { fetchActiveEditorUri, diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/launchFromLogFile.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/launchFromLogFile.ts index fe53819382..8e3f53d190 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/launchFromLogFile.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/launchFromLogFile.ts @@ -8,10 +8,10 @@ import * as vscode from 'vscode'; import { DebugConfigurationProvider } from '../adapter/debugConfigurationProvider'; -export function launchFromLogFile( +export const launchFromLogFile = ( logFile?: string, stopOnEntry: boolean = true -) { +) => { if ( !vscode.debug.activeDebugSession && vscode.workspace.workspaceFolders && @@ -22,4 +22,4 @@ export function launchFromLogFile( DebugConfigurationProvider.getConfig(logFile, stopOnEntry) ); } -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/quickLaunch.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/quickLaunch.ts index d57ab1c091..39448714d5 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/quickLaunch.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/commands/quickLaunch.ts @@ -164,14 +164,14 @@ export class TestDebuggerExecutor extends LibraryCommandletExecutor { } } -export async function setupAndDebugTests( +export const setupAndDebugTests = async ( className: string, methodName?: string -): Promise { +): Promise => { const executor = new TestDebuggerExecutor(); const response = { type: 'CONTINUE', data: [className, methodName] } as ContinueResponse; await executor.execute(response); -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/index.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/index.ts index 0f3a02ef79..87748cc455 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/index.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/index.ts @@ -48,14 +48,13 @@ const sfdxCoreExtension = vscode.extensions.getExtension( 'salesforce.salesforcedx-vscode-core' ); -function registerCommands(): vscode.Disposable { +const registerCommands = (): vscode.Disposable => { const dialogStartingPathUri = getDialogStartingPath(extContext); const promptForLogCmd = vscode.commands.registerCommand( 'extension.replay-debugger.getLogFileName', async () => { - const fileUris: - | vscode.Uri[] - | undefined = await vscode.window.showOpenDialog({ + const fileUris: vscode.Uri[] | undefined = + await vscode.window.showOpenDialog({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, @@ -97,9 +96,8 @@ function registerCommands(): vscode.Disposable { const launchFromLastLogFileCmd = vscode.commands.registerCommand( 'sfdx.launch.replay.debugger.last.logfile', () => { - const lastOpenedLog = extContext.workspaceState.get( - LAST_OPENED_LOG_KEY - ); + const lastOpenedLog = + extContext.workspaceState.get(LAST_OPENED_LOG_KEY); return launchFromLogFile(lastOpenedLog); } ); @@ -121,30 +119,30 @@ function registerCommands(): vscode.Disposable { sfdxCreateCheckpointsCmd, sfdxToggleCheckpointCmd ); -} +}; -export function updateLastOpened( +export const updateLastOpened = ( extensionContext: vscode.ExtensionContext, logPath: string -) { +) => { extensionContext.workspaceState.update(LAST_OPENED_LOG_KEY, logPath); extensionContext.workspaceState.update( LAST_OPENED_LOG_FOLDER_KEY, path.dirname(logPath) ); -} +}; -export async function getDebuggerType( +export const getDebuggerType = async ( session: vscode.DebugSession -): Promise { +): Promise => { let type = session.type; if (type === LIVESHARE_DEBUGGER_TYPE) { type = await session.customRequest(LIVESHARE_DEBUG_TYPE_REQUEST); } return type; -} +}; -function registerDebugHandlers(): vscode.Disposable { +const registerDebugHandlers = (): vscode.Disposable => { const customEventHandler = vscode.debug.onDidReceiveDebugSessionCustomEvent( async event => { if (event && event.session) { @@ -171,9 +169,9 @@ function registerDebugHandlers(): vscode.Disposable { ); return vscode.Disposable.from(customEventHandler); -} +}; -export async function activate(extensionContext: vscode.ExtensionContext) { +export const activate = async (extensionContext: vscode.ExtensionContext) => { console.log('Apex Replay Debugger Extension Activated'); const extensionHRStart = process.hrtime(); @@ -231,9 +229,9 @@ export async function activate(extensionContext: vscode.ExtensionContext) { } telemetryService.sendExtensionActivationEvent(extensionHRStart); -} +}; -export async function retrieveLineBreakpointInfo(): Promise { +export const retrieveLineBreakpointInfo = async (): Promise => { const sfdxApex = vscode.extensions.getExtension( 'salesforce.salesforcedx-vscode-apex' ); @@ -289,17 +287,17 @@ export async function retrieveLineBreakpointInfo(): Promise { writeToDebuggerOutputWindow(errorMessage, true, VSCodeWindowTypeEnum.Error); return false; } -} +}; -function imposeSlightDelay(ms = 0) { +const imposeSlightDelay = (ms = 0) => { return new Promise(r => setTimeout(r, ms)); -} +}; -export function writeToDebuggerOutputWindow( +export const writeToDebuggerOutputWindow = ( output: string, showVSCodeWindow?: boolean, vsCodeWindowType?: VSCodeWindowTypeEnum -) { +) => { channelService.appendLine(output); channelService.showChannelOutput(); if (showVSCodeWindow && vsCodeWindowType) { @@ -318,9 +316,9 @@ export function writeToDebuggerOutputWindow( } } } -} +}; -export function deactivate() { +export const deactivate = () => { console.log('Apex Replay Debugger Extension Deactivated'); telemetryService.sendExtensionDeactivationEvent(); -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/messages/index.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/messages/index.ts index 3b1b069db8..6c7ae9dc7c 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/messages/index.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/messages/index.ts @@ -18,7 +18,7 @@ import { messages as jaMessages } from './i18n.ja'; const supportedLocales = [DEFAULT_LOCALE, LOCALE_JA]; -function loadMessageBundle(config?: Config): Message { +const loadMessageBundle = (config?: Config): Message => { const base = new Message(enMessages); const localeConfig = config ? config.locale : DEFAULT_LOCALE; @@ -32,7 +32,7 @@ function loadMessageBundle(config?: Config): Message { } return base; -} +}; export const nls = new Localization( loadMessageBundle( diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/src/utils/settings.ts b/packages/salesforcedx-vscode-apex-replay-debugger/src/utils/settings.ts index 622040529d..ba443ec052 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/src/utils/settings.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/src/utils/settings.ts @@ -8,8 +8,8 @@ import { SFDX_CORE_CONFIGURATION_NAME } from '@salesforce/salesforcedx-utils-vscode'; import * as vscode from 'vscode'; -export function retrieveTestCodeCoverage(): boolean { +export const retrieveTestCodeCoverage = (): boolean => { return vscode.workspace .getConfiguration(SFDX_CORE_CONFIGURATION_NAME) .get('retrieve-test-code-coverage', false); -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/adapter/MockApexExtension.ts b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/adapter/MockApexExtension.ts index 8d0c7b7030..46251ced86 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/adapter/MockApexExtension.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/adapter/MockApexExtension.ts @@ -39,15 +39,15 @@ class MockJorje { } public languageClientUtils = { - getStatus() { + getStatus: () => { return { - isReady() { + isReady: () => { return true; }, - failedToInitialize() { + failedToInitialize: () => { return false; }, - getStatusMessage() { + getStatusMessage: () => { return ''; } }; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/checkpoints/checkpointService.test.ts b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/checkpoints/checkpointService.test.ts index 064c9641d5..955d187755 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/checkpoints/checkpointService.test.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/checkpoints/checkpointService.test.ts @@ -680,10 +680,10 @@ describe('Verify SFDX Toggle Checkpoint callback, sfdxToggleCheckpoint', () => { }); }); -function breakpointsHaveSameUriAndSourceLine( +const breakpointsHaveSameUriAndSourceLine = ( bp1: vscode.Breakpoint, bp2: vscode.Breakpoint -): boolean { +): boolean => { // both breakpoints are source breakpoints if ( bp1 instanceof vscode.SourceBreakpoint && @@ -698,15 +698,15 @@ function breakpointsHaveSameUriAndSourceLine( } } return false; -} +}; -async function clearExistingBreakpoints() { - await vscode.debug.removeBreakpoints(vscode.debug.breakpoints); -} +const clearExistingBreakpoints = () => { + vscode.debug.removeBreakpoints(vscode.debug.breakpoints); +}; // Clean out the checkpoints from the checkpointService (has the added bonus of beating // on deleteCheckpointNode) -function clearOutCheckpoints() { +const clearOutCheckpoints = () => { for (const checkpoint of checkpointService.getChildren()) { // While every child here is a CheckpointNode, getChildren returns an // array of BaseNode and if we want to get at the methods on an actual @@ -717,4 +717,4 @@ function clearOutCheckpoints() { ); } } -} +}; diff --git a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/commands/apexExecutionOverlayActionCommand.test.ts b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/commands/apexExecutionOverlayActionCommand.test.ts index 805e58e92a..afd8b0ef85 100644 --- a/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/commands/apexExecutionOverlayActionCommand.test.ts +++ b/packages/salesforcedx-vscode-apex-replay-debugger/test/vscode-integration/commands/apexExecutionOverlayActionCommand.test.ts @@ -174,11 +174,11 @@ describe('ApexExecutionOverlayAction command', () => { }); // Support function to create an XHROptions object to verify call args against -export function createExpectedXHROptions( +export const createExpectedXHROptions = ( requestBody: string | undefined, requestUrl: string, restHttpMethodEnum: RestHttpMethodEnum -): XHROptions { +): XHROptions => { return { type: restHttpMethodEnum, url: requestUrl, @@ -194,4 +194,4 @@ export function createExpectedXHROptions( }, data: requestBody } as XHROptions; -} +};