require-direct-export.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 (node.type === 'CallExpression') {
  52. const {
  53. callee,
  54. arguments: [firstArg]
  55. } = node
  56. if (firstArg && firstArg.type === 'ObjectExpression') {
  57. if (
  58. (callee.type === 'Identifier' &&
  59. callee.name === 'defineComponent') ||
  60. (callee.type === 'MemberExpression' &&
  61. callee.object.type === 'Identifier' &&
  62. callee.object.name === 'Vue' &&
  63. callee.property.type === 'Identifier' &&
  64. callee.property.name === 'extend')
  65. ) {
  66. return
  67. }
  68. }
  69. }
  70. if (!disallowFunctional) {
  71. if (node.type === 'ArrowFunctionExpression') {
  72. if (node.body.type !== 'BlockStatement') {
  73. // OK
  74. return
  75. }
  76. maybeVue3Functional = {
  77. body: node.body,
  78. hasReturnArgument: false
  79. }
  80. return
  81. }
  82. if (
  83. node.type === 'FunctionExpression' ||
  84. node.type === 'FunctionDeclaration'
  85. ) {
  86. maybeVue3Functional = {
  87. body: node.body,
  88. hasReturnArgument: false
  89. }
  90. return
  91. }
  92. }
  93. context.report({
  94. node: node.parent,
  95. message: `Expected the component literal to be directly exported.`
  96. })
  97. },
  98. ...(disallowFunctional
  99. ? {}
  100. : {
  101. /** @param {BlockStatement} node */
  102. ':function > BlockStatement'(node) {
  103. if (!maybeVue3Functional) {
  104. return
  105. }
  106. scopeStack = {
  107. upper: scopeStack,
  108. withinVue3FunctionalBody: maybeVue3Functional.body === node
  109. }
  110. },
  111. /** @param {ReturnStatement} node */
  112. ReturnStatement(node) {
  113. if (
  114. scopeStack &&
  115. scopeStack.withinVue3FunctionalBody &&
  116. node.argument
  117. ) {
  118. maybeVue3Functional.hasReturnArgument = true
  119. }
  120. },
  121. ':function > BlockStatement:exit'() {
  122. scopeStack = scopeStack && scopeStack.upper
  123. },
  124. /** @param {ExportDefaultDeclaration} node */
  125. 'ExportDefaultDeclaration:exit'(node) {
  126. if (!maybeVue3Functional) {
  127. return
  128. }
  129. if (!maybeVue3Functional.hasReturnArgument) {
  130. context.report({
  131. node,
  132. message: `Expected the component literal to be directly exported.`
  133. })
  134. }
  135. }
  136. })
  137. }
  138. }
  139. }