Skip to content

fix(no-export-in-script-setup): better report location #2701

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/rules/no-export-in-script-setup.js
Original file line number Diff line number Diff line change
@@ -28,8 +28,11 @@ 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
@@ -46,14 +49,24 @@ module.exports = {
}
context.report({
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 let foo = 'foo', export class Foo {}, export function foo() {}
if (node.declaration) {
verify(node, context.getSourceCode().getFirstToken(node).loc)
}
// export { foo }, export { foo } from 'bar'
else {
verify(node, node.loc)
}
}
})
}
}
78 changes: 69 additions & 9 deletions tests/lib/rules/no-export-in-script-setup.js
Original file line number Diff line number Diff line change
@@ -92,20 +92,62 @@ ruleTester.run('no-export-in-script-setup', rule, {
export * from 'foo'
export default {}
export class A {}
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
line: 4,
endLine: 4,
column: 7,
endColumn: 24
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 5
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: 7,
endLine: 7,
column: 7,
endColumn: 13
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 9,
endLine: 9,
column: 7,
endColumn: 19
},
{
message: '`<script setup>` cannot contain ES module exports.',
line: 10,
endLine: 10,
column: 7,
endColumn: 32
}
]
},
@@ -124,15 +166,24 @@ ruleTester.run('no-export-in-script-setup', rule, {
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
}
]
},
@@ -154,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
}
]
}

Unchanged files with check annotations Beta

// turn off some rules from shared configs in all files
{
rules: {
'eslint-plugin/require-meta-default-options': 'off', // TODO: enable when all rules have defaultOptions

Check warning on line 48 in eslint.config.js

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: enable when all rules have...'
'eslint-plugin/require-meta-docs-recommended': 'off', // use `categories` instead
'eslint-plugin/require-meta-schema-description': 'off',