Skip to content

Commit 87bb3ec

Browse files
authored
fix(no-export-in-script-setup): better report location (#2701)
1 parent 9fab6bd commit 87bb3ec

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ module.exports = {
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,14 +49,24 @@ module.exports = {
4649
}
4750
context.report({
4851
node,
52+
loc,
4953
messageId: 'forbidden'
5054
})
5155
}
5256

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

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

+69-9
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,62 @@ ruleTester.run('no-export-in-script-setup', rule, {
9292
export * from 'foo'
9393
export default {}
9494
export class A {}
95+
export const test = '123'
96+
export function foo() {}
97+
const a = 1
98+
export { a }
99+
export { fao } from 'bar'
95100
</script>
96101
`,
97102
errors: [
98103
{
99104
message: '`<script setup>` cannot contain ES module exports.',
100-
line: 3
105+
line: 3,
106+
endLine: 3,
107+
column: 7,
108+
endColumn: 26
101109
},
102110
{
103111
message: '`<script setup>` cannot contain ES module exports.',
104-
line: 4
112+
line: 4,
113+
endLine: 4,
114+
column: 7,
115+
endColumn: 24
105116
},
106117
{
107118
message: '`<script setup>` cannot contain ES module exports.',
108-
line: 5
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
130+
},
131+
{
132+
message: '`<script setup>` cannot contain ES module exports.',
133+
line: 7,
134+
endLine: 7,
135+
column: 7,
136+
endColumn: 13
137+
},
138+
{
139+
message: '`<script setup>` cannot contain ES module exports.',
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
109151
}
110152
]
111153
},
@@ -124,15 +166,24 @@ ruleTester.run('no-export-in-script-setup', rule, {
124166
errors: [
125167
{
126168
message: '`<script setup>` cannot contain ES module exports.',
127-
line: 6
169+
line: 6,
170+
endLine: 6,
171+
column: 7,
172+
endColumn: 26
128173
},
129174
{
130175
message: '`<script setup>` cannot contain ES module exports.',
131-
line: 7
176+
line: 7,
177+
endLine: 7,
178+
column: 7,
179+
endColumn: 24
132180
},
133181
{
134182
message: '`<script setup>` cannot contain ES module exports.',
135-
line: 8
183+
line: 8,
184+
endLine: 8,
185+
column: 7,
186+
endColumn: 13
136187
}
137188
]
138189
},
@@ -154,15 +205,24 @@ ruleTester.run('no-export-in-script-setup', rule, {
154205
errors: [
155206
{
156207
message: '`<script setup>` cannot contain ES module exports.',
157-
line: 3
208+
line: 3,
209+
endLine: 3,
210+
column: 7,
211+
endColumn: 13
158212
},
159213
{
160214
message: '`<script setup>` cannot contain ES module exports.',
161-
line: 4
215+
line: 4,
216+
endLine: 4,
217+
column: 7,
218+
endColumn: 13
162219
},
163220
{
164221
message: '`<script setup>` cannot contain ES module exports.',
165-
line: 5
222+
line: 5,
223+
endLine: 5,
224+
column: 7,
225+
endColumn: 16
166226
}
167227
]
168228
}

0 commit comments

Comments
 (0)