|
| 1 | +/** |
| 2 | + * @author sharkykh |
| 3 | + * Based on https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/component-name-in-template-casing.js |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('eslint-plugin-vue/lib/utils'); |
| 12 | +const casing = require('eslint-plugin-vue/lib/utils/casing'); |
| 13 | + |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | +// Helpers |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +const VueBuiltInComponents = [ |
| 19 | + 'component', |
| 20 | + 'keep-alive', |
| 21 | + 'slot', |
| 22 | + 'transition', |
| 23 | + 'transition-group' |
| 24 | +]; |
| 25 | + |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | +// Rule Definition |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + meta: { |
| 32 | + type: 'problem', |
| 33 | + schema: [ |
| 34 | + { |
| 35 | + type: 'array', |
| 36 | + items: { |
| 37 | + type: 'string' |
| 38 | + }, |
| 39 | + uniqueItems: true, |
| 40 | + additionalItems: false |
| 41 | + } |
| 42 | + ] |
| 43 | + }, |
| 44 | + |
| 45 | + create(context) { |
| 46 | + const allowedComponents = VueBuiltInComponents.concat(context.options[0] || []); |
| 47 | + const registeredComponents = []; |
| 48 | + |
| 49 | + const tokens = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore(); |
| 50 | + |
| 51 | + let hasInvalidEOF = false; |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks whether the given node is the verification target node. |
| 55 | + * @param {VElement} node element node |
| 56 | + * @returns {boolean} `true` if the given node is the verification target node. |
| 57 | + */ |
| 58 | + function isVerifyTarget(node) { |
| 59 | + // If the component is allowed, ignore it |
| 60 | + try { |
| 61 | + if (allowedComponents.some(name => name === node.rawName)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } catch (_) {} |
| 65 | + |
| 66 | + // Ignore non-Vue-components (vanilla HTML elements and SVG) |
| 67 | + if ((!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) || |
| 68 | + utils.isHtmlWellKnownElementName(node.rawName) || |
| 69 | + utils.isSvgWellKnownElementName(node.rawName) |
| 70 | + ) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return utils.defineTemplateBodyVisitor( |
| 78 | + context, |
| 79 | + { |
| 80 | + VElement(node) { |
| 81 | + if (hasInvalidEOF) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (!isVerifyTarget(node)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const name = node.rawName; |
| 90 | + const casingName = casing.getConverter('PascalCase')(name); |
| 91 | + if (!registeredComponents.some(componentName => casingName === componentName)) { |
| 92 | + const { startTag } = node; |
| 93 | + const open = tokens.getFirstToken(startTag); |
| 94 | + |
| 95 | + context.report({ |
| 96 | + node: open, |
| 97 | + loc: open.loc, |
| 98 | + message: 'Component "{{name}}" is used but not registered.', |
| 99 | + data: { |
| 100 | + name |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + }, |
| 106 | + Object.assign( |
| 107 | + { |
| 108 | + Program(node) { |
| 109 | + hasInvalidEOF = utils.hasInvalidEOF(node); |
| 110 | + } |
| 111 | + }, |
| 112 | + utils.executeOnVue(context, obj => { |
| 113 | + registeredComponents.push(...utils.getRegisteredComponents(obj).map(n => n.name)); |
| 114 | + }) |
| 115 | + ) |
| 116 | + ); |
| 117 | + } |
| 118 | +}; |
0 commit comments