Skip to content

Commit db90eba

Browse files
committed
refactor: optimize code
1 parent b765d02 commit db90eba

File tree

2 files changed

+89
-44
lines changed

2 files changed

+89
-44
lines changed

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

+20-11
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ module.exports = {
2020
categories: ['vue3-essential', 'vue2-essential'],
2121
url: 'https://eslint.vuejs.org/rules/no-export-in-script-setup.html'
2222
},
23-
fixable: 'code',
23+
fixable: null,
2424
schema: [],
2525
messages: {
2626
forbidden: '`<script setup>` cannot contain ES module exports.'
2727
}
2828
},
2929
/** @param {RuleContext} context */
3030
create(context) {
31-
/** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */
32-
function verify(node) {
31+
/**
32+
* @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node
33+
* @param {SourceLocation} loc
34+
*/
35+
function verify(node, loc) {
3336
const tsNode =
3437
/** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
3538
node
@@ -46,18 +49,24 @@ module.exports = {
4649
}
4750
context.report({
4851
node,
49-
loc: node.loc,
50-
messageId: 'forbidden',
51-
fix(fixer) {
52-
return fixer.remove(node)
53-
}
52+
loc,
53+
messageId: 'forbidden'
5454
})
5555
}
5656

5757
return utils.defineScriptSetupVisitor(context, {
58-
ExportAllDeclaration: verify,
59-
ExportDefaultDeclaration: verify,
60-
ExportNamedDeclaration: verify
58+
ExportAllDeclaration: (node) => verify(node, node.loc),
59+
ExportDefaultDeclaration: (node) => verify(node, node.loc),
60+
ExportNamedDeclaration: (node) => {
61+
// export { foo }, export { foo } from 'bar'
62+
if (!node.declaration) {
63+
verify(node, node.loc)
64+
}
65+
// export let foo = 'foo', export class Foo {}, export function foo() {}
66+
else {
67+
verify(node, context.getSourceCode().getFirstToken(node).loc)
68+
}
69+
}
6170
})
6271
}
6372
}

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

+69-33
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,62 @@ ruleTester.run('no-export-in-script-setup', rule, {
9292
export * from 'foo'
9393
export default {}
9494
export class A {}
95-
</script>
96-
`,
97-
output: `
98-
<script setup>
99-
100-
101-
95+
export const test = '123'
96+
export function foo() {}
97+
const a = 1
98+
export { a }
99+
export { fao } from 'bar'
102100
</script>
103101
`,
104102
errors: [
105103
{
106104
message: '`<script setup>` cannot contain ES module exports.',
107-
line: 3
105+
line: 3,
106+
endLine: 3,
107+
column: 7,
108+
endColumn: 26
109+
},
110+
{
111+
message: '`<script setup>` cannot contain ES module exports.',
112+
line: 4,
113+
endLine: 4,
114+
column: 7,
115+
endColumn: 24
116+
},
117+
{
118+
message: '`<script setup>` cannot contain ES module exports.',
119+
line: 5,
120+
endLine: 5,
121+
column: 7,
122+
endColumn: 13
123+
},
124+
{
125+
message: '`<script setup>` cannot contain ES module exports.',
126+
line: 6,
127+
endLine: 6,
128+
column: 7,
129+
endColumn: 13
108130
},
109131
{
110132
message: '`<script setup>` cannot contain ES module exports.',
111-
line: 4
133+
line: 7,
134+
endLine: 7,
135+
column: 7,
136+
endColumn: 13
112137
},
113138
{
114139
message: '`<script setup>` cannot contain ES module exports.',
115-
line: 5
140+
line: 9,
141+
endLine: 9,
142+
column: 7,
143+
endColumn: 19
144+
},
145+
{
146+
message: '`<script setup>` cannot contain ES module exports.',
147+
line: 10,
148+
endLine: 10,
149+
column: 7,
150+
endColumn: 32
116151
}
117152
]
118153
},
@@ -126,30 +161,29 @@ ruleTester.run('no-export-in-script-setup', rule, {
126161
export * from 'foo'
127162
export default {}
128163
export class A {}
129-
</script>
130-
`,
131-
output: `
132-
<script>
133-
let foo;
134-
</script>
135-
<script setup>
136-
137-
138-
139164
</script>
140165
`,
141166
errors: [
142167
{
143168
message: '`<script setup>` cannot contain ES module exports.',
144-
line: 6
169+
line: 6,
170+
endLine: 6,
171+
column: 7,
172+
endColumn: 26
145173
},
146174
{
147175
message: '`<script setup>` cannot contain ES module exports.',
148-
line: 7
176+
line: 7,
177+
endLine: 7,
178+
column: 7,
179+
endColumn: 24
149180
},
150181
{
151182
message: '`<script setup>` cannot contain ES module exports.',
152-
line: 8
183+
line: 8,
184+
endLine: 8,
185+
column: 7,
186+
endColumn: 13
153187
}
154188
]
155189
},
@@ -160,13 +194,6 @@ ruleTester.run('no-export-in-script-setup', rule, {
160194
export const Foo = {}
161195
export enum Bar {}
162196
export {}
163-
</script>
164-
`,
165-
output: `
166-
<script setup lang="ts">
167-
168-
169-
170197
</script>
171198
`,
172199
languageOptions: {
@@ -178,15 +205,24 @@ ruleTester.run('no-export-in-script-setup', rule, {
178205
errors: [
179206
{
180207
message: '`<script setup>` cannot contain ES module exports.',
181-
line: 3
208+
line: 3,
209+
endLine: 3,
210+
column: 7,
211+
endColumn: 13
182212
},
183213
{
184214
message: '`<script setup>` cannot contain ES module exports.',
185-
line: 4
215+
line: 4,
216+
endLine: 4,
217+
column: 7,
218+
endColumn: 13
186219
},
187220
{
188221
message: '`<script setup>` cannot contain ES module exports.',
189-
line: 5
222+
line: 5,
223+
endLine: 5,
224+
column: 7,
225+
endColumn: 16
190226
}
191227
]
192228
}

0 commit comments

Comments
 (0)