/
no-restricted-props.js
184 lines (173 loc) · 4.62 KB
/
no-restricted-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
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
183
184
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const regexp = require('../utils/regexp')
/**
* @typedef {import('../utils').ComponentArrayProp} ComponentArrayProp
* @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp
* @typedef {import('../utils').ComponentTypeProp} ComponentTypeProp
*/
/**
* @typedef {object} ParsedOption
* @property { (name: string) => boolean } test
* @property {string|undefined} [message]
* @property {string|undefined} [suggest]
*/
/**
* @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 {string|{name:string, message?: string, suggest?:string}} option
* @returns {ParsedOption}
*/
function parseOption(option) {
if (typeof option === 'string') {
const matcher = buildMatcher(option)
return {
test(name) {
return matcher(name)
}
}
}
const parsed = parseOption(option.name)
parsed.message = option.message
parsed.suggest = option.suggest
return parsed
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow specific props',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-restricted-props.html'
},
fixable: null,
schema: {
type: 'array',
items: {
oneOf: [
{ type: ['string'] },
{
type: 'object',
properties: {
name: { type: 'string' },
message: { type: 'string', minLength: 1 },
suggest: { type: 'string' }
},
required: ['name'],
additionalProperties: false
}
]
},
uniqueItems: true,
minItems: 0
},
messages: {
// eslint-disable-next-line eslint-plugin/report-message-format
restrictedProp: '{{message}}',
instead: 'Instead, change to `{{suggest}}`.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {ParsedOption[]} */
const options = context.options.map(parseOption)
/**
* @param {(ComponentArrayProp | ComponentObjectProp | ComponentTypeProp)[]} props
* @param { { [key: string]: Property | undefined } } [withDefaultsProps]
*/
function processProps(props, withDefaultsProps) {
for (const prop of props) {
if (!prop.propName) {
continue
}
for (const option of options) {
if (option.test(prop.propName)) {
const message =
option.message ||
`Using \`${prop.propName}\` props is not allowed.`
context.report({
node: prop.key,
messageId: 'restrictedProp',
data: { message },
suggest: createSuggest(
prop.key,
option,
withDefaultsProps && withDefaultsProps[prop.propName]
)
})
break
}
}
}
}
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node, props) {
processProps(props, utils.getWithDefaultsProps(node))
}
}),
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
processProps(utils.getComponentProps(node))
}
})
)
}
}
/**
* @param {Expression} node
* @param {ParsedOption} option
* @param {Property} [withDefault]
* @returns {Rule.SuggestionReportDescriptor[]}
*/
function createSuggest(node, option, withDefault) {
if (!option.suggest) {
return []
}
/** @type {string} */
let replaceText
if (node.type === 'Literal' || node.type === 'TemplateLiteral') {
replaceText = JSON.stringify(option.suggest)
} else if (node.type === 'Identifier') {
replaceText = /^[a-z]\w*$/iu.exec(option.suggest)
? option.suggest
: JSON.stringify(option.suggest)
} else {
return []
}
return [
{
fix(fixer) {
const fixes = [fixer.replaceText(node, replaceText)]
if (withDefault) {
if (withDefault.shorthand) {
fixes.push(
fixer.insertTextBefore(withDefault.value, `${replaceText}:`)
)
} else {
fixes.push(fixer.replaceText(withDefault.key, replaceText))
}
}
return fixes.sort((a, b) => a.range[0] - b.range[0])
},
messageId: 'instead',
data: { suggest: option.suggest }
}
]
}