Skip to content

Commit d17d0b9

Browse files
eps1longaearon
authored andcommitted
Use public context.report interface in eslint rules (#14623)
1 parent 4f33288 commit d17d0b9

1 file changed

Lines changed: 22 additions & 24 deletions

File tree

‎packages/eslint-plugin-react-hooks/src/RulesOfHooks.js‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,14 @@ export default {
374374
for(consthookofreactHooks){
375375
// Report an error if a hook may be called more then once.
376376
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 `+
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.
396397
if(!cycled&&pathsFromStartToEnd!==allPathsFromStartToEnd){
397-
context.report(
398-
hook,
398+
constmessage=
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
}elseif(
409409
codePathNode.parent&&
@@ -418,13 +418,12 @@ export default {
418418
// call in a class, if it works, is unambigously *not* a hook.
419419
}elseif(codePathFunctionName){
420420
// Custom message if we found an invalid function name.
421-
context.report(
422-
hook,
421+
constmessage=
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
}elseif(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.
438437
if(isSomewhereInsideComponentOrHook){
439-
context.report(
440-
hook,
438+
constmessage=
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

Comments
 (0)