|
| 1 | +/** |
| 2 | + * @author Wayne Zhang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const { toRegExp } = require('../utils/regexp') |
| 9 | + |
| 10 | +const htmlElements = require('../utils/html-elements.json') |
| 11 | +const deprecatedHtmlElements = require('../utils/deprecated-html-elements.json') |
| 12 | +const svgElements = require('../utils/svg-elements.json') |
| 13 | +const vue2builtinComponents = require('../utils/vue2-builtin-components') |
| 14 | +const vue3builtinComponents = require('../utils/vue3-builtin-components') |
| 15 | + |
| 16 | +const reservedNames = new Set([ |
| 17 | + ...htmlElements, |
| 18 | + ...deprecatedHtmlElements, |
| 19 | + ...svgElements, |
| 20 | + ...vue2builtinComponents, |
| 21 | + ...vue3builtinComponents |
| 22 | +]) |
| 23 | + |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + type: 'problem', |
| 27 | + docs: { |
| 28 | + description: 'enforce using only specific component names', |
| 29 | + categories: undefined, |
| 30 | + url: 'https://eslint.vuejs.org/rules/restricted-component-names.html' |
| 31 | + }, |
| 32 | + fixable: null, |
| 33 | + schema: [ |
| 34 | + { |
| 35 | + type: 'object', |
| 36 | + additionalProperties: false, |
| 37 | + properties: { |
| 38 | + allow: { |
| 39 | + type: 'array', |
| 40 | + items: { type: 'string' }, |
| 41 | + uniqueItems: true, |
| 42 | + additionalItems: false |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + ], |
| 47 | + messages: { |
| 48 | + invalidName: 'Component name "{{name}}" is not allowed.' |
| 49 | + } |
| 50 | + }, |
| 51 | + /** @param {RuleContext} context */ |
| 52 | + create(context) { |
| 53 | + const options = context.options[0] || {} |
| 54 | + /** @type {RegExp[]} */ |
| 55 | + const allow = (options.allow || []).map(toRegExp) |
| 56 | + |
| 57 | + /** @param {string} name */ |
| 58 | + function isAllowedTarget(name) { |
| 59 | + return reservedNames.has(name) || allow.some((re) => re.test(name)) |
| 60 | + } |
| 61 | + |
| 62 | + return utils.defineTemplateBodyVisitor(context, { |
| 63 | + VElement(node) { |
| 64 | + const name = node.rawName |
| 65 | + if (isAllowedTarget(name)) { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + context.report({ |
| 70 | + node, |
| 71 | + loc: node.loc, |
| 72 | + messageId: 'invalidName', |
| 73 | + data: { |
| 74 | + name |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments