Fix test for hook name to match lint rule
Joe Savona committed
May 8, 2023 at 12:14 UTC
7dedc6cc724d168a81e0e2d7e9a943a0bb31a78e
3 files changed
+34
-5
compiler/forget/src/HIR/Environment.ts
+9
-3
@@ -28,8 +28,6 @@ import {
28
import { Hook } from "./Hooks";
29
import { FunctionSignature, ShapeRegistry } from "./ObjectShape";
30
31
-const HOOK_PATTERN = /^_?use/;
32
-
31
// TODO(mofeiZ): User defined global types (with corresponding shapes).
32
// User defined global types should have inline ObjectShapes instead of directly
33
// using ObjectShapes.ShapeRegistry, as a user-provided ShapeRegistry may be
@@ -83,7 +81,7 @@ export class Environment {
81
let resolvedGlobal: Global | null = this.#globals.get(name) ?? null;
82
if (resolvedGlobal === null) {
83
// Hack, since we don't track module level declarations and imports
86
- if (name.match(HOOK_PATTERN)) {
84
+ if (isHookName(name)) {
85
return {
86
kind: "Hook",
87
definition: {
@@ -132,3 +130,11 @@ export class Environment {
130
return null;
131
}
132
}
133
+
134
+// From https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/src/RulesOfHooks.js#LL18C1-L23C2
135
+function isHookName(name: string): boolean {
136
+ // if (__EXPERIMENTAL__) {
137
+ // return name === 'use' || /^use[A-Z0-9]/.test(name);
138
+ // }
139
+ return /^use[A-Z0-9]/.test(name);
140
+}
compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-7.expect.md
new
+25
@@ -0,0 +1,25 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// Valid because functions can call functions.
6
+function functionThatStartsWithUseButIsntAHook() {
7
+ if (cond) {
8
+ userFetch();
9
+ }
10
+}
11
+
12
+```
13
+
14
+## Code
15
+
16
+```javascript
17
+// Valid because functions can call functions.
18
+function functionThatStartsWithUseButIsntAHook() {
19
+ if (cond) {
20
+ userFetch();
21
+ }
22
+}
23
+
24
+```
25
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-7.js
renamed
-2
@@ -1,5 +1,3 @@
1
-// @skip
2
-
1
// Valid because functions can call functions.
2
function functionThatStartsWithUseButIsntAHook() {
3
if (cond) {