File tree Expand file tree Collapse file tree 3 files changed +29
-21
lines changed
tests/specs/nursery/useInlineScriptId Expand file tree Collapse file tree 3 files changed +29
-21
lines changed Original file line number Diff line number Diff line change 44
55Added the nursery rule [ ` useInlineScriptId ` ] ( https://biomejs.dev/linter/rules/use-inline-script-id/ ) to the Next.js domain.
66This 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+ ```
Original file line number Diff line number Diff 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 > {
Original file line number Diff line number Diff line change @@ -9,26 +9,24 @@ import Script from 'next/script'
99
1010export 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 5 │ return (
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
You can’t perform that action at this time.
0 commit comments