A Prettier plugin that identifies and highlights throw statements in your TypeScript code, optionally enforcing that only specific error classes are thrown.
- Highlight Throw Statements: Add comments above all throw statements for easy identification
- Enforce Error Classes: Define allowed error classes and receive warnings for non-compliant throws
- Flexible Configuration: Include or exclude files using glob patterns
# Using npm
npm install --save-dev prettier-plugin-detect-throw
# Using yarn
yarn add --dev prettier-plugin-detect-throw
# Using bun
bun add --dev prettier-plugin-detect-throwAdd the plugin to your Prettier configuration:
// .prettierrc.js
module.exports = {
plugins: ["prettier-plugin-detect-throw"],
highlightThrows: true,
allowedErrorClasses: '["Error", "CustomError", "ValidationError"]',
enforceAllowedClasses: true,
include: '["**/*.ts", "**/*.tsx"]',
exclude: '["**/node_modules/**"]',
}Or as JSON:
{
"plugins": ["prettier-plugin-detect-throw"],
"highlightThrows": true,
"allowedErrorClasses": "[\"Error\", \"CustomError\", \"ValidationError\"]",
"enforceAllowedClasses": true,
"include": "[\"**/*.ts\", \"**/*.tsx\"]",
"exclude": "[\"**/node_modules/**\"]"
}| Option | Type | Default | Description |
|---|---|---|---|
highlightThrows |
boolean | false |
When true, adds a /* THROW STATEMENT */ comment above each throw |
allowedErrorClasses |
string (JSON array) | "[]" |
JSON string array of allowed error class names |
enforceAllowedClasses |
boolean | true |
When true, adds warning comments to throws that use non-allowed error classes |
include |
string (JSON array) | "[\"**/*.ts\", \"**/*.tsx\"]" |
JSON string array of glob patterns for files to include |
exclude |
string (JSON array) | "[]" |
JSON string array of glob patterns for files to exclude |
Before:
function processData(data) {
if (!data) {
throw new Error("Data is required")
}
if (!isValid(data)) {
throw new InvalidDataError("Invalid data format")
}
try {
return transform(data)
} catch (e) {
throw e
}
}After (with highlightThrows: true and allowedErrorClasses: ["Error"]):
function processData(data) {
if (!data) {
/* THROW STATEMENT */
throw new Error("Data is required")
}
if (!isValid(data)) {
/* WARNING: Only throw instances of: Error */
/* THROW STATEMENT */
throw new InvalidDataError("Invalid data format")
}
try {
return transform(data)
} catch (e) {
/* THROW STATEMENT */
throw e
}
}# Install dependencies
bun install
# Build as edits are made
bun run watch
# Build once
bun run build
# Typecheck
bun run typecheck