Skip to content

Commit

Permalink
Allow React.useHook in consistent-function-scoping (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev committed Jan 12, 2022
1 parent 40bc603 commit e910633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {getFunctionHeadLocation, getFunctionNameWithKind} = require('eslint-utils');
const getReferences = require('./utils/get-references.js');
const {isNodeMatches} = require('./utils/is-node-matches.js');

const MESSAGE_ID = 'consistent-function-scoping';
const messages = {
Expand Down Expand Up @@ -77,7 +78,7 @@ function checkReferences(scope, parent, scopeManager) {
}

// https://reactjs.org/docs/hooks-reference.html
const reactHooks = new Set([
const reactHooks = [
'useState',
'useEffect',
'useContext',
Expand All @@ -88,13 +89,13 @@ const reactHooks = new Set([
'useImperativeHandle',
'useLayoutEffect',
'useDebugValue',
]);
].flatMap(hookName => [hookName, `React.${hookName}`]);

const isReactHook = scope =>
scope.block
&& scope.block.parent
&& scope.block.parent.callee
&& scope.block.parent.callee.type === 'Identifier'
&& reactHooks.has(scope.block.parent.callee.name);
&& isNodeMatches(scope.block.parent.callee, reactHooks);

const isArrowFunctionWithThis = scope =>
scope.type === 'function'
Expand Down
13 changes: 13 additions & 0 deletions test/consistent-function-scoping.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ test({
function foo() {}
}, [])
`,
outdent`
React.useEffect(() => {
function foo() {}
}, [])
`,
// IIFE
outdent`
(function() {
Expand Down Expand Up @@ -640,6 +645,14 @@ test({
`,
errors: [createError('function \'bar\'')],
},
{
code: outdent`
NotReact.useEffect(() => {
function foo() {}
}, [])
`,
errors: [createError('function \'foo\'')],
},
// IIFE
{
code: outdent`
Expand Down

0 comments on commit e910633

Please sign in to comment.