-
-
Notifications
You must be signed in to change notification settings - Fork 707
feat(linter): add vue/no-export-in-script-setup rule
#14307
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
graphite-app
merged 1 commit into
main
from
10-02-feat_linter_add_vue_no-export-in-script-setup_rule
Oct 6, 2025
+290
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
crates/oxc_linter/src/rules/vue/no_export_in_script_setup.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::Span; | ||
|
|
||
| use crate::{ | ||
| context::{ContextHost, LintContext}, | ||
| frameworks::FrameworkOptions, | ||
| rule::Rule, | ||
| }; | ||
|
|
||
| fn no_export_in_script_setup_diagnostic(span: Span) -> OxcDiagnostic { | ||
| OxcDiagnostic::warn("<script setup>` cannot contain ES module exports.").with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct NoExportInScriptSetup; | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// Disallow `export` in `<script setup>` | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// The previous version of `<script setup>` RFC used `export` to define variables used in templates, | ||
| /// but the new `<script setup>` RFC has been updated to define without using `export`. | ||
| /// See [Vue RFCs - 0040-script-setup](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md) for more details. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```vue | ||
| /// <script setup> | ||
| /// export let msg = 'Hello!' | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```vue | ||
| /// <script setup> | ||
| /// let msg = 'Hello!' | ||
| /// </script> | ||
| /// ``` | ||
| NoExportInScriptSetup, | ||
| vue, | ||
| correctness, | ||
| ); | ||
|
|
||
| impl Rule for NoExportInScriptSetup { | ||
| fn run_once(&self, ctx: &LintContext) { | ||
| let modules = ctx.module_record(); | ||
|
|
||
| for entry in &modules.local_export_entries { | ||
| if entry.is_type { | ||
| continue; | ||
| } | ||
|
|
||
| ctx.diagnostic(no_export_in_script_setup_diagnostic(entry.span)); | ||
| } | ||
|
|
||
| for entry in &modules.indirect_export_entries { | ||
| if entry.is_type { | ||
| continue; | ||
| } | ||
|
|
||
| ctx.diagnostic(no_export_in_script_setup_diagnostic(entry.span)); | ||
| } | ||
|
|
||
| for entry in &modules.star_export_entries { | ||
| if entry.is_type { | ||
| continue; | ||
| } | ||
| ctx.diagnostic(no_export_in_script_setup_diagnostic(entry.span)); | ||
| } | ||
|
|
||
| if let Some(span) = modules.export_default { | ||
| ctx.diagnostic(no_export_in_script_setup_diagnostic(span)); | ||
| } | ||
| } | ||
|
|
||
| fn should_run(&self, ctx: &ContextHost) -> bool { | ||
| ctx.frameworks_options() == FrameworkOptions::VueSetup | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
| use std::path::PathBuf; | ||
|
|
||
| let pass = vec![ | ||
| ( | ||
| " | ||
| <script> | ||
| export * from 'foo' | ||
| export default {} | ||
| export class A {} | ||
| </script> | ||
| ", | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.vue")), | ||
| ), | ||
| ( | ||
| " | ||
| <script> | ||
| export * from 'foo' | ||
| export default {} | ||
| export class A {} | ||
| </script> | ||
| <script setup> | ||
| let foo; | ||
| </script> | ||
| ", | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.vue")), | ||
| ), | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| ( | ||
| " | ||
| <script setup> | ||
| 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> | ||
| ", | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.vue")), | ||
| ), | ||
| ( | ||
| " | ||
| <script> | ||
| let foo; | ||
| </script> | ||
| <script setup> | ||
| export * from 'foo' | ||
| export default {} | ||
| export class A {} | ||
| </script> | ||
| ", | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.vue")), | ||
| ), | ||
| ( | ||
| r#" | ||
| <script setup lang="ts"> | ||
| export const Foo = {} | ||
| export enum Bar {} | ||
| export { } | ||
| </script> | ||
| "#, | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.vue")), | ||
| ), // { "parser": require("vue-eslint-parser"), "parserOptions": { "parser": require.resolve("@typescript-eslint/parser") } } | ||
| ]; | ||
|
|
||
| Tester::new(NoExportInScriptSetup::NAME, NoExportInScriptSetup::PLUGIN, pass, fail) | ||
| .test_and_snapshot(); | ||
| } |
114 changes: 114 additions & 0 deletions
114
crates/oxc_linter/src/snapshots/vue_no_export_in_script_setup.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| source: crates/oxc_linter/src/tester.rs | ||
| --- | ||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:4:25] | ||
| 3 │ export * from 'foo' | ||
| 4 │ export default {} | ||
| · ── | ||
| 5 │ export class A {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:5:17] | ||
| 4 │ export default {} | ||
| 5 │ export class A {} | ||
| · ────────── | ||
| 6 │ export const test = '123' | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:6:17] | ||
| 5 │ export class A {} | ||
| 6 │ export const test = '123' | ||
| · ────────────────── | ||
| 7 │ export function foo() {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:7:17] | ||
| 6 │ export const test = '123' | ||
| 7 │ export function foo() {} | ||
| · ───────────────── | ||
| 8 │ const a = 1 | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:9:19] | ||
| 8 │ const a = 1 | ||
| 9 │ export { a } | ||
| · ─ | ||
| 10 │ export { fao } from 'bar' | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:10:19] | ||
| 9 │ export { a } | ||
| 10 │ export { fao } from 'bar' | ||
| · ─── | ||
| 11 │ </script> | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:3:10] | ||
| 2 │ <script setup> | ||
| 3 │ export * from 'foo' | ||
| · ─────────────────── | ||
| 4 │ export default {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:4:17] | ||
| 3 │ export * from 'foo' | ||
| 4 │ export default {} | ||
| · ─────── | ||
| 5 │ export class A {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:7:25] | ||
| 6 │ export * from 'foo' | ||
| 7 │ export default {} | ||
| · ── | ||
| 8 │ export class A {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:8:17] | ||
| 7 │ export default {} | ||
| 8 │ export class A {} | ||
| · ────────── | ||
| 9 │ </script> | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:6:10] | ||
| 5 │ <script setup> | ||
| 6 │ export * from 'foo' | ||
| · ─────────────────── | ||
| 7 │ export default {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:7:17] | ||
| 6 │ export * from 'foo' | ||
| 7 │ export default {} | ||
| · ─────── | ||
| 8 │ export class A {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:3:17] | ||
| 2 │ <script setup lang="ts"> | ||
| 3 │ export const Foo = {} | ||
| · ────────────── | ||
| 4 │ export enum Bar {} | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-vue(no-export-in-script-setup): <script setup>` cannot contain ES module exports. | ||
| ╭─[no_export_in_script_setup.tsx:4:17] | ||
| 3 │ export const Foo = {} | ||
| 4 │ export enum Bar {} | ||
| · ─────────── | ||
| 5 │ export { } | ||
| ╰──── | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.