To bring in the power of any ESLint capabilities that aren’t supported by default in Salesforce Code Analyzer, use your own customized .eslintrc.json. You have the flexibility to use different parsers or plug-ins, and you can define your own custom ESLint ruleset.
To invoke a customized ESLint file, pass in your config file filepath as a value to --eslintconfig flag in scanner run command. For example:
1`$ sf scanner run --target /path/to/your/target --eslintconfig /path/to/.eslintrc.json`
Your ESLint Custom Configuration Responsibilities
Before setting up your custom configuration with ESLint through Code Analyzer, make sure that you understand your responsibilities.
All npm dependencies, including ESLint, must be installed in the directory where you run the scanner run command.
Your custom configuration cascades into your other project configurations as though you ran ESLint directly with the -c | –config flag. Your custom configuration must perform all necessary overrides.
Your .eslintrc.json file must be in .eslintrc format. However, there are no restrictions on the filename.
The .eslintignore flag isn’t evaluated. Instead, use the --target flag to define the targets that you want Code Analyzer to scan. --target accepts a comma-separated list of any combination of files, directories, positive patterns, and negations.
Any package.json files with embedded ESLint configuration aren’t supported.
If your configuration executes Typescript, make sure that your tsconfig file is added to the configuration under parserOptions.project. The --tsconfig flag can’t be used with --eslintconfig flag. For example:
ESLint usage in Code Analyzer has these restrictions.
When --eslintconfig is provided, no other default ESLint engines run in Code Analyzer
Rule filters such as --category and --ruleset aren’t evaluated on rules selected from an ESLint custom configuration
Engine filters can be used with --eslintconfig flag. However, only the rules included in the config execute. PMD runs with the default set of rules, but eslint-lwc isn’t invoked. Instead, ESLint is invoked based on the configuration in /path/to/.eslintrc.json. For example:
1$ sf scanner run --engine pmd,eslint-lwc --eslintconfig /path/to/.eslintrc.json --target /target/path
Authoring Custom ESLint Rules
Writing custom ESLint rules requires creating a custom ESLint plug-in and defining rules within it.ESLint rule definitions must contain documentation. Format your documentation like this:
1// Rule definition in index.js or where you choose to store it2 meta: {3 docs: {4 description: "Information about the rule"5 },6 ...7 }
After your rule is ready and tested, add it as a dependency to the npm setup in the same directory where Code Analyzer runs. Use the npm or yarn version of this command.
Make sure that your directory has all the required npm dependencies, including ESLint, your custom rule plug-in, and any other applicable dependencies, such as parsers or processors.
Run Rules
Execute the scanner run command in the directory with all the necessary npm dependencies. For example:
1$ sf scanner run --eslintconfig /path/to/my/.eslintrc.json --target /path/to/target