require-direct-export.js 4.1 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. module.exports = {
  8. meta: {
  9. type: 'suggestion',
  10. docs: {
  11. description: 'require the component to be directly exported',
  12. categories: undefined,
  13. url: 'https://eslint.vuejs.org/rules/require-direct-export.html'
  14. },
  15. fixable: null,
  16. schema: [
  17. {
  18. type: 'object',
  19. properties: {
  20. disallowFunctionalComponentFunction: { type: 'boolean' }
  21. },
  22. additionalProperties: false
  23. }
  24. ],
  25. messages: {
  26. expectedDirectExport:
  27. 'Expected the component literal to be directly exported.'
  28. }
  29. },
  30. /** @param {RuleContext} context */
  31. create(context) {
  32. const filePath = context.getFilename()
  33. if (!utils.isVueFile(filePath)) return {}
  34. const disallowFunctional = (context.options[0] || {})
  35. .disallowFunctionalComponentFunction
  36. /**
  37. * @typedef {object} ScopeStack
  38. * @property {ScopeStack | null} upper
  39. * @property {boolean} withinVue3FunctionalBody
  40. */
  41. /** @type { { body: BlockStatement, hasReturnArgument: boolean } } */
  42. let maybeVue3Functional
  43. /** @type {ScopeStack | null} */
  44. let scopeStack = null
  45. return {
  46. /** @param {Declaration | Expression} node */
  47. 'ExportDefaultDeclaration > *'(node) {
  48. if (node.type === 'ObjectExpression') {
  49. // OK
  50. return
  51. }
  52. if (node.type === 'CallExpression') {
  53. const {
  54. callee,
  55. arguments: [firstArg]
  56. } = node
  57. if (
  58. firstArg &&
  59. firstArg.type === 'ObjectExpression' &&
  60. ((callee.type === 'Identifier' &&
  61. callee.name === 'defineComponent') ||
  62. (callee.type === 'MemberExpression' &&
  63. callee.object.type === 'Identifier' &&
  64. callee.object.name === 'Vue' &&
  65. callee.property.type === 'Identifier' &&
  66. callee.property.name === 'extend'))
  67. ) {
  68. return
  69. }
  70. }
  71. if (!disallowFunctional) {
  72. if (node.type === 'ArrowFunctionExpression') {
  73. if (node.body.type !== 'BlockStatement') {
  74. // OK
  75. return
  76. }
  77. maybeVue3Functional = {
  78. body: node.body,
  79. hasReturnArgument: false
  80. }
  81. return
  82. }
  83. if (
  84. node.type === 'FunctionExpression' ||
  85. node.type === 'FunctionDeclaration'
  86. ) {
  87. maybeVue3Functional = {
  88. body: node.body,
  89. hasReturnArgument: false
  90. }
  91. return
  92. }
  93. }
  94. context.report({
  95. node: node.parent,
  96. messageId: 'expectedDirectExport'
  97. })
  98. },
  99. ...(disallowFunctional
  100. ? {}
  101. : {
  102. /** @param {BlockStatement} node */
  103. ':function > BlockStatement'(node) {
  104. if (!maybeVue3Functional) {
  105. return
  106. }
  107. scopeStack = {
  108. upper: scopeStack,
  109. withinVue3FunctionalBody: maybeVue3Functional.body === node
  110. }
  111. },
  112. /** @param {ReturnStatement} node */
  113. ReturnStatement(node) {
  114. if (
  115. scopeStack &&
  116. scopeStack.withinVue3FunctionalBody &&
  117. node.argument
  118. ) {
  119. maybeVue3Functional.hasReturnArgument = true
  120. }
  121. },
  122. ':function > BlockStatement:exit'() {
  123. scopeStack = scopeStack && scopeStack.upper
  124. },
  125. /** @param {ExportDefaultDeclaration} node */
  126. 'ExportDefaultDeclaration:exit'(node) {
  127. if (!maybeVue3Functional) {
  128. return
  129. }
  130. if (!maybeVue3Functional.hasReturnArgument) {
  131. context.report({
  132. node,
  133. messageId: 'expectedDirectExport'
  134. })
  135. }
  136. }
  137. })
  138. }
  139. }
  140. }