Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function isComponentOrHookLike(
}

function isHookName(s: string): boolean {
return /^use[A-Z0-9]/.test(s);
return /^use[A-Z0-9]/.test(s) || s === 'use$' || s === 'use_';
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ function hasMemoCacheFunctionImport(
}

function isHookName(s: string): boolean {
return /^use[A-Z0-9]/.test(s);
return /^use[A-Z0-9]/.test(s) || s === 'use$' || s === 'use_';
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,9 +1033,9 @@ export class Environment {

const REANIMATED_MODULE_NAME = 'react-native-reanimated';

// From https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/src/RulesOfHooks.js#LL18C1-L23C2
// From packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts
export function isHookName(name: string): boolean {
return /^use[A-Z0-9]/.test(name);
return /^use[A-Z0-9]/.test(name) || name === 'use$' || name === 'use_';
}

export function parseEnvironmentConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

## Input

```javascript
import {use$, use_} from 'shared-runtime';

function Component(props) {
if (props.cond) {
use$();
}
if (props.otherCond) {
use_();
}
return null;
}

```


## Error

```
Found 2 errors:

Error: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)

error.invalid-conditional-call-hook-symbol-names.ts:5:4
3 | function Component(props) {
4 | if (props.cond) {
> 5 | use$();
| ^^^^ Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)
6 | }
7 | if (props.otherCond) {
8 | use_();

Error: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)

error.invalid-conditional-call-hook-symbol-names.ts:8:4
6 | }
7 | if (props.otherCond) {
> 8 | use_();
| ^^^^ Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)
9 | }
10 | return null;
11 | }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {use$, use_} from 'shared-runtime';

function Component(props) {
if (props.cond) {
use$();
}
if (props.otherCond) {
use_();
}
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ const allTests = {
Namespace.useHook = () => { useState(); };
`,
},
{
code: normalizeIndent`
// Valid because hook names may use JavaScript identifier symbols.
function use$() { useState(); }
function use_() { useState(); }
`,
},
{
code: normalizeIndent`
// Valid because hooks can call hooks.
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {getAdditionalEffectHooksFromSettings} from '../shared/Utils';

/**
* Catch all identifiers that begin with "use" followed by an uppercase Latin
* character to exclude identifiers like "user".
* character or number, or the symbol-only hook names use$ and use_, to exclude
* identifiers like "user" and preserve existing behavior for names like
* "use_hook".
*/
function isHookName(s: string): boolean {
return s === 'use' || /^use[A-Z0-9]/.test(s);
return s === 'use' || /^use[A-Z0-9]/.test(s) || s === 'use$' || s === 'use_';
}

/**
Expand Down