Skip to content

Commit 779727f

Browse files
committed
fix(linter): improve span diagnostic loc within react/rules-of-hooks (#11589)
closes #11587
1 parent d29bbb2 commit 779727f

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

crates/oxc_linter/src/rules/react/rules_of_hooks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl Rule for RulesOfHooks {
207207
}
208208
}
209209
// Hooks can't be called from async function.
210-
AstKind::Function(Function { id: Some(id), r#async: true, .. }) => {
211-
return ctx.diagnostic(diagnostics::async_component(id.span, id.name.as_str()));
210+
AstKind::Function(Function { id: Some(_), r#async: true, .. }) => {
211+
return ctx.diagnostic(diagnostics::async_component(span, hook_name));
212212
}
213213
// Hooks can't be called from async arrow function.
214214
AstKind::ArrowFunctionExpression(ArrowFunctionExpression {

crates/oxc_linter/src/snapshots/react_rules_of_hooks.snap

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ source: crates/oxc_linter/src/tester.rs
585585
· ──────────
586586
╰────
587587

588-
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "AsyncComponent" cannot be called in an async function.
589-
╭─[rules_of_hooks.tsx:2:32]
590-
1 │
588+
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "useState" cannot be called in an async function.
589+
╭─[rules_of_hooks.tsx:3:21]
591590
2 │ async function AsyncComponent() {
592-
· ──────────────
593591
3useState();
592+
· ──────────
593+
4 │ }
594594
╰────
595595

596596
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "Anonymous" cannot be called in an async function.
@@ -602,28 +602,28 @@ source: crates/oxc_linter/src/tester.rs
602602
5 │
603603
╰────
604604

605-
eslint-plugin-react-hooks(rules-of-hooks): React Hook "Page" cannot be called in an async function.
606-
╭─[rules_of_hooks.tsx:2:32]
607-
1 │
605+
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "useId" cannot be called in an async function.
606+
╭─[rules_of_hooks.tsx:3:19]
608607
2 │ async function Page() {
609-
· ────
610608
3useId();
609+
· ───────
610+
4React.useId();
611611
╰────
612612

613-
eslint-plugin-react-hooks(rules-of-hooks): React Hook "Page" cannot be called in an async function.
614-
╭─[rules_of_hooks.tsx:2:32]
615-
1 │
616-
2 │ async function Page() {
617-
· ────
613+
eslint-plugin-react-hooks(rules-of-hooks): React Hook "useId" cannot be called in an async function.
614+
╭─[rules_of_hooks.tsx:4:19]
618615
3 │ useId();
616+
4React.useId();
617+
· ─────────────
618+
5 │ }
619619
╰────
620620

621-
eslint-plugin-react-hooks(rules-of-hooks): React Hook "useAsyncHook" cannot be called in an async function.
622-
╭─[rules_of_hooks.tsx:2:32]
623-
1 │
621+
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "useState" cannot be called in an async function.
622+
╭─[rules_of_hooks.tsx:3:21]
624623
2 │ async function useAsyncHook() {
625-
· ────────────
626624
3useState();
625+
· ──────────
626+
4 │ }
627627
╰────
628628

629629
⚠ eslint-plugin-react-hooks(rules-of-hooks): React Hook "useId" is called in function "notAHook" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use".
@@ -682,12 +682,12 @@ source: crates/oxc_linter/src/tester.rs
682682
5 │ }
683683
╰────
684684

685-
eslint-plugin-react-hooks(rules-of-hooks): React Hook "AsyncComponent" cannot be called in an async function.
686-
╭─[rules_of_hooks.tsx:2:28]
687-
1 │
685+
eslint-plugin-react-hooks(rules-of-hooks): React Hook "use" cannot be called in an async function.
686+
╭─[rules_of_hooks.tsx:3:21]
688687
2 │ async function AsyncComponent() {
689-
· ──────────────
690688
3use();
689+
· ─────
690+
4 │ }
691691
╰────
692692

693693
eslint-plugin-react-hooks(rules-of-hooks): React Hook "useState" is called in function "Anonymous" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use".

0 commit comments

Comments
 (0)