|
| 1 | +use oxc_ast::{AstKind, ast::ImportDeclarationSpecifier}; |
| 2 | +use oxc_diagnostics::OxcDiagnostic; |
| 3 | +use oxc_macros::declare_oxc_lint; |
| 4 | +use oxc_span::{GetSpan, Span}; |
| 5 | +use serde_json::Value; |
| 6 | + |
| 7 | +use crate::{AstNode, context::LintContext, rule::Rule}; |
| 8 | + |
| 9 | +fn consistent_type_specifier_style_diagnostic(span: Span, mode: &Mode) -> OxcDiagnostic { |
| 10 | + let (warn_msg, help_msg) = if *mode == Mode::PreferInline { |
| 11 | + ( |
| 12 | + "Prefer using inline type specifiers instead of a top-level type-only import.", |
| 13 | + "Replace top‐level import type with an inline type specifier.", |
| 14 | + ) |
| 15 | + } else { |
| 16 | + ( |
| 17 | + "Prefer using a top-level type-only import instead of inline type specifiers.", |
| 18 | + "Replace inline type specifiers with a top‐level import type statement.", |
| 19 | + ) |
| 20 | + }; |
| 21 | + OxcDiagnostic::warn(warn_msg).with_help(help_msg).with_label(span) |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Default, PartialEq, Clone)] |
| 25 | +enum Mode { |
| 26 | + #[default] |
| 27 | + PreferTopLevel, |
| 28 | + PreferInline, |
| 29 | +} |
| 30 | + |
| 31 | +impl Mode { |
| 32 | + pub fn from(raw: &str) -> Self { |
| 33 | + if raw == "prefer-inline" { Self::PreferInline } else { Self::PreferTopLevel } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Default, Clone)] |
| 38 | +pub struct ConsistentTypeSpecifierStyle { |
| 39 | + mode: Mode, |
| 40 | +} |
| 41 | + |
| 42 | +declare_oxc_lint!( |
| 43 | + /// ### What it does |
| 44 | + /// |
| 45 | + /// This rule either enforces or bans the use of inline type-only markers for named imports. |
| 46 | + /// |
| 47 | + /// ### Why is this bad? |
| 48 | + /// |
| 49 | + /// Mixing top-level `import type { Foo } from 'foo'` with inline `{ type Bar }` |
| 50 | + /// forces readers to mentally switch contexts when scanning your imports. |
| 51 | + /// Enforcing one style makes it immediately obvious which imports are types and which are value imports. |
| 52 | + /// |
| 53 | + /// ### Examples |
| 54 | + /// |
| 55 | + /// Examples of incorrect code for the default `prefer-top-level` option: |
| 56 | + /// ```typescript |
| 57 | + /// import {type Foo} from 'Foo'; |
| 58 | + /// import Foo, {type Bar} from 'Foo'; |
| 59 | + /// ``` |
| 60 | + /// |
| 61 | + /// Examples of correct code for the default option: |
| 62 | + /// ```typescript |
| 63 | + /// import type {Foo} from 'Foo'; |
| 64 | + /// import type Foo, {Bar} from 'Foo'; |
| 65 | + /// ``` |
| 66 | + /// |
| 67 | + /// Examples of incorrect code for the `prefer-inline` option: |
| 68 | + /// ```typescript |
| 69 | + /// import type {Foo} from 'Foo'; |
| 70 | + /// import type Foo, {Bar} from 'Foo'; |
| 71 | + /// ``` |
| 72 | + /// |
| 73 | + /// Examples of correct code for the `prefer-inline` option: |
| 74 | + /// ```typescript |
| 75 | + /// import {type Foo} from 'Foo'; |
| 76 | + /// import Foo, {type Bar} from 'Foo'; |
| 77 | + /// ``` |
| 78 | + ConsistentTypeSpecifierStyle, |
| 79 | + import, |
| 80 | + style, |
| 81 | + conditional_fix |
| 82 | +); |
| 83 | + |
| 84 | +impl Rule for ConsistentTypeSpecifierStyle { |
| 85 | + fn from_configuration(value: Value) -> Self { |
| 86 | + Self { mode: value.get(0).and_then(Value::as_str).map(Mode::from).unwrap_or_default() } |
| 87 | + } |
| 88 | + #[expect(clippy::cast_possible_truncation)] |
| 89 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 90 | + let AstKind::ImportDeclaration(import_decl) = node.kind() else { |
| 91 | + return; |
| 92 | + }; |
| 93 | + let Some(specifiers) = &import_decl.specifiers else { |
| 94 | + return; |
| 95 | + }; |
| 96 | + let len = specifiers.len(); |
| 97 | + if len == 0 |
| 98 | + || (len == 1 |
| 99 | + && !matches!(specifiers[0], ImportDeclarationSpecifier::ImportSpecifier(_))) |
| 100 | + { |
| 101 | + return; |
| 102 | + } |
| 103 | + if self.mode == Mode::PreferTopLevel && import_decl.import_kind.is_value() { |
| 104 | + for item in specifiers { |
| 105 | + if matches!(item, ImportDeclarationSpecifier::ImportSpecifier(specifier) if specifier.import_kind.is_type()) |
| 106 | + { |
| 107 | + ctx.diagnostic(consistent_type_specifier_style_diagnostic( |
| 108 | + item.span(), |
| 109 | + &self.mode, |
| 110 | + )); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + if self.mode == Mode::PreferInline && import_decl.import_kind.is_type() { |
| 115 | + ctx.diagnostic_with_fix( |
| 116 | + consistent_type_specifier_style_diagnostic(import_decl.span, &self.mode), |
| 117 | + |fixer| { |
| 118 | + let fixer = fixer.for_multifix(); |
| 119 | + let mut rule_fixes = fixer.new_fix_with_capacity(len); |
| 120 | + for item in specifiers { |
| 121 | + rule_fixes.push(fixer.insert_text_before(item, "type ")); |
| 122 | + } |
| 123 | + // find the 'type' keyword and remove it |
| 124 | + if let Some(type_token_span) = ctx |
| 125 | + .source_range(Span::new(import_decl.span.start, specifiers[0].span().start)) |
| 126 | + .find("type") |
| 127 | + .map(|pos| { |
| 128 | + let start = import_decl.span.start + pos as u32; |
| 129 | + Span::new(start, start + 4) |
| 130 | + }) |
| 131 | + { |
| 132 | + let remove_fix = fixer.delete_range(type_token_span); |
| 133 | + rule_fixes.push(remove_fix); |
| 134 | + } |
| 135 | + rule_fixes.with_message("Convert to an `inline` type import") |
| 136 | + }, |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test() { |
| 144 | + use crate::tester::Tester; |
| 145 | + use serde_json::json; |
| 146 | + |
| 147 | + let pass = vec![ |
| 148 | + ("import Foo from 'Foo'", None), |
| 149 | + ("import type Foo from 'Foo'", None), |
| 150 | + ("import { Foo } from 'Foo';", None), |
| 151 | + ("import { Foo as Bar } from 'Foo';", None), |
| 152 | + ("import * as Foo from 'Foo';", None), |
| 153 | + ("import 'Foo';", None), |
| 154 | + ("import {} from 'Foo';", None), |
| 155 | + ("import type {} from 'Foo';", None), |
| 156 | + ("import type { Foo as Bar } from 'Foo';", Some(json!(["prefer-top-level"]))), |
| 157 | + ("import type { Foo, Bar, Baz, Bam } from 'Foo';", Some(json!(["prefer-top-level"]))), |
| 158 | + ("import type {Foo} from 'Foo'", Some(json!(["prefer-top-level"]))), |
| 159 | + ("import {type Foo} from 'Foo'", Some(json!(["prefer-inline"]))), |
| 160 | + ("import Foo from 'Foo';", Some(json!(["prefer-inline"]))), |
| 161 | + ("import type Foo from 'Foo';", Some(json!(["prefer-inline"]))), |
| 162 | + ("import { Foo } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 163 | + ("import { Foo as Bar } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 164 | + ("import * as Foo from 'Foo';", Some(json!(["prefer-inline"]))), |
| 165 | + ("import 'Foo';", Some(json!(["prefer-inline"]))), |
| 166 | + ("import {} from 'Foo';", Some(json!(["prefer-inline"]))), |
| 167 | + ("import type {} from 'Foo';", Some(json!(["prefer-inline"]))), |
| 168 | + ("import { type Foo } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 169 | + ("import { type Foo as Bar } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 170 | + ("import { type Foo, type Bar, Baz, Bam } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 171 | + ("import type * as Foo from 'Foo';", None), |
| 172 | + ]; |
| 173 | + |
| 174 | + let fail = vec![ |
| 175 | + ("import { type Foo, type Bar } from 'Foo'", None), |
| 176 | + ("import type { Foo } from 'Foo'", Some(json!(["prefer-inline"]))), |
| 177 | + ("import { type Foo as Bar } from 'Foo';", None), |
| 178 | + ("import { type Foo, type Bar } from 'Foo';", None), |
| 179 | + ("import { Foo, type Bar } from 'Foo';", None), |
| 180 | + ("import { type Foo, Bar } from 'Foo';", None), |
| 181 | + ("import Foo, { type Bar } from 'Foo';", None), |
| 182 | + ("import Foo, { type Bar, Baz } from 'Foo';", None), |
| 183 | + ("import { Component, type ComponentProps } from 'package-1';", None), |
| 184 | + ("import type { Foo, Bar, Baz } from 'Foo';", Some(json!(["prefer-inline"]))), |
| 185 | + ]; |
| 186 | + |
| 187 | + let fix = vec![ |
| 188 | + ( |
| 189 | + "import type { foo, bar } from 'foo'", |
| 190 | + "import { type foo, type bar } from 'foo'", |
| 191 | + Some(json!(["prefer-inline"])), |
| 192 | + ), |
| 193 | + ( |
| 194 | + "import type{ foo } from 'foo'", |
| 195 | + "import { type foo } from 'foo'", |
| 196 | + Some(json!(["prefer-inline"])), |
| 197 | + ), |
| 198 | + ( |
| 199 | + "import type /** comment */{ foo } from 'foo'", |
| 200 | + "import /** comment */{ type foo } from 'foo'", |
| 201 | + Some(json!(["prefer-inline"])), |
| 202 | + ), |
| 203 | + ( |
| 204 | + "import type { foo, /** comments */ bar } from 'foo'", |
| 205 | + "import { type foo, /** comments */ type bar } from 'foo'", |
| 206 | + Some(json!(["prefer-inline"])), |
| 207 | + ), |
| 208 | + ( |
| 209 | + r" |
| 210 | + import type { |
| 211 | + bar, |
| 212 | + } from 'foo' |
| 213 | + ", |
| 214 | + r" |
| 215 | + import { |
| 216 | + type bar, |
| 217 | + } from 'foo' |
| 218 | + ", |
| 219 | + Some(json!(["prefer-inline"])), |
| 220 | + ), |
| 221 | + ]; |
| 222 | + |
| 223 | + Tester::new( |
| 224 | + ConsistentTypeSpecifierStyle::NAME, |
| 225 | + ConsistentTypeSpecifierStyle::PLUGIN, |
| 226 | + pass, |
| 227 | + fail, |
| 228 | + ) |
| 229 | + .expect_fix(fix) |
| 230 | + .test_and_snapshot(); |
| 231 | +} |
0 commit comments