-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathupdate-lib-configs.js
104 lines (94 loc) · 2.7 KB
/
update-lib-configs.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
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
/*
This script updates `lib/configs/*.js` files from rule's meta data.
*/
const fs = require('fs')
const path = require('path')
const { FlatESLint } = require('eslint/use-at-your-own-risk')
const { categories } = require('./lib/categories')
const errorCategories = new Set(['base', 'vue2-essential', 'vue3-essential'])
const extendsCategories = {
base: null,
'vue2-essential': 'base',
'vue3-essential': 'base',
'vue2-strongly-recommended': 'vue2-essential',
'vue3-strongly-recommended': 'vue3-essential',
'vue2-recommended': 'vue2-strongly-recommended',
'vue3-recommended': 'vue3-strongly-recommended',
'vue2-use-with-caution': 'vue2-recommended',
'vue3-use-with-caution': 'vue3-recommended'
}
function formatRules(rules, categoryId) {
const obj = Object.fromEntries(
rules.map((rule) => {
let options = errorCategories.has(categoryId) ? 'error' : 'warn'
const defaultOptions =
rule.meta && rule.meta.docs && rule.meta.docs.defaultOptions
if (defaultOptions) {
const v = categoryId.startsWith('vue3') ? 3 : 2
const defaultOption = defaultOptions[`vue${v}`]
if (defaultOption) {
options = [options, ...defaultOption]
}
}
return [rule.ruleId, options]
})
)
return JSON.stringify(obj, null, 2)
}
function formatCategory(category) {
const extendsCategoryId = extendsCategories[category.categoryId]
if (extendsCategoryId == null) {
return `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "npm run update"
*/
module.exports = {
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'vue'
],
rules: ${formatRules(category.rules, category.categoryId)},
overrides: [
{
files: '*.vue',
parser: require.resolve('vue-eslint-parser')
}
]
}
`
}
return `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update its content execute "npm run update"
*/
module.exports = {
extends: require.resolve('./${extendsCategoryId}'),
rules: ${formatRules(category.rules, category.categoryId)}
}
`
}
// Update files.
const ROOT = path.resolve(__dirname, '../lib/configs/')
for (const category of categories) {
const filePath = path.join(ROOT, `${category.categoryId}.js`)
const content = formatCategory(category)
fs.writeFileSync(filePath, content)
}
// Format files.
async function format() {
const linter = new FlatESLint({ fix: true })
const report = await linter.lintFiles([ROOT])
FlatESLint.outputFixes(report)
}
format()