1f3fa4bc3ec0e2106ceb2fa0621f052647734a2cc3292f80aa219f9d55c84904a32f1e8ea0c27539993b3cf51e2f2ae09946116b5ae6dc639c4c71f2c4514f 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @fileoverview Enforces that a return statement is present in computed property (return-in-computed-property)
  3. * @author Armano
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
  9. */
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: 'problem',
  16. docs: {
  17. description:
  18. 'enforce that a return statement is present in computed property',
  19. categories: ['vue3-essential', 'essential'],
  20. url: 'https://eslint.vuejs.org/rules/return-in-computed-property.html'
  21. },
  22. fixable: null, // or "code" or "whitespace"
  23. schema: [
  24. {
  25. type: 'object',
  26. properties: {
  27. treatUndefinedAsUnspecified: {
  28. type: 'boolean'
  29. }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. },
  35. /** @param {RuleContext} context */
  36. create(context) {
  37. const options = context.options[0] || {}
  38. const treatUndefinedAsUnspecified = !(
  39. options.treatUndefinedAsUnspecified === false
  40. )
  41. /**
  42. * @type {Set<ComponentComputedProperty>}
  43. */
  44. const computedProperties = new Set()
  45. // ----------------------------------------------------------------------
  46. // Public
  47. // ----------------------------------------------------------------------
  48. return Object.assign(
  49. {},
  50. utils.defineVueVisitor(context, {
  51. onVueObjectEnter(obj) {
  52. for (const computedProperty of utils.getComputedProperties(obj)) {
  53. computedProperties.add(computedProperty)
  54. }
  55. }
  56. }),
  57. utils.executeOnFunctionsWithoutReturn(
  58. treatUndefinedAsUnspecified,
  59. (node) => {
  60. computedProperties.forEach((cp) => {
  61. if (cp.value && cp.value.parent === node) {
  62. context.report({
  63. node,
  64. message:
  65. 'Expected to return a value in "{{name}}" computed property.',
  66. data: {
  67. name: cp.key || 'Unknown'
  68. }
  69. })
  70. }
  71. })
  72. }
  73. )
  74. )
  75. }
  76. }