|
| 1 | +# consistent-function-style |
| 2 | + |
| 3 | +📝 Enforce function syntax by role. |
| 4 | + |
| 5 | +🚫 This rule is _disabled_ in the following [configs](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config): ✅ `recommended`, ☑️ `unopinionated`. |
| 6 | + |
| 7 | +💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). |
| 8 | + |
| 9 | +<!-- end auto-generated rule header --> |
| 10 | +<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> |
| 11 | + |
| 12 | +Enforce function syntax by role. |
| 13 | + |
| 14 | +By default, this rule reports nothing. Configure the roles you want to enforce. |
| 15 | + |
| 16 | +This rule does not autofix. Function declarations, function expressions, arrow functions, and object methods are not always semantic equivalents, so `--fix` would be too risky for a style rule. |
| 17 | + |
| 18 | +The rule offers editor suggestions only for simple callback conversions between anonymous function expressions and arrow functions. Suggestions are skipped when the callback contains comments, `this`, `arguments`, `super`, `new.target`, direct `eval()`, generators, named function expressions, or TypeScript type parameters. |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | +```js |
| 23 | +// eslint unicorn/consistent-function-style: ["error", {namedFunctions: "declaration"}] |
| 24 | + |
| 25 | +// ❌ |
| 26 | +const parse = value => value; |
| 27 | + |
| 28 | +// ✅ |
| 29 | +function parse(value) { |
| 30 | + return value; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +```js |
| 35 | +// ✅ Reassigned variables are ignored by default. |
| 36 | +let transform = value => value; |
| 37 | + |
| 38 | +if (debug) { |
| 39 | + transform = wrap(transform); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```js |
| 44 | +// ✅ Callbacks are ignored by default. |
| 45 | +items.map(item => item.id); |
| 46 | +``` |
| 47 | + |
| 48 | +## Options |
| 49 | + |
| 50 | +Type: `object` |
| 51 | + |
| 52 | +Default: |
| 53 | + |
| 54 | +```js |
| 55 | +{ |
| 56 | + default: 'ignore', |
| 57 | + namedFunctions: 'ignore', |
| 58 | + namedExports: 'ignore', |
| 59 | + callbacks: 'ignore', |
| 60 | + objectProperties: 'ignore', |
| 61 | + reassignedVariables: 'ignore', |
| 62 | + typedVariables: 'ignore', |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Each option chooses the expected style for a role. |
| 67 | + |
| 68 | +When multiple roles apply, the most specific role wins. Roles are checked in this order: |
| 69 | + |
| 70 | +```js |
| 71 | +typedVariables |
| 72 | +reassignedVariables |
| 73 | +namedExports |
| 74 | +callbacks |
| 75 | +objectProperties |
| 76 | +namedFunctions |
| 77 | +default |
| 78 | +``` |
| 79 | + |
| 80 | +### `default` |
| 81 | + |
| 82 | +Allowed values: `'declaration'`, `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 83 | + |
| 84 | +Fallback style for functions that do not match a more specific role. |
| 85 | + |
| 86 | +### `namedFunctions` |
| 87 | + |
| 88 | +Allowed values: `'declaration'`, `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 89 | + |
| 90 | +Style for function declarations and named variable functions. |
| 91 | + |
| 92 | +### `namedExports` |
| 93 | + |
| 94 | +Allowed values: `'declaration'`, `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 95 | + |
| 96 | +Style for inline named function exports, such as `export function foo() {}` and `export const foo = () => {}`. Export specifier lists like `export {foo}` are ignored. |
| 97 | + |
| 98 | +### `callbacks` |
| 99 | + |
| 100 | +Allowed values: `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 101 | + |
| 102 | +Style for inline functions passed as call or constructor arguments. |
| 103 | + |
| 104 | +### `objectProperties` |
| 105 | + |
| 106 | +Allowed values: `'method'`, `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 107 | + |
| 108 | +Style for object literal function properties. |
| 109 | + |
| 110 | +### `reassignedVariables` |
| 111 | + |
| 112 | +Allowed values: `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 113 | + |
| 114 | +Style for variable functions whose binding is reassigned after initialization. |
| 115 | + |
| 116 | +### `typedVariables` |
| 117 | + |
| 118 | +Allowed values: `'function-expression'`, `'arrow-function'`, `'ignore'` |
| 119 | + |
| 120 | +Style for TypeScript variable functions with an explicit type annotation. |
| 121 | + |
| 122 | +## Limitations |
| 123 | + |
| 124 | +The rule ignores anonymous default exports, export specifier lists, IIFEs, accessors, class methods, class fields, destructuring defaults, TypeScript overload declarations, and generators when the expected style is `'arrow-function'`. Most reports do not have suggestions, because the safe rewrite surface is intentionally narrow. |
0 commit comments