Skip to content

Commit ee0faa1

Browse files
committed
fix: feedback
1 parent b76cace commit ee0faa1

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

.changeset/evil-houses-hide.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@
44

55
Added the nursery rule [`useInlineScriptId`](https://biomejs.dev/linter/rules/use-inline-script-id/) to the Next.js domain.
66
This rule enforces `id` attribute on `next/script` components with inline content or `dangerouslySetInnerHTML`.
7+
8+
The following code is invalid:
9+
10+
```jsx
11+
import Script from 'next/script';
12+
13+
export default function Page() {
14+
return (
15+
<Script>{`console.log('Hello');`}</Script> // must have `id` attribute
16+
);
17+
}
18+
```

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ impl Rule for UseInlineScriptId {
107107
match argument {
108108
AnyJsExpression::JsObjectExpression(obj_expr) => {
109109
collect_property_names(&obj_expr, &mut attribute_names)?;
110-
continue;
111110
}
112111
AnyJsExpression::JsIdentifierExpression(ident_expr) => {
113112
let reference = ident_expr.name().ok()?;
@@ -121,25 +120,24 @@ impl Rule for UseInlineScriptId {
121120
if let AnyJsExpression::JsObjectExpression(obj_expr) = expression {
122121
collect_property_names(&obj_expr, &mut attribute_names)?;
123122
}
124-
continue;
125-
}
126-
_ => {
127-
continue;
128123
}
124+
_ => {}
129125
}
130126
}
131-
_ => continue,
127+
_ => {}
132128
}
133129
}
134130

135-
let has_children = !jsx_element.parent::<JsxElement>()?.children().is_empty();
136-
if has_children || attribute_names.contains("dangerouslySetInnerHTML") {
137-
if !attribute_names.contains("id") {
138-
return Some(jsx_element.syntax().text_range_with_trivia());
139-
}
131+
let has_children = jsx_element
132+
.parent::<JsxElement>()
133+
.is_some_and(|parent| !parent.children().is_empty());
134+
if (has_children || attribute_names.contains("dangerouslySetInnerHTML"))
135+
&& !attribute_names.contains("id")
136+
{
137+
return Some(jsx_element.syntax().text_range_with_trivia());
140138
}
141139

142-
return None;
140+
None
143141
}
144142

145143
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {

crates/biome_js_analyze/tests/specs/nursery/useInlineScriptId/invalid-02.jsx.snap

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ import Script from 'next/script'
99
1010
export default function Page() {
1111
return (
12-
<>
13-
<Script dangerouslySetInnerHTML={{ __html: `console.log('Hello world!');` }}></Script>
14-
</>
12+
<Script dangerouslySetInnerHTML={{ __html: `console.log('Hello world!');` }} />
1513
)
1614
}
1715
1816
```
1917

2018
# Diagnostics
2119
```
22-
invalid-02.jsx:7:7 lint/nursery/useInlineScriptId ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
20+
invalid-02.jsx:6:5 lint/nursery/useInlineScriptId ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2321
2422
! next/script components with inline content or `dangerouslySetInnerHTML` must specify id attribute.
2523
24+
4 │ export default function Page() {
2625
5return (
27-
6 │ <>
28-
> 7 │ <Script dangerouslySetInnerHTML={{ __html: `console.log('Hello world!');` }}></Script>
29-
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
8 │ </>
31-
9 │ )
26+
> 6<Script dangerouslySetInnerHTML={{ __html: `console.log('Hello world!');` }} />
27+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
7 │ )
29+
8}
3230
3331
i See the Next.js docs for more details.
3432

0 commit comments

Comments
 (0)