no-export-in-script-setup.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. /**
  8. * @typedef {import('@typescript-eslint/types').TSESTree.ExportAllDeclaration} TSESTreeExportAllDeclaration
  9. * @typedef {import('@typescript-eslint/types').TSESTree.ExportDefaultDeclaration} TSESTreeExportDefaultDeclaration
  10. * @typedef {import('@typescript-eslint/types').TSESTree.ExportNamedDeclaration} TSESTreeExportNamedDeclaration
  11. */
  12. module.exports = {
  13. meta: {
  14. type: 'problem',
  15. docs: {
  16. description: 'disallow `export` in `<script setup>`',
  17. categories: ['vue3-essential', 'vue2-essential'],
  18. url: 'https://eslint.vuejs.org/rules/no-export-in-script-setup.html'
  19. },
  20. fixable: null,
  21. schema: [],
  22. messages: {
  23. forbidden: '`<script setup>` cannot contain ES module exports.'
  24. }
  25. },
  26. /** @param {RuleContext} context */
  27. create(context) {
  28. /** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */
  29. function verify(node) {
  30. const tsNode =
  31. /** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
  32. node
  33. )
  34. if (tsNode.exportKind === 'type') {
  35. return
  36. }
  37. if (
  38. tsNode.type === 'ExportNamedDeclaration' &&
  39. tsNode.specifiers.length > 0 &&
  40. tsNode.specifiers.every((spec) => spec.exportKind === 'type')
  41. ) {
  42. return
  43. }
  44. context.report({
  45. node,
  46. messageId: 'forbidden'
  47. })
  48. }
  49. return utils.defineScriptSetupVisitor(context, {
  50. ExportAllDeclaration: verify,
  51. ExportDefaultDeclaration: verify,
  52. ExportNamedDeclaration: verify
  53. })
  54. }
  55. }