@@ -36,9 +36,7 @@ impl CurlyType {
3636}
3737
3838#[ derive( Debug , Default , Clone ) ]
39- pub struct Curly {
40- pub config : CurlyConfig ,
41- }
39+ pub struct Curly ( CurlyConfig ) ;
4240
4341#[ derive( Debug , Clone ) ]
4442pub struct CurlyConfig {
@@ -277,14 +275,38 @@ declare_oxc_lint!(
277275 fix
278276) ;
279277
278+ impl Rule for Curly {
279+ fn from_configuration ( value : Value ) -> Self {
280+ let curly_type =
281+ value. get ( 0 ) . and_then ( Value :: as_str) . map ( CurlyType :: from) . unwrap_or_default ( ) ;
282+
283+ let consistent =
284+ value. get ( 1 ) . and_then ( Value :: as_str) . is_some_and ( |value| value == "consistent" ) ;
285+
286+ Self ( CurlyConfig { curly_type, consistent } )
287+ }
288+
289+ fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
290+ match node. kind ( ) {
291+ AstKind :: IfStatement ( stmt) => self . run_for_if_statement ( stmt, ctx) ,
292+ AstKind :: ForStatement ( stmt) => self . run_for_loop ( "for" , & stmt. body , ctx) ,
293+ AstKind :: ForInStatement ( stmt) => self . run_for_loop ( "for-in" , & stmt. body , ctx) ,
294+ AstKind :: ForOfStatement ( stmt) => self . run_for_loop ( "for-of" , & stmt. body , ctx) ,
295+ AstKind :: WhileStatement ( stmt) => self . run_for_loop ( "while" , & stmt. body , ctx) ,
296+ AstKind :: DoWhileStatement ( stmt) => self . run_for_loop ( "do" , & stmt. body , ctx) ,
297+ _ => { }
298+ }
299+ }
300+ }
301+
280302impl < ' a > Curly {
281303 fn run_for_if_statement ( & self , stmt : & ' a IfStatement < ' a > , ctx : & LintContext < ' a > ) {
282- let branches = get_if_branches_from_statement ( stmt, & self . config . curly_type , ctx) ;
304+ let branches = get_if_branches_from_statement ( stmt, & self . 0 . curly_type , ctx) ;
283305 let does_any_branch_need_braces =
284306 branches. iter ( ) . any ( |b| b. should_have_braces . unwrap_or ( b. has_braces ) ) ;
285307
286308 for branch in & branches {
287- let should_have_braces = if self . config . consistent {
309+ let should_have_braces = if self . 0 . consistent {
288310 Some ( does_any_branch_need_braces)
289311 } else {
290312 branch. should_have_braces
@@ -302,35 +324,11 @@ impl<'a> Curly {
302324
303325 fn run_for_loop ( & self , keyword : & str , body : & Statement < ' a > , ctx : & LintContext < ' a > ) {
304326 let has_braces = has_braces ( body) ;
305- let should_have_braces = should_have_braces ( & self . config . curly_type , body, ctx) ;
327+ let should_have_braces = should_have_braces ( & self . 0 . curly_type , body, ctx) ;
306328 report_if_needed ( ctx, body, keyword, has_braces, should_have_braces) ;
307329 }
308330}
309331
310- impl Rule for Curly {
311- fn from_configuration ( value : Value ) -> Self {
312- let curly_type =
313- value. get ( 0 ) . and_then ( Value :: as_str) . map ( CurlyType :: from) . unwrap_or_default ( ) ;
314-
315- let consistent =
316- value. get ( 1 ) . and_then ( Value :: as_str) . is_some_and ( |value| value == "consistent" ) ;
317-
318- Self { config : CurlyConfig { curly_type, consistent } }
319- }
320-
321- fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
322- match node. kind ( ) {
323- AstKind :: IfStatement ( stmt) => self . run_for_if_statement ( stmt, ctx) ,
324- AstKind :: ForStatement ( stmt) => self . run_for_loop ( "for" , & stmt. body , ctx) ,
325- AstKind :: ForInStatement ( stmt) => self . run_for_loop ( "for-in" , & stmt. body , ctx) ,
326- AstKind :: ForOfStatement ( stmt) => self . run_for_loop ( "for-of" , & stmt. body , ctx) ,
327- AstKind :: WhileStatement ( stmt) => self . run_for_loop ( "while" , & stmt. body , ctx) ,
328- AstKind :: DoWhileStatement ( stmt) => self . run_for_loop ( "do" , & stmt. body , ctx) ,
329- _ => { }
330- }
331- }
332- }
333-
334332fn get_if_branches_from_statement < ' a > (
335333 stmt : & ' a IfStatement < ' a > ,
336334 curly_type : & CurlyType ,
@@ -389,10 +387,10 @@ fn should_have_braces<'a>(
389387) -> Option < bool > {
390388 let braces_necessary = are_braces_necessary ( body, ctx) ;
391389
392- if let Statement :: BlockStatement ( block) = body {
393- if block. body . len ( ) != 1 || braces_necessary {
394- return Some ( true ) ;
395- }
390+ if let Statement :: BlockStatement ( block) = body
391+ && ( block. body . len ( ) != 1 || braces_necessary)
392+ {
393+ return Some ( true ) ;
396394 }
397395
398396 match curly_type {
0 commit comments