Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_analyze): Fix false positive diagnostics that useHookAtTopLevel caused to as or satisfies expressions. #4467

Merged
merged 2 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ when there are breaking changes.
- Fix false positive diagnostics that [`useCamelCase`](https://docs.rome.tools/lint/rules/usecamelcase/) caused to default exported components
- Fix false positive diagnostics that [`useCamelCase`](https://docs.rome.tools/lint/rules/usecamelcase/) caused to private class members
- Fix false positive diagnostics that [`useHookAtTopLevel`](https://docs.rome.tools/lint/rules/usehookattoplevel/) caused to arrow functions, export default functions and function expressions.
- Fix false positive diagnostics that [`useHookAtTopLevel`](https://docs.rome.tools/lint/rules/usehookattoplevel/) caused to `as` or `satisfies` expression.
- Fix false positive diagnostics that [`noHeadeScope`](https://docs.rome.tools/lint/rules/noheaderscope/) caused to custom components
- Fix false negative diagnostics that [`noNoninteractiveElementToInteractiveRole`](https://docs.rome.tools/lint/rules/nononinteractiveelementtointeractiverole/) and [`noNoninteractiveTabindex`](https://docs.rome.tools/lint/rules/nononinteractivetabindex/) caused to non-interactive elements.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ fn enclosing_function_if_call_is_at_top_level(call: &JsCallExpression) -> Option
| JsSyntaxKind::JS_VARIABLE_DECLARATOR
| JsSyntaxKind::JS_VARIABLE_DECLARATOR_LIST
| JsSyntaxKind::JS_VARIABLE_DECLARATION
| JsSyntaxKind::TS_AS_EXPRESSION
| JsSyntaxKind::TS_SATISFIES_EXPRESSION
)
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Component1 = () => {
useEffect() as [];
};

const Component2 = () => {
if (a == 1) {
Component1();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
---
# Input
```js
const Component1 = () => {
useEffect() as [];
};

const Component2 = () => {
if (a == 1) {
Component1();
}
};

```

# Diagnostics
```
invalid.ts:2:3 lint/nursery/useHookAtTopLevel ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This hook is being called indirectly and conditionally, but all hooks must be called in the exact same order in every component render.

1 │ const Component1 = () => {
> 2 │ useEffect() as [];
│ ^^^^^^^^^
3 │ };
4 │

i This is the call path until the hook.

5 │ const Component2 = () => {
> 6 │ if (a == 1) {
> 7 │ Component1();
│ ^^^^^^^^^^^^
8 │ }
9 │ };

i For React to preserve state between calls, hooks needs to be called unconditionally and always in the same order.

i See https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level


```


Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* does not generate diagnostics */

const Component1 = () => {
useEffect() as [];
};

const Component2 = () => {
useEffect() satisfies [];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: valid.ts
---
# Input
```js
/* does not generate diagnostics */

const Component1 = () => {
useEffect() as [];
};

const Component2 = () => {
useEffect() satisfies [];
};

```


Loading