-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-restricted-v-bind.js
182 lines (175 loc) · 4.4 KB
/
no-restricted-v-bind.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const regexp = require('../utils/regexp')
/**
* @typedef {object} ParsedOption
* @property { (key: VDirectiveKey) => boolean } test
* @property {string[]} modifiers
* @property {boolean} [useElement]
* @property {string} [message]
*/
const DEFAULT_OPTIONS = [
{
argument: '/^v-/',
message:
'Using `:v-xxx` is not allowed. Instead, remove `:` and use it as directive.'
}
]
/**
* @param {string} str
* @returns {(str: string) => boolean}
*/
function buildMatcher(str) {
if (regexp.isRegExp(str)) {
const re = regexp.toRegExp(str)
return (s) => {
re.lastIndex = 0
return re.test(s)
}
}
return (s) => s === str
}
/**
* @param {any} option
* @returns {ParsedOption}
*/
function parseOption(option) {
if (typeof option === 'string') {
const matcher = buildMatcher(option)
return {
test(key) {
return Boolean(
key.argument &&
key.argument.type === 'VIdentifier' &&
matcher(key.argument.rawName)
)
},
modifiers: []
}
}
if (option === null) {
return {
test(key) {
return key.argument === null
},
modifiers: []
}
}
const parsed = parseOption(option.argument)
if (option.modifiers) {
const argTest = parsed.test
parsed.test = (key) => {
if (!argTest(key)) {
return false
}
return /** @type {string[]} */ (option.modifiers).every((modName) =>
key.modifiers.some((mid) => mid.name === modName)
)
}
parsed.modifiers = option.modifiers
}
if (option.element) {
const argTest = parsed.test
const tagMatcher = buildMatcher(option.element)
parsed.test = (key) => {
if (!argTest(key)) {
return false
}
const element = key.parent.parent.parent
return tagMatcher(element.rawName)
}
parsed.useElement = true
}
parsed.message = option.message
return parsed
}
/**
* @param {VDirectiveKey} key
* @param {ParsedOption} option
*/
function defaultMessage(key, option) {
const vbind = key.name.rawName === ':' ? '' : 'v-bind'
const arg =
key.argument != null && key.argument.type === 'VIdentifier'
? `:${key.argument.rawName}`
: ''
const mod =
option.modifiers.length > 0 ? `.${option.modifiers.join('.')}` : ''
let on = ''
if (option.useElement) {
on = ` on \`<${key.parent.parent.parent.rawName}>\``
}
return `Using \`${vbind + arg + mod}\`${on} is not allowed.`
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow specific argument in `v-bind`',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-restricted-v-bind.html'
},
fixable: null,
schema: {
type: 'array',
items: {
oneOf: [
{ type: ['string', 'null'] },
{
type: 'object',
properties: {
argument: { type: ['string', 'null'] },
modifiers: {
type: 'array',
items: {
type: 'string',
enum: ['prop', 'camel', 'sync', 'attr']
},
uniqueItems: true
},
element: { type: 'string' },
message: { type: 'string', minLength: 1 }
},
required: ['argument'],
additionalProperties: false
}
]
},
uniqueItems: true,
minItems: 0
},
messages: {
// eslint-disable-next-line eslint-plugin/report-message-format
restrictedVBind: '{{message}}'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {ParsedOption[]} */
const options = (
context.options.length === 0 ? DEFAULT_OPTIONS : context.options
).map(parseOption)
return utils.defineTemplateBodyVisitor(context, {
/**
* @param {VDirectiveKey} node
*/
"VAttribute[directive=true][key.name.name='bind'] > VDirectiveKey"(node) {
for (const option of options) {
if (option.test(node)) {
const message = option.message || defaultMessage(node, option)
context.report({
node,
messageId: 'restrictedVBind',
data: { message }
})
return
}
}
}
})
}
}