require-valid-default-prop.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. type: 'suggestion',
  22. docs: {
  23. description: 'enforce props default values to be valid',
  24. category: 'essential',
  25. url: 'https://eslint.vuejs.org/rules/require-valid-default-prop.html'
  26. },
  27. fixable: null,
  28. schema: []
  29. },
  30. create (context) {
  31. // ----------------------------------------------------------------------
  32. // Helpers
  33. // ----------------------------------------------------------------------
  34. function isPropertyIdentifier (node) {
  35. return node.type === 'Property' && node.key.type === 'Identifier'
  36. }
  37. function getPropertyNode (obj, name) {
  38. return obj.properties.find(p =>
  39. isPropertyIdentifier(p) &&
  40. p.key.name === name
  41. )
  42. }
  43. function getTypes (node) {
  44. if (node.type === 'Identifier') {
  45. return [node.name]
  46. } else if (node.type === 'ArrayExpression') {
  47. return node.elements
  48. .filter(item => item.type === 'Identifier')
  49. .map(item => item.name)
  50. }
  51. return []
  52. }
  53. function ucFirst (text) {
  54. return text[0].toUpperCase() + text.slice(1)
  55. }
  56. function getValueType (node) {
  57. if (node.type === 'CallExpression') { // Symbol(), Number() ...
  58. if (node.callee.type === 'Identifier' && NATIVE_TYPES.has(node.callee.name)) {
  59. return node.callee.name
  60. }
  61. } else if (node.type === 'TemplateLiteral') { // String
  62. return 'String'
  63. } else if (node.type === 'Literal') { // String, Boolean, Number
  64. if (node.value === null) return null
  65. const type = ucFirst(typeof node.value)
  66. if (NATIVE_TYPES.has(type)) {
  67. return type
  68. }
  69. } else if (node.type === 'ArrayExpression') { // Array
  70. return 'Array'
  71. } else if (node.type === 'ObjectExpression') { // Object
  72. return 'Object'
  73. }
  74. // FunctionExpression, ArrowFunctionExpression
  75. return null
  76. }
  77. // ----------------------------------------------------------------------
  78. // Public
  79. // ----------------------------------------------------------------------
  80. return utils.executeOnVue(context, obj => {
  81. const props = utils.getComponentProps(obj)
  82. .filter(prop => prop.key && prop.value && prop.value.type === 'ObjectExpression')
  83. for (const prop of props) {
  84. const type = getPropertyNode(prop.value, 'type')
  85. if (!type) continue
  86. const typeNames = new Set(getTypes(type.value)
  87. .map(item => item === 'Object' || item === 'Array' ? 'Function' : item) // Object and Array require function
  88. .filter(item => NATIVE_TYPES.has(item)))
  89. // There is no native types detected
  90. if (typeNames.size === 0) continue
  91. const def = getPropertyNode(prop.value, 'default')
  92. if (!def) continue
  93. const defType = getValueType(def.value)
  94. if (!defType || typeNames.has(defType)) continue
  95. const propName = prop.propName != null ? prop.propName : `[${context.getSourceCode().getText(prop.key)}]`
  96. context.report({
  97. node: def,
  98. message: "Type of the default value for '{{name}}' prop must be a {{types}}.",
  99. data: {
  100. name: propName,
  101. types: Array.from(typeNames).join(' or ').toLowerCase()
  102. }
  103. })
  104. }
  105. })
  106. }
  107. }