Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt
Use this file to discover all available pages before exploring further.
A function that does not reference variables from its enclosing scope can be moved to a higher scope without any change in behavior. Doing so improves readability, directly improves performance, and gives JavaScript engines better opportunities to optimize the code.
| |
|---|
| Recommended | ✅ recommended |
| Fixable | — |
| Suggestions | — |
Examples
export function doFoo(foo) {
// doBar does not use `foo`, so it can be moved to the outer scope
function doBar(bar) {
return bar === 'bar';
}
return doBar;
}
function doFoo(foo) {
const doBar = bar => {
return bar === 'bar';
};
}
// Arrow functions returned from functions are also checked
export function someAction() {
return dispatch => dispatch({ type: 'SOME_TYPE' });
}
Configuration
export default [
{
rules: {
'unicorn/consistent-function-scoping': [
'error',
{ checkArrowFunctions: true },
],
},
},
];
checkArrowFunctions
Type: boolean — Default: true
Set to false to skip checking arrow functions. Only named function declarations and function expressions are then checked.
'unicorn/consistent-function-scoping': ['error', { checkArrowFunctions: false }]
Limitations
This rule does not detect extraneous block statements inside functions:
function doFoo(foo) {
{
// doBar is inside a block but the rule won't flag it
function doBar(bar) {
return bar;
}
}
return foo;
}
Functions containing JSX elements are ignored because JSX scope references are not tracked:
function doFoo(FooComponent) {
function Bar() {
return <FooComponent />;
}
return Bar;
}
Immediately invoked function expressions (IIFEs) are ignored:
(function () {
function doFoo(bar) {
return bar;
}
})();
React built-in hooks are ignored:
useEffect(() => {
async function getItems() {}
getItems();
}, []);