@@ -106,6 +106,7 @@ const tests = {
106
106
({useHook() { useState(); }});
107
107
const {useHook3 = () => { useState(); }} = {};
108
108
({useHook = () => { useState(); }} = {});
109
+ Namespace.useHook = () => { useState(); };
109
110
` ,
110
111
`
111
112
// Valid because hooks can call hooks.
@@ -191,47 +192,13 @@ const tests = {
191
192
}
192
193
}
193
194
` ,
194
- `
195
- // Currently valid.
196
- // We *could* make this invalid if we want, but it creates false positives
197
- // (see the FooStore case).
198
- class C {
199
- m() {
200
- This.useHook();
201
- Super.useHook();
202
- }
203
- }
204
- ` ,
205
- `
206
- // Valid although we *could* consider these invalid.
207
- // But it doesn't bring much benefit since it's an immediate runtime error anyway.
208
- // So might as well allow it.
209
- Hook.use();
210
- Hook._use();
211
- Hook.useState();
212
- Hook._useState();
213
- Hook.use42();
214
- Hook.useHook();
215
- Hook.use_hook();
216
- ` ,
217
195
`
218
196
// Valid -- this is a regression test.
219
197
jest.useFakeTimers();
220
198
beforeEach(() => {
221
199
jest.useRealTimers();
222
200
})
223
201
` ,
224
- `
225
- // Valid because that's a false positive we've seen quite a bit.
226
- // This is a regression test.
227
- class Foo extends Component {
228
- render() {
229
- if (cond) {
230
- FooStore.useFeatureFlag();
231
- }
232
- }
233
- }
234
- ` ,
235
202
`
236
203
// Valid because they're not matching use[A-Z].
237
204
fooState();
@@ -240,16 +207,6 @@ const tests = {
240
207
_useState();
241
208
use_hook();
242
209
` ,
243
- `
244
- // This is grey area.
245
- // Currently it's valid (although React.useCallback would fail here).
246
- // We could also get stricter and disallow it, just like we did
247
- // with non-namespace use*() top-level calls.
248
- const History = require('history-2.1.2');
249
- const browserHistory = History.useBasename(History.createHistory)({
250
- basename: '/',
251
- });
252
- ` ,
253
210
`
254
211
// Regression test for some internal code.
255
212
// This shows how the "callback rule" is more relaxed,
@@ -352,7 +309,6 @@ const tests = {
352
309
if (c) {} else {}
353
310
if (c) {} else {}
354
311
if (c) {} else {}
355
-
356
312
// 10 hooks
357
313
useHook();
358
314
useHook();
@@ -392,6 +348,68 @@ const tests = {
392
348
` ,
393
349
errors : [ conditionalError ( 'useConditionalHook' ) ] ,
394
350
} ,
351
+ {
352
+ code : `
353
+ Hook.use();
354
+ Hook._use();
355
+ Hook.useState();
356
+ Hook._useState();
357
+ Hook.use42();
358
+ Hook.useHook();
359
+ Hook.use_hook();
360
+ ` ,
361
+ errors : [
362
+ topLevelError ( 'Hook.useState' ) ,
363
+ topLevelError ( 'Hook.use42' ) ,
364
+ topLevelError ( 'Hook.useHook' ) ,
365
+ ] ,
366
+ } ,
367
+ {
368
+ code : `
369
+ class C {
370
+ m() {
371
+ This.useHook();
372
+ Super.useHook();
373
+ }
374
+ }
375
+ ` ,
376
+ errors : [ classError ( 'This.useHook' ) , classError ( 'Super.useHook' ) ] ,
377
+ } ,
378
+ {
379
+ code : `
380
+ class Foo extends Component {
381
+ render() {
382
+ if (cond) {
383
+ FooStore.useFeatureFlag();
384
+ }
385
+ }
386
+ }
387
+ ` ,
388
+ errors : [ classError ( 'FooStore.useFeatureFlag' ) ] ,
389
+ } ,
390
+ {
391
+ code : `
392
+ // Invalid because it's dangerous and might not warn otherwise.
393
+ // This *must* be invalid.
394
+ function ComponentWithConditionalHook() {
395
+ if (cond) {
396
+ Namespace.useConditionalHook();
397
+ }
398
+ }
399
+ ` ,
400
+ errors : [ conditionalError ( 'Namespace.useConditionalHook' ) ] ,
401
+ } ,
402
+ {
403
+ code : `
404
+ // Extending the isHook check to [A-Z].*\.use
405
+ // to not let it be called on top level
406
+ const History = require('history-2.1.2');
407
+ const browserHistory = History.useBasename(History.createHistory)({
408
+ basename: '/',
409
+ });
410
+ ` ,
411
+ errors : [ topLevelError ( 'History.useBasename' ) ] ,
412
+ } ,
395
413
{
396
414
code : `
397
415
// Invalid because it's dangerous and might not warn otherwise.
0 commit comments