-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.mjs
More file actions
240 lines (216 loc) · 6.43 KB
/
eslint.config.mjs
File metadata and controls
240 lines (216 loc) · 6.43 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
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
import js from '@eslint/js';
import comments from '@eslint-community/eslint-plugin-eslint-comments/configs';
import stylistic from '@stylistic/eslint-plugin';
import configPrettier from 'eslint-config-prettier/flat';
import cypress from 'eslint-plugin-cypress';
import i18nJson from 'eslint-plugin-i18n-json';
import importPlugin from 'eslint-plugin-import-x';
import lodash from 'eslint-plugin-lodash';
import jest from 'eslint-plugin-jest';
import jsxA11y from 'eslint-plugin-jsx-a11y';
import n from 'eslint-plugin-n';
import react from 'eslint-plugin-react';
import reactHooks from 'eslint-plugin-react-hooks';
import globals from 'globals';
import tsEslint from 'typescript-eslint';
import {
uiRules,
unitTestRules,
cypressRules,
nodeProjectRules,
i18nRules,
} from './eslint/rules/index.js';
import packageJson from './package.json' with { type: 'json' };
// Import types
/**
* @typedef {import('typescript-eslint').ConfigWithExtends} ConfigWithExtends
* @typedef {import('typescript-eslint').InfiniteDepthConfigWithExtends} InfiniteDepthConfigWithExtends
* @typedef {import('@typescript-eslint/utils/dist/ts-eslint/Config.d.ts').FlatConfig.Plugin} Plugin
*/
const expectedI18nEslintPluginVersion = '4.0.1';
const i18nEslintPluginVersion =
packageJson?.devDependencies?.['eslint-plugin-i18n-json'];
// At the time of writing eslint-plugin-i18n-json does not provide proper
// ESLint V9 compatible ESLint plugin implementation, so we need to manually
// define a plugin for it. Future versions might provide a proper plugin upstream.
// If you have updated the package, please see if it provides a proper plugin
// implementation and get rid of this custom one, and this version assert.
if (expectedI18nEslintPluginVersion !== i18nEslintPluginVersion) {
throw new Error(
`Expected to eslint-plugin-i18n-json version to be defined in the package.json devDependencies section and to have value '${expectedI18nEslintPluginVersion}' but value is '${i18nEslintPluginVersion}'! Please see eslint.config.mjs for more information.`,
);
}
/**
* @type {Plugin}
*/
const i18nPlugin = {
meta: {
name: 'eslint-plugin-i18n-json',
namespace: 'i18n-json',
version: expectedI18nEslintPluginVersion,
},
rules: i18nJson.rules,
processors: {
json: {
meta: { name: 'json', version: expectedI18nEslintPluginVersion },
...i18nJson.processors['.json'],
},
},
};
/**
* @param {boolean} useReact
* @param {boolean} useNode
* @param {boolean} useJest
* @param {boolean} useCypress
* @return {InfiniteDepthConfigWithExtends}
*/
function getExtends({
react: useReact = false,
node: useNode = false,
jest: useJest = false,
cypress: useCypress = false,
} = {}) {
return [
js.configs.recommended,
...(useReact
? [
react.configs.flat['recommended'],
react.configs.flat['jsx-runtime'],
reactHooks.configs.flat['recommended-latest'],
jsxA11y.flatConfigs['recommended'],
]
: []),
importPlugin.flatConfigs.recommended,
importPlugin.flatConfigs.typescript,
tsEslint.configs.recommended,
comments.recommended,
...(useNode ? [n.configs['flat/recommended-module']] : []),
...(useJest ? [jest.configs['flat/recommended']] : []),
...(useCypress ? [cypress.configs.recommended] : []),
configPrettier,
];
}
/**
* @param {string} tsconfigPath
* @param {ConfigWithExtends} config
* @return ConfigWithExtends
*/
function tsConfig(tsconfigPath, config) {
const userLangOptions = config.languageOptions ?? {};
const userParserOptions = userLangOptions.parserOptions ?? {};
const userSettings = config.settings ?? {};
const userImportResolverSettings = userSettings['import/resolver'] ?? {};
return config;
return {
...config,
languageOptions: {
...userLangOptions,
parserOptions: {
tsconfigRootDir: import.meta.dirname,
projectService: true,
// Allow overriding properties
...userParserOptions,
},
},
settings: {
...userSettings,
'import/resolver': {
typescript: tsconfigPath,
// Allow overriding properties
...userImportResolverSettings,
},
},
};
}
export default tsEslint.config(
// Global ignores
{
ignores: [
'jore4-hasura',
'test-db-manager/dist',
'test-db-manager/ts-dist',
'**/generated',
'ui/jest.setup.ts',
'ui/.next',
'ui/next-env.d.ts',
'ui/out',
'**/.rollup.cache',
],
},
// Generic shared setup for ts/tsx files
{
files: ['**/*.{ts,tsx}'],
languageOptions: {
parserOptions: {
ecmaVersion: 15,
sourceType: 'module',
projectService: true,
},
globals: globals.es2024,
},
settings: {
'import/resolver': {
typescript: {},
},
react: { version: 'detect' },
node: { version: '>=23.9.0' },
},
plugins: {
// Other plugins get automatically added in by the shared configs (extends)
'@stylistic': stylistic,
js,
lodash,
},
},
// UI code
tsConfig('ui/tsconfig.json', {
files: ['ui/**/*.{ts,tsx}'],
ignores: ['ui/**/*.spec.{ts,tsx}'],
languageOptions: { globals: globals.browser },
extends: getExtends({ react: true }),
rules: uiRules,
}),
// Jest based unit/integration tests in UI dir
tsConfig('ui/tsconfig.json', {
files: ['ui/**/*.spec.{ts,tsx}'],
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
},
extends: getExtends({ react: true, node: true, jest: true }),
rules: unitTestRules,
}),
// Localization files
{
files: ['ui/src/locales/**/*.json'],
plugins: { 'i18n-json': i18nPlugin },
processor: 'i18n-json/json',
rules: {
...i18nJson.configs.recommended.rules,
...i18nRules,
},
},
// Cypress
tsConfig('cypress/tsconfig.json', {
files: ['cypress/**/*.ts'],
languageOptions: { globals: globals.node },
extends: getExtends({ node: true, cypress: true }),
rules: cypressRules,
}),
// Test DB Manager
tsConfig('test-db-manager/tsconfig.json', {
files: ['test-db-manager/**/*.ts'],
languageOptions: { globals: globals.node },
extends: getExtends({ node: true }),
rules: nodeProjectRules,
}),
// Codegen
tsConfig('codegen/tsconfig.json', {
files: ['codegen/**/*.ts'],
languageOptions: { globals: globals.node },
extends: getExtends({ node: true }),
rules: nodeProjectRules,
}),
);