-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-reserved-props.js
83 lines (76 loc) · 1.91 KB
/
no-reserved-props.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
78
79
80
81
82
83
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
/**
* @typedef {import('../utils').ComponentProp} ComponentProp
*/
const RESERVED = {
3: ['key', 'ref'],
2: ['key', 'ref', 'is', 'slot', 'slot-scope', 'slotScope', 'class', 'style']
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow reserved names in props',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/no-reserved-props.html',
defaultOptions: {
vue2: [{ vueVersion: 2 }]
}
},
fixable: null,
schema: [
{
type: 'object',
properties: {
vueVersion: {
enum: [2, 3]
}
},
additionalProperties: false
}
],
messages: {
reserved:
"'{{propName}}' is a reserved attribute and cannot be used as props."
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
/** @type {2|3} */
const vueVersion = options.vueVersion || 3
const reserved = new Set(RESERVED[vueVersion])
/**
* @param {ComponentProp[]} props
*/
function processProps(props) {
for (const prop of props) {
if (prop.propName != null && reserved.has(prop.propName)) {
context.report({
node: prop.node,
messageId: `reserved`,
data: {
propName: casing.kebabCase(prop.propName)
}
})
}
}
}
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(_node, props) {
processProps(props)
}
}),
utils.executeOnVue(context, (obj) => {
processProps(utils.getComponentPropsFromOptions(obj))
})
)
}
}