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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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'],
  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 (tsNode.type === 'ExportNamedDeclaration') {
  38. if (
  39. tsNode.specifiers.length > 0 &&
  40. tsNode.specifiers.every((spec) => spec.exportKind === 'type')
  41. ) {
  42. return
  43. }
  44. }
  45. context.report({
  46. node,
  47. messageId: 'forbidden'
  48. })
  49. }
  50. return utils.defineScriptSetupVisitor(context, {
  51. ExportAllDeclaration: verify,
  52. ExportDefaultDeclaration: verify,
  53. ExportNamedDeclaration: verify
  54. })
  55. }
  56. }