-
-
Notifications
You must be signed in to change notification settings - Fork 723
feat(linter): add vue/prefer-import-from-vue rule
#14284
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-01-feat_linter_add_vue_prefer-import-from-vue_rule
Oct 2, 2025
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
153 changes: 153 additions & 0 deletions
153
crates/oxc_linter/src/rules/vue/prefer_import_from_vue.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,153 @@ | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::Span; | ||
|
|
||
| use crate::{context::LintContext, rule::Rule}; | ||
|
|
||
| fn prefer_import_from_vue_diagnostic(span: Span) -> OxcDiagnostic { | ||
| OxcDiagnostic::warn("enforce import from 'vue' instead of import from '@vue/*'") | ||
| .with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct PreferImportFromVue; | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// Enforce import from 'vue' instead of import from '@vue/*'. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// Imports from the following modules are almost always wrong. You should import from vue instead. | ||
| /// - `@vue/runtime-dom` | ||
| /// - `@vue/runtime-core` | ||
| /// - `@vue/reactivity` | ||
| /// - `@vue/shared` | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```js | ||
| /// import { createApp } from '@vue/runtime-dom' | ||
| /// import { Component } from '@vue/runtime-core' | ||
| /// import { ref } from '@vue/reactivity' | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```js | ||
| /// import { createApp, ref, Component } from 'vue' | ||
| /// ``` | ||
| PreferImportFromVue, | ||
| vue, | ||
| correctness, | ||
| fix | ||
| ); | ||
|
|
||
| const VUE_MODULES: &[&str; 4] = | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| &["@vue/reactivity", "@vue/runtime-core", "@vue/runtime-dom", "@vue/shared"]; | ||
| impl Rule for PreferImportFromVue { | ||
| fn run_once(&self, ctx: &LintContext) { | ||
| let records = ctx.module_record(); | ||
|
|
||
| for entry in &records.import_entries { | ||
| if VUE_MODULES.contains(&entry.module_request.name.as_str()) { | ||
| ctx.diagnostic_with_fix( | ||
| prefer_import_from_vue_diagnostic(entry.module_request.span), | ||
| |fixer| fixer.replace(entry.module_request.span, "'vue'".to_string()), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| for entry in &records.indirect_export_entries { | ||
| let Some(name) = &entry.module_request else { | ||
| continue; | ||
| }; | ||
| if VUE_MODULES.contains(&name.name.as_str()) { | ||
| ctx.diagnostic_with_fix(prefer_import_from_vue_diagnostic(name.span), |fixer| { | ||
| fixer.replace(name.span, "'vue'".to_string()) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| for entry in &records.star_export_entries { | ||
| let Some(name) = &entry.module_request else { | ||
| continue; | ||
| }; | ||
| if VUE_MODULES.contains(&name.name.as_str()) { | ||
| ctx.diagnostic_with_fix(prefer_import_from_vue_diagnostic(name.span), |fixer| { | ||
| fixer.replace(name.span, "'vue'".to_string()) | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { | ||
| !ctx.source_type().is_typescript_definition() | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
| use std::path::PathBuf; | ||
|
|
||
| let pass = vec![ | ||
| ("import { createApp } from 'vue'", None, None, None), | ||
| ("import { ref, reactive } from '@vue/composition-api'", None, None, None), | ||
| ("export { createApp } from 'vue'", None, None, None), | ||
| ("export * from 'vue'", None, None, None), | ||
| ("import Foo from 'foo'", None, None, None), | ||
| ( | ||
| "import { createApp } from 'vue' | ||
| export { createApp }", | ||
| None, | ||
| None, | ||
| None, | ||
| ), | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ( | ||
| "import { unknown } from '@vue/runtime-dom'", | ||
| None, | ||
| None, | ||
| Some(PathBuf::from("test.d.ts")), | ||
| ), | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| ("import { createApp } from '@vue/runtime-dom'", None, None, None), | ||
| ("import { computed } from '@vue/runtime-core'", None, None, None), | ||
| ("import { computed } from '@vue/reactivity'", None, None, None), | ||
| ("import { normalizeClass } from '@vue/shared'", None, None, None), | ||
| ("import { unknown } from '@vue/reactivity'", None, None, None), | ||
| ("import { unknown } from '@vue/runtime-dom'", None, None, None), | ||
| ("import * as Foo from '@vue/reactivity'", None, None, None), | ||
| ("import * as Foo from '@vue/runtime-dom'", None, None, None), | ||
| ("export * from '@vue/reactivity'", None, None, None), | ||
| ("export * from '@vue/runtime-dom'", None, None, None), | ||
| ("export { computed } from '@vue/reactivity'", None, None, None), | ||
| ("export { computed } from '@vue/runtime-dom'", None, None, None), | ||
| ("export { unknown } from '@vue/reactivity'", None, None, None), | ||
| ("export { unknown } from '@vue/runtime-dom'", None, None, None), | ||
| ("import unknown from '@vue/reactivity'", None, None, None), | ||
| ("import unknown from '@vue/runtime-dom'", None, None, None), | ||
| ]; | ||
|
|
||
| let fix = vec![ | ||
| ("import { createApp } from '@vue/runtime-dom'", "import { createApp } from 'vue'", None), | ||
| ("import { computed } from '@vue/runtime-core'", "import { computed } from 'vue'", None), | ||
| ("import { computed } from '@vue/reactivity'", "import { computed } from 'vue'", None), | ||
| ( | ||
| "import { normalizeClass } from '@vue/shared'", | ||
| "import { normalizeClass } from 'vue'", | ||
| None, | ||
| ), | ||
| ("import { unknown } from '@vue/runtime-dom'", "import { unknown } from 'vue'", None), | ||
| ("import * as Foo from '@vue/runtime-dom'", "import * as Foo from 'vue'", None), | ||
| ("export { computed } from '@vue/reactivity'", "export { computed } from 'vue'", None), | ||
| ("export { computed } from '@vue/runtime-dom'", "export { computed } from 'vue'", None), | ||
| ("import unknown from '@vue/runtime-dom'", "import unknown from 'vue'", None), | ||
| ]; | ||
| Tester::new(PreferImportFromVue::NAME, PreferImportFromVue::PLUGIN, pass, fail) | ||
| .expect_fix(fix) | ||
| .test_and_snapshot(); | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
crates/oxc_linter/src/snapshots/vue_prefer_import_from_vue.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(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:27] | ||
| 1 │ import { createApp } from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:26] | ||
| 1 │ import { computed } from '@vue/runtime-core' | ||
| · ─────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-core'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:26] | ||
| 1 │ import { computed } from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:32] | ||
| 1 │ import { normalizeClass } from '@vue/shared' | ||
| · ───────────── | ||
| ╰──── | ||
| help: Replace `'@vue/shared'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:25] | ||
| 1 │ import { unknown } from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:25] | ||
| 1 │ import { unknown } from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:22] | ||
| 1 │ import * as Foo from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:22] | ||
| 1 │ import * as Foo from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:15] | ||
| 1 │ export * from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:15] | ||
| 1 │ export * from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:26] | ||
| 1 │ export { computed } from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:26] | ||
| 1 │ export { computed } from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:25] | ||
| 1 │ export { unknown } from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:25] | ||
| 1 │ export { unknown } from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:21] | ||
| 1 │ import unknown from '@vue/reactivity' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/reactivity'` with `'vue'`. | ||
|
|
||
| ⚠ eslint-plugin-vue(prefer-import-from-vue): enforce import from 'vue' instead of import from '@vue/*' | ||
| ╭─[prefer_import_from_vue.tsx:1:21] | ||
| 1 │ import unknown from '@vue/runtime-dom' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Replace `'@vue/runtime-dom'` with `'vue'`. |
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.