Skip to content

Commit 6a3094c

Browse files
committed
fix: feedback
1 parent 7efaf59 commit 6a3094c

File tree

5 files changed

+166
-30
lines changed

5 files changed

+166
-30
lines changed

crates/biome_js_analyze/src/lint/nursery/no_duplicated_spread_props.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ declare_node_union! {
4545
| JsxSelfClosingElement
4646
}
4747

48-
fn validate_attributes(list: &JsxAttributeList) -> Option<String> {
49-
let mut seen_spreads = HashSet::new();
50-
51-
for attribute in list {
52-
if let AnyJsxAttribute::JsxSpreadAttribute(spread) = attribute
53-
&& let Some(argument) = spread.argument().ok()
54-
&& let Some(express) = argument.as_js_identifier_expression()
55-
{
56-
let name = express.name().ok()?;
57-
let value_token = name.value_token().ok()?;
58-
let text = value_token.text_trimmed().to_string();
59-
if !seen_spreads.insert(text.clone()) {
60-
return Some(text);
61-
}
62-
}
63-
}
64-
65-
None
66-
}
67-
6848
impl Rule for NoDuplicatedSpreadProps {
6949
type Query = Ast<NoDuplicatedSpreadPropsQuery>;
7050
type State = String;
@@ -102,3 +82,23 @@ impl Rule for NoDuplicatedSpreadProps {
10282
)
10383
}
10484
}
85+
86+
fn validate_attributes(list: &JsxAttributeList) -> Option<String> {
87+
let mut seen_spreads = HashSet::new();
88+
89+
for attribute in list {
90+
if let AnyJsxAttribute::JsxSpreadAttribute(spread) = attribute
91+
&& let Some(argument) = spread.argument().ok()
92+
&& let Some(express) = argument.as_js_identifier_expression()
93+
&& let Some(name) = express.name().ok()
94+
&& let Some(value_token) = name.value_token().ok()
95+
{
96+
let text = value_token.text_trimmed().to_string();
97+
if !seen_spreads.insert(text.clone()) {
98+
return Some(text);
99+
}
100+
}
101+
}
102+
103+
None
104+
}
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
const Invalid1 = () => {
2-
return <div {...props} something="else" {...props} />
2+
return <div {...props} something="else" {...props}></div>
33
}
44

55
const Invalid2 = () => {
6+
return <div {...foo.bar} {...props} {...props}></div>
7+
}
8+
9+
const Invalid3 = () => {
10+
return <div {...{}} {...props} {...props}></div>
11+
}
12+
13+
const Invalid4 = () => {
14+
return <div {...props} something="else" {...props} />
15+
}
16+
17+
const Invalid5 = () => {
618
return <div {...foo.bar} {...props} {...props} />
719
}
20+
21+
const Invalid6 = () => {
22+
return <div {...{}} {...props} {...props} />
23+
}

crates/biome_js_analyze/tests/specs/nursery/noDuplicatedSpreadProps/invalid.jsx.snap

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ expression: invalid.jsx
55
# Input
66
```jsx
77
const Invalid1 = () => {
8-
return <div {...props} something="else" {...props} />
8+
return <div {...props} something="else" {...props}></div>
99
}
1010
1111
const Invalid2 = () => {
12+
return <div {...foo.bar} {...props} {...props}></div>
13+
}
14+
15+
const Invalid3 = () => {
16+
return <div {...{}} {...props} {...props}></div>
17+
}
18+
19+
const Invalid4 = () => {
20+
return <div {...props} something="else" {...props} />
21+
}
22+
23+
const Invalid5 = () => {
1224
return <div {...foo.bar} {...props} {...props} />
1325
}
1426
27+
const Invalid6 = () => {
28+
return <div {...{}} {...props} {...props} />
29+
}
30+
1531
```
1632

1733
# Diagnostics
@@ -21,8 +37,8 @@ invalid.jsx:2:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━
2137
i The expression props has spread more than once.
2238
2339
1 │ const Invalid1 = () => {
24-
> 2return <div {...props} something="else" {...props} />
25-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
> 2return <div {...props} something="else" {...props}></div>
41+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2642
3}
2743
4 │
2844
@@ -37,12 +53,76 @@ invalid.jsx:6:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━
3753
i The expression props has spread more than once.
3854
3955
5 │ const Invalid2 = () => {
40-
> 6return <div {...foo.bar} {...props} {...props} />
41-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
> 6return <div {...foo.bar} {...props} {...props}></div>
57+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4258
7}
4359
8 │
4460
4561
i Spreading an expression more than once will lead to unnecessary computations being performed. Reduce spreads of this expression down to 1.
4662
4763
4864
```
65+
66+
```
67+
invalid.jsx:10:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
68+
69+
i The expression props has spread more than once.
70+
71+
9 │ const Invalid3 = () => {
72+
> 10return <div {...{}} {...props} {...props}></div>
73+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
74+
11}
75+
12 │
76+
77+
i Spreading an expression more than once will lead to unnecessary computations being performed. Reduce spreads of this expression down to 1.
78+
79+
80+
```
81+
82+
```
83+
invalid.jsx:14:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
84+
85+
i The expression props has spread more than once.
86+
87+
13 │ const Invalid4 = () => {
88+
> 14return <div {...props} something="else" {...props} />
89+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
15}
91+
16 │
92+
93+
i Spreading an expression more than once will lead to unnecessary computations being performed. Reduce spreads of this expression down to 1.
94+
95+
96+
```
97+
98+
```
99+
invalid.jsx:18:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
100+
101+
i The expression props has spread more than once.
102+
103+
17 │ const Invalid5 = () => {
104+
> 18return <div {...foo.bar} {...props} {...props} />
105+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106+
19}
107+
20 │
108+
109+
i Spreading an expression more than once will lead to unnecessary computations being performed. Reduce spreads of this expression down to 1.
110+
111+
112+
```
113+
114+
```
115+
invalid.jsx:22:9 lint/nursery/noDuplicatedSpreadProps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
116+
117+
i The expression props has spread more than once.
118+
119+
21 │ const Invalid6 = () => {
120+
> 22return <div {...{}} {...props} {...props} />
121+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122+
23}
123+
24 │
124+
125+
i Spreading an expression more than once will lead to unnecessary computations being performed. Reduce spreads of this expression down to 1.
126+
127+
128+
```
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
/* should not generate diagnostics */
22
const Valid1 = () => {
3-
return <div {...props} something="else" />
3+
return <div {...props} something="else"></div>
44
}
55

66
const Valid2 = () => {
7-
return <div something="else" {...props} />
7+
return <div something="else" {...props}></div>
88
}
99

1010
const Valid3 = () => {
11+
return <div {...props} something="else" {...otherProps}></div>
12+
}
13+
14+
const Valid4 = () => {
15+
return <div {...{}} something="else" {...otherProps}></div>
16+
}
17+
18+
const Valid5 = () => {
19+
return <div {...props} something="else" />
20+
}
21+
22+
const Valid6 = () => {
23+
return <div something="else" {...props} />
24+
}
25+
26+
const Valid7 = () => {
1127
return <div {...props} something="else" {...otherProps} />
1228
}
29+
30+
const Valid8 = () => {
31+
return <div {...{}} something="else" {...otherProps} />
32+
}

crates/biome_js_analyze/tests/specs/nursery/noDuplicatedSpreadProps/valid.jsx.snap

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,35 @@ expression: valid.jsx
66
```jsx
77
/* should not generate diagnostics */
88
const Valid1 = () => {
9-
return <div {...props} something="else" />
9+
return <div {...props} something="else"></div>
1010
}
1111
1212
const Valid2 = () => {
13-
return <div something="else" {...props} />
13+
return <div something="else" {...props}></div>
1414
}
1515
1616
const Valid3 = () => {
17+
return <div {...props} something="else" {...otherProps}></div>
18+
}
19+
20+
const Valid4 = () => {
21+
return <div {...{}} something="else" {...otherProps}></div>
22+
}
23+
24+
const Valid5 = () => {
25+
return <div {...props} something="else" />
26+
}
27+
28+
const Valid6 = () => {
29+
return <div something="else" {...props} />
30+
}
31+
32+
const Valid7 = () => {
1733
return <div {...props} something="else" {...otherProps} />
1834
}
1935
36+
const Valid8 = () => {
37+
return <div {...{}} something="else" {...otherProps} />
38+
}
39+
2040
```

0 commit comments

Comments
 (0)