max-props.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @author kevsommer Kevin Sommer
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. module.exports = {
  8. meta: {
  9. type: 'problem',
  10. docs: {
  11. description: 'enforce maximum number of props in Vue component',
  12. categories: undefined,
  13. url: 'https://eslint.vuejs.org/rules/max-props.html'
  14. },
  15. fixable: null,
  16. schema: [
  17. {
  18. type: 'object',
  19. properties: {
  20. maxProps: {
  21. type: 'integer',
  22. minimum: 1
  23. }
  24. },
  25. additionalProperties: false,
  26. minProperties: 1
  27. }
  28. ],
  29. messages: {
  30. tooManyProps:
  31. 'Component has too many props ({{propCount}}). Maximum allowed is {{limit}}.'
  32. }
  33. },
  34. /** @param {RuleContext} context */
  35. create(context) {
  36. /** @type {Record<string, number>} */
  37. const option = context.options[0] || {}
  38. /**
  39. * @param {import('../utils').ComponentProp[]} props
  40. */
  41. function checkMaxNumberOfProps(props) {
  42. if (props.length > option.maxProps && props[0].node) {
  43. context.report({
  44. node: props[0].node.parent,
  45. messageId: 'tooManyProps',
  46. data: {
  47. propCount: props.length,
  48. limit: option.maxProps
  49. }
  50. })
  51. }
  52. }
  53. return utils.compositingVisitors(
  54. utils.executeOnVue(context, (obj) => {
  55. checkMaxNumberOfProps(utils.getComponentPropsFromOptions(obj))
  56. }),
  57. utils.defineScriptSetupVisitor(context, {
  58. onDefinePropsEnter(_, props) {
  59. checkMaxNumberOfProps(props)
  60. }
  61. })
  62. )
  63. }
  64. }