Note! These instructions are for Salesforce Code Scanner 4.x. The rule configurations will change in 5.x
For running specific Lightning Web Component ESLint rules (or vanilla ESLint rules) with Salesforce Code Scanner you need to provide an eslint config file where the rules are defined (eslint-lwc_ruleset.json) and the engine list needs to include the eslint-lwc engine. You can also run for example PMD rules at the same time (and provide a config for defining the rules)
sf scanner run --target src --engine eslint-lwc --eslintconfig eslint-lwc_ruleset.json
Install the required dependencies with
npm install eslint @babel/core @babel/eslint-parser @lwc/eslint-plugin-lwc --save-dev
ESLint config file
"extends": "eslint:recommended" – only the recommened vanilla ESLint rules are used, see list here: https://eslint.org/docs/latest/rules/. It is also possible to define the rules one-by-one instead of using the recommended flag
"files": ["lwc/*/.js"] – to run the rules only for Lightning Web Component in the lwc folder provided the target folder is the parent folder. This need to be inside the overrides array. Note! If the rules are run inside aura folder it gives false positives
"parser": "@babel/eslint-parser" – set the parser field to @babel/eslint-parser and install the parser as well with npm. The out-of-the-box parser from ESLint doesn’t support the ES stage 4 syntax yet.
"rules" – the rules can take 3 values: off, warn or error. The further customize the rules see https://eslint.org/docs/latest/use/configure/rules
"ignorePatterns" – ignore running scanning inside aura folder
Example eslint config file:
{
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false,
"babelOptions": {
"parserOpts": {
"plugins": ["classProperties", ["decorators", { "decoratorsBeforeExport": false }]]
}
}
},
"overrides": [
{
"files": ["src/lwc/**/*.js"],
"rules": {
"@lwc/lwc/no-api-reassignments": "error",
"@lwc/lwc/no-deprecated": "error",
"@lwc/lwc/no-document-query": "error",
"@lwc/lwc/no-attributes-during-construction": "error",
"@lwc/lwc/no-disallowed-lwc-imports": "error",
"@lwc/lwc/no-leading-uppercase-api-name": "error",
"@lwc/lwc/valid-api": "error",
"@lwc/lwc/valid-track": "error",
"@lwc/lwc/valid-wire:": "error",
"@lwc/lwc/no-restricted-browser-globals-during-ssr": "error",
"@lwc/lwc/no-unsupported-ssr-properties": "error",
"@lwc/lwc/no-node-env-in-ssr": "error",
"@lwc/lwc/valid-graphql-wire-adapter-callback-parameters": "error",
"@lwc/lwc/no-host-mutation-in-connected-callback": "error",
"@lwc/lwc/consistent-component-name": "error",
"@lwc/lwc/no-async-operation": "error",
"@lwc/lwc/no-dupe-class-members": "error",
"@lwc/lwc/no-inner-html": "error",
"@lwc/lwc/no-template-children": "error",
"@lwc/lwc/no-leaky-event-listeners": "error",
"@lwc/lwc/prefer-custom-event": "error",
"@lwc/lwc/no-async-await": "off",
"@lwc/lwc/no-rest-parameter": "off",
"@lwc/lwc/lwc/no-for-of": "off",
"@lwc/lwc/ssr/no-unsupported-node-api": "error",
"@lwc/lwc/ssr/no-unsupported-properties": "error",
"@lwc/lwc/ssr/no-restricted-browser-globals": "error",
"@lwc/lwc/ssr/no-node-env": "error",
"@lwc/lwc/ssr/no-static-imports-of-user-specific-scoped-modules": "error",
"@lwc/lwc/ssr/no-form-factor": "error",
"@lwc/lwc/no-unexpected-wire-adapter-usages": "off",
"@lwc/lwc/no-unknown-wire-adapters": "off"
}
}
],
"extends": "eslint:recommended",
"ignorePatterns": ["src/aura/**/*.js"],
"plugins": ["@lwc/eslint-plugin-lwc"]
}
The following rules have been set to off because they gave errors when running the analysis
"@lwc/lwc/no-unexpected-wire-adapter-usages": "off",
"@lwc/lwc/no-unknown-wire-adapters": "off"
For running specific PMD Apex rules see Running specific PMD rules with Salesforce Code Analyzer

Leave a comment