Skip to content

Commit 40082da

Browse files
authored
Fix and optimize ESLint config (#25)
## Changes These changes fix a number of issues I noticed or ran into when opening the top-level directory in VS Code. - Change the ESLint settings file from a JSON file to a JS file, where we now use `__dirname` for `tsconfigRootDir`. The previous setting wasn't working for me. This also allows us to add inline code comments to explain the confusing aspects of the file (basically every line). - Set `root: true` so ESLint doesn't attempt to continue searching for lint settings above the `app` directory - Move Jest and TypeScript-specific linter settings into their own `overrides` blocks. This is an optimization, but also fixed an issue I was running into (see screenshot below) ## Testing Before: <img width="515" alt="CleanShot 2022-09-26 at 17 27 10@2x" src="https://user-images.githubusercontent.com/371943/192404198-b9a1d52e-5058-4e8b-84c2-d81ea660e49b.png"> After moving TypeScript lint settings into `overrides` (working TS and ESLint actions, but redundant errors): <img width="726" alt="CleanShot 2022-09-26 at 17 28 58@2x" src="https://user-images.githubusercontent.com/371943/192404275-c4a4e758-8b7c-43b2-b497-9fceac2ee7fa.png"> After `plugin:@typescript-eslint/eslint-recommended` was added to the list (less redundant errors) <img width="904" alt="CleanShot 2022-09-28 at 18 12 20@2x" src="https://user-images.githubusercontent.com/371943/192916141-832f2f5e-97fc-4609-8e61-052ebac67742.png">
1 parent 3abec8e commit 40082da

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# MacOS files
22
.DS_Store
3+
# Developer-specific IDE settings
4+
.vscode/settings.json

app/.eslintrc.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module.exports = {
2+
root: true,
3+
extends: [
4+
"nava",
5+
// Disable ESLint code formatting rules which conflict with Prettier
6+
"prettier",
7+
// `next` should be extended last according to their docs
8+
// https://nextjs.org/docs/basic-features/eslint
9+
"next/core-web-vitals",
10+
],
11+
// Additional lint rules. These get layered onto the top-level rules.
12+
overrides: [
13+
// Lint config specific to Test files
14+
{
15+
files: ["tests/**"],
16+
plugins: ["jest"],
17+
extends: ["plugin:jest/recommended"],
18+
},
19+
// Lint config specific to TypeScript files
20+
{
21+
files: "**/*.+(ts|tsx)",
22+
parserOptions: {
23+
// These paths need defined to support rules that require type information
24+
tsconfigRootDir: __dirname,
25+
project: ["./tsconfig.json"],
26+
},
27+
extends: [
28+
"plugin:@typescript-eslint/recommended",
29+
// Disable vanilla ESLint rules that conflict with those in @typescript-eslint
30+
"plugin:@typescript-eslint/eslint-recommended",
31+
// Rules that specifically require type information
32+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
33+
],
34+
plugins: ["@typescript-eslint"],
35+
rules: {
36+
// Prevent dead code accumulation
37+
"@typescript-eslint/no-unused-vars": "error",
38+
// The usage of `any` defeats the purpose of typescript. Consider using `unknown` type instead instead.
39+
"@typescript-eslint/no-explicit-any": "error",
40+
},
41+
},
42+
],
43+
settings: {
44+
// Support projects where Next.js isn't installed in the root directory (such as a monorepo)
45+
next: {
46+
rootDir: __dirname,
47+
},
48+
},
49+
};

app/.eslintrc.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ For linting, this application is leveraging `eslint`, `prettier` and Nava's [esl
3030

3131
In VSCode, do so by creating a `.vscode/settings.json` file with:
3232

33-
```
33+
```json
3434
{
3535
"editor.codeActionsOnSave": {
3636
"source.fixAll.eslint": true
3737
},
3838
"editor.formatOnSave": true,
39-
"editor.defaultFormatter": "esbenp.prettier-vscode"
39+
"editor.defaultFormatter": "esbenp.prettier-vscode",
40+
"eslint.workingDirectories": ["./app"]
4041
}
4142
```
4243

@@ -48,15 +49,6 @@ For typechecking, this application is leveraging Next.js' [incremental typecheck
4849

4950
Note: make sure TypeScript and Javascript Language Features are enabled in VS Code Extensions.
5051

51-
### Eslint rules explained
52-
53-
- "@typescript-eslint/no-unused-vars": "error"
54-
- Disallows unused variables-- prevents dead code accumulation.
55-
- "@typescript-eslint/no-explicit-any": "error"
56-
- Disallows usage of `any` type. The usage of `any` defeats the purpose of typescript. Consider using `unknown` type instead instead.
57-
- "react/resct-in-jsx-scope": "off"
58-
- suppress errors for missing 'import React' in files because NextJS does this for us.
59-
6052
### Tsconfig additions to auto-generated file
6153

6254
- `target: es6` -- nextJS set this valule to `es5` by default.

0 commit comments

Comments
 (0)