Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consistent-function-scoping: Ignore within useEffect in React Hooks #588

Merged
merged 7 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function checkReferences(scope, parent, scopeManager) {
);
}

const isReactHooks = scope =>
fisker marked this conversation as resolved.
Show resolved Hide resolved
scope.block &&
scope.block.parent &&
scope.block.parent.callee &&
scope.block.parent.callee.type === 'Identifier' &&
scope.block.parent.callee.name === 'useEffect';
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

function checkNode(node, scopeManager) {
const scope = scopeManager.acquire(node);
if (!scope) {
Expand Down Expand Up @@ -87,7 +94,7 @@ function checkNode(node, scopeManager) {
}

const parentScope = scopeManager.acquire(parentNode);
if (!parentScope || parentScope.type === 'global') {
if (!parentScope || parentScope.type === 'global' || isReactHooks(parentScope)) {
return true;
}

Expand Down
25 changes: 25 additions & 0 deletions test/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ ruleTester.run('consistent-function-scoping', rule, {
return Bar;
};
`,
// React Hooks
outdent`
useEffect(() => {
function foo() {}
}, [])
`,
// #391
outdent`
const enrichErrors = (packageName, cliArgs, f) => async (...args) => {
Expand Down Expand Up @@ -362,6 +368,25 @@ ruleTester.run('consistent-function-scoping', rule, {
}
`,
errors: [functionError]
},
// React Hooks
{
code: outdent`
useEffect(() => {
function foo() {
function bar() {
}
}
}, [])
`,
errors: [
{
ruleId: 'consistent-function-scoping',
messageId: 'FunctionDeclaration',
column: 3,
line: 3
}
]
fisker marked this conversation as resolved.
Show resolved Hide resolved
}
]
});
Expand Down