ESLint Customization
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:
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 underparserOptions.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, buteslint-lwc
isn’t invoked. Instead, ESLint is invoked based on the configuration in/path/to/.eslintrc.json
. For example:
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:
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.
After your rule is added, ensure that the node_modules
directory:
- Has a child directory with your plug-in’s name
- Contains the
package.json
,index.js
, and other files that you created
Custom rules on ESLint are handled through Code Analyzer’s ESLint Custom Configuration.
To add your custom rules to a custom .eslintrc.json
file, follow these steps.
- Add your plug-in:
- Add your rule:
- 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.
Execute the scanner run
command in the directory with all the necessary npm dependencies. For example: