Skip to content

Commit 5b0d89d

Browse files
committed
fix(linter): Fix the docs for consistent-type-specifier-style rule.
This takes a string config, and the docs were set up as a configuration object. Also simplify/refactor the code a bit.
1 parent a63bfb7 commit 5b0d89d

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

crates/oxc_linter/src/rules/import/consistent_type_specifier_style.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ use schemars::JsonSchema;
1111
use serde::{Deserialize, Serialize};
1212
use serde_json::Value;
1313

14-
use crate::{AstNode, context::LintContext, fixer::RuleFixer, rule::Rule};
14+
use crate::{
15+
AstNode,
16+
context::LintContext,
17+
fixer::RuleFixer,
18+
rule::{DefaultRuleConfig, Rule},
19+
};
1520

1621
fn consistent_type_specifier_style_diagnostic(span: Span, mode: &Mode) -> OxcDiagnostic {
1722
let (warn_msg, help_msg) = if *mode == Mode::PreferInline {
@@ -31,25 +36,15 @@ fn consistent_type_specifier_style_diagnostic(span: Span, mode: &Mode) -> OxcDia
3136
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
3237
#[serde(rename_all = "kebab-case")]
3338
enum Mode {
39+
/// Prefer `import type { Foo } from 'foo'` for type imports.
3440
#[default]
3541
PreferTopLevel,
42+
/// Prefer `import { type Foo } from 'foo'` for type imports.
3643
PreferInline,
3744
}
3845

39-
impl Mode {
40-
pub fn from(raw: &str) -> Self {
41-
if raw == "prefer-inline" { Self::PreferInline } else { Self::PreferTopLevel }
42-
}
43-
}
44-
45-
#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]
46-
#[serde(rename_all = "camelCase", default)]
47-
pub struct ConsistentTypeSpecifierStyle {
48-
/// Specify whether to prefer top-level type-only imports or inline type specifiers.
49-
/// - `"prefer-top-level"`: `import type { Foo } from 'foo'`
50-
/// - `"prefer-inline"`: `import { type Foo } from 'foo'`
51-
mode: Mode,
52-
}
46+
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
47+
pub struct ConsistentTypeSpecifierStyle(Mode);
5348

5449
declare_oxc_lint!(
5550
/// ### What it does
@@ -91,12 +86,14 @@ declare_oxc_lint!(
9186
import,
9287
style,
9388
conditional_fix,
94-
config = ConsistentTypeSpecifierStyle,
89+
config = Mode,
9590
);
9691

9792
impl Rule for ConsistentTypeSpecifierStyle {
9893
fn from_configuration(value: Value) -> Self {
99-
Self { mode: value.get(0).and_then(Value::as_str).map(Mode::from).unwrap_or_default() }
94+
serde_json::from_value::<DefaultRuleConfig<ConsistentTypeSpecifierStyle>>(value)
95+
.unwrap_or_default()
96+
.into_inner()
10097
}
10198
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
10299
let AstKind::ImportDeclaration(import_decl) = node.kind() else {
@@ -112,15 +109,15 @@ impl Rule for ConsistentTypeSpecifierStyle {
112109
{
113110
return;
114111
}
115-
if self.mode == Mode::PreferTopLevel && import_decl.import_kind.is_value() {
112+
if self.0 == Mode::PreferTopLevel && import_decl.import_kind.is_value() {
116113
let (value_specifiers, type_specifiers) = split_import_specifiers_by_kind(specifiers);
117114
if type_specifiers.is_empty() {
118115
return;
119116
}
120117

121118
for item in &type_specifiers {
122119
ctx.diagnostic_with_fix(
123-
consistent_type_specifier_style_diagnostic(item.span(), &self.mode),
120+
consistent_type_specifier_style_diagnostic(item.span(), &self.0),
124121
|fixer| {
125122
let mut import_source = String::new();
126123

@@ -141,9 +138,9 @@ impl Rule for ConsistentTypeSpecifierStyle {
141138
);
142139
}
143140
}
144-
if self.mode == Mode::PreferInline && import_decl.import_kind.is_type() {
141+
if self.0 == Mode::PreferInline && import_decl.import_kind.is_type() {
145142
ctx.diagnostic_with_fix(
146-
consistent_type_specifier_style_diagnostic(import_decl.span, &self.mode),
143+
consistent_type_specifier_style_diagnostic(import_decl.span, &self.0),
147144
|fixer| {
148145
let fixer = fixer.for_multifix();
149146
let mut rule_fixes = fixer.new_fix_with_capacity(len);

0 commit comments

Comments
 (0)