diff --git a/package.json b/package.json index 083dd65b..20c6ee96 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 65b976a7..aef06be2 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 e7c7e89a..34f3fde6 100644 --- a/src/commands/lint/configureLintingToolsCommand.ts +++ b/src/commands/lint/configureLintingToolsCommand.ts @@ -11,29 +11,48 @@ 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 packageConfigPropertyId: string; + readonly eslintConfigToExtend: string; + + constructor( + name: string, + packageConfigPropertyId: string, + eslintConfigToExtend: string + ) { + this.name = name; + this.packageConfigPropertyId = packageConfigPropertyId; + this.eslintConfigToExtend = eslintConfigToExtend; + } + + getVersion(): string { + return config.get(this.packageConfigPropertyId) 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; @@ -64,7 +83,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 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 ); @@ -135,10 +154,10 @@ export class ConfigureLintingToolsCommand { let modified = false; if (devDependencies) { - eslintDependencies.forEach((nameValuePair) => { - const [name, value] = nameValuePair; + eslintDependencies.forEach((dependencyConfig) => { + const { name } = dependencyConfig; if (!devDependencies[name]) { - devDependencies[name] = value; + devDependencies[name] = dependencyConfig.getVersion(); modified = true; } }); @@ -170,15 +189,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.eslintConfigToExtend)) { + eslintrcExtends.push(config.eslintConfigToExtend); + modified = true; + } + }); if (modified) { // Save json only if the content was modified. @@ -192,11 +208,11 @@ export class ConfigureLintingToolsCommand { } else { // Create eslintrc const eslintrc = { - extends: [ - `${eslintRecommended}`, - `${lwcGraphAnalyzerRecommended}` - ] + extends: eslintDependencies.map((config) => { + return `${config.eslintConfigToExtend}`; + }) }; + 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 0a3e0f2f..7927a663 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' }