require-emit-validator.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @fileoverview Emit definitions should be detailed
  3. * @author Pig Fang
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @typedef {import('../utils').ComponentEmit} ComponentEmit
  9. */
  10. module.exports = {
  11. meta: {
  12. hasSuggestions: true,
  13. type: 'suggestion',
  14. docs: {
  15. description: 'require type definitions in emits',
  16. categories: undefined,
  17. url: 'https://eslint.vuejs.org/rules/require-emit-validator.html'
  18. },
  19. fixable: null,
  20. messages: {
  21. missing: 'Emit "{{name}}" should define at least its validator function.',
  22. skipped:
  23. 'Emit "{{name}}" should not skip validation, or you may define a validator function with no parameters.',
  24. emptyValidation: 'Replace with a validator function with no parameters.'
  25. },
  26. schema: []
  27. },
  28. /** @param {RuleContext} context */
  29. create(context) {
  30. /**
  31. * @param {ComponentEmit} emit
  32. */
  33. function checker(emit) {
  34. if (emit.type !== 'object' && emit.type !== 'array') {
  35. return
  36. }
  37. const { value, node, emitName } = emit
  38. const hasType =
  39. !!value &&
  40. (value.type === 'ArrowFunctionExpression' ||
  41. value.type === 'FunctionExpression' ||
  42. // validator may from outer scope
  43. value.type === 'Identifier')
  44. if (!hasType) {
  45. const name =
  46. emitName ||
  47. (node.type === 'Identifier' && node.name) ||
  48. 'Unknown emit'
  49. if (value && value.type === 'Literal' && value.value === null) {
  50. context.report({
  51. node,
  52. messageId: 'skipped',
  53. data: { name },
  54. suggest: [
  55. {
  56. messageId: 'emptyValidation',
  57. fix: (fixer) => fixer.replaceText(value, '() => true')
  58. }
  59. ]
  60. })
  61. return
  62. }
  63. context.report({
  64. node,
  65. messageId: 'missing',
  66. data: { name }
  67. })
  68. }
  69. }
  70. return utils.compositingVisitors(
  71. utils.executeOnVue(context, (obj) => {
  72. for (const emit of utils.getComponentEmitsFromOptions(obj)) {
  73. checker(emit)
  74. }
  75. }),
  76. utils.defineScriptSetupVisitor(context, {
  77. onDefineEmitsEnter(_node, emits) {
  78. for (const emit of emits) {
  79. checker(emit)
  80. }
  81. }
  82. })
  83. )
  84. }
  85. }