|
1 | 1 | # @vue-storefront/eslint-config |
2 | 2 |
|
3 | | -> Common eslint configuration used in alokai projects |
| 3 | +> Common ESLint configuration used in Alokai projects. These configurations are compatible with ESLint 9. |
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | | -#### Install |
| 7 | +### Install |
8 | 8 |
|
9 | 9 | ```bash |
10 | 10 | yarn add -D eslint prettier @vue-storefront/eslint-config |
11 | 11 | ``` |
12 | 12 |
|
13 | | -#### Config `.eslintrc` |
| 13 | +### Building Blocks |
14 | 14 |
|
15 | | -```json |
16 | | -{ |
17 | | - "extends": "@vue-storefront/eslint-config" |
| 15 | +This ESLint configuration is composed of several building blocks, each tailored for specific use cases: |
| 16 | + |
| 17 | +- `ecma`: For ECMAScript projects. |
| 18 | +- `typescript`: For TypeScript projects. |
| 19 | +- `style`: For Prettier and Perfectionist plugins. |
| 20 | +- `nextjs`: For Next.js projects. |
| 21 | +- `playwright`: For Playwright projects. |
| 22 | +- `architecture`: For enforcing architectural rules. |
| 23 | + |
| 24 | +### Options |
| 25 | + |
| 26 | +All options are optional. You don't have to set these options if you are okay with the default configuration. |
| 27 | + |
| 28 | +#### `ecma` |
| 29 | + |
| 30 | +- `files` (default: `"**/*.{mjs,cjs,js,jsx}"`): The glob pattern for files to lint. |
| 31 | +- `isStrict` (default: `true`): Enables extra rules for stricter checking. |
| 32 | +- `withImport` (default: `true`): Enables `eslint-plugin-import`. |
| 33 | + |
| 34 | + |
| 35 | +#### `typescript` |
| 36 | + |
| 37 | +- `files` (default: `"**/*.{ts,tsx,mts,cts,mtsx,ctsx}"`): The glob pattern for files to lint. |
| 38 | +- `isStrict` (default: `true`): Enables extra rules for stricter checking. |
| 39 | +- `withImport` (default: `true`): Enables `eslint-plugin-import`. |
| 40 | + |
| 41 | +### `style` |
| 42 | + |
| 43 | +- `files` (default: `"**/*.{mjs,cjs,js,jsx,ts,tsx,mts,cts,mtsx,ctsx}"`): The glob pattern for files to lint. |
| 44 | + |
| 45 | +### `nextjs` |
| 46 | + |
| 47 | +- `files` (default: `"**/*.{mjs,cjs,js,jsx,ts,tsx,mts,cts,mtsx,ctsx}"`): The glob pattern for files to lint. |
| 48 | +Here you can pass the general file glob pattern for all directories with Next.js/React code. But for better results please pass the glob for the components folders and hooks. Those two are passed to the special rules just for them. |
| 49 | + |
| 50 | +```ts |
| 51 | +files: { |
| 52 | + general: "**/*.{js,jsx,ts,tsx}", |
| 53 | + components: "src/components/**/*.{js,jsx,ts,tsx}", |
| 54 | + hooks: "src/hooks/**/*.{js,jsx,ts,tsx}" |
18 | 55 | } |
19 | 56 | ``` |
20 | 57 |
|
21 | | -### Without Prettier `.eslintrc` |
| 58 | +- `isStrict` (default: `true`): Enables extra rules for stricter checking. |
22 | 59 |
|
23 | | -If you do not want to use `prettier`, you can easily compose your own config: |
| 60 | +### `playwright` |
24 | 61 |
|
25 | | -```json |
26 | | -{ |
27 | | - "extends": [ |
28 | | - "@vue-storefront/eslint-config/ecma", |
29 | | - "@vue-storefront/eslint-config/typescript", |
30 | | - "@vue-storefront/eslint-config/json" |
31 | | - ] |
32 | | -} |
| 62 | +- `files` (default: `"**/*.test.ts"`): The glob pattern for files to lint. |
| 63 | + |
| 64 | +### `architecture` |
| 65 | + |
| 66 | +- `files` (default: `"**/*.{mjs,cjs,js,jsx,ts,tsx,mts,cts,mtsx,ctsx}"`): The glob pattern for files to lint. |
| 67 | +- `maxComplexity` (default: `6`): The maximum cyclomatic complexity allowed in a program. |
| 68 | +- `maxDepth` (default: `4`): The maximum depth that blocks can be nested. |
| 69 | +- `maxStatementsPerLine` (default: `1`): The maximum number of statements allowed per line. |
| 70 | +- `maxLines` (default: `300`): The maximum number of lines per file. |
| 71 | +- `maxLinesPerFunction` (default: `60`): The maximum number of lines of code in a function. |
| 72 | +- `maxStatements` (default: `10`): The maximum number of statements allowed in function blocks. |
| 73 | +- `maxNestedCallbacks` (default: `5`): The maximum depth that callbacks can be nested. |
| 74 | +- `maxParams` (default: `3`): The maximum number of parameters in function definitions. |
| 75 | + |
| 76 | + |
| 77 | +## Example configurations |
| 78 | + |
| 79 | +### Config `eslint.config.js` |
| 80 | + |
| 81 | +```javascript |
| 82 | +import { ecma, typescript, style } from "@vue-storefront/eslint-config"; |
| 83 | + |
| 84 | +export default [ |
| 85 | + ecma(), |
| 86 | + typescript(), |
| 87 | + style() |
| 88 | +]; |
33 | 89 | ``` |
34 | 90 |
|
35 | | -### Usage with React `.eslintrc` |
| 91 | +### Usage with Next.js `eslint.config.js` |
36 | 92 |
|
37 | | -Vue Storefront `React` specific linting rules. |
| 93 | +Vue Storefront `Next.js` specific linting rules. |
38 | 94 |
|
39 | | -```json |
40 | | -{ |
41 | | - "extends": [ |
42 | | - "@vue-storefront/eslint-config", |
43 | | - "@vue-storefront/eslint-config/react" |
44 | | - ] |
45 | | -} |
| 95 | +```javascript |
| 96 | +import { ecma, nextjs } from "@vue-storefront/eslint-config"; |
| 97 | + |
| 98 | +export default [ |
| 99 | + ecma(), |
| 100 | + nextjs({ |
| 101 | + files: { |
| 102 | + general: "**/*.{js,jsx,ts,tsx}", |
| 103 | + components: "src/components/**/*.{js,jsx,ts,tsx}", |
| 104 | + hooks: "src/hooks/**/*.{js,jsx,ts,tsx}" |
| 105 | + }, |
| 106 | + isStrict: true |
| 107 | + }) |
| 108 | +]; |
46 | 109 | ``` |
47 | 110 |
|
48 | | -### Usage with Vue `.eslintrc` |
| 111 | +### Usage with Nuxt 3 |
| 112 | + |
| 113 | +For projects using Nuxt 3, we recommend using the Nuxt ESLint module and adding styling and architecture configurations to it. |
| 114 | + |
| 115 | +Here is a basic config using the ESLint Nuxt module: |
| 116 | + |
| 117 | +```javascript |
| 118 | +import withNuxt from './.nuxt/eslint.config.mjs'; |
| 119 | +import { ecma, typescript, style, architecture } from "@vue-storefront/eslint-config"; |
| 120 | + |
| 121 | +export default withNuxt( |
| 122 | + ecma(), |
| 123 | + typescript(), |
| 124 | + style(), |
| 125 | + architecture({ |
| 126 | + maxComplexity: 10, |
| 127 | + maxDepth: 5, |
| 128 | + maxStatementsPerLine: 1, |
| 129 | + maxLines: 500, |
| 130 | + maxLinesPerFunction: 100, |
| 131 | + maxStatements: 15, |
| 132 | + maxNestedCallbacks: 3, |
| 133 | + maxParams: 4 |
| 134 | + }) |
| 135 | +); |
| 136 | +``` |
49 | 137 |
|
50 | | -Vue Storefront `Vue` specific linting rules. |
| 138 | +### Usage with Node.js `eslint.config.js` |
51 | 139 |
|
52 | | -```json |
53 | | -{ |
54 | | - "extends": [ |
55 | | - "@vue-storefront/eslint-config", |
56 | | - "@vue-storefront/eslint-config/vue" |
57 | | - ] |
58 | | -} |
| 140 | +Vue Storefront `Node.js` specific linting rules. |
| 141 | + |
| 142 | +```javascript |
| 143 | +import { ecma } from "@vue-storefront/eslint-config"; |
| 144 | + |
| 145 | +export default [ |
| 146 | + ecma() |
| 147 | +]; |
59 | 148 | ``` |
60 | 149 |
|
61 | | -### Usage with Vue3 `.eslintrc` |
| 150 | +### Usage with Playwright `eslint.config.js` |
62 | 151 |
|
63 | | -Vue Storefront `Vue3` specific linting rules. |
| 152 | +Vue Storefront `Playwright` specific linting rules. |
64 | 153 |
|
65 | | -```json |
66 | | -{ |
67 | | - "extends": [ |
68 | | - "@vue-storefront/eslint-config", |
69 | | - "@vue-storefront/eslint-config/vue3" |
70 | | - ] |
71 | | -} |
| 154 | +```javascript |
| 155 | +import { ecma, playwright } from "@vue-storefront/eslint-config"; |
| 156 | + |
| 157 | +export default [ |
| 158 | + ecma(), |
| 159 | + playwright({ |
| 160 | + files: "**/*.test.ts", |
| 161 | + isStrict: true |
| 162 | + }) |
| 163 | +]; |
72 | 164 | ``` |
73 | 165 |
|
74 | | -### Usage with NextJs `.eslintrc` |
| 166 | +### Usage with TypeScript `eslint.config.js` |
75 | 167 |
|
76 | | -Vue Storefront `NextJs` specific linting rules. |
| 168 | +Vue Storefront `TypeScript` specific linting rules. |
77 | 169 |
|
78 | | -```json |
79 | | -{ |
80 | | - "extends": [ |
81 | | - "@vue-storefront/eslint-config/next" |
82 | | - ] |
83 | | -} |
| 170 | +```javascript |
| 171 | +import { ecma, typescript } from "@vue-storefront/eslint-config"; |
| 172 | + |
| 173 | +export default [ |
| 174 | + ecma(), |
| 175 | + typescript() |
| 176 | +]; |
| 177 | +``` |
| 178 | + |
| 179 | +### Usage with Architectural Rules `eslint.config.js` |
| 180 | + |
| 181 | +Vue Storefront `Architectural` specific linting rules. |
| 182 | + |
| 183 | +```javascript |
| 184 | +import { ecma, architecture } from "@vue-storefront/eslint-config"; |
| 185 | + |
| 186 | +export default [ |
| 187 | + ecma(), |
| 188 | + architecture({ |
| 189 | + maxComplexity: 10, |
| 190 | + maxDepth: 5, |
| 191 | + maxStatementsPerLine: 1, |
| 192 | + maxLines: 500, |
| 193 | + maxLinesPerFunction: 100, |
| 194 | + maxStatements: 15, |
| 195 | + maxNestedCallbacks: 3, |
| 196 | + maxParams: 4 |
| 197 | + }) |
| 198 | +]; |
| 199 | +``` |
| 200 | + |
| 201 | +### Using `concat` function |
| 202 | + |
| 203 | +You can use the `concat` function to combine configurations from different sources. |
| 204 | + |
| 205 | +```javascript |
| 206 | +import { ecma, typescript, concat } from "@vue-storefront/eslint-config"; |
| 207 | +import customConfig from "./custom-eslint-config"; |
| 208 | + |
| 209 | +export default concat( |
| 210 | + ecma(), |
| 211 | + typescript(), |
| 212 | + customConfig |
| 213 | +); |
| 214 | +``` |
| 215 | + |
| 216 | +### Overriding rules in a factory |
| 217 | + |
| 218 | +You can override rules in one of our factories by passing a custom rules object. |
| 219 | + |
| 220 | +```javascript |
| 221 | +import { ecma, typescript } from "@vue-storefront/eslint-config"; |
| 222 | + |
| 223 | +export default [ |
| 224 | + ecma(), |
| 225 | + typescript({}, { |
| 226 | + name: 'custom-config', |
| 227 | + files: ['**/*.ts'], |
| 228 | + rules: { |
| 229 | + "@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }] |
| 230 | + } |
| 231 | + }) |
| 232 | +]; |
84 | 233 | ``` |
85 | 234 |
|
86 | 235 | ## Used rulesets & plugins |
87 | 236 |
|
88 | 237 | - [unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) |
89 | 238 | - [no-secrets](https://github.com/nickdeis/eslint-plugin-no-secrets) |
90 | 239 | - [promise](https://github.com/eslint-community/eslint-plugin-promise) |
91 | | -- [sonarjs](https://github.com/SonarSource/eslint-plugin-sonarjs) |
92 | | -- [@microsoft/sdl](https://github.com/microsoft/eslint-plugin-sdl) |
93 | 240 | - [unused-imports](https://www.npmjs.com/package/eslint-plugin-unused-imports) |
94 | 241 | - [prettier](https://github.com/prettier/eslint-plugin-prettier) |
95 | | - |
|
0 commit comments