Functions rules
no-boolean-parameters
Section titled “no-boolean-parameters”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.
What to do
Section titled “What to do”Take an options object with a named boolean, or split the function into one function for each case.
Example
Section titled “Example”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).
no-empty-return-value
Section titled “no-empty-return-value”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.
What to do
Section titled “What to do”Return the intended value from the function, or call it without using its result.
Example
Section titled “Example”function write() { save(); } const value = write(); reports the use of write’s result.
no-excess-call-arguments
Section titled “no-excess-call-arguments”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.
What to do
Section titled “What to do”Remove the extra arguments, or declare the parameters the function uses.
Example
Section titled “Example”function read(value) { return value; } read(first, second); reports the extra argument.
no-identical-functions
Section titled “no-identical-functions”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.
What to do
Section titled “What to do”Keep one function and call it from both places, or make the second one differ for a reason.
Example
Section titled “Example”Two functions whose three-line bodies are the same report identical at the second.
Options
Section titled “Options”| Option | Type | Default | What it does |
|---|---|---|---|
minLines |
Whole number | 3 |
The fewest lines a function body spans before a copy of it is reported. |
no-ignored-return
Section titled “no-ignored-return”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.
What to do
Section titled “What to do”Use the result, such as name = name.trim();, or delete the call. Use forEach instead of map to run a callback for each item.
Example
Section titled “Example”title.toUpperCase(); reports ignored. Write title = title.toUpperCase();.
no-inconsistent-function-call
Section titled “no-inconsistent-function-call”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.
What to do
Section titled “What to do”Call the function the same way everywhere: always with new for a constructor, never for a plain function.
Example
Section titled “Example”const first = new Point(1, 2); const second = Point(3, 4); reports inconsistent at the second call.
no-invariant-returns
Section titled “no-invariant-returns”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.
What to do
Section titled “What to do”Return the value that each branch should give, or remove the return value and the code that checks it.
Example
Section titled “Example”function isValid(value) { if (value) return true; return true; } reports invariant.
no-mixed-return-types
Section titled “no-mixed-return-types”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.
What to do
Section titled “What to do”Return one kind of value from every branch, or split the function so that each part returns one kind.
Example
Section titled “Example”function read(raw: boolean) { if (raw) return "1"; return 1; } reports mixed.
no-nested-functions
Section titled “no-nested-functions”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.
What to do
Section titled “What to do”Move the inner functions to module level or into named functions, and pass them the values they need.
Example
Section titled “Example”With the default threshold, the sixth function nested inside five others reports nested.
Options
Section titled “Options”| Option | Type | Default | What it does |
|---|---|---|---|
threshold |
Whole number | 5 |
The deepest nesting of functions allowed. |
no-selector-parameter
Section titled “no-selector-parameter”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.
What to do
Section titled “What to do”Split the function into one function for each action, and call the one that fits.
Example
Section titled “Example”function save(draft: boolean) { if (draft) store(); else publish(); } reports selector at draft.
no-swapped-arguments
Section titled “no-swapped-arguments”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.
What to do
Section titled “What to do”Pass the arguments in the order of the parameters, or rename the variables when the swap is deliberate.
Example
Section titled “Example”function divide(dividend, divisor) {} divide(divisor, dividend); reports swapped.
no-trivial-functions
Section titled “no-trivial-functions”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.
What to do
Section titled “What to do”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.
Example
Section titled “Example”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.
Options
Section titled “Options”| Option | Type | Default | What it does |
|---|---|---|---|
trivialFunctionStatements |
Whole number | 2 |
The rule reports each function with this many statements or fewer. |
no-undefined-argument
Section titled “no-undefined-argument”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.
What to do
Section titled “What to do”Remove the last argument.
Example
Section titled “Example”format(value, undefined) reports undefined when format(value, style?) declares style optional.