require-direct-export.js 4.1 KB

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