11import * as AST from "@eslint-react/ast" ;
22import { isForwardRefCall } from "@eslint-react/core" ;
33import { decodeSettings , normalizeSettings } from "@eslint-react/shared" ;
4+ import { O } from "@eslint-react/tools" ;
45import type { RuleContext } from "@eslint-react/types" ;
56import type { TSESTree } from "@typescript-eslint/types" ;
67import { AST_NODE_TYPES } from "@typescript-eslint/types" ;
78import type { RuleFix , RuleFixer } from "@typescript-eslint/utils/ts-eslint" ;
89import { compare } from "compare-versions" ;
910import type { CamelCase } from "string-ts" ;
11+ import { match } from "ts-pattern" ;
1012
1113import { createRule } from "../utils" ;
1214
@@ -68,16 +70,23 @@ function getComponentPropsFixes(
6870 fixer : RuleFixer ,
6971 context : RuleContext ,
7072) : RuleFix [ ] {
73+ const getText = ( node : TSESTree . Node ) => context . sourceCode . getText ( node ) ;
7174 const [ arg0 , arg1 ] = node . params ;
7275 const [ typeArg0 , typeArg1 ] = typeArguments ;
73- if ( arg0 ?. type !== AST_NODE_TYPES . Identifier ) return [ ] ;
76+ if ( ! arg0 ) return [ ] ;
77+ const fixedArg0Text = match ( arg0 )
78+ . with ( { type : AST_NODE_TYPES . Identifier } , ( n ) => O . some ( `...${ n . name } ` ) )
79+ . with ( { type : AST_NODE_TYPES . ObjectPattern } , ( n ) => O . some ( n . properties . map ( getText ) . join ( ", " ) ) )
80+ . otherwise ( O . none ) ;
81+ if ( O . isNone ( fixedArg0Text ) ) return [ ] ;
82+ const fixedPropsText = fixedArg0Text . value ;
7483 if ( ! arg1 ) {
7584 return [ fixer . replaceText (
7685 arg0 ,
7786 [
7887 "{" ,
7988 "ref," ,
80- `... ${ arg0 . name } ` ,
89+ fixedPropsText ,
8190 "}" ,
8291 ] . join ( " " ) ,
8392 ) ] as const ;
@@ -92,15 +101,14 @@ function getComponentPropsFixes(
92101 arg1 . name === "ref"
93102 ? `ref,`
94103 : `ref: ${ arg1 . name } ,` ,
95- `... ${ arg0 . name } ` ,
104+ fixedPropsText ,
96105 "}" ,
97106 ] . join ( " " ) ,
98107 ) ,
99108 fixer . remove ( arg1 ) ,
100109 fixer . removeRange ( [ arg0 . range [ 1 ] , arg1 . range [ 0 ] ] ) ,
101110 ] as const ;
102111 }
103- const getText = ( node : TSESTree . Node ) => context . sourceCode . getText ( node ) ;
104112 return [
105113 fixer . replaceText (
106114 arg0 ,
@@ -109,7 +117,7 @@ function getComponentPropsFixes(
109117 arg1 . name === "ref"
110118 ? `ref,`
111119 : `ref: ${ arg1 . name } ,` ,
112- `... ${ arg0 . name } ` ,
120+ fixedPropsText ,
113121 "}:" ,
114122 getText ( typeArg1 ) ,
115123 "&" ,
0 commit comments