|
| 1 | +use oxc_diagnostics::OxcDiagnostic; |
| 2 | +use oxc_macros::declare_oxc_lint; |
| 3 | +use oxc_span::Span; |
| 4 | + |
| 5 | +use crate::{context::LintContext, rule::Rule}; |
| 6 | + |
| 7 | +fn prefer_import_from_vue_diagnostic(span: Span) -> OxcDiagnostic { |
| 8 | + OxcDiagnostic::warn("enforce import from 'vue' instead of import from '@vue/*'") |
| 9 | + .with_label(span) |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Default, Clone)] |
| 13 | +pub struct PreferImportFromVue; |
| 14 | + |
| 15 | +declare_oxc_lint!( |
| 16 | + /// ### What it does |
| 17 | + /// |
| 18 | + /// Enforce import from 'vue' instead of import from '@vue/*'. |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// |
| 22 | + /// Imports from the following modules are almost always wrong. You should import from vue instead. |
| 23 | + /// - `@vue/runtime-dom` |
| 24 | + /// - `@vue/runtime-core` |
| 25 | + /// - `@vue/reactivity` |
| 26 | + /// - `@vue/shared` |
| 27 | + /// |
| 28 | + /// ### Examples |
| 29 | + /// |
| 30 | + /// Examples of **incorrect** code for this rule: |
| 31 | + /// ```js |
| 32 | + /// import { createApp } from '@vue/runtime-dom' |
| 33 | + /// import { Component } from '@vue/runtime-core' |
| 34 | + /// import { ref } from '@vue/reactivity' |
| 35 | + /// ``` |
| 36 | + /// |
| 37 | + /// Examples of **correct** code for this rule: |
| 38 | + /// ```js |
| 39 | + /// import { createApp, ref, Component } from 'vue' |
| 40 | + /// ``` |
| 41 | + PreferImportFromVue, |
| 42 | + vue, |
| 43 | + correctness, |
| 44 | + fix |
| 45 | +); |
| 46 | + |
| 47 | +const VUE_MODULES: &[&str; 4] = |
| 48 | + &["@vue/reactivity", "@vue/runtime-core", "@vue/runtime-dom", "@vue/shared"]; |
| 49 | +impl Rule for PreferImportFromVue { |
| 50 | + fn run_once(&self, ctx: &LintContext) { |
| 51 | + let records = ctx.module_record(); |
| 52 | + |
| 53 | + for entry in &records.import_entries { |
| 54 | + if VUE_MODULES.contains(&entry.module_request.name.as_str()) { |
| 55 | + ctx.diagnostic_with_fix( |
| 56 | + prefer_import_from_vue_diagnostic(entry.module_request.span), |
| 57 | + |fixer| fixer.replace(entry.module_request.span, "'vue'".to_string()), |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + for entry in &records.indirect_export_entries { |
| 63 | + let Some(name) = &entry.module_request else { |
| 64 | + continue; |
| 65 | + }; |
| 66 | + if VUE_MODULES.contains(&name.name.as_str()) { |
| 67 | + ctx.diagnostic_with_fix(prefer_import_from_vue_diagnostic(name.span), |fixer| { |
| 68 | + fixer.replace(name.span, "'vue'".to_string()) |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for entry in &records.star_export_entries { |
| 74 | + let Some(name) = &entry.module_request else { |
| 75 | + continue; |
| 76 | + }; |
| 77 | + if VUE_MODULES.contains(&name.name.as_str()) { |
| 78 | + ctx.diagnostic_with_fix(prefer_import_from_vue_diagnostic(name.span), |fixer| { |
| 79 | + fixer.replace(name.span, "'vue'".to_string()) |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { |
| 86 | + !ctx.source_type().is_typescript_definition() |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test() { |
| 92 | + use crate::tester::Tester; |
| 93 | + use std::path::PathBuf; |
| 94 | + |
| 95 | + let pass = vec![ |
| 96 | + ("import { createApp } from 'vue'", None, None, None), |
| 97 | + ("import { ref, reactive } from '@vue/composition-api'", None, None, None), |
| 98 | + ("export { createApp } from 'vue'", None, None, None), |
| 99 | + ("export * from 'vue'", None, None, None), |
| 100 | + ("import Foo from 'foo'", None, None, None), |
| 101 | + ( |
| 102 | + "import { createApp } from 'vue' |
| 103 | + export { createApp }", |
| 104 | + None, |
| 105 | + None, |
| 106 | + None, |
| 107 | + ), |
| 108 | + ( |
| 109 | + "import { unknown } from '@vue/runtime-dom'", |
| 110 | + None, |
| 111 | + None, |
| 112 | + Some(PathBuf::from("test.d.ts")), |
| 113 | + ), |
| 114 | + ]; |
| 115 | + |
| 116 | + let fail = vec![ |
| 117 | + ("import { createApp } from '@vue/runtime-dom'", None, None, None), |
| 118 | + ("import { computed } from '@vue/runtime-core'", None, None, None), |
| 119 | + ("import { computed } from '@vue/reactivity'", None, None, None), |
| 120 | + ("import { normalizeClass } from '@vue/shared'", None, None, None), |
| 121 | + ("import { unknown } from '@vue/reactivity'", None, None, None), |
| 122 | + ("import { unknown } from '@vue/runtime-dom'", None, None, None), |
| 123 | + ("import * as Foo from '@vue/reactivity'", None, None, None), |
| 124 | + ("import * as Foo from '@vue/runtime-dom'", None, None, None), |
| 125 | + ("export * from '@vue/reactivity'", None, None, None), |
| 126 | + ("export * from '@vue/runtime-dom'", None, None, None), |
| 127 | + ("export { computed } from '@vue/reactivity'", None, None, None), |
| 128 | + ("export { computed } from '@vue/runtime-dom'", None, None, None), |
| 129 | + ("export { unknown } from '@vue/reactivity'", None, None, None), |
| 130 | + ("export { unknown } from '@vue/runtime-dom'", None, None, None), |
| 131 | + ("import unknown from '@vue/reactivity'", None, None, None), |
| 132 | + ("import unknown from '@vue/runtime-dom'", None, None, None), |
| 133 | + ]; |
| 134 | + |
| 135 | + let fix = vec![ |
| 136 | + ("import { createApp } from '@vue/runtime-dom'", "import { createApp } from 'vue'", None), |
| 137 | + ("import { computed } from '@vue/runtime-core'", "import { computed } from 'vue'", None), |
| 138 | + ("import { computed } from '@vue/reactivity'", "import { computed } from 'vue'", None), |
| 139 | + ( |
| 140 | + "import { normalizeClass } from '@vue/shared'", |
| 141 | + "import { normalizeClass } from 'vue'", |
| 142 | + None, |
| 143 | + ), |
| 144 | + ("import { unknown } from '@vue/runtime-dom'", "import { unknown } from 'vue'", None), |
| 145 | + ("import * as Foo from '@vue/runtime-dom'", "import * as Foo from 'vue'", None), |
| 146 | + ("export { computed } from '@vue/reactivity'", "export { computed } from 'vue'", None), |
| 147 | + ("export { computed } from '@vue/runtime-dom'", "export { computed } from 'vue'", None), |
| 148 | + ("import unknown from '@vue/runtime-dom'", "import unknown from 'vue'", None), |
| 149 | + ]; |
| 150 | + Tester::new(PreferImportFromVue::NAME, PreferImportFromVue::PLUGIN, pass, fail) |
| 151 | + .expect_fix(fix) |
| 152 | + .test_and_snapshot(); |
| 153 | +} |
0 commit comments