forked from vuejs/eslint-plugin-vue
/
no-invalid-attribute-name.js
49 lines (45 loc) · 1.12 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
/**
* @author Doug Wade <douglas.b.wade@gmail.com>
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const xnv = require('xml-name-validator')
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require valid attribute names',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-invalid-attribute-name.html'
},
fixable: null,
schema: [],
messages: {
unexpected: 'Attribute name {{name}} is not valid.'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @param {string | VIdentifier} key
* @return {string}
*/
const getName = (key) => (typeof key === 'string' ? key : key.name)
return utils.defineTemplateBodyVisitor(context, {
/** @param {VDirective | VAttribute} node */
VAttribute(node) {
const name = getName(node.key.name)
if (!xnv.name(name)) {
context.report({
node,
messageId: 'unexpected',
data: {
name
}
})
}
}
})
}
}