no-unregistered-components.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @fileoverview Report used components that are not registered
  3. * @author Jesús Ángel González Novez
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const utils = require('../utils')
  10. const casing = require('../utils/casing')
  11. // ------------------------------------------------------------------------------
  12. // Rule helpers
  13. // ------------------------------------------------------------------------------
  14. /**
  15. * Check whether the given node is a built-in component or not.
  16. *
  17. * Includes `suspense` and `teleport` from Vue 3.
  18. *
  19. * @param {VElement} node The start tag node to check.
  20. * @returns {boolean} `true` if the node is a built-in component.
  21. */
  22. const isBuiltInComponent = (node) => {
  23. const rawName = node && casing.kebabCase(node.rawName)
  24. return (
  25. utils.isHtmlElementNode(node) &&
  26. !utils.isHtmlWellKnownElementName(node.rawName) &&
  27. utils.isBuiltInComponentName(rawName)
  28. )
  29. }
  30. // ------------------------------------------------------------------------------
  31. // Rule Definition
  32. // ------------------------------------------------------------------------------
  33. module.exports = {
  34. meta: {
  35. type: 'suggestion',
  36. docs: {
  37. description:
  38. 'disallow using components that are not registered inside templates',
  39. categories: null,
  40. recommended: false,
  41. url: 'https://eslint.vuejs.org/rules/no-unregistered-components.html'
  42. },
  43. fixable: null,
  44. schema: [
  45. {
  46. type: 'object',
  47. properties: {
  48. ignorePatterns: {
  49. type: 'array'
  50. }
  51. },
  52. additionalProperties: false
  53. }
  54. ]
  55. },
  56. /** @param {RuleContext} context */
  57. create(context) {
  58. if (utils.isScriptSetup(context)) {
  59. return {}
  60. }
  61. const options = context.options[0] || {}
  62. /** @type {string[]} */
  63. const ignorePatterns = options.ignorePatterns || []
  64. /** @type { { node: VElement | VDirective | VAttribute, name: string }[] } */
  65. const usedComponentNodes = []
  66. /** @type { { node: Property, name: string }[] } */
  67. const registeredComponents = []
  68. return utils.defineTemplateBodyVisitor(
  69. context,
  70. {
  71. /** @param {VElement} node */
  72. VElement(node) {
  73. if (
  74. (!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) ||
  75. utils.isHtmlWellKnownElementName(node.rawName) ||
  76. utils.isSvgWellKnownElementName(node.rawName) ||
  77. isBuiltInComponent(node)
  78. ) {
  79. return
  80. }
  81. usedComponentNodes.push({ node, name: node.rawName })
  82. },
  83. /** @param {VDirective} node */
  84. "VAttribute[directive=true][key.name.name='bind'][key.argument.name='is'], VAttribute[directive=true][key.name.name='is']"(
  85. node
  86. ) {
  87. if (
  88. !node.value ||
  89. node.value.type !== 'VExpressionContainer' ||
  90. !node.value.expression
  91. )
  92. return
  93. if (node.value.expression.type === 'Literal') {
  94. if (
  95. utils.isHtmlWellKnownElementName(`${node.value.expression.value}`)
  96. )
  97. return
  98. usedComponentNodes.push({
  99. node,
  100. name: `${node.value.expression.value}`
  101. })
  102. }
  103. },
  104. /** @param {VAttribute} node */
  105. "VAttribute[directive=false][key.name='is']"(node) {
  106. if (
  107. !node.value // `<component is />`
  108. )
  109. return
  110. const value = node.value.value.startsWith('vue:') // Usage on native elements 3.1+
  111. ? node.value.value.slice(4)
  112. : node.value.value
  113. if (utils.isHtmlWellKnownElementName(value)) return
  114. usedComponentNodes.push({ node, name: value })
  115. },
  116. /** @param {VElement} node */
  117. "VElement[name='template'][parent.type='VDocumentFragment']:exit"() {
  118. // All registered components, transformed to kebab-case
  119. const registeredComponentNames = registeredComponents.map(
  120. ({ name }) => casing.kebabCase(name)
  121. )
  122. // All registered components using kebab-case syntax
  123. const componentsRegisteredAsKebabCase = registeredComponents
  124. .filter(({ name }) => name === casing.kebabCase(name))
  125. .map(({ name }) => name)
  126. usedComponentNodes
  127. .filter(({ name }) => {
  128. const kebabCaseName = casing.kebabCase(name)
  129. // Check ignored patterns in first place
  130. if (
  131. ignorePatterns.find((pattern) => {
  132. const regExp = new RegExp(pattern)
  133. return (
  134. regExp.test(kebabCaseName) ||
  135. regExp.test(casing.pascalCase(name)) ||
  136. regExp.test(casing.camelCase(name)) ||
  137. regExp.test(casing.snakeCase(name)) ||
  138. regExp.test(name)
  139. )
  140. })
  141. )
  142. return false
  143. // Component registered as `foo-bar` cannot be used as `FooBar`
  144. if (
  145. casing.isPascalCase(name) &&
  146. componentsRegisteredAsKebabCase.indexOf(kebabCaseName) !== -1
  147. ) {
  148. return true
  149. }
  150. // Otherwise
  151. return registeredComponentNames.indexOf(kebabCaseName) === -1
  152. })
  153. .forEach(({ node, name }) =>
  154. context.report({
  155. node,
  156. message:
  157. 'The "{{name}}" component has been used but not registered.',
  158. data: {
  159. name
  160. }
  161. })
  162. )
  163. }
  164. },
  165. utils.executeOnVue(context, (obj) => {
  166. registeredComponents.push(...utils.getRegisteredComponents(obj))
  167. const nameProperty = utils.findProperty(obj, 'name')
  168. if (nameProperty) {
  169. if (nameProperty.value.type === 'Literal') {
  170. registeredComponents.push({
  171. node: nameProperty,
  172. name: `${nameProperty.value.value}`
  173. })
  174. }
  175. }
  176. })
  177. )
  178. }
  179. }