Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 2.14 KB

README.md

File metadata and controls

61 lines (45 loc) · 2.14 KB

Rules

The following rules are provided by @foxglove/eslint-plugin.

Key: 🔧 = fixable, 💭 = requires type information (TypeScript only)

Prohibit boolean parameters to functions, including optional parameters and default values.

In languages without argument labels, boolean parameters often point to an API anti-pattern called the boolean trap. For example, with a function like repaint(immediate: boolean), a reader looking at a call site sees repaint(true); and loses key context that true means the repaint should be immediate.

This rule does currently allow unions of booleans and other types like value: boolean | number. This approach was chosen because some common library types like React.ReactNode are unions of primitives, and it would be too noisy to prohibit all of these.

Examples of incorrect code for this rule:

function draw(immediate: boolean) {}
const draw = (immediate?: boolean) => {};
const draw = (immediate = false) => {};

Examples of correct code for this rule:

function draw({ immediate }: { immediate: boolean }) {}
const draw = ({ immediate }: { immediate?: boolean }) => {};
const draw = ({ immediate = false }: { immediate: boolean }) => {};

Disallow returning Promise.resolve(...) or Promise.reject(...) inside an async function. This is redundant since an async function will always return a Promise — use return or throw directly instead.

Examples of incorrect code for this rule:

async function foo() {
  return Promise.resolve(0);
}
const bar = async function () {
  return Promise.resolve(9);
};
async () => Promise.resolve(3);
async () => Promise.reject(new Error("boom"));

Examples of correct code for this rule:

async function foo() {
  return 0;
}
const bar = async function () {
  return 9;
};
async () => 3;
async () => {
  throw new Error("boom");
};