1
1
use oxc_ast:: {
2
2
AstKind ,
3
- ast:: { Argument , Expression } ,
3
+ ast:: { Argument , CallExpression , Expression } ,
4
4
} ;
5
5
use oxc_diagnostics:: OxcDiagnostic ;
6
6
use oxc_macros:: declare_oxc_lint;
@@ -43,6 +43,9 @@ declare_oxc_lint!(
43
43
fix
44
44
) ;
45
45
46
+ // skip React.Children because we are only looking at `StaticMemberExpression.property` and not its object
47
+ const IGNORE_OBJECTS : [ & str ; 1 ] = [ /* "React.Children", */ "Children" ] ;
48
+
46
49
impl Rule for PreferArrayFlatMap {
47
50
fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
48
51
let AstKind :: CallExpression ( flat_call_expr) = node. kind ( ) else {
@@ -67,6 +70,11 @@ impl Rule for PreferArrayFlatMap {
67
70
return ;
68
71
}
69
72
73
+ // Check for Call Expressions which should be ignored
74
+ if is_ignored_call_expression ( call_expr) {
75
+ return ;
76
+ }
77
+
70
78
if let Some ( first_arg) = flat_call_expr. arguments . first ( ) {
71
79
// `Array.prototype.flat` rounds down the argument.
72
80
// So `.flat(1.5)` is equivalent to `.flat(1)`.
@@ -96,6 +104,20 @@ impl Rule for PreferArrayFlatMap {
96
104
}
97
105
}
98
106
107
+ /// Returns true if the object of the method call is `Children` or `React.Children`.
108
+ fn is_ignored_call_expression ( call_expr : & CallExpression ) -> bool {
109
+ let Some ( member_expr) = call_expr. callee . get_member_expr ( ) else {
110
+ return false ;
111
+ } ;
112
+ match member_expr. object ( ) . get_inner_expression ( ) {
113
+ Expression :: Identifier ( ident) => IGNORE_OBJECTS . contains ( & ident. name . as_str ( ) ) ,
114
+ Expression :: StaticMemberExpression ( mem) => {
115
+ IGNORE_OBJECTS . contains ( & mem. property . name . as_str ( ) )
116
+ }
117
+ _ => false ,
118
+ }
119
+ }
120
+
99
121
#[ test]
100
122
fn test ( ) {
101
123
use crate :: tester:: Tester ;
@@ -125,6 +147,9 @@ fn test() {
125
147
( "const bar = [[1],[2],[3]].map(i => [i]).flat(+1)" , None ) ,
126
148
( "const bar = [[1],[2],[3]].map(i => [i]).flat(foo)" , None ) ,
127
149
( "const bar = [[1],[2],[3]].map(i => [i]).flat(foo.bar)" , None ) ,
150
+ // Allowed
151
+ ( "Children.map(children, fn).flat()" , None ) , // `import {Children} from 'react';`
152
+ ( "React.Children.map(children, fn).flat()" , None ) ,
128
153
] ;
129
154
130
155
let fail = vec ! [
0 commit comments