require-valid-default-prop.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @fileoverview Enforces props default values to be valid.
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. const NATIVE_TYPES = new Set([
  8. 'String',
  9. 'Number',
  10. 'Boolean',
  11. 'Function',
  12. 'Object',
  13. 'Array',
  14. 'Symbol'
  15. ])
  16. // ------------------------------------------------------------------------------
  17. // Rule Definition
  18. // ------------------------------------------------------------------------------
  19. module.exports = {
  20. meta: {
  21. docs: {
  22. description: 'enforce props default values to be valid',
  23. category: 'essential',
  24. url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.1/docs/rules/require-valid-default-prop.md'
  25. },
  26. fixable: null,
  27. schema: []
  28. },
  29. create (context) {
  30. // ----------------------------------------------------------------------
  31. // Helpers
  32. // ----------------------------------------------------------------------
  33. function isPropertyIdentifier (node) {
  34. return node.type === 'Property' && node.key.type === 'Identifier'
  35. }
  36. function getPropertyNode (obj, name) {
  37. return obj.properties.find(p =>
  38. isPropertyIdentifier(p) &&
  39. p.key.name === name
  40. )
  41. }
  42. function getTypes (node) {
  43. if (node.type === 'Identifier') {
  44. return [node.name]
  45. } else if (node.type === 'ArrayExpression') {
  46. return node.elements
  47. .filter(item => item.type === 'Identifier')
  48. .map(item => item.name)
  49. }
  50. return []
  51. }
  52. function ucFirst (text) {
  53. return text[0].toUpperCase() + text.slice(1)
  54. }
  55. function getValueType (node) {
  56. if (node.type === 'CallExpression') { // Symbol(), Number() ...
  57. if (node.callee.type === 'Identifier' && NATIVE_TYPES.has(node.callee.name)) {
  58. return node.callee.name
  59. }
  60. } else if (node.type === 'TemplateLiteral') { // String
  61. return 'String'
  62. } else if (node.type === 'Literal') { // String, Boolean, Number
  63. if (node.value === null) return null
  64. const type = ucFirst(typeof node.value)
  65. if (NATIVE_TYPES.has(type)) {
  66. return type
  67. }
  68. } else if (node.type === 'ArrayExpression') { // Array
  69. return 'Array'
  70. } else if (node.type === 'ObjectExpression') { // Object
  71. return 'Object'
  72. }
  73. // FunctionExpression, ArrowFunctionExpression
  74. return null
  75. }
  76. // ----------------------------------------------------------------------
  77. // Public
  78. // ----------------------------------------------------------------------
  79. return utils.executeOnVue(context, obj => {
  80. const props = obj.properties.find(p =>
  81. isPropertyIdentifier(p) &&
  82. p.key.name === 'props' &&
  83. p.value.type === 'ObjectExpression'
  84. )
  85. if (!props) return
  86. const properties = props.value.properties.filter(p =>
  87. isPropertyIdentifier(p) &&
  88. p.value.type === 'ObjectExpression'
  89. )
  90. for (const prop of properties) {
  91. const type = getPropertyNode(prop.value, 'type')
  92. if (!type) continue
  93. const typeNames = new Set(getTypes(type.value)
  94. .map(item => item === 'Object' || item === 'Array' ? 'Function' : item) // Object and Array require function
  95. .filter(item => NATIVE_TYPES.has(item)))
  96. // There is no native types detected
  97. if (typeNames.size === 0) continue
  98. const def = getPropertyNode(prop.value, 'default')
  99. if (!def) continue
  100. const defType = getValueType(def.value)
  101. if (!defType || typeNames.has(defType)) continue
  102. context.report({
  103. node: def,
  104. message: "Type of the default value for '{{name}}' prop must be a {{types}}.",
  105. data: {
  106. name: prop.key.name,
  107. types: Array.from(typeNames).join(' or ').toLowerCase()
  108. }
  109. })
  110. }
  111. })
  112. }
  113. }