@crap4ts/crap4ts

中文文档
1.2.0发布于 2026/10/26MIT零依赖构建来源已验证近 30 天下载 2023

CRAP metric quality gate for TypeScript and TSX using complexity and test coverage

原始使用文档
文档目录3 节
  1. Quick start
  2. Using crap4ts in an agent workflow
  3. Scoring and CI

crap4ts

Inspired by Robert C. Martin (Uncle Bob)'s crap4java and crap4clj, adapted for TypeScript and TSX. His later crapper solution calculates CRAP scores for several languages, including TypeScript.

Find complex, poorly tested TypeScript functions and fail CI when their risk exceeds your limit. crap4ts combines cyclomatic complexity and test coverage into a per-function CRAP (Change Risk Anti-Patterns) score.

It analyzes TypeScript/TSX with Istanbul JSON or LCOV coverage and produces text or JSON reports. Supports per-file thresholds, independent monorepo package groups, optional branch scoring, Express route labels, and Git change selection.

Quick start

Requires Node.js >=20.19.0 <25. The package bundles binaries for Linux (glibc 2.35+) and macOS on x64/arm64, plus Windows x64.

npm install --save-dev @crap4ts/crap4ts
# Generate coverage with your test runner first, then:
npx crap4ts --coverage coverage/coverage-final.json src

For LCOV or JSON output:

npx crap4ts --coverage coverage/lcov.info --coverage-format lcov src
npx crap4ts --coverage coverage/coverage-final.json src --format json --threshold 12
npx crap4ts --coverage coverage/coverage-final.json src --coverage-metric branch
npx crap4ts --coverage coverage/coverage-final.json src --changed-since main

Run coverage once with the Vitest/Jest recipes. Generated commands have a configurable five-minute timeout.

Using crap4ts in an agent workflow

Give a coding agent concrete feedback on complex, poorly tested functions: generate coverage, run crap4ts, inspect flagged functions, improve tests or simplify the code, then rerun the gate. JSON reports support automated feedback; the same thresholds can run in CI.

See the agent workflow recipe for configuration and reusable agent instructions.

Scoring and CI

CRAP = complexity² × (1 − coverage)³ + complexity, where coverage is a fraction from 0 to 1. Lower scores are better.

The default threshold is 8. Exit codes: 0 passes, 2 means a score exceeds its threshold, 1 means invalid input or analysis failure. Missing or ambiguous coverage fails by default; --report-only keeps unknown scores visible while still enforcing thresholds on measured scores.

Tests, declarations, and generated output are excluded by default. JavaScript sources and raw V8 coverage are unsupported.

Usage and configuration · How it’s made · MIT license

版本与发布记录

@crap4ts/crap4ts 共发布 8 个版本,首次发布于 2026/08/26;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.0latest2026/10/2619 天前
1.1.02026/10/041 个月前
1.0.52026/10/031 个月前
1.0.42026/10/031 个月前
1.0.32026/10/021 个月前
1.0.22026/09/112 个月前

关于 @crap4ts/crap4ts 的常见问题

@crap4ts/crap4ts 是什么?

CRAP metric quality gate for TypeScript and TSX using complexity and test coverage

如何安装 @crap4ts/crap4ts?

在项目目录运行 npm install @crap4ts/crap4ts,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 crap4ts,可以用 npx crap4ts 直接运行。

@crap4ts/crap4ts 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/crap4ts__crap4ts 是否存在。

@crap4ts/crap4ts 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0 <25。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@crap4ts/crap4ts 可以商用吗?

@crap4ts/crap4ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@crap4ts/crap4ts 的下载量有多少?

根据 npm 官方下载统计,@crap4ts/crap4ts 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,023 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@crap4ts/crap4ts 还在维护吗?

最新版本 1.2.0 发布于 2026/10/26(19 天前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。