/
component-options-name-casing.js
77 lines (69 loc) · 2.25 KB
/
component-options-name-casing.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
/**
* @author Pig Fang
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
const casing = require('../utils/casing')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'enforce the casing of component name in `components` options',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/component-options-name-casing.html'
},
fixable: 'code',
schema: [{ enum: casing.allowedCaseOptions }],
messages: {
caseNotMatched: 'Component name "{{component}}" is not {{caseType}}.'
}
},
/** @param {RuleContext} context */
create(context) {
const caseType = context.options[0] || 'PascalCase'
const checkCase = casing.getChecker(caseType)
const convert = casing.getConverter(caseType)
return utils.executeOnVue(context, (obj) => {
const node = utils.findProperty(obj, 'components')
if (!node || node.value.type !== 'ObjectExpression') {
return
}
node.value.properties.forEach((property) => {
if (
property.type === 'Property' &&
property.key.type === 'Identifier'
) {
const { name } = property.key
if (checkCase(name)) {
return
}
context.report({
node: property.key,
messageId: 'caseNotMatched',
data: {
component: name,
caseType
},
fix: (fixer) => {
const converted = convert(name)
if (caseType === 'kebab-case') {
return property.shorthand
? fixer.replaceText(property, `'${converted}': ${name}`)
: fixer.replaceText(property.key, `'${converted}'`)
}
return fixer.replaceText(property.key, converted)
}
})
}
})
})
}
}