Skip to content

Commit

Permalink
refactor: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji committed Mar 5, 2025
1 parent b765d02 commit bf87c65
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 44 deletions.
31 changes: 20 additions & 11 deletions lib/rules/no-export-in-script-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ module.exports = {
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/no-export-in-script-setup.html'
},
fixable: 'code',
fixable: null,
schema: [],
messages: {
forbidden: '`<script setup>` cannot contain ES module exports.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */
function verify(node) {
/**
* @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node
* @param {SourceLocation} loc
*/
function verify(node, loc) {
const tsNode =
/** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
node
Expand All @@ -46,18 +49,24 @@ module.exports = {
}
context.report({
node,
loc: node.loc,
messageId: 'forbidden',
fix(fixer) {
return fixer.remove(node)
}
loc,
messageId: 'forbidden'
})
}

return utils.defineScriptSetupVisitor(context, {
ExportAllDeclaration: verify,
ExportDefaultDeclaration: verify,
ExportNamedDeclaration: verify
ExportAllDeclaration: (node) => verify(node, node.loc),
ExportDefaultDeclaration: (node) => verify(node, node.loc),
ExportNamedDeclaration: (node) => {
// export { foo }, export { foo } from 'bar'
if (node.declaration) {
verify(node, context.getSourceCode().getFirstToken(node).loc)
}
// export let foo = 'foo', export class Foo {}, export function foo() {}
else {
verify(node, node.loc)
}
}
})
}
}
102 changes: 69 additions & 33 deletions tests/lib/rules/no-export-in-script-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,62 @@ ruleTester.run('no-export-in-script-setup', rule, {
export * from 'foo'
export default {}
export class A {}
</script>
`,
output: `
<script setup>
export const test = '123'
export function foo() {}
const a = 1
export { a }
export { fao } from 'bar'
</script>
`,
errors: [
{
message: '`<script setup>` cannot contain ES module exports.',
line: 3
line: 3,
endLine: 3,
column: 7,
endColumn: 26
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 4,
endLine: 4,
column: 7,
endColumn: 24
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 5,
endLine: 5,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 6,
endLine: 6,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 4
line: 7,
endLine: 7,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 5
line: 9,
endLine: 9,
column: 7,
endColumn: 19
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 10,
endLine: 10,
column: 7,
endColumn: 32
}
]
},
Expand All @@ -126,30 +161,29 @@ ruleTester.run('no-export-in-script-setup', rule, {
export * from 'foo'
export default {}
export class A {}
</script>
`,
output: `
<script>
let foo;
</script>
<script setup>
</script>
`,
errors: [
{
message: '`<script setup>` cannot contain ES module exports.',
line: 6
line: 6,
endLine: 6,
column: 7,
endColumn: 26
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 7
line: 7,
endLine: 7,
column: 7,
endColumn: 24
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 8
line: 8,
endLine: 8,
column: 7,
endColumn: 13
}
]
},
Expand All @@ -160,13 +194,6 @@ ruleTester.run('no-export-in-script-setup', rule, {
export const Foo = {}
export enum Bar {}
export {}
</script>
`,
output: `
<script setup lang="ts">
</script>
`,
languageOptions: {
Expand All @@ -178,15 +205,24 @@ ruleTester.run('no-export-in-script-setup', rule, {
errors: [
{
message: '`<script setup>` cannot contain ES module exports.',
line: 3
line: 3,
endLine: 3,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 4
line: 4,
endLine: 4,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 5
line: 5,
endLine: 5,
column: 7,
endColumn: 16
}
]
}
Expand Down

0 comments on commit bf87c65

Please sign in to comment.