Skip to content

Commit be2bbc0

Browse files
Merge pull request #7 from EOX-A/v2
feat!: convert to eslint flat config, include typescript
2 parents bab002d + 1b6f353 commit be2bbc0

4 files changed

Lines changed: 819 additions & 1149 deletions

File tree

README.md

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
# EOX ESLint & Prettier config for JS projects
2+
3+
## About
4+
5+
This reusable config aims to be our default way of setting up linting and formatting for JS projects. It includes:
6+
7+
- eslint dependency
8+
- prettier dependency
9+
- typescript dependency
10+
- recommended eslint config
11+
- recommended typescript-eslint
12+
- typescript-eslint parser
13+
- vue-eslint parser
14+
- cypress rules
15+
- chai friendly errors
16+
17+
The config also includes a minimal set of rules, defined in [index.js](./index.js).
18+
219
## Installation
20+
321
```js
422
npm install --save-dev @eox/eslint-config
523
```
24+
625
## Usage
26+
727
### Prettier
28+
829
To run prettier and format all your files in the current folder, use
30+
931
```bash
1032
npx prettier --write .
1133
or
@@ -15,23 +37,50 @@ npx prettier --write "**/*"
1537
Please refer to the [Prettier CLI docs](https://prettier.io/docs/en/cli.html) for further details.
1638

1739
### ESLint
18-
To include ESLint in the project, create a file called `.eslintrc.js` in the app root:
40+
41+
To include ESLint in the project, create a file called `eslint.config.mjs` in the app root:
42+
1943
```js
20-
module.exports = {
21-
extends: "@eox"
22-
}
44+
import eox from "@eox/eslint-config";
45+
46+
export default [...eox];
2347
```
48+
2449
Finally, to run ESLint with auto-fixing, use
50+
2551
```bash
26-
npx eslint . --fix
52+
npx eslint --fix .
2753
```
2854

29-
Please refer to the [ESLint CLI docs](https://eslint.org/docs/latest/user-guide/command-line-interface) for further details.
55+
Please refer to the [ESLint configuration docs](https://eslint.org/docs/latest/use/configure/) and [ESLint CLI docs](https://eslint.org/docs/latest/user-guide/command-line-interface) for further details.
56+
57+
### TypeScript
58+
59+
To use typescript checks, create a `tsconfig.json` in the project root
60+
61+
```json
62+
{
63+
"compilerOptions": {
64+
"allowJs": true,
65+
"checkJs": true,
66+
...
67+
}
68+
}
69+
```
70+
71+
and run the check with
72+
73+
```bash
74+
npx tsc
75+
```
3076

3177
## Ignoring files
32-
You can add [.eslintignore](https://eslint.org/docs/latest/user-guide/configuring/ignoring-code) and [.prettierignore](https://prettier.io/docs/en/ignore.html) files to ignore certain folders or files/patterns.
3378

34-
Both ignore files should ideally have the same content, e.g.:
79+
not any longer -> only for prettier?
80+
81+
You can add a [.prettierignore](https://prettier.io/docs/en/ignore.html) file to ignore certain folders or files/patterns.
82+
83+
E.g.:
3584

3685
```
3786
public
@@ -42,32 +91,71 @@ dist
4291
# adapt according to your project needs
4392
```
4493

94+
For ESlint, use the [`ignores` property in the config file](https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files). Example:
95+
96+
```js
97+
// other config
98+
{ ignores: ["dist"] },
99+
// other config
100+
```
101+
45102
## Custom configuration & rules
103+
46104
Both Prettier and ESLint allow for some rule customization. Please check the [Prettier configuration options](https://prettier.io/docs/en/options.html) and the [ESLint rules](https://eslint.org/docs/rules/) for an explanation of rules that could be added/modified to this file for individual projects.
47105

48106
BUT: please consider if this is really necessary, or if it could be included in the centralized config rather than in an individual project. Ideally, the individual projects should not use any custom rules.
49107

108+
## Extend with plugins
109+
110+
Example: Cypress & Vue
111+
112+
```js
113+
// eslint.config.js
114+
import eox from "@eox/eslint-config";
115+
import pluginCypress from "eslint-plugin-cypress/flat";
116+
import pluginVue from "eslint-plugin-vue";
117+
118+
export default [
119+
...eox,
120+
pluginCypress.configs.recommended,
121+
...pluginVue.configs["flat/recommended"],
122+
{
123+
rules: {
124+
// override/add rules settings here
125+
},
126+
},
127+
];
128+
```
129+
50130
## Checking for valid code
131+
51132
To check if your code is valid before committing or inside a CI pipeline, use
133+
52134
```bash
53135
npx prettier --check .
54136
npx eslint .
55137
```
56138

57139
## Automation in VS Code (extension, settings)
140+
58141
### Prettier
142+
59143
Although you can use prettier via command line, in a pre-commit hook or in a CI pipeline, you can also use the VS Code extension to format files (or file sections) via a handy command or even automatically (e.g. on file save) right inside your code editor.
60144

61145
--> [Prettier - Code formatter extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
62146

63147
Please refer to the description about setting up and using the extension!
64148

65149
As a bare minimum, create a `.prettierrc.json` file in the app root to let the editor know prettier is used in this project
150+
66151
```js
67152
echo {} > .prettierrc.json
68153
```
154+
69155
### ESLint
156+
70157
As for ESLint, automatically running and fixing your code can be obtained by changing the settings in your workspace or dev container settings, e.g.:
158+
71159
```
72160
"editor.codeActionsOnSave": {
73161
"source.fixAll.eslint": true

index.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
module.exports = {
2-
root: true,
3-
env: {
4-
node: true,
5-
browser: true
1+
import eslintConfigPrettier from "eslint-config-prettier";
2+
import globals from "globals";
3+
import eslint from "@eslint/js";
4+
import tseslint from "typescript-eslint";
5+
import tsParser from "@typescript-eslint/parser";
6+
import vueParser from "vue-eslint-parser";
7+
import pluginCypress from "eslint-plugin-cypress/flat";
8+
import pluginChaiFriendly from "eslint-plugin-chai-friendly";
9+
10+
export default [
11+
pluginCypress.configs.recommended,
12+
eslint.configs.recommended,
13+
...tseslint.configs.recommended,
14+
eslintConfigPrettier,
15+
{
16+
plugins: { "chai-friendly": pluginChaiFriendly },
17+
languageOptions: {
18+
parser: vueParser,
19+
parserOptions: {
20+
parser: tsParser,
21+
},
22+
globals: {
23+
...globals.node,
24+
...globals.browser,
25+
},
26+
},
27+
rules: {
28+
"@typescript-eslint/no-explicit-any": "warn",
29+
"@typescript-eslint/no-unused-vars": [
30+
"error",
31+
{
32+
varsIgnorePattern: "^_",
33+
argsIgnorePattern: "^_",
34+
caughtErrorsIgnorePattern: "^_",
35+
},
36+
],
37+
"@typescript-eslint/no-unused-expressions": "off", // disable original rule
38+
"chai-friendly/no-unused-expressions": "error",
39+
},
640
},
7-
extends: [
8-
"eslint:recommended",
9-
"plugin:vue/recommended",
10-
"prettier"
11-
],
12-
rules: {
13-
},
14-
};
41+
];

0 commit comments

Comments
 (0)