Skip to content

Commit

Permalink
False positives for type-only exports in `vue/no-export-in-script-set…
Browse files Browse the repository at this point in the history
…up` rule
  • Loading branch information
ota-meshi committed Nov 16, 2021
1 parent 6c64a09 commit 1d55ad1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
29 changes: 25 additions & 4 deletions lib/rules/no-export-in-script-setup.js
Expand Up @@ -6,6 +6,12 @@

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

/**
* @typedef {import('@typescript-eslint/types').TSESTree.ExportAllDeclaration} TSESTreeExportAllDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.ExportDefaultDeclaration} TSESTreeExportDefaultDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.ExportNamedDeclaration} TSESTreeExportNamedDeclaration
*/

module.exports = {
meta: {
type: 'problem',
Expand All @@ -23,17 +29,32 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
/** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */
function report(node) {
function verify(node) {
const tsNode =
/** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
node
)
if (tsNode.exportKind === 'type') {
return
}
if (tsNode.type === 'ExportNamedDeclaration') {
if (
tsNode.specifiers.length > 0 &&
tsNode.specifiers.every((spec) => spec.exportKind === 'type')
) {
return
}
}
context.report({
node,
messageId: 'forbidden'
})
}

return utils.defineScriptSetupVisitor(context, {
ExportAllDeclaration: report,
ExportDefaultDeclaration: report,
ExportNamedDeclaration: report
ExportAllDeclaration: verify,
ExportDefaultDeclaration: verify,
ExportNamedDeclaration: verify
})
}
}
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -65,7 +65,7 @@
"@types/natural-compare": "^1.4.0",
"@types/node": "^13.13.5",
"@types/semver": "^7.2.0",
"@typescript-eslint/parser": "^5.0.0-0",
"@typescript-eslint/parser": "^5.4.0",
"@vuepress/plugin-pwa": "^1.4.1",
"acorn": "^8.5.0",
"env-cmd": "^10.1.0",
Expand All @@ -82,7 +82,7 @@
"mocha": "^7.1.2",
"nyc": "^15.1.0",
"prettier": "^2.4.1",
"typescript": "^4.4.3",
"typescript": "^4.5.0-0",
"vue-eslint-editor": "^1.1.0",
"vuepress": "^1.8.2"
}
Expand Down
67 changes: 66 additions & 1 deletion tests/lib/rules/no-export-in-script-setup.js
Expand Up @@ -8,6 +8,7 @@
// Requirements
// ------------------------------------------------------------------------------

const semver = require('semver')
const eslint = require('eslint')
const rule = require('../../../lib/rules/no-export-in-script-setup')

Expand Down Expand Up @@ -48,7 +49,43 @@ ruleTester.run('no-export-in-script-setup', rule, {
let foo;
</script>
`
}
},
...(semver.gte(
require('@typescript-eslint/parser/package.json').version,
'5.4.0'
) &&
semver.satisfies(require('typescript/package.json').version, '>=4.5.0-0')
? [
{
filename: 'test.vue',
code: `
<script setup lang="ts">
export { type Foo } from "foo"
export type Bar = {}
export interface Bar {}
</script>
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
}
}
]
: [
{
filename: 'test.vue',
code: `
<script setup lang="ts">
export type Bar = {}
export interface Bar {}
</script>
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
}
}
])
],

invalid: [
Expand Down Expand Up @@ -102,6 +139,34 @@ ruleTester.run('no-export-in-script-setup', rule, {
line: 8
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
export const Foo = {}
export enum Bar {}
export {}
</script>
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
},
errors: [
{
message: '`<script setup>` cannot contain ES module exports.',
line: 3
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 4
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 5
}
]
}
]
})

0 comments on commit 1d55ad1

Please sign in to comment.