/
no-duplicate-attr-inheritance.js
62 lines (55 loc) · 1.75 KB
/
no-duplicate-attr-inheritance.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
/**
* @fileoverview Disable inheritAttrs when using v-bind="$attrs"
* @author Hiroki Osame
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"`',
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html'
},
fixable: null,
schema: [
// fill in your schema
]
},
create (context) {
let inheritsAttrs = true
return Object.assign(
utils.executeOnVue(context, (node) => {
const inheritAttrsProp = node.properties.find(prop => (prop.type === 'Property' && utils.getStaticPropertyName(prop) === 'inheritAttrs'))
if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
inheritsAttrs = inheritAttrsProp.value.value
}
}),
utils.defineTemplateBodyVisitor(context, {
"VAttribute[directive=true][key.name.name='bind'][key.argument=null] > VExpressionContainer" (node) {
if (!inheritsAttrs) {
return
}
const attrsRef = node.references.find(reference => {
if (reference.variable != null) {
// Not vm reference
return false
}
return reference.id.name === '$attrs'
})
if (attrsRef) {
context.report({
node: attrsRef.id,
message: 'Set "inheritAttrs" to false.'
})
}
}
})
)
}
}