# consistent-function-scoping

Rule `unicorn/consistent-function-scoping`. Move function definitions to the highest possible scope.

- Source: unicorn
- Technologies: JavaScript
- Kind: suspicious
- Needs type information: no
- Autofix: no
- Reach: 11.6M weekly downloads of presets that enable it
- Trend: +42.5% last quarter
- Rank: #854 by reach, #930 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/consistent-function-scoping` | no | no | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/consistent-function-scoping.md) |
| Oxlint | `unicorn/consistent-function-scoping` | no | no | plugin `unicorn`, category suspicious | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/consistent-function-scoping.html) |

## Code examples

### Incorrect: the rule reports this

```js
export function doFoo(foo) {
  // Does not capture anything from the scope, can be moved to the outer scope
  function doBar(bar) {
    return bar === 'bar';
  }

  return doBar;
}
```

```js
function doFoo() {
  // Does not capture anything from the scope, can be moved to the outer scope
  return bar => bar === 'bar';
}
```

```js
export function someAction() {
  return dispatch => dispatch({type: 'SOME_TYPE'});
}
```

### Correct: the rule accepts this

```js
function doBar(bar) {
  return bar === 'bar';
}

export function doFoo(foo) {
  return doBar;
}
```

```js
const doBar = bar => bar === 'bar';

function doFoo() {
  return doBar;
}
```

```js
const handleDispatch = dispatch => dispatch({type: 'SOME_TYPE'});

export function someAction() {
  return handleDispatch;
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/consistent-function-scoping": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/consistent-function-scoping": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |

## Related rules

- [unicorn/import-style](https://lintradar.dev/rules/unicorn/import-style.md): Enforce specific import styles per module.
- [unicorn/no-useless-undefined](https://lintradar.dev/rules/unicorn/no-useless-undefined.md): Disallow useless `undefined`.
- [unicorn/number-literal-case](https://lintradar.dev/rules/unicorn/number-literal-case.md): Enforce proper case for numeric literals.
- [unicorn/switch-case-break-position](https://lintradar.dev/rules/unicorn/switch-case-break-position.md): Enforce consistent `break`/`return`/`continue`/`throw` position in `case` clauses.
- [unicorn/switch-case-braces](https://lintradar.dev/rules/unicorn/switch-case-braces.md): Enforce consistent brace style for `case` clauses.
- [unicorn/prefer-spread](https://lintradar.dev/rules/unicorn/prefer-spread.md): Prefer the spread operator over `Array.from(…)`, `Array#concat(…)`, `Array#{slice,toSpliced}()`, and trivial `for…of` copies.

---

Web page: https://lintradar.dev/rules/unicorn/consistent-function-scoping. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
