require-default-prop.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @fileoverview Require default value for props
  3. * @author Michał Sajnóg <msajnog93@gmail.com> (http://github.com/michalsnik)
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'require default value for props',
  14. category: 'strongly-recommended',
  15. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/require-default-prop.md'
  16. },
  17. fixable: null, // or "code" or "whitespace"
  18. schema: []
  19. },
  20. create: function (context) {
  21. // ----------------------------------------------------------------------
  22. // Helpers
  23. // ----------------------------------------------------------------------
  24. /**
  25. * Checks if the passed prop is required
  26. * @param {Property} prop - Property AST node for a single prop
  27. * @return {boolean}
  28. */
  29. function propIsRequired (prop) {
  30. const propRequiredNode = prop.value.properties
  31. .find(p =>
  32. p.type === 'Property' &&
  33. p.key.name === 'required' &&
  34. p.value.type === 'Literal' &&
  35. p.value.value === true
  36. )
  37. return Boolean(propRequiredNode)
  38. }
  39. /**
  40. * Checks if the passed prop has a default value
  41. * @param {Property} prop - Property AST node for a single prop
  42. * @return {boolean}
  43. */
  44. function propHasDefault (prop) {
  45. const propDefaultNode = prop.value.properties
  46. .find(p =>
  47. p.key &&
  48. (p.key.name === 'default' || p.key.value === 'default')
  49. )
  50. return Boolean(propDefaultNode)
  51. }
  52. /**
  53. * Finds all props that don't have a default value set
  54. * @param {Property} propsNode - Vue component's "props" node
  55. * @return {boolean}
  56. */
  57. function findPropsWithoutDefaultValue (propsNode) {
  58. return propsNode.value.properties
  59. .filter(prop => prop.type === 'Property')
  60. .filter(prop => {
  61. if (prop.value.type !== 'ObjectExpression') {
  62. return true
  63. }
  64. return !propIsRequired(prop) && !propHasDefault(prop)
  65. })
  66. }
  67. // ----------------------------------------------------------------------
  68. // Public
  69. // ----------------------------------------------------------------------
  70. return utils.executeOnVue(context, (obj) => {
  71. const propsNode = obj.properties
  72. .find(p =>
  73. p.type === 'Property' &&
  74. p.key.type === 'Identifier' &&
  75. p.key.name === 'props' &&
  76. p.value.type === 'ObjectExpression'
  77. )
  78. if (!propsNode) return
  79. const propsWithoutDefault = findPropsWithoutDefaultValue(propsNode)
  80. propsWithoutDefault.forEach(prop => {
  81. context.report({
  82. node: prop,
  83. message: `Prop '{{propName}}' requires default value to be set.`,
  84. data: {
  85. propName: prop.key.name
  86. }
  87. })
  88. })
  89. })
  90. }
  91. }