SLDS Linter is an installable npm package that analyzes your code against Salesforce Lightning Design System 2 (SLDS 2) rules and offers suggestions to better align your code with SLDS 2 guidelines. SLDS Linter can also apply the suggestions in bulk across your repository and automatically fix your code.
For detailed information about SLDS Linter, see the SLDS Linter developer guide.
SLDS Linter is a custom-built linting solution based on open-source ESLint projects. It supports linting for both types of Lightning components.
- Lightning web components: HTML and Cascading Style Sheet (CSS) files
- Aura components: Component (CMP) and CSS files
Run SLDS Linter in a terminal window or in Visual Studio (VS) Code. We recommend running in VS Code.
Follow these steps to integrate SLDS Linter into your project.
- Install VS Code
- Install the SARIF Viewer VS Code extension. With this extension, you can view SLDS Linter violation reports.
- Install Node.js
- The minimum supported version is v18.4.0
- We recommend using the latest Active LTS version of Node.js.
- Open your project in VS Code.
- In a VS Code terminal, enter
npx @salesforce-ux/slds-linter@latest lintto run the installer. - To install the SLDS Linter package, type
y.
The SLDS Linter package installs in a temporary location on your system.
For more information about slds-linter commands, run npx @salesforce-ux/slds-linter@latest --help.
Usage: npx @salesforce-ux/slds-linter@latest [options] [command]
SLDS Linter CLI tool for linting styles and components
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
lint [options] [directory] Run both style and component linting
report [options] [directory] Generate SARIF report from linting results
emit [options] Emits the configuration files used by slds-linter cli
help [command] display help for command
Run SLDS Linter against your project in the VS Code terminal to check for any violations and generate a SARIF report. This report helps you identify the components you need to update.
In your project root directory, follow these instructions.
-
In your project in VS Code, open Terminal.
-
Run
npx @salesforce-ux/slds-linter lint. The linting output displayed in the console includes the row and column numbers on the left. Navigate to specific lines in your source code by clicking on the displayed numbers (Command + Click on Mac). -
(Optional) To run SLDS Linter on a specific folder, specify the directory to be linted:
npx @salesforce-ux/slds-linter [directory name]. This option accepts directories or folders using glob patterns, enabling flexible and efficient matching of multiple paths. For example, runnpx @salesforce-ux/slds-linter lint "com/CORE/**"gives the lint output for all the files undercom/CORE. -
To produce a SARIF report in your project root directory and specify an output directory, run
npx @salesforce-ux/slds-linter report -o [output directory]. The output file is named asslds-linter-report.sarif. -
Open the generated
.sarifreport file. -
Make a note of how many components SLDS Linter has identified that you must update.
-
(Optional) To automatically fix validation errors in bulk, run the
lintcommand with thefixoption,npx @salesforce-ux/slds-linter lint --fix. -
(Optional) To emit the configuration files used by
slds-linter, runnpx @salesforce-ux/slds-linter emitin your component source directory. Note that this command defaults to current working directory. These configuration files are discovered by your VS Code ESLint extension to display squiggly lines in HTML, CSS and CMP files when opened in your code editor.
If the SARIF viewer doesn’t automatically go to the line of code when you click on an error or warning, follow these steps.
- In the SARIF viewer pop-up window, click Locate.
- In the file explorer or code editor, locate the file.
- Click on the errors in the SARIF viewer, and it navigates directly to the relevant line of code.
Use these commands to run SLDS Linter rules. Review the output violations and fix any issues to uplift your code to SLDS best practices.
npx @salesforce-ux/slds-linter lint. Runs ESLint rules on HTML, CSS, and CMP files.npx @salesforce-ux/slds-linter report. Generates a SARIF report for static analysis.npx @salesforce-ux/slds-linter emit. Emits the configuration files used byslds-linter. Defaults to current directory.
These options are available on SLDS Linter commands.
| Option | Description | Availability |
|---|---|---|
-d, --directory <path> |
Target directory to scan (defaults to current directory). Supports glob patterns. | lint, report |
-o, --output <path> |
Output directory for reports (defaults to current directory) | report |
--fix |
Automatically fix problems | lint |
--config-eslint <path> |
Path to eslint config file | lint, report |
--editor <editor> |
Editor to open files with (e.g., vscode, atom, sublime). Defaults to vscode | lint |
To view help for these options, add --help to each command. For example, run npx @salesforce-ux/slds-linter lint --help to see which options you can use with lint.
To enhance your linting and error analysis experience, we recommend that you install these VS Code extensions. This extension significantly improves your development workflow and makes it easier to navigate and address linting issues.
- ESLint Extension: Essential for JavaScript, TypeScript, HTML, CMP, and CSS linting. It checks your code and highlights any violations of the ESLint rules with squiggly lines.
- Run
npx @salesforce-ux/slds-linter lintto see the lint output on Terminal. - To run SLDS Linter on a specific folder, input as argument. For example,
npx @salesforce-ux/slds-linter lint <directory>
Recursively linting all style and markup files in the aura directory:
npx @salesforce-ux/slds-linter lint auraRecursively linting all style and markup files in the aura and lwc directory:
npx @salesforce-ux/slds-linter lint "**/{aura,lwc}/**"Recursively linting all .css files in the aura directory:
npx @salesforce-ux/slds-linter lint "aura/**/*.css"Linting all .css, .scss, and .sass files:
npx @salesforce-ux/slds-linter lint "**/*.{css,scss,sass}"Recursively linting all .html files in the aura directory:
npx @salesforce-ux/slds-linter lint "aura/**/*.html"Linting all .html and .cmp files:
npx @salesforce-ux/slds-linter lint "**/*.{html,cmp}"For any questions or issues, open an issue in this repository.