@@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
1313use crate :: {
1414 AstNode ,
1515 context:: { ContextHost , LintContext } ,
16- rule:: Rule ,
16+ rule:: { DefaultRuleConfig , Rule } ,
1717} ;
1818
1919fn consistent_type_definitions_diagnostic (
@@ -28,33 +28,26 @@ fn consistent_type_definitions_diagnostic(
2828 OxcDiagnostic :: warn ( message) . with_label ( span)
2929}
3030
31- #[ derive( Debug , Default , Clone , JsonSchema , Deserialize , Serialize ) ]
32- #[ serde( rename_all = "camelCase" , default ) ]
33- pub struct ConsistentTypeDefinitions {
34- /// Configuration option to enforce either `interface` or `type` for object type definitions.
35- ///
36- /// Setting to `type` enforces the use of types for object type definitions.
37- ///
38- /// Examples of **incorrect** code for this option:
31+ #[ derive( Debug , Default , Clone , Deserialize , Serialize ) ]
32+ pub struct ConsistentTypeDefinitions ( ConsistentTypeDefinitionsConfig ) ;
33+
34+ #[ derive( Debug , Default , Clone , Copy , Eq , PartialEq , JsonSchema , Deserialize , Serialize ) ]
35+ #[ serde( rename_all = "kebab-case" ) ]
36+ enum ConsistentTypeDefinitionsConfig {
37+ /// Prefer `interface` over `type` for object type definitions:
3938 ///
4039 /// ```typescript
4140 /// interface T {
4241 /// x: number;
4342 /// }
4443 /// ```
44+ #[ default]
45+ Interface ,
46+ /// Prefer `type` over `interface` for object type definitions:
4547 ///
46- /// Examples of **correct** code for this option:
4748 /// ```typescript
4849 /// type T = { x: number };
4950 /// ```
50- config : ConsistentTypeDefinitionsConfig ,
51- }
52-
53- #[ derive( Debug , Default , Clone , Copy , Eq , PartialEq , JsonSchema , Deserialize , Serialize ) ]
54- #[ serde( rename_all = "kebab-case" ) ]
55- enum ConsistentTypeDefinitionsConfig {
56- #[ default]
57- Interface ,
5851 Type ,
5952}
6053
@@ -71,7 +64,7 @@ declare_oxc_lint!(
7164 ///
7265 /// ### Examples
7366 ///
74- /// By default this rule enforces the use of interfaces for object types.
67+ /// By default this rule enforces the use of `interface` for defining object types.
7568 ///
7669 /// Examples of **incorrect** code for this rule:
7770 /// ```typescript
@@ -91,26 +84,21 @@ declare_oxc_lint!(
9184 typescript,
9285 style,
9386 fix,
94- config = ConsistentTypeDefinitions ,
87+ config = ConsistentTypeDefinitionsConfig ,
9588) ;
9689
9790impl Rule for ConsistentTypeDefinitions {
9891 fn from_configuration ( value : serde_json:: Value ) -> Self {
99- let config = value. get ( 0 ) . and_then ( serde_json:: Value :: as_str) . map_or_else (
100- ConsistentTypeDefinitionsConfig :: default,
101- |value| match value {
102- "type" => ConsistentTypeDefinitionsConfig :: Type ,
103- _ => ConsistentTypeDefinitionsConfig :: Interface ,
104- } ,
105- ) ;
106- Self { config }
92+ serde_json:: from_value :: < DefaultRuleConfig < ConsistentTypeDefinitions > > ( value)
93+ . unwrap_or_default ( )
94+ . into_inner ( )
10795 }
10896
10997 fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
11098 match node. kind ( ) {
11199 AstKind :: TSTypeAliasDeclaration ( decl) => match & decl. type_annotation {
112100 TSType :: TSTypeLiteral ( _)
113- if self . config == ConsistentTypeDefinitionsConfig :: Interface =>
101+ if self . 0 == ConsistentTypeDefinitionsConfig :: Interface =>
114102 {
115103 let start = if decl. declare {
116104 let base_start = decl. span . start + 7 ;
@@ -155,7 +143,7 @@ impl Rule for ConsistentTypeDefinitions {
155143
156144 AstKind :: ExportDefaultDeclaration ( exp) => match & exp. declaration {
157145 ExportDefaultDeclarationKind :: TSInterfaceDeclaration ( decl)
158- if self . config == ConsistentTypeDefinitionsConfig :: Type =>
146+ if self . 0 == ConsistentTypeDefinitionsConfig :: Type =>
159147 {
160148 let name_span_start = & decl. id . span . start ;
161149 let mut name_span_end = & decl. id . span . end ;
@@ -192,7 +180,7 @@ impl Rule for ConsistentTypeDefinitions {
192180 } ,
193181
194182 AstKind :: TSInterfaceDeclaration ( decl)
195- if self . config == ConsistentTypeDefinitionsConfig :: Type =>
183+ if self . 0 == ConsistentTypeDefinitionsConfig :: Type =>
196184 {
197185 let start = if decl. declare {
198186 let base_start = decl. span . start + 7 ;
0 commit comments