Akronae / eslint-plugin-exception-handling

💣 Lints unhandled functions that might throw errors. For JavaScript/TypeScript eslint.
26 stars 1 forks source link
eslint eslint-plugin exception-handling

eslint-plugin-exception-handling

💣 Lints unhandled functions that might throw errors & ensure best practices. For JavaScript/TypeScript eslint.


image

no-unhandled might-throw use-error-cause
no-unhandled might-throw cause

Installation

yarn add -D eslint-plugin-exception-handling
npm i -D eslint-plugin-exception-handling
pnpm add -D eslint-plugin-exception-handling

Usage

Sample eslint.config.js:

For TypeScript:

// @ts-check

import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import { plugin as ex } from "eslint-plugin-exception-handling";

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  { plugins: { ex }, rules: { "ex/no-unhandled": "error" } }
);

For JavaScript:

import globals from "globals";
import pluginJs from "@eslint/js";
import { plugin as ex } from "eslint-plugin-exception-handling";

export default [
  { files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
  { plugins: { ex } },
  { rules: { "ex/no-unhandled": "error" } },
];

Limitations & Caveats

Rules

Name            Description
might-throw Warns about function calls that might throw exceptions.
no-unhandled Warns about function calls that might throw exceptions and are not handled at all further up the stack.
use-error-cause On Error re-thrown, forces the use of cause property in order to preserve stack traces. See: Error: cause