require-direct-export.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @fileoverview require the component to be directly exported
  3. * @author Hiroki Osame <hiroki.osame@gmail.com>
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: 'suggestion',
  13. docs: {
  14. description: 'require the component to be directly exported',
  15. categories: undefined,
  16. url: 'https://eslint.vuejs.org/rules/require-direct-export.html'
  17. },
  18. fixable: null, // or "code" or "whitespace"
  19. schema: [
  20. {
  21. type: 'object',
  22. properties: {
  23. disallowFunctionalComponentFunction: { type: 'boolean' }
  24. },
  25. additionalProperties: false
  26. }
  27. ]
  28. },
  29. /** @param {RuleContext} context */
  30. create(context) {
  31. const filePath = context.getFilename()
  32. if (!utils.isVueFile(filePath)) return {}
  33. const disallowFunctional = (context.options[0] || {})
  34. .disallowFunctionalComponentFunction
  35. /**
  36. * @typedef {object} ScopeStack
  37. * @property {ScopeStack | null} upper
  38. * @property {boolean} withinVue3FunctionalBody
  39. */
  40. /** @type { { body: BlockStatement, hasReturnArgument: boolean } } */
  41. let maybeVue3Functional
  42. /** @type {ScopeStack | null} */
  43. let scopeStack = null
  44. return {
  45. /** @param {Declaration | Expression} node */
  46. 'ExportDefaultDeclaration > *'(node) {
  47. if (node.type === 'ObjectExpression') {
  48. // OK
  49. return
  50. }
  51. if (!disallowFunctional) {
  52. if (node.type === 'ArrowFunctionExpression') {
  53. if (node.body.type !== 'BlockStatement') {
  54. // OK
  55. return
  56. }
  57. maybeVue3Functional = {
  58. body: node.body,
  59. hasReturnArgument: false
  60. }
  61. return
  62. }
  63. if (
  64. node.type === 'FunctionExpression' ||
  65. node.type === 'FunctionDeclaration'
  66. ) {
  67. maybeVue3Functional = {
  68. body: node.body,
  69. hasReturnArgument: false
  70. }
  71. return
  72. }
  73. }
  74. context.report({
  75. node: node.parent,
  76. message: `Expected the component literal to be directly exported.`
  77. })
  78. },
  79. ...(disallowFunctional
  80. ? {}
  81. : {
  82. /** @param {BlockStatement} node */
  83. ':function > BlockStatement'(node) {
  84. if (!maybeVue3Functional) {
  85. return
  86. }
  87. scopeStack = {
  88. upper: scopeStack,
  89. withinVue3FunctionalBody: maybeVue3Functional.body === node
  90. }
  91. },
  92. /** @param {ReturnStatement} node */
  93. ReturnStatement(node) {
  94. if (
  95. scopeStack &&
  96. scopeStack.withinVue3FunctionalBody &&
  97. node.argument
  98. ) {
  99. maybeVue3Functional.hasReturnArgument = true
  100. }
  101. },
  102. ':function > BlockStatement:exit'() {
  103. scopeStack = scopeStack && scopeStack.upper
  104. },
  105. /** @param {ExportDefaultDeclaration} node */
  106. 'ExportDefaultDeclaration:exit'(node) {
  107. if (!maybeVue3Functional) {
  108. return
  109. }
  110. if (!maybeVue3Functional.hasReturnArgument) {
  111. context.report({
  112. node,
  113. message: `Expected the component literal to be directly exported.`
  114. })
  115. }
  116. }
  117. })
  118. }
  119. }
  120. }