@@ -374,13 +374,14 @@ export default {
374374for ( const hook of reactHooks ) {
375375// Report an error if a hook may be called more then once.
376376if ( 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 ` +
380381'more than once. Possibly because it is called in a loop. ' +
381382'React Hooks must be called in the exact same order in ' +
382383'every component render.' ,
383- ) ;
384+ } ) ;
384385}
385386
386387// If this is not a valid code path for React hooks then we need to
@@ -394,16 +395,15 @@ export default {
394395//
395396// Special case when we think there might be an early return.
396397if ( ! cycled && pathsFromStartToEnd !== allPathsFromStartToEnd ) {
397- context . report (
398- hook ,
398+ const message =
399399`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 } ) ;
407407}
408408} else if (
409409codePathNode . parent &&
@@ -418,13 +418,12 @@ export default {
418418// call in a class, if it works, is unambigously *not* a hook.
419419} else if ( codePathFunctionName ) {
420420// Custom message if we found an invalid function name.
421- context . report (
422- hook ,
421+ const message =
423422`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 } ) ;
428427} else if ( codePathNode . type === 'Program' ) {
429428// For now, ignore if it's in top level scope.
430429// We could warn here but there are false positives related
@@ -436,12 +435,11 @@ export default {
436435// enough in the common case that the incorrect message in
437436// uncommon cases doesn't matter.
438437if ( isSomewhereInsideComponentOrHook ) {
439- context . report (
440- hook ,
438+ const message =
441439`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 } ) ;
445443}
446444}
447445}
0 commit comments