Skip to content

Commit 5a646be

Browse files
committed
feat(vue-cli-plugin-eslint): Add uncategorized category, add tests
1 parent 1a35def commit 5a646be

File tree

5 files changed

+104
-11
lines changed

5 files changed

+104
-11
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const configDescriptor = require('../ui/configDescriptor')
2+
const { getEslintConfigName, getDefaultValue } = configDescriptor
3+
4+
describe('getEslintConfigName', () => {
5+
describe('for "extend" of string type', () => {
6+
it('returns null if it doesn\'t extend vue plugin config', () => {
7+
expect(getEslintConfigName({
8+
extends: 'eslint:recommended'
9+
})).toBe(null)
10+
})
11+
12+
it('returns vue config used', () => {
13+
expect(getEslintConfigName({
14+
extends: 'plugin:vue/recommended'
15+
})).toBe('plugin:vue/recommended')
16+
})
17+
})
18+
19+
describe('for "extend" of array type', () => {
20+
it('returns null if it doesn\'t extend vue plugin config', () => {
21+
expect(getEslintConfigName({
22+
extends: ['eslint:recommended', 'standard']
23+
})).toBe(null)
24+
})
25+
26+
it('returns vue config used', () => {
27+
expect(getEslintConfigName({
28+
extends: ['eslint:recommended', 'plugin:vue/recommended']
29+
})).toBe('plugin:vue/recommended')
30+
})
31+
})
32+
})
33+
34+
describe('getDefaultValue', () => {
35+
const getResult = (config, ruleCategory) => {
36+
const rule = {
37+
meta: {
38+
docs: {
39+
category: ruleCategory
40+
}
41+
}
42+
}
43+
44+
const data = {
45+
eslint: {
46+
extends: config
47+
}
48+
}
49+
50+
return getDefaultValue(rule, data)
51+
}
52+
53+
it('returns "ERROR" value if the rule belongs to the selected configuration', () => {
54+
expect(getResult('plugin:vue/base', 'base')).toBe('error')
55+
expect(getResult('plugin:vue/essential', 'base')).toBe('error')
56+
expect(getResult('plugin:vue/essential', 'essential')).toBe('error')
57+
expect(getResult('plugin:vue/strongly-recommended', 'base')).toBe('error')
58+
expect(getResult('plugin:vue/strongly-recommended', 'essential')).toBe('error')
59+
expect(getResult('plugin:vue/strongly-recommended', 'strongly-recommended')).toBe('error')
60+
expect(getResult('plugin:vue/recommended', 'base')).toBe('error')
61+
expect(getResult('plugin:vue/recommended', 'essential')).toBe('error')
62+
expect(getResult('plugin:vue/recommended', 'strongly-recommended')).toBe('error')
63+
expect(getResult('plugin:vue/recommended', 'recommended')).toBe('error')
64+
})
65+
66+
it('returns "OFF" value if the rule doesn\'t belong to the selected configuration', () => {
67+
expect(getResult('plugin:vue/base', 'essential')).toBe('off')
68+
expect(getResult('plugin:vue/base', 'strongly-recommended')).toBe('off')
69+
expect(getResult('plugin:vue/base', 'recommended')).toBe('off')
70+
expect(getResult('plugin:vue/essential', 'strongly-recommended')).toBe('off')
71+
expect(getResult('plugin:vue/essential', 'recommended')).toBe('off')
72+
expect(getResult('plugin:vue/strongly-recommended', 'recommended')).toBe('off')
73+
expect(getResult('plugin:vue/base', undefined)).toBe('off')
74+
expect(getResult('plugin:vue/essential', undefined)).toBe('off')
75+
expect(getResult('plugin:vue/strongly-recommended', undefined)).toBe('off')
76+
expect(getResult('plugin:vue/recommended', undefined)).toBe('off')
77+
})
78+
})

packages/@vue/cli-plugin-eslint/ui/configDescriptor.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const CATEGORIES = [
44
'base',
55
'essential',
66
'strongly-recommended',
7-
'recommended'
7+
'recommended',
8+
'uncategorized'
89
]
910

1011
const DEFAULT_CATEGORY = 'essential'
@@ -35,15 +36,15 @@ function getEslintConfigName (eslint) {
3536
config = eslint.extends.find(configName => configName.startsWith('plugin:vue/'))
3637
}
3738

38-
return config
39+
return config && config.startsWith('plugin:vue/') ? config : null
3940
}
4041

4142
// Sets default value regarding selected global config
4243
function getDefaultValue (rule, data) {
4344
const { category: ruleCategory } = rule.meta.docs
4445
const currentCategory = getEslintConfigName(data.eslint)
4546

46-
if (!currentCategory) return RULE_SETTING_OFF
47+
if (!currentCategory || ruleCategory === undefined) return RULE_SETTING_OFF
4748

4849
return CATEGORIES.indexOf(ruleCategory) <= CATEGORIES.indexOf(currentCategory.split('/')[1])
4950
? RULE_SETTING_ERROR
@@ -60,7 +61,10 @@ function getEslintPrompts (data, rules) {
6061
return CATEGORIES
6162
.map(category =>
6263
allRules.filter(rule =>
63-
rule.meta.docs.category === category
64+
rule.meta.docs.category === category || (
65+
category === 'uncategorized' &&
66+
rule.meta.docs.category === undefined
67+
)
6468
)
6569
)
6670
.reduce((acc, rulesArr) => [...acc, ...rulesArr], [])
@@ -73,7 +77,7 @@ function getEslintPrompts (data, rules) {
7377
name: rule.name,
7478
type: 'list',
7579
message: rule.name,
76-
group: `org.vue.eslint.config.eslint.groups.${rule.meta.docs.category}`,
80+
group: `org.vue.eslint.config.eslint.groups.${rule.meta.docs.category || 'uncategorized'}`,
7781
description: rule.meta.docs.description,
7882
link: rule.meta.docs.url,
7983
default: JSON.stringify(getDefaultValue(rule, data)),
@@ -113,7 +117,7 @@ function onRead ({ data, cwd }) {
113117
description: 'org.vue.eslint.config.eslint.general.config.description',
114118
link: 'https://github.com/vuejs/eslint-plugin-vue',
115119
default: `plugin:vue/${DEFAULT_CATEGORY}`,
116-
choices: CATEGORIES.map(category => ({
120+
choices: CATEGORIES.filter(category => category !== 'uncategorized').map(category => ({
117121
name: `org.vue.eslint.config.eslint.groups.${category}`,
118122
value: `plugin:vue/${category}`
119123
})),
@@ -154,7 +158,7 @@ async function onWrite ({ data, api, prompts }) {
154158
api.setData('vue', vueData)
155159
}
156160

157-
module.exports = {
161+
const config = {
158162
id: CONFIG,
159163
name: 'ESLint configuration',
160164
description: 'org.vue.eslint.config.eslint.description',
@@ -173,3 +177,10 @@ module.exports = {
173177
onRead,
174178
onWrite
175179
}
180+
181+
module.exports = {
182+
config,
183+
getEslintConfigName,
184+
getDefaultValue,
185+
getEslintPrompts
186+
}

packages/@vue/cli-plugin-eslint/ui/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const CONFIG = 'org.vue.eslintrc'
55
const OPEN_ESLINTRC = 'org.vue.eslint.open-eslintrc'
66

77
module.exports = api => {
8-
api.describeConfig(configDescriptor)
9-
api.describeTask(taskDescriptor)
8+
api.describeConfig(configDescriptor.config)
9+
api.describeTask(taskDescriptor.task)
1010

1111
api.onViewOpen(({ view }) => {
1212
if (view.id !== 'vue-project-configurations') {

packages/@vue/cli-plugin-eslint/ui/taskDescriptor.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
const task = {
22
match: /vue-cli-service lint/,
33
description: 'org.vue.eslint.tasks.lint.description',
44
link: 'https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint#injected-commands',
@@ -14,3 +14,7 @@ module.exports = {
1414
if (answers.noFix) args.push('--no-fix')
1515
}
1616
}
17+
18+
module.exports = {
19+
task
20+
}

packages/@vue/cli-ui/locales/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@
585585
"strongly-recommended": "Strongly recommended",
586586
"recommended": "Recommended",
587587
"use-with-caution": "Use with caution",
588-
"undefined": "Uncategorized"
588+
"uncategorized": "Uncategorized"
589589
},
590590
"setting": {
591591
"off": "Off",

0 commit comments

Comments
 (0)