The PWA uses ESLint for static code analysis and to enforce best practices.
In order to use ESLint with Angular, we use the typescript-eslint
and angular-eslint
packages extensively.
The PWA's ruleset is configured in .eslintrc.json
and includes base rulesets to extend from, plugins with additional rules as well as a configuration for our custom rules.
To read more about the Intershop PWA's custom rules, see Custom ESLint rules.
An ESLint rule configuration is a key-value-pair of a rules
object in .eslintrc.json
.
There are separate rulesets for typescript
and html
files.
Consider the following example:
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": ["ish"],
"style": "kebab-case"
}
],
The key specifies the rule to configure, in this case @angular-eslint/component-selector
.
The value specifies a rule's options and can be structured in two different ways:
- Only a string, with a value of
error
,warn
oroff
. This simply enables or disables the rule and sets a severity level. - An array with at least one entry, where the first entry is always the severity level (
error
orwarn
). The following entries are rule-specific configuration and will be passed to the rule.
Refer to the relevant documentation of the standard ESLint ruleset, various plugins or our custom rules to learn more about what each rule does and how to configure them.
For more information on how to configure every aspect of ESLint, refer to the extensive documentation of ESLint as well as typescript-eslint
and angular-eslint
.
To keep the codebase clean, easily maintainable and functional, the Intershop PWA provides a large number of custom ESLint rules.
These rules are located in the eslint-rules
project.
The custom rules are developed separately from the main source code, which uses the compiled rules as a local developer dependency in its package.json
.
To work with and develop new custom ESLint rules, follow these steps:
- Generate a new rule using our schematic:
ng g eslint-rule
(aliaser
). This generates a new rule file ateslint-rules/src/rules
and a test file ateslint-rules/tests
. - Write the rule code. Refer to Working with Rules as well as examples from available rules to understand how. Add reusable helper functions to
helpers.ts
to reduce repetition. - Write rule tests. Refer to Testing for documentation on the
RuleTester
API that is used in tests. Usenpm run test:eslint-rules
to execute your tests. - Build the
eslint-rules
project with your changes usingnpm run build:eslint-rules
. The resulting JavaScript files will be located in theeslint-rules/dist
folder. A generatedindex.ts
exports the rules to be consumed in the.eslintrc.json
configuration file. - Add the new rule to the ESLint configuration in
.eslintrc.json
. - Optional: Restart the ESLint server using the
ESLint: Restart ESLint server
command to see your new configuration applied in VSCode. You can access the command via the editor commands (default keybinding:ctrl + shift + p
).