Skip to content

Functions rules

Reports a positional parameter typed boolean, by its annotation or by a true or false default.

On in the all config.

A true or false at the call site says nothing about what it turns on.

Take an options object with a named boolean, or split the function into one function for each case.

export function print(error: unknown, isVerbose: boolean) {} reports positional at isVerbose, and so does const save = (draft = false) => {};. function save({ draft }: { draft: boolean }) {} does not report this finding, and neither does a function whose signature a declared type fixes, such as the callback in items.filter((isReady: boolean) => isReady).

Reports using the result of a local function that has no value-returning statement.

On in the recommended and all configs.

Using a missing return value passes undefined into code that expects a result.

Return the intended value from the function, or call it without using its result.

function write() { save(); } const value = write(); reports the use of write’s result.

Reports more call arguments than a local function declares, unless it accepts variadic arguments.

On in the recommended and all configs.

An extra argument can hide a mistaken function call or an outdated parameter list.

Remove the extra arguments, or declare the parameters the function uses.

function read(value) { return value; } read(first, second); reports the extra argument.

Reports a function declaration, method, or function assigned to a variable whose body has the same tokens as an earlier one of the file, when the body spans minLines lines or more, three by default.

On in the all config.

Two copies of one implementation drift apart when only one of them gets a fix.

Keep one function and call it from both places, or make the second one differ for a reason.

Two functions whose three-line bodies are the same report identical at the second.

Option Type Default What it does
minLines Whole number 3 The fewest lines a function body spans before a copy of it is reported.

Reports a call statement that drops the result of a method without side effects on a string, number, array, Date, Math, or RegExp value, such as name.trim();.

On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.

These methods return a new value and change nothing, so a call whose result is dropped does nothing.

Use the result, such as name = name.trim();, or delete the call. Use forEach instead of map to run a callback for each item.

title.toUpperCase(); reports ignored. Write title = title.toUpperCase();.

Reports a call of a function declared in the file when an earlier call of the same function used new and this one does not, or the reverse. Each function is reported once.

On in the recommended and all configs.

A function meant for new sets up this, and a plain function does not, so one of the two calls behaves differently than intended.

Call the function the same way everywhere: always with new for a constructor, never for a plain function.

const first = new Point(1, 2); const second = Point(3, 4); reports inconsistent at the second call.

Reports a function with two or more return statements that all return the same literal value, or the same variable that nothing changes.

On in the recommended and all configs.

A function that always returns the same value tells its caller nothing, so its return value or its branches are probably wrong.

Return the value that each branch should give, or remove the return value and the code that checks it.

function isValid(value) { if (value) return true; return true; } reports invariant.

Reports a function whose return statements return values of different kinds, such as a string in one branch and a number in another.

On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.

A caller of a function that returns different kinds of values has to test the kind of each result before it can use it.

Return one kind of value from every branch, or split the function so that each part returns one kind.

function read(raw: boolean) { if (raw) return "1"; return 1; } reports mixed.

Reports a function nested deeper than threshold functions, five by default. Declarations, expressions, and arrow functions count. The callbacks of test structure calls, such as describe, it, and beforeEach, do not count.

On in the all config.

Deeply nested functions are hard to follow, because each level adds its own scope and captured values.

Move the inner functions to module level or into named functions, and pass them the values they need.

With the default threshold, the sixth function nested inside five others reports nested.

Option Type Default What it does
threshold Whole number 5 The deepest nesting of functions allowed.

Reports a boolean parameter that the only statement of a function tests to choose between two actions.

On in the all config. It reports nothing without the type information of a TypeScript project.

A boolean that selects the behavior makes one function do two jobs, and each call site reads as an unexplained true or false.

Split the function into one function for each action, and call the one that fits.

function save(draft: boolean) { if (draft) store(); else publish(); } reports selector at draft.

Reports a call that passes two variables named like two parameters of the called function, each at the position of the other. Without type information, it reads local functions only.

On in the recommended and all configs.

Arguments with the names of the parameters in another order usually mean that the caller swapped them by mistake.

Pass the arguments in the order of the parameters, or rename the variables when the swap is deliberate.

function divide(dividend, divisor) {} divide(divisor, dividend); reports swapped.

Reports a function of trivialFunctionStatements statements or fewer, nested callbacks included, that the module does not export and that at most one place in the file uses, and a class method of that size.

On in the all config.

An unnecessary function adds another name and another place to read.

Inline the function at its call site and delete it. Keep a function whose identity matters, such as a listener that code adds and removes by name, and give its suppression that reason.

The function function one() { return 1; } with the one call total(one()) reports trivial. Replace the call with total(1) and delete the function. A function that two places use, or that the module exports, is not reported.

Option Type Default What it does
trivialFunctionStatements Whole number 2 The rule reports each function with this many statements or fewer.

Reports undefined passed as the last argument for a parameter that is optional or has a default value.

On in the all config. It reports nothing without the type information of a TypeScript project.

An optional parameter is already undefined when the argument is left out, so the explicit value only adds noise.

Remove the last argument.

format(value, undefined) reports undefined when format(value, style?) declares style optional.