-
Notifications
You must be signed in to change notification settings - Fork 8
/
eslint.config.mjs
210 lines (196 loc) · 6.68 KB
/
eslint.config.mjs
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
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import stylisticPlugin from '@stylistic/eslint-plugin'
import typescriptEslintPlugin from '@typescript-eslint/eslint-plugin'
import typescriptEslintParser from '@typescript-eslint/parser'
import prettierConfig from 'eslint-config-prettier'
import pluginImport from 'eslint-plugin-import'
import pluginImportConfig from 'eslint-plugin-import/config/recommended.js'
import prettierPlugin from 'eslint-plugin-prettier'
import globals from 'globals'
const projectDirname = dirname(fileURLToPath(import.meta.url))
const context = (() => {
if (typeof process.env.NODE_ENV === 'undefined') return 'default'
if (process.env.NODE_ENV === 'development') return 'dev'
if (process.env.NODE_ENV === 'production') return 'prod'
if (process.env.NODE_ENV === 'repo') return 'repo'
new Error('Invalid NODE_ENV')
return 'error'
})()
const projectFilesToIgnore = context === 'repo' ? [] : ['.release-it.ts', 'zotero-plugin.config.ts']
const tsconfig = (() => {
if (context === 'default') return './tsconfig.json'
if (context === 'dev') return './tsconfig.dev.json'
if (context === 'prod') return './tsconfig.prod.json'
if (context === 'repo') return './tsconfig.repo.json'
new Error('Invalid context')
return 'error'
})()
const allTsExtensionsArray = ['ts', 'mts', 'cts', 'tsx', 'mtsx']
const allJsExtensionsArray = ['js', 'mjs', 'cjs', 'jsx', 'mjsx']
const allTsExtensions = allTsExtensionsArray.join(',')
const allJsExtensions = allJsExtensionsArray.join(',')
const allExtensions = [...allTsExtensionsArray, ...allJsExtensionsArray].join(',')
const importRules = {
'import/no-unresolved': 'error',
'import/namespace': 'off',
'sort-imports': [
'error',
{
allowSeparatedGroups: true,
ignoreCase: true,
ignoreDeclarationSort: true,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
},
],
'import/order': [
'error',
{
'groups': [
'builtin', // Built-in imports (come from NodeJS native) go first
'external', // External imports
'internal', // Absolute imports
'parent', // Relative imports
'sibling', // Relative imports
// ['sibling', 'parent'], // Relative imports, the sibling and parent types they can be mingled together
'index', // index imports
'type', // type imports
'object', // object imports
'unknown', // unknown
],
'newlines-between': 'always',
'alphabetize': {
order: 'asc',
caseInsensitive: true, // ignore case
},
},
],
}
const baseRules = {
'prettier/prettier': 'warn',
'@stylistic/max-len': [
'warn',
{ code: 120, ignoreComments: true, ignoreTrailingComments: true, ignoreStrings: true, ignoreUrls: true },
],
'@stylistic/indent': ['error', 2, { SwitchCase: 1 }],
'@stylistic/semi': ['error', 'never'],
'@stylistic/quotes': ['warn', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
'@stylistic/object-curly-spacing': ['warn', 'always'],
'@stylistic/array-element-newline': ['error', 'consistent'],
// '@stylistic/multiline-ternary': ['warn', 'always'],
}
const typescriptRules = {
...prettierConfig.rules,
...pluginImportConfig.rules,
...typescriptEslintPlugin.configs.recommended.rules,
...typescriptEslintPlugin.configs['recommended-type-checked'].rules,
//
// ...typescriptEslintPlugin.configs.strict.rules,
// ...typescriptEslintPlugin.configs['strict-type-checked'].rules,
//
...typescriptEslintPlugin.configs['stylistic-type-checked'].rules,
...stylisticPlugin.configs['disable-legacy'].rules,
...importRules,
...baseRules,
}
const javascriptRules = {}
const typescriptRulesDev = {
'@typescript-eslint/no-explicit-any': ['off', { ignoreRestArgs: true }],
'@typescript-eslint/no-unsafe-assignment': ['warn'],
'@typescript-eslint/no-unsafe-member-access': ['off'],
'@typescript-eslint/no-unsafe-return': ['warn'],
'@typescript-eslint/no-unsafe-argument': ['warn'],
'@typescript-eslint/no-unsafe-call': ['off'],
'@typescript-eslint/no-unused-vars': ['off'],
'@typescript-eslint/prefer-nullish-coalescing': ['off'],
'@typescript-eslint/no-inferrable-types': ['off'],
'@typescript-eslint/no-floating-promises': ['warn'],
'@typescript-eslint/require-await': ['warn'],
// '@typescript-eslint/dot-notation': ['off'],
// '@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/ban-ts-comment': [
'warn',
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': 'allow-with-description',
'ts-nocheck': 'allow-with-description',
'ts-check': 'allow-with-description',
},
],
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
}
const javascriptRulesDev = {
'@typescript-eslint/no-unused-vars': ['warn'],
}
const config = [
{
/* setup parser for all files */
files: [`**/*.{${allTsExtensions}}`],
languageOptions: {
parser: typescriptEslintParser,
parserOptions: {
ecmaVersion: 'latest', // 2024 sets the ecmaVersion parser option to 15
tsconfigRootDir: resolve(projectDirname),
sourceType: 'module',
project: tsconfig,
},
},
},
{
/* all typescript files, except config files */
files: [`**/*.{${allTsExtensions}}`],
ignores: [`**/*.config.{${allTsExtensions}}`],
languageOptions: {
globals: {
...globals.browser,
// ...globals.node,
...globals.es2021,
},
},
plugins: {
'@typescript-eslint': typescriptEslintPlugin,
'@stylistic': stylisticPlugin,
'import': pluginImport,
'prettier': prettierPlugin,
},
rules: {
...typescriptRules,
},
},
{
/* +strict for typescript files NOT in ./src/ folder */
files: [`**/*.{${allTsExtensions}}`],
ignores: [`src/**/*.{${allTsExtensions}}`, `typing/**/*.d.ts`, `**/*.config.{${allTsExtensions}}`],
plugins: {
'@typescript-eslint': typescriptEslintPlugin,
'@stylistic': stylisticPlugin,
},
rules: {
...typescriptEslintPlugin.configs.strict.rules,
...typescriptEslintPlugin.configs['strict-type-checked'].rules,
},
},
{
/* +lenient for typescript files in ./src/ folder */
files: [`src/**/*.{${allTsExtensions}}`, `typing/**/*.d.ts`],
ignores: [`**/*.config.{${allTsExtensions}}`],
settings: {
'import/resolver': {
node: {
extensions: ['.ts'],
},
typescript: {
extensions: ['.ts', '.d.ts'],
},
},
},
rules: {
...typescriptRulesDev,
},
},
{
ignores: ['build', 'scripts', '**/*.js', '**/*.bak', ...projectFilesToIgnore],
},
]
export default config