|
| 1 | +/** |
| 2 | + * @author ItMaga <https://github.com/ItMaga> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const casing = require('../utils/casing') |
| 9 | +const { isRegExp, toRegExp } = require('../utils/regexp') |
| 10 | + |
| 11 | +/** |
| 12 | + * @typedef {object} OptionParsed |
| 13 | + * @property { (name: string) => boolean } test |
| 14 | + * @property {string|undefined} [message] |
| 15 | + * @property {string|undefined} [suggest] |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {string} str |
| 20 | + * @returns {(str: string) => boolean} |
| 21 | + * @private |
| 22 | + */ |
| 23 | +function buildMatcher(str) { |
| 24 | + if (isRegExp(str)) { |
| 25 | + const regex = toRegExp(str) |
| 26 | + return (s) => regex.test(s) |
| 27 | + } |
| 28 | + return (s) => s === casing.pascalCase(str) || s === casing.kebabCase(str) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {string|{name: string, message?: string, suggest?: string}} option |
| 33 | + * @returns {OptionParsed} |
| 34 | + * @private |
| 35 | + * */ |
| 36 | +function parseOption(option) { |
| 37 | + if (typeof option === 'string') { |
| 38 | + const matcher = buildMatcher(option) |
| 39 | + return { test: matcher } |
| 40 | + } |
| 41 | + const parsed = parseOption(option.name) |
| 42 | + parsed.message = option.message |
| 43 | + parsed.suggest = option.suggest |
| 44 | + return parsed |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {Property | AssignmentProperty} property |
| 49 | + * @param {string | undefined} suggest |
| 50 | + * @returns {Rule.SuggestionReportDescriptor[]} |
| 51 | + * @private |
| 52 | + * */ |
| 53 | +function createSuggest(property, suggest) { |
| 54 | + if (!suggest) { |
| 55 | + return [] |
| 56 | + } |
| 57 | + |
| 58 | + return [ |
| 59 | + { |
| 60 | + fix(fixer) { |
| 61 | + return fixer.replaceText(property.value, JSON.stringify(suggest)) |
| 62 | + }, |
| 63 | + messageId: 'suggest', |
| 64 | + data: { suggest } |
| 65 | + } |
| 66 | + ] |
| 67 | +} |
| 68 | + |
| 69 | +module.exports = { |
| 70 | + meta: { |
| 71 | + hasSuggestions: true, |
| 72 | + type: 'suggestion', |
| 73 | + docs: { |
| 74 | + description: 'disallow specific component names', |
| 75 | + categories: undefined, |
| 76 | + url: 'https://eslint.vuejs.org/rules/no-restricted-component-names.html' |
| 77 | + }, |
| 78 | + fixable: null, |
| 79 | + schema: { |
| 80 | + type: 'array', |
| 81 | + items: { |
| 82 | + oneOf: [ |
| 83 | + { type: 'string' }, |
| 84 | + { |
| 85 | + type: 'object', |
| 86 | + properties: { |
| 87 | + name: { type: 'string' }, |
| 88 | + message: { type: 'string', minLength: 1 }, |
| 89 | + suggest: { type: 'string' } |
| 90 | + }, |
| 91 | + required: ['name'], |
| 92 | + additionalProperties: false |
| 93 | + } |
| 94 | + ] |
| 95 | + }, |
| 96 | + uniqueItems: true, |
| 97 | + minItems: 0 |
| 98 | + }, |
| 99 | + messages: { |
| 100 | + // eslint-disable-next-line eslint-plugin/report-message-format |
| 101 | + disallow: '{{message}}', |
| 102 | + suggest: 'Instead, change to `{{suggest}}`.' |
| 103 | + } |
| 104 | + }, |
| 105 | + /** @param {RuleContext} context */ |
| 106 | + create(context) { |
| 107 | + /** @type {OptionParsed[]} */ |
| 108 | + const options = context.options.map(parseOption) |
| 109 | + |
| 110 | + /** |
| 111 | + * @param {ObjectExpression} node |
| 112 | + */ |
| 113 | + function verify(node) { |
| 114 | + const property = utils.findProperty(node, 'name') |
| 115 | + if (!property) return |
| 116 | + |
| 117 | + const propertyName = utils.getStaticPropertyName(property) |
| 118 | + if (propertyName === 'name' && property.value.type === 'Literal') { |
| 119 | + const componentName = property.value.value?.toString() |
| 120 | + if (!componentName) { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + for (const option of options) { |
| 125 | + if (option.test(componentName)) { |
| 126 | + context.report({ |
| 127 | + node: property.value, |
| 128 | + messageId: 'disallow', |
| 129 | + data: { |
| 130 | + message: |
| 131 | + option.message || |
| 132 | + `Using component name \`${componentName}\` is not allowed.` |
| 133 | + }, |
| 134 | + suggest: createSuggest(property, option.suggest) |
| 135 | + }) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return utils.compositingVisitors( |
| 142 | + utils.defineVueVisitor(context, { |
| 143 | + onVueObjectEnter(node) { |
| 144 | + verify(node) |
| 145 | + } |
| 146 | + }), |
| 147 | + utils.defineScriptSetupVisitor(context, { |
| 148 | + onDefineOptionsEnter(node) { |
| 149 | + const expression = node.arguments[0] |
| 150 | + if (expression.type === 'ObjectExpression') { |
| 151 | + verify(expression) |
| 152 | + } |
| 153 | + } |
| 154 | + }) |
| 155 | + ) |
| 156 | + } |
| 157 | +} |
0 commit comments