Repository navigation
Expand file tree
/
Copy patheslint.config.js
More file actions
214 lines (200 loc) · 7.45 KB
/
Copy patheslint.config.js
File metadata and controls
214 lines (200 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import js from '@eslint/js';
import { defineConfig, globalIgnores } from 'eslint/config';
import prettier from 'eslint-config-prettier';
import astro from 'eslint-plugin-astro';
import jsxA11y from 'eslint-plugin-jsx-a11y';
import globals from 'globals';
import tseslint from 'typescript-eslint';
/**
* Flat ESLint configuration, ordered from broadest to most specific.
*
* Layer order matters: base JS rules, then the `typescript-eslint` type-aware
* presets, then Astro/a11y support, then targeted relaxations, and finally
* `eslint-config-prettier` so formatting is owned by Prettier alone.
*
* `.astro` templates are parsed by `astro-eslint-parser`, which cannot fully
* resolve types inside template expressions. A small set of type-aware rules is
* therefore disabled for `.astro` only — see the comment on that block.
*/
export default defineConfig([
globalIgnores([
'dist/',
'node_modules/',
'.astro/',
'.vercel/',
'.workspace/',
'pnpm-lock.yaml',
// Imported verbatim from the book and produced by scripts/tutorial/build.mjs.
'src/tutorial/content/',
'src/tutorial/source-figures/',
'src/tutorial/notes/',
'src/tutorial/code/',
'src/data/tutorial/generated/',
]),
js.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
...globals.browser,
...globals.node,
},
},
linterOptions: {
reportUnusedDisableDirectives: 'error',
reportUnusedInlineConfigs: 'error',
},
rules: {
// Correctness
eqeqeq: ['error', 'always', { null: 'ignore' }],
'no-implicit-coercion': ['error', { boolean: false }],
'no-param-reassign': ['error', { props: true }],
'no-unneeded-ternary': 'error',
'no-useless-rename': 'error',
'object-shorthand': ['error', 'always'],
'prefer-const': ['error', { destructuring: 'all' }],
'prefer-template': 'error',
// Type safety
'@typescript-eslint/consistent-type-exports': [
'error',
{ fixMixedExportsWithInlineTypeSpecifier: true },
],
'@typescript-eslint/consistent-type-imports': [
'error',
{ fixStyle: 'inline-type-imports', prefer: 'type-imports' },
],
'@typescript-eslint/explicit-function-return-type': [
'error',
{ allowExpressions: true, allowTypedFunctionExpressions: true },
],
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/method-signature-style': ['error', 'property'],
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/no-unnecessary-condition': [
'error',
{ allowConstantLoopConditions: 'only-allowed-literals' },
],
'@typescript-eslint/no-unsafe-type-assertion': 'error',
'@typescript-eslint/prefer-readonly': 'error',
'@typescript-eslint/require-array-sort-compare': ['error', { ignoreStringArrays: true }],
'@typescript-eslint/strict-boolean-expressions': [
'error',
{
allowAny: false,
allowNullableBoolean: false,
allowNullableEnum: false,
allowNullableNumber: false,
allowNullableObject: false,
allowNullableString: false,
allowNumber: false,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
allowString: false,
},
],
'@typescript-eslint/switch-exhaustiveness-check': [
'error',
{ considerDefaultExhaustiveForUnions: true },
],
},
},
// Astro components: recommended Astro rules plus strict accessibility checks.
...astro.configs.recommended,
...astro.configs['flat/jsx-a11y-strict'],
{
files: ['**/*.astro'],
languageOptions: {
parserOptions: {
// `astro-eslint-parser` supports the legacy `project` option only.
project: true,
projectService: false,
tsconfigRootDir: import.meta.dirname,
},
},
plugins: { 'jsx-a11y': jsxA11y },
rules: {
...jsxA11y.flatConfigs.strict.rules,
// Template expressions in `.astro` are not fully typed by
// `astro-eslint-parser`, so the `no-unsafe-*` family reports on every
// rendered expression. They stay enabled for `.ts`/`.js` sources below.
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
// `{value && <markup />}` is the idiomatic guard in Astro templates and
// `value` is already `T | undefined` from the component props.
'@typescript-eslint/strict-boolean-expressions': [
'error',
{
allowNullableBoolean: true,
allowNullableNumber: true,
allowNullableObject: true,
allowNullableString: true,
},
],
// Frontmatter values are typed through `Props`, not through inference.
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off',
// Astro specific conventions.
'astro/no-set-html-directive': 'error',
'astro/prefer-class-list-directive': 'error',
'astro/prefer-object-class-list': 'error',
'astro/prefer-split-class-list': 'error',
'astro/sort-attributes': 'error',
},
},
// Tests may lean on non-null assertions and looser return types.
{
files: ['**/*.test.ts', '**/*.spec.ts', 'tests/**/*.ts'],
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
},
},
// Config files are plain scripts with dynamic imports.
{
files: ['**/*.config.{js,mjs,ts}', 'eslint.config.js'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
// Config files import packages that ship no type declarations
// (e.g. `eslint-plugin-jsx-a11y`), so member access cannot be typed.
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
},
},
// `scripts/` holds plain JavaScript that Node runs directly, with no type
// annotations to check. The base rules still apply; the rules that need type
// information are turned off rather than fed guesses.
{
files: ['scripts/**/*.{js,mjs}'],
...tseslint.configs.disableTypeChecked,
rules: {
...tseslint.configs.disableTypeChecked.rules,
// Plain JavaScript has no type annotations to declare.
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
prettier,
{
/*
The tutorial generator scripts are plain Node ESM. They are still
type-checked by `astro check`, but the TypeScript-syntax rules below
cannot apply to a `.mjs` file that carries no type annotations.
*/
files: ['scripts/**/*.{mjs,js}'],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
},
},
]);