From 86f03e90b8d7d93595cab2d3824b3972cd1d36f3 Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Mon, 24 Jun 2024 17:38:26 -0700 Subject: [PATCH 1/3] feat: config lwc mobile eslint plugin --- package.json | 5 ++ package.nls.json | 1 + .../lint/configureLintingToolsCommand.ts | 86 +++++++++++-------- .../lint/configureLintingToolsCommand.test.ts | 2 + 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 083dd65..20c6ee9 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,11 @@ "configuration": { "title": "%salesforce.mobile.extensions%", "properties": { + "mobileOfflineLinting.eslint-plugin-lwc-mobile": { + "type": "string", + "default": "^1.0.0", + "description": "%extension.commands.salesforce-mobile-offline.lwc-mobile.version%" + }, "mobileOfflineLinting.eslint-plugin-lwc-graph-analyzer": { "type": "string", "default": "^0.9.0", diff --git a/package.nls.json b/package.nls.json index 65b976a..aef06be 100644 --- a/package.nls.json +++ b/package.nls.json @@ -3,6 +3,7 @@ "extension.commands.config-wizard.category": "Offline Starter Kit", "extension.commands.config-linting-tools.title": "Configure Linting Tools", "extension.commands.salesforce-mobile-offline.category": "Salesforce Mobile Offline", + "extension.commands.salesforce-mobile-offline.lwc-mobile.version": "Version of ESLint Plugin LWC Mobile to include in devDependencies", "extension.commands.salesforce-mobile-offline.komaci.version": "Version of ESLint Plugin LWC Graph Analyzer to include in devDependencies", "extension.commands.salesforce-mobile-offline.eslint.version": "Version of ESLint to include in devDependencies", "salesforce.mobile.extensions": "Salesforce Mobile Extensions", diff --git a/src/commands/lint/configureLintingToolsCommand.ts b/src/commands/lint/configureLintingToolsCommand.ts index e7c7e89..09cb226 100644 --- a/src/commands/lint/configureLintingToolsCommand.ts +++ b/src/commands/lint/configureLintingToolsCommand.ts @@ -11,29 +11,46 @@ import * as path from 'path'; import { WorkspaceUtils } from '../../utils/workspaceUtils'; import { JSON_INDENTATION_SPACES } from '../../utils/constants'; -const config = workspace.getConfiguration(); -const eslintPluginLwcGraphAnalyzer = - '@salesforce/eslint-plugin-lwc-graph-analyzer'; -const eslintPluginLwcGraphAnalyzerConfig = - 'mobileOfflineLinting.eslint-plugin-lwc-graph-analyzer'; -const eslintPluginLwcGraphAnalyzerVersion = config.get( - eslintPluginLwcGraphAnalyzerConfig -) as string; - -const eslint = 'eslint'; -const eslintConfig = 'mobileOfflineLinting.eslint'; -const eslintVersion = config.get(eslintConfig) as string; - const configureLintingToolsCommand = 'salesforcedx-vscode-offline-app.configureLintingTools'; -const eslintDependencies = [ - [eslintPluginLwcGraphAnalyzer, eslintPluginLwcGraphAnalyzerVersion], - [eslint, eslintVersion] -]; -const lwcGraphAnalyzerRecommended: string = - 'plugin:@salesforce/lwc-graph-analyzer/recommended'; -const eslintRecommended = 'eslint:recommended'; +const config = workspace.getConfiguration(); + +class EslintDependencyConfig { + readonly name: string; + readonly packageJsonPropertyId: string; + readonly version: string; + readonly eslintrcExtend: string; + + constructor( + name: string, + packageJsonPropertyId: string, + eslintrcExtend: string + ) { + this.name = name; + this.packageJsonPropertyId = packageJsonPropertyId; + this.eslintrcExtend = eslintrcExtend; + this.version = config.get(this.packageJsonPropertyId) as string; + } +} + +const eslintDependencies: EslintDependencyConfig[] = [ + new EslintDependencyConfig( + '@salesforce/eslint-plugin-lwc-mobile', + 'mobileOfflineLinting.eslint-plugin-lwc-mobile', + 'plugin:@salesforce/lwc-mobile/recommended' + ), + new EslintDependencyConfig( + '@salesforce/eslint-plugin-lwc-graph-analyzer', + 'mobileOfflineLinting.eslint-plugin-lwc-graph-analyzer', + 'plugin:@salesforce/lwc-graph-analyzer/recommended' + ), + new EslintDependencyConfig( + 'eslint', + 'mobileOfflineLinting.eslint', + 'eslint:recommended' + ) +]; interface PackageJson { devDependencies?: Record; @@ -135,10 +152,10 @@ export class ConfigureLintingToolsCommand { let modified = false; if (devDependencies) { - eslintDependencies.forEach((nameValuePair) => { - const [name, value] = nameValuePair; + eslintDependencies.forEach((dependencyConfig) => { + const { name, version } = dependencyConfig; if (!devDependencies[name]) { - devDependencies[name] = value; + devDependencies[name] = version; modified = true; } }); @@ -170,15 +187,12 @@ export class ConfigureLintingToolsCommand { let modified = false; - if (!eslintrcExtends.includes(eslintRecommended)) { - eslintrcExtends.push(eslintRecommended); - modified = true; - } - - if (!eslintrcExtends.includes(lwcGraphAnalyzerRecommended)) { - eslintrc.extends.push(lwcGraphAnalyzerRecommended); - modified = true; - } + eslintDependencies.forEach((config) => { + if (!eslintrcExtends.includes(config.eslintrcExtend)) { + eslintrcExtends.push(config.eslintrcExtend); + modified = true; + } + }); if (modified) { // Save json only if the content was modified. @@ -192,11 +206,11 @@ export class ConfigureLintingToolsCommand { } else { // Create eslintrc const eslintrc = { - extends: [ - `${eslintRecommended}`, - `${lwcGraphAnalyzerRecommended}` - ] + extends: eslintDependencies.map((config) => { + return `${config.eslintrcExtend}`; + }) }; + const jsonString = JSON.stringify( eslintrc, null, diff --git a/src/test/suite/commands/lint/configureLintingToolsCommand.test.ts b/src/test/suite/commands/lint/configureLintingToolsCommand.test.ts index 0a3e0f2..7927a66 100644 --- a/src/test/suite/commands/lint/configureLintingToolsCommand.test.ts +++ b/src/test/suite/commands/lint/configureLintingToolsCommand.test.ts @@ -147,6 +147,8 @@ suite('Configure Linting Tools Command Test Suite', () => { devDependencies: { lwc: '1.2.3', // eslint-disable-next-line @typescript-eslint/naming-convention + '@salesforce/eslint-plugin-lwc-mobile': '^1.0.0', + // eslint-disable-next-line @typescript-eslint/naming-convention '@salesforce/eslint-plugin-lwc-graph-analyzer': '^0.9.0', eslint: '^8.47.0' } From 7efca937c4321c1b6b63b47829cd99b0e9a73556 Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Mon, 24 Jun 2024 17:47:15 -0700 Subject: [PATCH 2/3] fix: touch up wording --- src/commands/lint/configureLintingToolsCommand.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/lint/configureLintingToolsCommand.ts b/src/commands/lint/configureLintingToolsCommand.ts index 09cb226..083c0da 100644 --- a/src/commands/lint/configureLintingToolsCommand.ts +++ b/src/commands/lint/configureLintingToolsCommand.ts @@ -81,7 +81,7 @@ export class ConfigureLintingToolsCommand { // Ask user to add eslint plugin const result = await this.showMessage( - 'Do you want to add the ESLint plugin for LWC graph analysis to your project? This will give you linting feedback on code patterns that will not support your LWCs working offline, for mobile use cases.', + 'Do you want to add the ESLint plugins related to LWC offline capabilities to your project? This will give you linting feedback on code patterns that will not support your LWCs working offline, for mobile use cases.', MessageType.InformationYesNo ); From 6ac6338099f91e478888fba55f1b05e9e778c958 Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Tue, 25 Jun 2024 16:01:15 -0700 Subject: [PATCH 3/3] code tweak for pr comments --- .../lint/configureLintingToolsCommand.ts | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/commands/lint/configureLintingToolsCommand.ts b/src/commands/lint/configureLintingToolsCommand.ts index 083c0da..34f3fde 100644 --- a/src/commands/lint/configureLintingToolsCommand.ts +++ b/src/commands/lint/configureLintingToolsCommand.ts @@ -18,19 +18,21 @@ const config = workspace.getConfiguration(); class EslintDependencyConfig { readonly name: string; - readonly packageJsonPropertyId: string; - readonly version: string; - readonly eslintrcExtend: string; + readonly packageConfigPropertyId: string; + readonly eslintConfigToExtend: string; constructor( name: string, - packageJsonPropertyId: string, - eslintrcExtend: string + packageConfigPropertyId: string, + eslintConfigToExtend: string ) { this.name = name; - this.packageJsonPropertyId = packageJsonPropertyId; - this.eslintrcExtend = eslintrcExtend; - this.version = config.get(this.packageJsonPropertyId) as string; + this.packageConfigPropertyId = packageConfigPropertyId; + this.eslintConfigToExtend = eslintConfigToExtend; + } + + getVersion(): string { + return config.get(this.packageConfigPropertyId) as string; } } @@ -81,7 +83,7 @@ export class ConfigureLintingToolsCommand { // Ask user to add eslint plugin const result = await this.showMessage( - 'Do you want to add the ESLint plugins related to LWC offline capabilities to your project? This will give you linting feedback on code patterns that will not support your LWCs working offline, for mobile use cases.', + 'Do you want to add Salesforce code linting guidance for Mobile and Offline capabilities? These tools will identify code patterns that cause problems in Mobile and Offline use cases.', MessageType.InformationYesNo ); @@ -153,9 +155,9 @@ export class ConfigureLintingToolsCommand { if (devDependencies) { eslintDependencies.forEach((dependencyConfig) => { - const { name, version } = dependencyConfig; + const { name } = dependencyConfig; if (!devDependencies[name]) { - devDependencies[name] = version; + devDependencies[name] = dependencyConfig.getVersion(); modified = true; } }); @@ -188,8 +190,8 @@ export class ConfigureLintingToolsCommand { let modified = false; eslintDependencies.forEach((config) => { - if (!eslintrcExtends.includes(config.eslintrcExtend)) { - eslintrcExtends.push(config.eslintrcExtend); + if (!eslintrcExtends.includes(config.eslintConfigToExtend)) { + eslintrcExtends.push(config.eslintConfigToExtend); modified = true; } }); @@ -207,7 +209,7 @@ export class ConfigureLintingToolsCommand { // Create eslintrc const eslintrc = { extends: eslintDependencies.map((config) => { - return `${config.eslintrcExtend}`; + return `${config.eslintConfigToExtend}`; }) };