Skip to content

Latest commit

 

History

History
105 lines (70 loc) · 2.17 KB

prefer-tacit.md

File metadata and controls

105 lines (70 loc) · 2.17 KB

Replaces x => f(x) with just f (functional/prefer-tacit)

⚠️🚫 This rule warns in the 🎨 stylistic config. This rule is disabled in the disableTypeChecked config.

💡 This rule is manually fixable by editor suggestions.

💭 This rule requires type information.

This rule enforces using functions directly if they can be without wrapping them.

Rule Details

If a function can be used directly without being in a callback wrapper, then it's generally better to use it directly. Extra inline lambdas can slow the runtime down.

⚠️ Warning ⚠️: Use with caution as if not all parameters should be passed to the function, a wrapper function is then required.

❌ Incorrect

/* eslint functional/prefer-tacit: "error" */

function f(x) {
  return x + 1;
}

const foo = [1, 2, 3].map((x) => f(x));

✅ Correct

/* eslint functional/prefer-tacit: "error" */

function f(x) {
  return x + 1;
}

const foo = [1, 2, 3].map(f);

const bar = { f };
const baz = [1, 2, 3].map((x) => bar.f(x)); // Allowed unless using `checkMemberExpressions`

Options

This rule accepts an options object of the following type:

type Options = {
  checkMemberExpressions: boolean;
};

Default Options

type Options = {
  checkMemberExpressions: false;
};

checkMemberExpressions

If true, calls of member expressions are checked as well. If false, only calls of identifiers are checked.

❌ Incorrect

/* eslint functional/prefer-tacit: ["error", { "checkMemberExpressions": true }] */

const bar = {
  f(x) {
    return x + 1;
  },
};

const foo = [1, 2, 3].map((x) => bar.f(x));

✅ Correct

/* eslint functional/prefer-tacit: ["error", { "checkMemberExpressions": true }] */

const bar = {
  f(x) {
    return x + 1;
  },
};

const foo = [1, 2, 3].map(bar.f.bind(bar));