forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-lib-configs.js
97 lines (87 loc) · 2.32 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
/**
* @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 eslint = require('eslint')
const categories = require('./lib/categories')
const errorCategories = ['base', 'essential', 'vue3-essential']
const extendsCategories = {
base: null,
essential: 'base',
'vue3-essential': 'base',
'strongly-recommended': 'essential',
'vue3-strongly-recommended': 'vue3-essential',
recommended: 'strongly-recommended',
'vue3-recommended': 'vue3-strongly-recommended',
'use-with-caution': 'recommended',
'vue3-use-with-caution': 'vue3-recommended'
}
function formatRules(rules, categoryId) {
const obj = rules.reduce((setting, rule) => {
setting[rule.ruleId] = errorCategories.includes(categoryId)
? 'error'
: 'warn'
return setting
}, {})
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 it's content execute "npm run update"
*/
module.exports = {
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
}
},
env: {
browser: true,
es6: true
},
plugins: [
'vue'
],
rules: ${formatRules(category.rules, category.categoryId)}
}
`
}
return `/*
* IMPORTANT!
* This file has been automatically generated,
* in order to update it's 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/')
categories.forEach((category) => {
const filePath = path.join(ROOT, `${category.categoryId}.js`)
const content = formatCategory(category)
fs.writeFileSync(filePath, content)
})
// Format files.
async function format() {
const linter = new eslint.ESLint({ fix: true })
const report = await linter.lintFiles([ROOT])
eslint.ESLint.outputFixes(report)
}
format()