require-name-property.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @fileoverview Require a name property in Vue components
  3. * @author LukeeeeBennett
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const { getVueComponentDefinitionType } = require('../utils')
  8. /**
  9. * @param {Property | SpreadElement} node
  10. * @returns {node is ObjectExpressionProperty}
  11. */
  12. function isNameProperty(node) {
  13. return (
  14. node.type === 'Property' &&
  15. utils.getStaticPropertyName(node) === 'name' &&
  16. !node.computed
  17. )
  18. }
  19. module.exports = {
  20. meta: {
  21. type: 'suggestion',
  22. docs: {
  23. description: 'require a name property in Vue components',
  24. categories: undefined,
  25. url: 'https://eslint.vuejs.org/rules/require-name-property.html'
  26. },
  27. fixable: null,
  28. schema: []
  29. },
  30. /** @param {RuleContext} context */
  31. create(context) {
  32. if (utils.isScriptSetup(context)) {
  33. return {}
  34. }
  35. return utils.executeOnVue(context, (component, type) => {
  36. if (type === 'definition') {
  37. const defType = getVueComponentDefinitionType(component)
  38. if (defType === 'mixin') {
  39. return
  40. }
  41. }
  42. if (component.properties.some(isNameProperty)) return
  43. context.report({
  44. node: component,
  45. message: 'Required name property is not set.'
  46. })
  47. })
  48. }
  49. }