Skip to content

Commit b2bec75

Browse files
committed
False positives for type-only exports in vue/no-export-in-script-setup rule
1 parent 6c64a09 commit b2bec75

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

lib/rules/no-export-in-script-setup.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
const utils = require('../utils')
88

9+
/**
10+
* @typedef {import('@typescript-eslint/types').TSESTree.ExportAllDeclaration} TSESTreeExportAllDeclaration
11+
* @typedef {import('@typescript-eslint/types').TSESTree.ExportDefaultDeclaration} TSESTreeExportDefaultDeclaration
12+
* @typedef {import('@typescript-eslint/types').TSESTree.ExportNamedDeclaration} TSESTreeExportNamedDeclaration
13+
*/
14+
915
module.exports = {
1016
meta: {
1117
type: 'problem',
@@ -23,17 +29,32 @@ module.exports = {
2329
/** @param {RuleContext} context */
2430
create(context) {
2531
/** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */
26-
function report(node) {
32+
function verify(node) {
33+
const tsNode =
34+
/** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
35+
node
36+
)
37+
if (tsNode.exportKind === 'type') {
38+
return
39+
}
40+
if (tsNode.type === 'ExportNamedDeclaration') {
41+
if (
42+
tsNode.specifiers.length > 0 &&
43+
tsNode.specifiers.every((spec) => spec.exportKind === 'type')
44+
) {
45+
return
46+
}
47+
}
2748
context.report({
2849
node,
2950
messageId: 'forbidden'
3051
})
3152
}
3253

3354
return utils.defineScriptSetupVisitor(context, {
34-
ExportAllDeclaration: report,
35-
ExportDefaultDeclaration: report,
36-
ExportNamedDeclaration: report
55+
ExportAllDeclaration: verify,
56+
ExportDefaultDeclaration: verify,
57+
ExportNamedDeclaration: verify
3758
})
3859
}
3960
}

tests/lib/rules/no-export-in-script-setup.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ ruleTester.run('no-export-in-script-setup', rule, {
4848
let foo;
4949
</script>
5050
`
51+
},
52+
{
53+
filename: 'test.vue',
54+
code: `
55+
<script setup lang="ts">
56+
export { type Foo } from "foo"
57+
export type Bar = {}
58+
export interface Bar {}
59+
</script>
60+
`,
61+
parser: require.resolve('vue-eslint-parser'),
62+
parserOptions: {
63+
parser: require.resolve('@typescript-eslint/parser')
64+
}
5165
}
5266
],
5367

@@ -102,6 +116,34 @@ ruleTester.run('no-export-in-script-setup', rule, {
102116
line: 8
103117
}
104118
]
119+
},
120+
{
121+
filename: 'test.vue',
122+
code: `
123+
<script setup lang="ts">
124+
export const Foo = {}
125+
export enum Bar {}
126+
export {}
127+
</script>
128+
`,
129+
parser: require.resolve('vue-eslint-parser'),
130+
parserOptions: {
131+
parser: require.resolve('@typescript-eslint/parser')
132+
},
133+
errors: [
134+
{
135+
message: '`<script setup>` cannot contain ES module exports.',
136+
line: 3
137+
},
138+
{
139+
message: '`<script setup>` cannot contain ES module exports.',
140+
line: 4
141+
},
142+
{
143+
message: '`<script setup>` cannot contain ES module exports.',
144+
line: 5
145+
}
146+
]
105147
}
106148
]
107149
})

0 commit comments

Comments
 (0)