Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positives for type-only exports in vue/no-export-in-script-setup rule #1713

Merged
merged 2 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -25,7 +25,7 @@ jobs:
# - v2-npm-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package.json" }}
- run:
name: Install dependencies
command: npm install
command: npm install --legacy-peer-deps
- run:
name: Test
command: npm test
Expand Down
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
}
]
}
]
})