match-component-import-name.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @author Doug Wade <douglas.b.wade@gmail.com>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const casing = require('../utils/casing')
  8. /**
  9. * @param {Identifier} identifier
  10. * @return {Array<String>}
  11. */
  12. function getExpectedNames(identifier) {
  13. return [casing.pascalCase(identifier.name), casing.kebabCase(identifier.name)]
  14. }
  15. module.exports = {
  16. meta: {
  17. type: 'problem',
  18. docs: {
  19. description:
  20. 'require the registered component name to match the imported component name',
  21. categories: undefined,
  22. url: 'https://eslint.vuejs.org/rules/match-component-import-name.html'
  23. },
  24. fixable: null,
  25. schema: [],
  26. messages: {
  27. unexpected:
  28. 'Component alias {{importedName}} should be one of: {{expectedName}}.'
  29. }
  30. },
  31. /**
  32. * @param {RuleContext} context
  33. * @returns {RuleListener}
  34. */
  35. create(context) {
  36. return utils.executeOnVueComponent(context, (obj) => {
  37. const components = utils.findProperty(obj, 'components')
  38. if (
  39. !components ||
  40. !components.value ||
  41. components.value.type !== 'ObjectExpression'
  42. ) {
  43. return
  44. }
  45. for (const property of components.value.properties) {
  46. if (
  47. property.type === 'SpreadElement' ||
  48. property.value.type !== 'Identifier' ||
  49. property.computed === true
  50. ) {
  51. continue
  52. }
  53. const importedName = utils.getStaticPropertyName(property) || ''
  54. const expectedNames = getExpectedNames(property.value)
  55. if (!expectedNames.includes(importedName)) {
  56. context.report({
  57. node: property,
  58. messageId: 'unexpected',
  59. data: {
  60. importedName,
  61. expectedName: expectedNames.join(', ')
  62. }
  63. })
  64. }
  65. }
  66. })
  67. }
  68. }