-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint.config.mjs
134 lines (118 loc) · 4 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
import { configApp, IGNORE_LIST, INCLUDE_LIST, RULES_LIST } from '@adonisjs/eslint-config'
import eslint from '@eslint/js'
import { createOxcImportResolver } from 'eslint-import-resolver-oxc'
// import globals from 'globals'
import eslintPluginImportX from 'eslint-plugin-import-x'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'
import tseslint from 'typescript-eslint'
const keysToRemove = ['unicorn/no-null']
const customDefaultRules = {
'unicorn/prevent-abbreviations': [
'error',
{
allowList: {
env: true,
ctx: true,
dbConfig: true,
},
},
],
'@typescript-eslint/return-await': ['error', 'always'],
'unicorn/consistent-function-scoping': ['error', { checkArrowFunctions: false },]
}
const eslintRecommendedRules = extractRules([eslint.configs.recommended])
const unicornRecommendedRules = extractRules([eslintPluginUnicorn.configs['flat/recommended']])
const strictTypeCheckedRules = extractRules(tseslint.configs.strictTypeChecked)
const stylisticTypeCheckedRules = extractRules(tseslint.configs.stylisticTypeChecked)
const eslintPluginImportXRules = eslintPluginImportX.flatConfigs.recommended.rules
const customConfigObject = {
name: 'Custom Config',
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
files: [...INCLUDE_LIST, '**/*.tsx', '**/*.mts', '**/*.cts'],
ignores: [...IGNORE_LIST],
rules: deepMerge(
eslintRecommendedRules,
unicornRecommendedRules,
strictTypeCheckedRules,
stylisticTypeCheckedRules,
modifyUnicornKeys(RULES_LIST),
customDefaultRules,
eslintPluginImportXRules
),
}
const mergedDefaultConfig = deepMerge(...configApp())
// Ensure all the unicorn keys are in the correct format
mergedDefaultConfig.rules = modifyUnicornKeys(mergedDefaultConfig.rules)
delete mergedDefaultConfig.plugins['@unicorn']
mergedDefaultConfig.plugins.unicorn = eslintPluginUnicorn
// Ensure all the plugins merged into a single config
mergedDefaultConfig.plugins = deepMerge(
mergedDefaultConfig.plugins,
eslintPluginImportX.flatConfigs.recommended.plugins
)
const mergedConfig = deepMerge(mergedDefaultConfig, customConfigObject)
const resolverNext = {
'import-x/resolver-next': [createOxcImportResolver()],
}
mergedConfig.settings = resolverNext
// Remove rules if required
mergedConfig.rules = removeRules(mergedConfig.rules, keysToRemove)
// console.dir(mergedConfig, {depth: 1})
export default [mergedConfig]
// Helper Functions
function extractRules(configObjectsList) {
const rulesObjects = configObjectsList
.filter((configObject) => configObject.rules !== undefined)
.map((configObject) => configObject.rules)
.reduce((accumulator, object) => ({ ...accumulator, ...object }))
return rulesObjects
}
function modifyUnicornKeys(unicornRuleObject) {
return Object.fromEntries(
Object.entries(unicornRuleObject).map(([key, value]) => {
if (key.startsWith('@unicorn')) {
return [key.slice(1), value]
}
return [key, value]
})
)
}
function removeRules(ruleObject, keysToRemove) {
if (keysToRemove.length !== 0) {
return Object.fromEntries(
Object.entries(ruleObject)
.map(([key, value]) => {
if (keysToRemove.includes(key)) {
return undefined
}
return [key, value]
})
.filter(Boolean)
)
} else {
return ruleObject
}
}
function deepMerge(...objects) {
return objects.reduce((mergedObject, currentObject) => {
for (const [key, value] of Object.entries(currentObject)) {
mergedObject[key] =
value &&
typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Object]' &&
mergedObject[key] &&
typeof mergedObject[key] === 'object' &&
Object.prototype.toString.call(mergedObject[key]) === '[object Object]'
? deepMerge(mergedObject[key], value)
: value
}
return mergedObject
}, {})
}