-
Notifications
You must be signed in to change notification settings - Fork 9
/
eslint.config.js
419 lines (382 loc) · 14 KB
/
eslint.config.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// @ts-check
// TODO: consider using any of the zillion "TypeScript at runtime in Node"
// options so we can configure ESLint with a .ts file. Managing the types in JS
// syntax is maddening and highly error prone.
// TODO: look into a rule about aging TODOs. One from past usage required
// annotating with a date, but it'd be nice if we could just derive that from
// git history or whatever.
/// <reference path="./vendor-types/@vue/eslint-config-typescript/index.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-no-only-tests.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/configs/base.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/configs/vue3-essential.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/configs/vue3-strongly-recommended.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/configs/vue3-recommended.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/index.d.ts" />
/// <reference path="./vendor-types/eslint-plugin-vue/lib/processor.d.ts" />
import eslint from '@eslint/js';
import eslintConfigPrettier from 'eslint-config-prettier';
import jsdoc from 'eslint-plugin-jsdoc';
import noOnlyTestsPlugin from 'eslint-plugin-no-only-tests';
import prettierVuePlugin from 'eslint-plugin-prettier-vue';
import vuePlugin from 'eslint-plugin-vue';
import vueBase from 'eslint-plugin-vue/lib/configs/base.js';
import vue3Essential from 'eslint-plugin-vue/lib/configs/vue3-essential.js';
import vue3Recommended from 'eslint-plugin-vue/lib/configs/vue3-recommended.js';
import vue3StronglyRecommended from 'eslint-plugin-vue/lib/configs/vue3-strongly-recommended.js';
import vueProcessor from 'eslint-plugin-vue/lib/processor.js';
import { builtinModules } from 'node:module';
import tseslint from 'typescript-eslint';
import vueESLintParser from 'vue-eslint-parser';
/**
* @param {string} pathSansExtension
* @param {readonly string[]} extensions
* @returns {string}
*/
const extensionsGlob = (pathSansExtension, extensions) => {
const extensionsGlobPattern = `.{${extensions.join(',')}}`;
return `${pathSansExtension}${extensionsGlobPattern}`;
};
const typeScriptFileExtensions = /** @type {const} */ ([
'cjs',
'cts',
'js',
'jsx',
'mjs',
'mts',
'ts',
'tsx',
]);
const vueFileExtensions = [...typeScriptFileExtensions, 'vue'];
/**
* @param {string} pathSansExtensions
*/
const vueGlob = (pathSansExtensions) => {
return extensionsGlob(pathSansExtensions, vueFileExtensions);
};
const vuePackageGlob = vueGlob('packages/web-forms/**/*');
/**
* @typedef {import('eslint').Linter.FlatConfig} FlatConfig
*/
export default tseslint.config(
{
ignores: [
'.changeset',
'.github',
'examples/**/*',
'packages/*/index.d.ts',
'packages/**/*.min.js',
'packages/**/dist/**/*',
'packages/common/fixtures/test-javarosa/**/*',
'packages/tree-sitter-xpath/grammar.js',
'packages/tree-sitter-xpath/bindings/**/*',
'packages/tree-sitter-xpath/types/**/*',
'packages/ui-solid/**/*',
'packages/web-forms/dist-demo/**/*',
'packages/xforms-engine/api-docs/**/*',
'**/vendor',
],
},
{
plugins: { jsdoc },
rules: {
'jsdoc/no-undefined-types': [
'error',
{
markVariablesAsUsed: true,
disableReporting: true,
},
],
},
},
{
extends: [
eslint.configs.recommended,
// TODO: consider applying just the plugin and parser here, and applying
// the base rules in a separate config (as was done with the Vue split,
// also below).
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
/**
* For future reference, there's a **lot** going on here. As briefly as
* reasonably possible, all of this happens for the `web-forms` package:
*
* 1. ESLint uses the Vue parser (`vue-eslint-parser`)
* 2. The Vue parser, in turn, uses the TypeScript parser
* (`@typescript-eslint/parser`) for:
*
* - `<script>` and `<script lang="ts">` within .vue files
* - JavaScript and TypeScript source files
* 3. The Vue parser uses the espree parser (ESLint's default) for
* `<template>` within .vue files
* 4. To ensure all of these play nicely, these plugins need to be (re-)
* defined:
*
* - `eslint-plugin-vue`
* - `@typescript-eslint`
* 5. The Vue plugin's **processor** must also be (re-)applied.
*
* Absence of any of these caused either ESLint to fail to apply shared
* rules in affected areas, or ESLint itself to crash.
*
* Note: Contrary to the defaults provided in a Vue template project, it
* was found that linting applies most consistently by applying
* Vue-specific parsing **after** TypeScript.
*/
{
files: [vuePackageGlob],
languageOptions: {
parser: vueESLintParser,
parserOptions: {
/**
* @see {@link https://github.com/vuejs/vue-eslint-parser/issues/173#issuecomment-1367298274}
*/
parser: {
// Script parser for `<script>` (without `lang` attribute)
js: '@typescript-eslint/parser',
// Script parser for `<script lang="ts">`
ts: '@typescript-eslint/parser',
// Script parser for vue directives (e.g. `v-if=` or `:attribute=`)
// and vue interpolations (e.g. `{{variable}}`).
// If not specified, the parser determined by `<script lang ="...">` is used.
'<template>': 'espree',
},
},
},
plugins: {
vue: vuePlugin,
'prettier-vue': prettierVuePlugin,
'@typescript-eslint': tseslint.plugin,
},
processor: vueProcessor,
settings: {
'prettier-vue': {
SFCBlocks: {
template: false,
script: true,
style: true,
},
fileInfoOptions: {
ignorePath: './packages/ui-vue/.prettierignore',
},
},
},
},
eslintConfigPrettier,
],
languageOptions: {
parserOptions: {
ecmaFeatures: {
modules: true,
},
ecmaVersion: 'latest',
/**
* This **mostly** works, instructing the TypeScript parser and plugin
* to regard Vue modules as (special) TypeScript source modules. There
* is one known issue: when importing a .vue file within a traditional
* TypeScript module, there is some disagreement between aspects of the
* tools about what has been imported:
*
* - VSCode's TypeScript language server will not understand the import,
* but this is addressed by the Volar VSCode extension. (Some
* resources discuss something called "take-over mode", which I did
* not need to enable explicitly; but there's a good chance this may
* be an issue depending on a user's local setup.)
*
* With Volar working as expected, the import is recognized in-editor
* as a type representing a Vue module.
*
* - ESLint recognizes the import.
*
* - `@typescript-eslint` partially recognizes the import. Some rules
* which might be affected apply correctly. Rules which depend on type
* information, however, apparently fail to recognize the type that
* the Volar plugin recognizes. There is a variety of discussion that
* can be tracked down on the topic, but from what I can tell this is
* a breakdown in how the `@typescript-eslint` and `vue-plugin-eslint`
* internals communicate about the `typescript` package's internal
* `Program` representation. I do not believe this can be meaningfully
* resolved at this time without undue effort. In the meantime, the
* most likely workaround will be something like this (using the
* `router.ts` from Vue's template project as an example):
*
* ```ts
* import { createRouter, createWebHistory } from 'vue-router';
* import HomeView from '../views/HomeView.vue';
*
* const router = createRouter({
* history: createWebHistory(import.meta.env.BASE_URL),
* routes: [
* {
* path: '/',
* name: 'home',
* // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
* component: HomeView,
* },
* ],
* });
* ```
*
* In particular, note that the
* `@typescript-eslint/no-unsafe-assignment` rule is disabled where
* the imported Vue component is assigned to a property. This is due
* to a manifest of this breakdown between the tool internals
* reasoning about the underlying TypeScript `Program`: as far as
* `@typescript-eslint` is aware, the value has an `any` type (and we
* error when assigning `any` values, even though their type is
* technically assignable, as an extra safety check).
*/
extraFileExtensions: ['.vue'],
project: [
'./tsconfig.json',
'./tsconfig.tools.json',
'./tsconfig.vendor-types.json',
'./examples/*/tsconfig.json',
'./packages/**/tsconfig.json',
// NOTE: `@typescript-eslint` doesn't resolve composite projects. For
// now we just list all of these directly. In the future, we should
// consider whether we can have a single composite config for the full
// project. This is something I wanted to explore on project init, but
// backed off because the documentation was kind of impenetrable. But
// Vue's project template does a great job of illustrating the use,
// and I suspect we'd benefit from applying the pattern throughout the
// entire monorepo.
'./packages/web-forms/tsconfig.json',
'./packages/web-forms/tsconfig.app.json',
'./packages/web-forms/tsconfig.node.json',
'./packages/web-forms/tsconfig.vitest.json',
'./scripts/tsconfig.json',
],
},
sourceType: 'module',
},
linterOptions: {
reportUnusedDisableDirectives: true,
},
plugins: {
'no-only-tests': noOnlyTestsPlugin,
},
// Base rules, applied across project and throughout all packages (unless
// overridden in subsequent configs)
rules: {
// This override is recommended by typescript-eslint, because TypeScript
// does a much better job of it, and gives us better control over which
// globals are present in a given context. This also obviates any need
// for specifying other globals in this config.
'no-undef': 'off',
'@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
'@typescript-eslint/no-unused-vars': [
'error',
{ argsIgnorePattern: '^_', ignoreRestSiblings: true },
],
'no-only-tests/no-only-tests': 'error',
'@typescript-eslint/no-shadow': 'error',
'no-console': 'error',
'@typescript-eslint/class-literal-property-style': 'error',
'@typescript-eslint/consistent-indexed-object-style': 'error',
'@typescript-eslint/consistent-type-definitions': 'error',
'@typescript-eslint/dot-notation': [
'error',
{
allowIndexSignaturePropertyAccess: true,
},
],
'@typescript-eslint/no-base-to-string': 'error',
'@typescript-eslint/no-empty-function': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-redundant-type-constituents': 'error',
'@typescript-eslint/no-this-alias': 'error',
'@typescript-eslint/no-unsafe-argument': 'error',
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'@typescript-eslint/only-throw-error': 'warn',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/prefer-string-starts-ends-with': 'error',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/restrict-template-expressions': 'error',
'@typescript-eslint/sort-type-constituents': 'warn',
'@typescript-eslint/unbound-method': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-empty-interface': [
'error',
{
allowSingleExtends: true,
},
],
'@typescript-eslint/no-empty-object-type': [
'error',
{
allowInterfaces: 'with-single-extends',
},
],
'prefer-const': 'error',
// Ensure Node built-ins aren't used by default
'no-restricted-imports': [
'error',
{
paths: [...builtinModules],
patterns: ['node:*'],
},
],
},
},
{
files: [vuePackageGlob],
/**
* These are the rules applied by the Vue project template. We can of course
* refine from there to suit our needs.
*
* TODO: The Vue template project included only the "essential" rules by
* default. We've added both the "strongly recommended" and "recommended"
* sets, but we'll likely want to relax and/or configure some of them as we
* run into issues.
*/
rules: {
...vueBase.rules,
...vue3Essential.rules,
...vue3StronglyRecommended.rules,
...vue3Recommended.rules,
// See explanation of typescript-eslint recommendation above
'no-undef': 'off',
// Consistent with all other source code
'prettier-vue/prettier': 'error',
'vue/html-indent': ['error', 'tab'],
'vue/html-comment-indent': ['error', 'tab'],
'vue/script-indent': ['error', 'tab'],
// should be based on the printWidth
'vue/max-attributes-per-line': 'off',
'vue/no-undef-components': 'error',
'vue/no-empty-component-block': 'error',
// Consistent with Prettier behavior, in all other source code
'eol-last': ['error', 'always'],
'no-trailing-spaces': 'error',
'no-mixed-spaces-and-tabs': 'error',
},
},
{
files: ['eslint.config.js', 'packages/**/*.d.ts', 'vendor-types/**/*.d.ts'],
rules: {
'@typescript-eslint/triple-slash-reference': 'off',
},
},
{
files: [
'eslint.config.js',
'scripts/**/*.js',
'packages/*/playwright.config.ts',
'packages/*/vite.config.ts',
'packages/*/vitest.config.ts',
'packages/xforms-engine/vite.*.config.ts',
'packages/*/tools/**/*',
'packages/tree-sitter-xpath/scripts/build/*.mjs',
],
rules: {
'no-restricted-imports': 'off',
},
}
);