useConsistentFunctionStyle (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useConsistentFunctionStyle - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
func-style
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentFunctionStyle": "error" } } }}Description
Section titled “Description”Enforce consistent use of function declarations or expressions assigned to variables.
A consistent function style makes function definitions easier to recognize.
Use the style option to choose the style that matches your project’s conventions.
In both styles, callbacks, methods, and default exports are ignored. Named exports follow the configured style.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function greet() { return "Hello";}code-block.js:1:10 lint/nursery/useConsistentFunctionStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Function expressions are required here, but this is a declaration.
> 1 │ function greet() {
│ ^^^^^
2 │ return "Hello";
3 │ }
ℹ Using a consistent function style makes function definitions easier to recognize.
ℹ Use a function expression assigned to a variable.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const greet = function() { return "Hello";};const farewell = () => "Goodbye";export default function greet() { return "Hello";}Options
Section titled “Options”Type: "expression" | "declaration"
Default: "expression"
With "expression", the rule requires function expressions or arrow functions
assigned to variables.
These functions can only be called after the variable is initialized. TypeScript overloads are allowed because they require declarations.
function identity(value: string): string;function identity(value: number): number;function identity(value: string | number) { return value;}With "declaration", the rule requires function declarations.
These are hoisted and can be called before their definition. Arrow functions that directly use
this or super are allowed to preserve their lexical binding.
Variables with TypeScript type annotations are also allowed because a function
declaration cannot be annotated with an existing function type.
A return type annotation on the function does not qualify for this exception.
The following examples use "declaration":
{ "linter": { "rules": { "nursery": { "useConsistentFunctionStyle": { "level": "on", "options": { "style": "declaration" } } } } }}Invalid
Section titled “Invalid”const greet = function() {};code-block.js:1:7 lint/nursery/useConsistentFunctionStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Function declarations are required here, but this is an expression.
> 1 │ const greet = function() {};
│ ^^^^^
2 │
ℹ Using a consistent function style makes function definitions easier to recognize.
ℹ Use a function declaration.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const greet = (): string => "Hello";code-block.ts:1:7 lint/nursery/useConsistentFunctionStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Function declarations are required here, but this is an expression.
> 1 │ const greet = (): string => "Hello";
│ ^^^^^
2 │
ℹ Using a consistent function style makes function definitions easier to recognize.
ℹ Use a function declaration.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function greet() {}const getContext = () => this;type Greeting = () => string;const greet: Greeting = () => "Hello";const farewell: Greeting = function() { return "Goodbye"; };allowArrowFunctions
Section titled “allowArrowFunctions”Type: boolean
Default: false
Allow arrow functions regardless of style setting. Arrow functions are always allowed when expressions are required.
{ "linter": { "rules": { "nursery": { "useConsistentFunctionStyle": { "level": "on", "options": { "style": "declaration", "allowArrowFunctions": true } } } } }}Invalid
Section titled “Invalid”const greet = function() { return "Hello"; };code-block.js:1:7 lint/nursery/useConsistentFunctionStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Function declarations are required here, but this is an expression.
> 1 │ const greet = function() { return "Hello"; };
│ ^^^^^
2 │
ℹ Using a consistent function style makes function definitions easier to recognize.
ℹ Use a function declaration.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function greet() { return "Hello"; }const farewell = () => "Goodbye";See Also
Section titled “See Also”Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.