forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-invalid-attribute-name.js
54 lines (49 loc) · 1.14 KB
/
no-invalid-attribute-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
/**
* @author Doug Wade <[email protected]>
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'require the attributes to match the imported component name',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-invalid-attribute-name.html'
},
fixable: null,
schema: [],
messages: {
unexpected: '{{name}} is not a valid attribute name'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @param {string | VIdentifier} key
* @return {string}
*/
const getName = (key) => {
if (typeof key === 'string') {
return key
}
return key.name
}
return utils.defineTemplateBodyVisitor(context, {
VAttribute(node) {
const name = getName(node.key.name)
if (!/^[_:a-zA-Z][_:.\-a-zA-Z0-9]+/.test(name)) {
context.report({
node,
messageId: 'unexpected',
data: {
name
}
})
}
}
})
}
}