-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathmatch-component-import-name.js
73 lines (68 loc) · 1.85 KB
/
match-component-import-name.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
/**
* @author Doug Wade <douglas.b.wade@gmail.com>
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
/**
* @param {Identifier} identifier
* @return {Array<String>}
*/
function getExpectedNames(identifier) {
return [casing.pascalCase(identifier.name), casing.kebabCase(identifier.name)]
}
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'require the registered component name to match the imported component name',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/match-component-import-name.html'
},
fixable: null,
schema: [],
messages: {
unexpected:
'Component alias {{importedName}} should be one of: {{expectedName}}.'
}
},
/**
* @param {RuleContext} context
* @returns {RuleListener}
*/
create(context) {
return utils.executeOnVueComponent(context, (obj) => {
const components = utils.findProperty(obj, 'components')
if (
!components ||
!components.value ||
components.value.type !== 'ObjectExpression'
) {
return
}
for (const property of components.value.properties) {
if (
property.type === 'SpreadElement' ||
property.value.type !== 'Identifier' ||
property.computed === true
) {
continue
}
const importedName = utils.getStaticPropertyName(property) || ''
const expectedNames = getExpectedNames(property.value)
if (!expectedNames.includes(importedName)) {
context.report({
node: property,
messageId: 'unexpected',
data: {
importedName,
expectedName: expectedNames.join(', ')
}
})
}
}
})
}
}