@@ -374,13 +374,14 @@ export default {
374
374
for ( const hook of reactHooks ) {
375
375
// Report an error if a hook may be called more then once.
376
376
if ( cycled ) {
377
- context . report (
378
- hook ,
379
- `React Hook "${ context . getSource ( hook ) } " may be executed ` +
377
+ context . report ( {
378
+ node : hook ,
379
+ message :
380
+ `React Hook "${ context . getSource ( hook ) } " may be executed ` +
380
381
'more than once. Possibly because it is called in a loop. ' +
381
382
'React Hooks must be called in the exact same order in ' +
382
383
'every component render.' ,
383
- ) ;
384
+ } ) ;
384
385
}
385
386
386
387
// If this is not a valid code path for React hooks then we need to
@@ -394,16 +395,15 @@ export default {
394
395
//
395
396
// Special case when we think there might be an early return.
396
397
if ( ! cycled && pathsFromStartToEnd !== allPathsFromStartToEnd ) {
397
- context . report (
398
- hook ,
398
+ const message =
399
399
`React Hook "${ context . getSource ( hook ) } " is called ` +
400
- 'conditionally. React Hooks must be called in the exact ' +
401
- 'same order in every component render.' +
402
- ( possiblyHasEarlyReturn
403
- ? ' Did you accidentally call a React Hook after an' +
404
- ' early return?'
405
- : '' ) ,
406
- ) ;
400
+ 'conditionally. React Hooks must be called in the exact ' +
401
+ 'same order in every component render.' +
402
+ ( possiblyHasEarlyReturn
403
+ ? ' Did you accidentally call a React Hook after an' +
404
+ ' early return?'
405
+ : '' ) ;
406
+ context . report ( { node : hook , message } ) ;
407
407
}
408
408
} else if (
409
409
codePathNode . parent &&
@@ -418,13 +418,12 @@ export default {
418
418
// call in a class, if it works, is unambigously *not* a hook.
419
419
} else if ( codePathFunctionName ) {
420
420
// Custom message if we found an invalid function name.
421
- context . report (
422
- hook ,
421
+ const message =
423
422
`React Hook "${ context . getSource ( hook ) } " is called in ` +
424
- `function "${ context . getSource ( codePathFunctionName ) } " ` +
425
- 'which is neither a React function component or a custom ' +
426
- 'React Hook function.' ,
427
- ) ;
423
+ `function "${ context . getSource ( codePathFunctionName ) } " ` +
424
+ 'which is neither a React function component or a custom ' +
425
+ 'React Hook function.' ;
426
+ context . report ( { node : hook , message } ) ;
428
427
} else if ( codePathNode . type === 'Program' ) {
429
428
// For now, ignore if it's in top level scope.
430
429
// We could warn here but there are false positives related
@@ -436,12 +435,11 @@ export default {
436
435
// enough in the common case that the incorrect message in
437
436
// uncommon cases doesn't matter.
438
437
if ( isSomewhereInsideComponentOrHook ) {
439
- context . report (
440
- hook ,
438
+ const message =
441
439
`React Hook "${ context . getSource ( hook ) } " cannot be called ` +
442
- 'inside a callback. React Hooks must be called in a ' +
443
- 'React function component or a custom React Hook function.' ,
444
- ) ;
440
+ 'inside a callback. React Hooks must be called in a ' +
441
+ 'React function component or a custom React Hook function.' ;
442
+ context . report ( { node : hook , message } ) ;
445
443
}
446
444
}
447
445
}
0 commit comments