ESLint Customization (Retired)
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 runcommand. -
Your custom configuration cascades into your other project configurations as though you ran ESLint directly with the
-c | –configflag. Your custom configuration must perform all necessary overrides. -
Your
.eslintrc.jsonfile must be in.eslintrcformat. However, there are no restrictions on the filename. -
The
.eslintignoreflag isn’t evaluated. Instead, use the--targetflag to define the targets that you want Code Analyzer to scan.--targetaccepts a comma-separated list of any combination of files, directories, positive patterns, and negations. -
Any
package.jsonfiles with embedded ESLint configuration aren’t supported. -
If your configuration executes Typescript, make sure that your
tsconfigfile is added to the configuration underparserOptions.project. The--tsconfigflag can’t be used with--eslintconfigflag. For example:
ESLint usage in Code Analyzer has these restrictions.
-
When
--eslintconfigis provided, no other default ESLint engines run in Code Analyzer -
Rule filters such as
--categoryand--rulesetaren’t evaluated on rules selected from an ESLint custom configuration -
Engine filters can be used with
--eslintconfigflag. However, only the rules included in the config execute. PMD runs with the default set of rules, buteslint-lwcisn’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:
- ESLint: Cascading and Hierarchy
- ESLint: Configure ESLint
- ESLint: Custom Rules
- ESLint: Create Plugins