-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-restricted-component-names.js
157 lines (146 loc) · 3.84 KB
/
no-restricted-component-names.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
/**
* @author ItMaga <https://github.com/ItMaga>
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
const { isRegExp, toRegExp } = require('../utils/regexp')
/**
* @typedef {object} OptionParsed
* @property { (name: string) => boolean } test
* @property {string|undefined} [message]
* @property {string|undefined} [suggest]
*/
/**
* @param {string} str
* @returns {(str: string) => boolean}
* @private
*/
function buildMatcher(str) {
if (isRegExp(str)) {
const regex = toRegExp(str)
return (s) => regex.test(s)
}
return (s) => s === casing.pascalCase(str) || s === casing.kebabCase(str)
}
/**
* @param {string|{name: string, message?: string, suggest?: string}} option
* @returns {OptionParsed}
* @private
* */
function parseOption(option) {
if (typeof option === 'string') {
const matcher = buildMatcher(option)
return { test: matcher }
}
const parsed = parseOption(option.name)
parsed.message = option.message
parsed.suggest = option.suggest
return parsed
}
/**
* @param {Property | AssignmentProperty} property
* @param {string | undefined} suggest
* @returns {Rule.SuggestionReportDescriptor[]}
* @private
* */
function createSuggest(property, suggest) {
if (!suggest) {
return []
}
return [
{
fix(fixer) {
return fixer.replaceText(property.value, JSON.stringify(suggest))
},
messageId: 'suggest',
data: { suggest }
}
]
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow specific component names',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-restricted-component-names.html'
},
fixable: null,
hasSuggestions: true,
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
disallow: '{{message}}',
suggest: 'Instead, change to `{{suggest}}`.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {OptionParsed[]} */
const options = context.options.map(parseOption)
/**
* @param {ObjectExpression} node
*/
function verify(node) {
const property = utils.findProperty(node, 'name')
if (!property) return
const propertyName = utils.getStaticPropertyName(property)
if (propertyName === 'name' && property.value.type === 'Literal') {
const componentName = property.value.value?.toString()
if (!componentName) {
return
}
for (const option of options) {
if (option.test(componentName)) {
context.report({
node: property.value,
messageId: 'disallow',
data: {
message:
option.message ||
`Using component name \`${componentName}\` is not allowed.`
},
suggest: createSuggest(property, option.suggest)
})
}
}
}
}
return utils.compositingVisitors(
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
verify(node)
}
}),
utils.defineScriptSetupVisitor(context, {
onDefineOptionsEnter(node) {
const expression = node.arguments[0]
if (expression.type === 'ObjectExpression') {
verify(expression)
}
}
})
)
}
}