<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5HGSQD2L" height="0" width="0" style="display:none;visibility:hidden" title="GTM"></iframe>

ESLint plugin

SWAN's ESLint package is a custom linting plugin designed to enforce the Vista Design System's evolving design language across JavaScript and TypeScript files. It helps developers write consistent and accessible SWAN-specific code by identifying deviations from SWANโ€™s design principles and coding conventions, thereby reducing variations.

Getting started

Prerequisites

  • An ESLint setup in your project
  • Peer dependencies
    • @vp/swan >= 3.18.1 < 4
    • @typescript-eslint/parser ^7 || ^8
    • eslint ^8.45.0 || ^9
    • typescript >= 4.8.4
  • Node.js >= 18.18.0

Installation

Usage

You can integrate the plugin into your project by adding it to your ESLint config file.

Flat ESLint Config (>= ESLint v9)

Legacy ESLint Config (< ESLint v9)

Setting individual rules

To best align with SWAN's best practices, it's advised to use the recommended configuration (shown above). However, it's possible to enable, disable, or alter the severity of individual rules if required.

Flat ESLint Config (>= ESLint v9)
Legacy ESLint Config (< ESLint v9)

TypeScript Projects: Type-Aware Rule Setup

Some rules in this plugin (like @vp/swan/no-deprecated) use TypeScript's type information to provide more accurate linting. These "type-aware" rules require special configuration to work properly. After setting up @vp/eslint-plugin-swan and running ESLint, if you get any parsing errors like the example below, ESLint is trying to lint files that are not a part of your typescript project i.e. files not included in your root tsconfig.json file.

Parsing error: /path/to/repo/test.tsx was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

To fix these errors you can add the erroring file/s to the allowDefaultProject config in ESLint or you can add them to your typescript project. See below for examples for each ESLint config type.

Using allowDefaultProject

Adding files to allowDefaultProject tells ESLint to compile these files with the default project compiler options which means you don't need to update your typescript project to include these files. For more information on projectService see the typescript-eslint docs.

Notes:

  • allowDefaultProject doesn't allow ** in the path to prevent overuse
  • Files in allowDefaultProject may not also be included in their nearest tsconfig.json
Flat ESLint Config eslint.config.js
Legacy ESLint Config .eslintrc.js
Updating your typescript project in tsconfig.json

You can also add the erroring files to your typescript project. To do that, add each file or directory that isn't already included to your tsconfig.json under the include parameter. If you are linting js files you also need to set allowJs: true so that they are included in your typescript project. E.g:

If desired, you can separate out your typescript config for ESLint by creating a separate tsconfig.eslint.json file. Note you need to set the defaultProject parameter of projectService or EXPERIMENTAL_useProjectService to point to this file instead if you decide to do this. More info on that here.

Rules included

Each rule has its own README with usage, rationale, and examples.

Rule IDDescriptionDefault SeverityDocs with examplesSuggestion available
@vp/swan/enforce-font-skin
Introduced in v1.0
Using individual font styling props (fontSize, fontWeight, fontFamily) can lead to inconsistent typography that doesn't align with the design system. The recommended path for setting typography is the fontSkin prop, which bundles together all typography related styling decisions. Don't use other explicit typographic props.warnREADMEPartially
@vp/swan/enforce-grid-structure
Introduced in v1.0
This rule ensures that grid layout components are nested in a valid hierarchy: GridContainer > Row > Column each immediate child of it's previous or a React fragment.errorREADME-
@vp/swan/no-classnames
Introduced in v1.0
Disallow the use of SWAN classes. Manually applying SWAN classes such as swan-* class whether on any element or components, can make the UI fragile and inconsistent, since SWAN may update or remove these class names at any timeerrorREADME-
@vp/swan/no-deprecated
Introduced in v1.0
Flags all deprecated SWAN symbols (components, props, functions, etc) which should be avoided where possible as they will be removed in a future version of SWAN. If a replacement is recommended, that should be used instead, otherwise remove usage of the symbol.errorREADMEPartially
@vp/swan/no-deprecated-tokens
Introduced in v1.0
Deprecated SWAN tokens should be avoided where possible as they will be removed in a future version of SWAN. If a replacement is recommended, developers should use that instead otherwise should remove usage of the token.errorREADMEPartially
@vp/swan/no-internal-components
Introduced in v1.0
This rule ensures that developers do not use internal-only components from the @vp/swan package. These components are not part of the public API and may change without notice. Includes components like Dialog, PopoverTrigger and all BaseTile relatederrorREADMENo
@vp/swan/no-internal-tokens
Introduced in v1.0
Disallows use of internal SWAN tokens like SwanInternal*, SwanComp* which are not part of the public or semantic API and may change without notice. Developers should rely on semantic (SwanSem*) tokens. If no suitable semantic token exists for the use case, use a base token or reach out to the design system team before considering alternatives.errorREADMENo
@vp/swan/no-mixed-font-props
Introduced in v1.0
The fontSkin prop is designed to encapsulate all typographic styling decisions. Using it alongside individual font-related props (fontSize, fontWeight, or fontFamily) introduces ambiguity and breaks design consistency. This rule ensures that once fontSkin is used, all other individual font props are flagged.errorREADME-
@vp/swan/required-accessibility-props
Introduced in v1.0
This rule helps developers add appropriate accessibility-related props to respective SWAN components. These props were introduced as optional to avoid breaking changes, but will be made required in the next major SWAN update.errorREADMEYes

Troubleshooting/FAQs

Update your ESLint version to 8.45 or above.

If your SWAN rule config changes (like severity overrides) arenโ€™t showing up right away in your IDE, try restarting the ESLint server or just reload the IDE.

We understand that setting up type-aware linting can be challenging (especially in monorepos with complex tsconfig setups).

We've documented a general solution in the above sectionTypeScript Projects: Type-Aware Rule Setup that can be adapted to most setups. However, consumers may need to explore and adjust the configuration based on their specific project structure.

If the documented solution doesn't work for you, feel free to reach out to the #help-swan channel. We're happy to take a quick lookโ€”if the fix looks straightforward, weโ€™ll guide you through it.

Changelog

See the complete Changelog

Related