@samitouri / QOS-React / commits / d17d0b99c1

Use public context.report interface in eslint rules (#14623)

Sebastian Silbermann committed Jan 18, 2019 at 10:26 UTC d17d0b99c15893206a9035314fd18141c6117115
1 file changed +22 -24
packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
+22 -24
@@ -374,13 +374,14 @@ export default {
374 for (const hook of reactHooks) {
375 // Report an error if a hook may be called more then once.
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 ` +
381 'more than once. Possibly because it is called in a loop. ' +
382 'React Hooks must be called in the exact same order in ' +
383 'every component render.',
383 - );
384 + });
385 }
386
387 // If this is not a valid code path for React hooks then we need to
@@ -394,16 +395,15 @@ export default {
395 //
396 // Special case when we think there might be an early return.
397 if (!cycled && pathsFromStartToEnd !== allPathsFromStartToEnd) {
397 - context.report(
398 - hook,
398 + const message =
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 }
408 } else if (
409 codePathNode.parent &&
@@ -418,13 +418,12 @@ export default {
418 // call in a class, if it works, is unambigously *not* a hook.
419 } else if (codePathFunctionName) {
420 // Custom message if we found an invalid function name.
421 - context.report(
422 - hook,
421 + const message =
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});
427 } else if (codePathNode.type === 'Program') {
428 // For now, ignore if it's in top level scope.
429 // We could warn here but there are false positives related
@@ -436,12 +435,11 @@ export default {
435 // enough in the common case that the incorrect message in
436 // uncommon cases doesn't matter.
437 if (isSomewhereInsideComponentOrHook) {
439 - context.report(
440 - hook,
438 + const message =
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});
443 }
444 }
445 }