Skip to content

code-analyzer-lwc

GitHub stars GitHub release (latest SemVer) GitHub last commit GitHub commit activity GitHub contributors

Salesforce Code Analyzer for Lightning Web Components is a specialized linting tool that focuses on Lightning Web Components (LWC), using ESLint with LWC-specific rules to ensure components follow modern web standards and Salesforce platform best practices.

Key Features:

  • LWC-Specific Rules: Comprehensive ESLint rules designed specifically for Lightning Web Component development patterns
  • Modern JavaScript Analysis: Validates modern JavaScript/ES6+ usage within the LWC framework constraints
  • Security Validation: Identifies security vulnerabilities and potential XSS issues in LWC components
  • Performance Optimization: Detects performance anti-patterns and inefficient rendering patterns
  • Lightning Platform Integration: Ensures proper use of Lightning platform APIs and decorators (@api, @track, @wire)
  • Component Lifecycle: Validates correct component lifecycle method usage and patterns
  • Wire Service Validation: Ensures proper use of @wire decorators and Lightning Data Service
  • Event Handling: Validates custom event creation and handling patterns
  • Template Validation: Analyzes HTML templates for proper binding and directive usage
  • Accessibility Compliance: Built-in accessibility checks for inclusive Lightning component design

If your root folder is not force-app, please set variable SALESFORCE_SFDX_SCANNER_LWC_DIRECTORY

You can select categories and single rules by defining custom arguments (example: SALESFORCE_SFDX_SCANNER_ARGUMENTS: -c "Best Practices,Security")

See more details in Help

Workaround: Restricted to LWC

code-analyzer-lwc documentation

code-analyzer - GitHub

Configuration in MegaLinter

Variable Description Default value
SALESFORCE_CODE_ANALYZER_LWC_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
SALESFORCE_CODE_ANALYZER_LWC_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
Ex: -s --foo "bar"
SALESFORCE_CODE_ANALYZER_LWC_CLI_LINT_MODE Override default CLI lint mode
⚠️ As default value is project, overriding might not work
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
project
SALESFORCE_CODE_ANALYZER_LWC_PRE_COMMANDS List of bash commands to run before the linter None
SALESFORCE_CODE_ANALYZER_LWC_POST_COMMANDS List of bash commands to run after the linter None
SALESFORCE_CODE_ANALYZER_LWC_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling SALESFORCE_CODE_ANALYZER_LWC and its pre/post commands None
SALESFORCE_CODE_ANALYZER_LWC_CONFIG_FILE code-analyzer-lwc configuration file name
Use LINTER_DEFAULT to let the linter find it
code-analyzer.yml
SALESFORCE_CODE_ANALYZER_LWC_RULES_PATH Path where to find linter configuration file Workspace folder, then MegaLinter default rules
SALESFORCE_CODE_ANALYZER_LWC_DISABLE_ERRORS Run linter but consider errors as warnings false
SALESFORCE_CODE_ANALYZER_LWC_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
SALESFORCE_CODE_ANALYZER_LWC_CLI_EXECUTABLE Override CLI executable ['sf']
SALESFORCE_DIRECTORY Directory containing SALESFORCE files (use any to always activate the linter) force-app

IDE Integration

Use code-analyzer-lwc in your favorite IDE to catch errors before MegaLinter !

IDE Extension Name Install
Visual Studio Code Salesforce Extension Pack Install in VSCode

MegaLinter Flavors

This linter is available in the following flavors

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 130 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 56 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • Activated only if sub-directory force-app is found. (directory name can be overridden with SALESFORCE_DIRECTORY)
  • If this linter is active, all files will always be linted

How the linting is performed

code-analyzer-lwc is called once on the whole project directory (project CLI lint mode)

  • filtering can not be done using MegaLinter configuration variables,it must be done using code-analyzer-lwc configuration or ignore file (if existing)
  • VALIDATE_ALL_CODEBASE: false doesn't make code-analyzer-lwc analyze only updated files

Example calls

sf code-analyzer run --rule-selector eslint:LWC --workspace . --target '**/lwc/**/*.js' --output-file report.csv

Help content

Analyze your code with a selection of rules to ensure good coding practices.

USAGE
  $ sf code-analyzer run [--flags-dir <value>] [-w <value>...] [-t <value>...]
    [-r <value>...] [-s <value>] [-v detail|table] [-f <value>...] [-c <value>]

FLAGS
  -c, --config-file=<value>         Path to the configuration file used to
                                    customize the engines and rules.
  -f, --output-file=<value>...      Name of the file where the analysis results
                                    are written. The file format depends on the
                                    extension you specify, such as .csv, .html,
                                    .xml, and so on.
  -r, --rule-selector=<value>...    [default: Recommended] Selection of rules,
                                    based on engine name, severity level, rule
                                    name, tag, or a combination of criteria
                                    separated by colons.
  -s, --severity-threshold=<value>  Severity level of a found violation that
                                    must be met or exceeded to cause this
                                    command to fail with a non-zero exit code.
  -t, --target=<value>...           Subset of files within your workspace to be
                                    targeted for analysis.
  -v, --view=<option>               Format to display the command results in the
                                    terminal.
                                    <options: detail|table>
  -w, --workspace=<value>...        [default: .] Set of files that make up your
                                    workspace.

GLOBAL FLAGS
  --flags-dir=<value>  Import flag values from a directory.


Streaming logs in real time to:
    /tmp/sfca-2025_11_24_22_16_17_945.log

Selecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 0%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 14%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 28%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 30%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 31%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 32%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 33%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 34%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 35%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 36%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 37%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 38%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 39%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 40%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 41%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 42%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 53%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 57%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 62%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 72%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 74%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 75%; Elapsed time: 0sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 75%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 84%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 85%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 87%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 96%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 98%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 99%; Elapsed time: 1sSelecting rules... Eligible engines: retire-js, regex, eslint, flow, pmd, cpd, sfge; Completion: 100%; Elapsed time: 1sSelecting rules... done.

  #    Name                                                              Engine   Severity       Tag
 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  1    @lwc/lwc-platform/no-aura                                         eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  2    @lwc/lwc-platform/no-aura-libs                                    eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  3    @lwc/lwc-platform/no-community-import                             eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  4    @lwc/lwc-platform/no-create-context-provider                      eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  5    @lwc/lwc-platform/no-deprecated-module-import                     eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  6    @lwc/lwc-platform/no-dynamic-import-identifier                    eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  7    @lwc/lwc-platform/no-inline-disable                               eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  8    @lwc/lwc-platform/no-interop                                      eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  9    @lwc/lwc-platform/no-interop-create                               eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  10   @lwc/lwc-platform/no-interop-dispatch                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  11   @lwc/lwc-platform/no-interop-execute                              eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  12   @lwc/lwc-platform/no-interop-execute-controller-with-client-def   eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  13   @lwc/lwc-platform/no-interop-execute-privileged                   eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  14   @lwc/lwc-platform/no-interop-execute-raw-response                 eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  15   @lwc/lwc-platform/no-interop-execute-with-callback                eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  16   @lwc/lwc-platform/no-interop-get-event                            eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  17   @lwc/lwc-platform/no-interop-get-module                           eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  18   @lwc/lwc-platform/no-interop-is-external-definition               eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  19   @lwc/lwc-platform/no-interop-load-definitions                     eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  20   @lwc/lwc-platform/no-interop-module-instrumentation               eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  21   @lwc/lwc-platform/no-interop-module-storage                       eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  22   @lwc/lwc-platform/no-interop-register                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  23   @lwc/lwc-platform/no-interop-render                               eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  24   @lwc/lwc-platform/no-interop-sanitize                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  25   @lwc/lwc-platform/no-lds-aura-controller-method                   eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  26   @lwc/lwc-platform/no-process-env                                  eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  27   @lwc/lwc-platform/no-restricted-namespaces                        eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  28   @lwc/lwc-platform/no-site-import                                  eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  29   @lwc/lwc-platform/no-wire-service                                 eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  30   @lwc/lwc-platform/valid-dynamic-import-hint                       eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  31   @lwc/lwc/no-api-reassignments                                     eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  32   @lwc/lwc/no-async-operation                                       eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  33   @lwc/lwc/no-attributes-during-construction                        eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  34   @lwc/lwc/no-deprecated                                            eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  35   @lwc/lwc/no-disallowed-lwc-imports                                eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  36   @lwc/lwc/no-document-query                                        eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  37   @lwc/lwc/no-inner-html                                            eslint   2 (High)       Recommended, LWC, Security, JavaScript
  38   @lwc/lwc/no-leading-uppercase-api-name                            eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  39   @lwc/lwc/no-template-children                                     eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  40   @lwc/lwc/no-unexpected-wire-adapter-usages                        eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  41   @lwc/lwc/no-unknown-wire-adapters                                 eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  42   @lwc/lwc/prefer-custom-event                                      eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  43   @lwc/lwc/valid-api                                                eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  44   @lwc/lwc/valid-graphql-wire-adapter-callback-parameters           eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  45   @lwc/lwc/valid-track                                              eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  46   @lwc/lwc/valid-wire                                               eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  47   @salesforce/lightning/valid-apex-method-invocation                eslint   3 (Moderate)   Recommended, LWC, ErrorProne, JavaScript
  48   import/default                                                    eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  49   import/export                                                     eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  50   import/named                                                      eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  51   import/namespace                                                  eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  52   jest/expect-expect                                                eslint   5 (Info)       Recommended, LWC, BestPractices, JavaScript
  53   jest/no-alias-methods                                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  54   jest/no-commented-out-tests                                       eslint   5 (Info)       Recommended, LWC, BestPractices, JavaScript
  55   jest/no-conditional-expect                                        eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  56   jest/no-disabled-tests                                            eslint   5 (Info)       Recommended, LWC, BestPractices, JavaScript
  57   jest/no-done-callback                                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  58   jest/no-export                                                    eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  59   jest/no-focused-tests                                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  60   jest/no-identical-title                                           eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  61   jest/no-interpolation-in-snapshots                                eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  62   jest/no-jasmine-globals                                           eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  63   jest/no-mocks-import                                              eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  64   jest/no-standalone-expect                                         eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  65   jest/no-test-prefixes                                             eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  66   jest/valid-describe-callback                                      eslint   2 (High)       Recommended, LWC, ErrorProne, JavaScript
  67   jest/valid-expect                                                 eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  68   jest/valid-expect-in-promise                                      eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript
  69   jest/valid-title                                                  eslint   3 (Moderate)   Recommended, LWC, BestPractices, JavaScript



=== Summary

Found 69 rule(s) from 1 engine(s):
    69 eslint rule(s) found.

Additional log information written to:
    /tmp/sfca-2025_11_24_22_16_17_945.log

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
# renovate: datasource=npm depName=@salesforce/cli
ARG NPM_SALESFORCE_CLI_VERSION=2.113.6
# renovate: datasource=npm depName=@salesforce/plugin-packaging
ARG NPM_SALESFORCE_PLUGIN_PACKAGING_VERSION=2.23.3
# renovate: datasource=npm depName=sfdx-hardis
ARG SFDX_HARDIS_VERSION=6.12.7
ENV JAVA_HOME=/usr/lib/jvm/java-21-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
RUN sf plugins install @salesforce/plugin-packaging@${NPM_SALESFORCE_PLUGIN_PACKAGING_VERSION} \
    && echo y|sf plugins install sfdx-hardis@${SFDX_HARDIS_VERSION} \
    && (npm cache clean --force || true) \
    && rm -rf /root/.npm/_cacache
ENV SF_AUTOUPDATE_DISABLE=true SF_CLI_DISABLE_AUTOUPDATE=true
# Linter install
# renovate: datasource=npm depName=@salesforce/plugin-code-analyzer
ARG SALESFORCE_CODE_ANALYZER_VERSION=5.6.1
RUN sf plugins install code-analyzer@${SALESFORCE_CODE_ANALYZER_VERSION} \
    && (npm cache clean --force || true) \
    && rm -rf /root/.npm/_cacache