Skip to content

Commit

Permalink
chore: vscode lwc to arrow functions (#5283)
Browse files Browse the repository at this point in the history
@W-14564471@
Refactor vscode lwc to use arrow functions
  • Loading branch information
peternhale authored Dec 21, 2023
1 parent 4513562 commit d25810a
Show file tree
Hide file tree
Showing 29 changed files with 163 additions and 162 deletions.
1 change: 1 addition & 0 deletions packages/salesforcedx-vscode-lwc/fix-these-rules.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"prefer-arrow/prefer-arrow-functions": ["error", {}],
"@typescript-eslint/no-floating-promises": "warn",
"@typescript-eslint/no-misused-promises": "warn",
"@typescript-eslint/no-unsafe-argument": "warn",
Expand Down
8 changes: 4 additions & 4 deletions packages/salesforcedx-vscode-lwc/src/commands/commandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { channelService } from '../channel';
import { nls } from '../messages';
import { telemetryService } from '../telemetry';

export function showError(e: Error, logName: string, commandName: string) {
export const showError = (e: Error, logName: string, commandName: string) => {
telemetryService.sendException(`${logName}_error`, e.message);
notificationService.showErrorMessage(e.message);
notificationService.showErrorMessage(
nls.localize('command_failure', commandName)
);
channelService.appendLine(`Error: ${e.message}`);
channelService.showChannelOutput();
}
};

export function openBrowser(url: string) {
export const openBrowser = (url: string) => {
return env.openExternal(Uri.parse(url));
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { openBrowser, showError } from './commandUtils';
const logName = 'force_lightning_lwc_open';
const commandName = nls.localize('force_lightning_lwc_open_text');

export async function forceLightningLwcOpen() {
export const forceLightningLwcOpen = async () => {
const startTime = process.hrtime();

if (DevServerService.instance.isServerHandlerRegistered()) {
Expand All @@ -29,4 +29,4 @@ export async function forceLightningLwcOpen() {
await vscode.commands.executeCommand('sfdx.force.lightning.lwc.start');
telemetryService.sendCommandEvent(logName, startTime);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,26 @@ const sfdxDeviceListCommand = 'force:lightning:local:device:list';
const sfdxMobilePreviewCommand = 'force:lightning:lwc:preview';
const androidSuccessString = 'Launching... Opening Browser';

export async function forceLightningLwcPreview(sourceUri: vscode.Uri) {
export const forceLightningLwcPreview = async (sourceUri: vscode.Uri) => {
const preview = getPreview();
preview(sourceUri);
}
};

export function getPreview() {
export const getPreview = () => {
if (isSFContainerMode()) {
return lwcPreviewContainerMode;
} else {
return lwcPreview;
}
}
};

function lwcPreviewContainerMode() {
const lwcPreviewContainerMode = () => {
const message = nls.localize('force_lightning_lwc_preview_container_mode');
vscode.window.showErrorMessage(message);
return;
}
};

export async function lwcPreview(sourceUri: vscode.Uri) {
export const lwcPreview = async (sourceUri: vscode.Uri) => {
const startTime = process.hrtime();

if (!sourceUri) {
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function lwcPreview(sourceUri: vscode.Uri) {
}

await executePreview(startTime, componentName, resourcePath);
}
};

/**
* Performs the action of previewing the LWC. It takes care of prompting the user
Expand All @@ -189,11 +189,11 @@ export async function lwcPreview(sourceUri: vscode.Uri) {
* @param componentName name of the lwc
* @param resourcePath path to the lwc
*/
async function executePreview(
const executePreview = async (
startTime: [number, number],
componentName: string,
resourcePath: string
) {
) => {
const commandCancelledMessage = nls.localize(
'force_lightning_lwc_operation_cancelled'
);
Expand Down Expand Up @@ -249,7 +249,7 @@ async function executePreview(
componentName,
startTime
);
}
};

/**
* Starts the lwc server if it is not already running.
Expand All @@ -258,11 +258,11 @@ async function executePreview(
* @param componentName name of the component to preview
* @param startTime start time of the preview command
*/
async function startServer(
const startServer = async (
isDesktop: boolean,
componentName: string,
startTime: [number, number]
) {
) => {
if (!DevServerService.instance.isServerHandlerRegistered()) {
console.log(`${logName}: server was not running, starting...`);
const preconditionChecker = new SfdxWorkspaceChecker();
Expand Down Expand Up @@ -291,29 +291,29 @@ async function startServer(
showError(e, logName, commandName);
}
}
}
};

/**
* Prompts the user to select a platform to preview the LWC on.
* @returns the selected platform or undefined if no selection was made.
*/
async function selectPlatform(): Promise<PreviewQuickPickItem | undefined> {
const selectPlatform = async (): Promise<PreviewQuickPickItem | undefined> => {
const platformSelection = await vscode.window.showQuickPick(platformOptions, {
placeHolder: nls.localize('force_lightning_lwc_platform_selection')
});

return platformSelection;
}
};

/**
* Prompts the user to select a device to preview the LWC on.
*
* @param platformSelection the selected platform
* @returns the name of the selected device or undefined if no selection was made.
*/
async function selectTargetDevice(
const selectTargetDevice = async (
platformSelection: PreviewQuickPickItem
): Promise<string | undefined> {
): Promise<string | undefined> => {
const isAndroid = platformSelection.id === PreviewPlatformType.Android;
const lastTarget = PreviewService.instance.getRememberedDevice(
platformSelection.platformName
Expand Down Expand Up @@ -453,7 +453,7 @@ async function selectTargetDevice(
}

return target;
}
};

/**
* Prompts the user to select an app to preview the LWC on. Defaults to browser.
Expand All @@ -462,10 +462,10 @@ async function selectTargetDevice(
* @param configFile path to a config file
* @returns the name of the selected device or undefined if user cancels selection.
*/
async function selectTargetApp(
const selectTargetApp = async (
platformSelection: PreviewQuickPickItem,
configFile: string | undefined
): Promise<string | undefined> {
): Promise<string | undefined> => {
let targetApp: string | undefined = 'browser';
const items: vscode.QuickPickItem[] = [];
const browserItem: vscode.QuickPickItem = {
Expand Down Expand Up @@ -517,7 +517,7 @@ async function selectTargetApp(
}

return targetApp;
}
};

/**
* Prompts the user to select a device to preview the LWC on.
Expand All @@ -530,15 +530,15 @@ async function selectTargetApp(
* @param componentName name of the component to preview
* @param startTime start time of the preview command
*/
async function executeMobilePreview(
const executeMobilePreview = async (
platformSelection: PreviewQuickPickItem,
targetDevice: string,
targetApp: string,
projectDir: string | undefined,
configFile: string | undefined,
componentName: string,
startTime: [number, number]
) {
) => {
const isAndroid = platformSelection.id === PreviewPlatformType.Android;

let commandBuilder = new SfdxCommandBuilder()
Expand Down Expand Up @@ -607,7 +607,7 @@ async function executeMobilePreview(
}
});
}
}
};

/**
* Given a path, it recursively goes through that directory and upwards, until it finds
Expand All @@ -616,7 +616,7 @@ async function executeMobilePreview(
* @param startPath starting path to search for the config file.
* @returns the path to the folder containing the config file, or undefined if config file not found
*/
export function getProjectRootDirectory(startPath: string): string | undefined {
export const getProjectRootDirectory = (startPath: string): string | undefined => {
if (!fs.existsSync(startPath)) {
return undefined;
}
Expand All @@ -636,15 +636,15 @@ export function getProjectRootDirectory(startPath: string): string | undefined {

// couldn't determine the root dir
return undefined;
}
};

/**
* Given a path to a directory, returns a path that is one level up.
*
* @param directory path to a directory
* @returns path to a directory that is one level up, or undefined if cannot go one level up.
*/
export function directoryLevelUp(directory: string): string | undefined {
export const directoryLevelUp = (directory: string): string | undefined => {
const levelUp = path.dirname(directory);

if (levelUp === directory) {
Expand All @@ -653,4 +653,4 @@ export function directoryLevelUp(directory: string): string | undefined {
}

return levelUp;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class ForceLightningLwcStartExecutor extends SfdxCommandletExecutor<{}> {
}
}

export async function forceLightningLwcStart() {
export const forceLightningLwcStart = async () => {
if (DevServerService.instance.isServerHandlerRegistered()) {
const warningMessage = nls.localize(
'force_lightning_lwc_start_already_running'
Expand Down Expand Up @@ -248,4 +248,4 @@ export async function forceLightningLwcStart() {
);

await commandlet.run();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { showError } from './commandUtils';
const logName = 'force_lightning_lwc_stop';
const commandName = nls.localize('force_lightning_lwc_stop_text');

export async function forceLightningLwcStop() {
export const forceLightningLwcStop = async () => {
const startTime = process.hrtime();

try {
Expand All @@ -39,4 +39,4 @@ export async function forceLightningLwcStop() {
} catch (e) {
showError(e, logName, commandName);
}
}
};
16 changes: 8 additions & 8 deletions packages/salesforcedx-vscode-lwc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from './testSupport';
import { WorkspaceUtils } from './util/workspaceUtils';

export async function activate(extensionContext: ExtensionContext) {
export const activate = async (extensionContext: ExtensionContext) => {
const extensionHRStart = process.hrtime();
log('Activation Mode: ' + getActivationMode());
// Run our auto detection routine before we activate
Expand Down Expand Up @@ -125,23 +125,23 @@ export async function activate(extensionContext: ExtensionContext) {

// Notify telemetry that our extension is now active
telemetryService.sendExtensionActivationEvent(extensionHRStart);
}
};

export async function deactivate() {
export const deactivate = async () => {
if (DevServerService.instance.isServerHandlerRegistered()) {
await DevServerService.instance.stopServer();
}
log('Lightning Web Components Extension Deactivated');
telemetryService.sendExtensionDeactivationEvent();
}
};

function getActivationMode(): string {
const getActivationMode = (): string => {
const config = workspace.getConfiguration('salesforcedx-vscode-lightning');
return config.get('activationMode') || 'autodetect'; // default to autodetect
}
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function registerCommands(_extensionContext: ExtensionContext): Disposable {
const registerCommands = (_extensionContext: ExtensionContext): Disposable => {
return Disposable.from(
commands.registerCommand(
'sfdx.force.lightning.lwc.start',
Expand All @@ -160,4 +160,4 @@ function registerCommands(_extensionContext: ExtensionContext): Disposable {
forceLightningLwcPreview
)
);
}
};
12 changes: 6 additions & 6 deletions packages/salesforcedx-vscode-lwc/src/languageClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import {
} from 'vscode-languageclient';

// See https://github.com/Microsoft/vscode-languageserver-node/issues/105
export function code2ProtocolConverter(value: Uri) {
export const code2ProtocolConverter = (value: 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
return value.toString().replace('%3A', ':');
} else {
return value.toString();
}
}
};

function protocol2CodeConverter(value: string) {
const protocol2CodeConverter = (value: string) => {
return Uri.parse(value);
}
};

export function createLanguageClient(serverPath: string): LanguageClient {
export const createLanguageClient = (serverPath: string): LanguageClient => {
// Setup the language server
const debugOptions = { execArgv: ['--nolazy', '--inspect=6030'] };
// If the extension is launched in debug mode then the debug server options are used
Expand Down Expand Up @@ -75,4 +75,4 @@ export function createLanguageClient(serverPath: string): LanguageClient {
serverOptions,
clientOptions
);
}
};
4 changes: 2 additions & 2 deletions packages/salesforcedx-vscode-lwc/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { messages as enMessages } from './i18n';
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;
Expand All @@ -31,7 +31,7 @@ function loadMessageBundle(config?: Config): Message {
}

return base;
}
};

export const nls = new Localization(
loadMessageBundle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export const lwcTestCodeLensProvider = new LwcTestCodeLensProvider();
* Register Code Lens Provider with the extension context
* @param extensionContext Extension context
*/
export function registerLwcTestCodeLensProvider(extensionContext: ExtensionContext) {
export const registerLwcTestCodeLensProvider = (extensionContext: ExtensionContext) => {
extensionContext.subscriptions.push(
languages.registerCodeLensProvider(
LWC_TEST_DOCUMENT_SELECTOR,
lwcTestCodeLensProvider
)
);
}
};
Loading

0 comments on commit d25810a

Please sign in to comment.