Show HN: Catch hidden exceptions in JavaScript with ESLint and JSDoc

1 month ago 16
Code.mp4

Just as Java’s throws keyword does, enforcing the use of JSDoc’s @throws tag to explicitly specify which exceptions a function can throw to solve unpredictable propagation of exceptions happening which also known as a JavaScript's "hidden exceptions".

See examples for more.

  • Reports and provides fixes for throwable functions that are not annotated with @throws.
  • Reports and provides fixes for async functions and Promise rejections.
  • Verifies that the exception types match the documented types.

Install dependencies

# https://typescript-eslint.io/getting-started/#step-1-installation npm install --save-dev eslint @eslint/js typescript typescript-eslint

Install plugin

npm install --save-dev eslint-plugin-explicit-exceptions

Create eslint.config.mjs

// @ts-check import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; import explicitExceptionsLint from 'eslint-plugin-explicit-exceptions'; export default tseslint.config( eslint.configs.recommended, tseslint.configs.recommended, explicitExceptionsLint.configs.recommendTypeChecked, { languageOptions: { parserOptions: { projectService: true, tsconfigRootDir: import.meta.dirname, }, }, }, );

MIT License

Read Entire Article