Skip to content

Commit

Permalink
Merge pull request #68 from ben-zhang-at-salesforce/bundleInLwcMobile…
Browse files Browse the repository at this point in the history
…Eslint

feat: config lwc mobile eslint plugin
  • Loading branch information
ben-zhang-at-salesforce committed Jun 28, 2024
2 parents 47eae88 + 6ac6338 commit 0b07787
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
90 changes: 53 additions & 37 deletions src/commands/lint/configureLintingToolsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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;
}
});
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down

0 comments on commit 0b07787

Please sign in to comment.