@barney-media/crap-typescript-core

中文文档
2.0.0发布于 2026/10/19Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 4.6万

Core CRAP metric analyzer for TypeScript projects.

原始使用文档
文档目录4 节
  1. Install
  2. API
  3. Formula
  4. License

@barney-media/crap-typescript-core

Core analysis engine for computing CRAP (Change Risk Anti-Pattern) scores on TypeScript projects. Combines cyclomatic complexity with function-level Istanbul coverage data.

Install

npm install @barney-media/crap-typescript-core

API

import { analyzeProject, formatAnalysisReport } from "@barney-media/crap-typescript-core";

const result = await analyzeProject({ projectRoot: "." });
const report = formatAnalysisReport(result.metrics, { format: "toon" });
console.log(report);

Key exports: analyzeProject, calculateCrapScore, formatAnalysisReport, formatReport, parseCoverageReport, parseFileMethods.

analyzeProject accepts threshold to override the default CRAP threshold of 6.0. Values below 4.0 emit a warning because they are likely too noisy; values above 8.0 emit a warning because they are too lenient even for hard gates.

Commands run by the default command executor, including coverage commands and changed-file discovery, time out after 300 seconds. Set CRAP_TYPESCRIPT_COMMAND_TIMEOUT_MS to a non-negative millisecond value to override this default; 0 disables the timeout. Captured stdout and stderr are capped at 10 MiB per stream. User-facing command failures may include truncated output, while internal commands that require complete output, such as git status --porcelain -z, fail if the cap is exceeded.

analyzeProject also accepts excludes, excludePathRegexes, excludeGeneratedMarkers, and useDefaultExclusions. These filters run after explicitPaths or changedOnly select candidate files and before parsing, coverage attribution, reporting, and threshold evaluation. Baseline analyzability exclusions always remain active for declarations, test/spec files, __tests__/, dist/, coverage/, and node_modules/; useDefaultExclusions: false only disables generated-code defaults.

Generated-code defaults exclude directory segments containing generated, directory segments exactly named gen, common generated filename suffixes such as *.generated.ts, *.gen.ts, *Generated.ts, protobuf outputs, and Angular generated artifacts. Leading generated-header markers are matched only in comments before the first non-comment token.

formatAnalysisReport supports toon, json, text, junit, and none. Primary reports expose run-level status and threshold; method-level entries use status, crap, cc, cov, covKind, method, src, lineStart, and lineEnd, with src set to the project-relative source file path. format: "none" returns empty primary content. Pass failuresOnly: true to include failed method entries only. Pass omitRedundancy: true to omit method-level status while keeping run-level metadata; with format: "junit", this omits the custom testcase status property while preserving JUnit failure and skipped elements. JUnit output is GitLab-oriented: it uses a <testsuites> wrapper, classname/file source-path testcase attributes, metric-bearing testcase names, time="0", and testcase-level system-out plus failure/skipped body text with CRAP score, threshold, coverage kind, source path, and line range. JUnit still writes custom properties for compatibility. Sidecar JUnit reports should be generated by formatting the full metrics set separately with format: "junit" and without failuresOnly, omitRedundancy, or agent.

Pass sourceExclusionAudit: result.sourceExclusionAudit to formatAnalysisReport when reports should expose excluded-source counts. Full reports and JUnit sidecars include the audit when files were excluded. Optimized primary reports from default agent mode omit it by default.

agent: true is a composite shortcut for primary reports. It defaults format to toon in the CLI and adapters, and defaults failuresOnly and omitRedundancy to true in core formatting. Explicit format, failuresOnly: false, or omitRedundancy: false options override those defaults at the call site.

Formula

CRAP = CC^2 * (1 - coverage)^3 + CC

See the main documentation for full details.

License

Apache-2.0

版本与发布记录

@barney-media/crap-typescript-core 共发布 14 个版本,首次发布于 2026/04/08;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/1920 天前
1.0.02026/09/281 个月前
0.5.32026/09/261 个月前
0.5.22026/09/052 个月前
0.5.12026/08/302 个月前
0.5.02026/08/092 个月前

关于 @barney-media/crap-typescript-core 的常见问题

@barney-media/crap-typescript-core 是什么?

Core CRAP metric analyzer for TypeScript projects.

如何安装 @barney-media/crap-typescript-core?

在项目目录运行 npm install @barney-media/crap-typescript-core,或使用 pnpm add / yarn add / bun add。

@barney-media/crap-typescript-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@barney-media/crap-typescript-core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@barney-media/crap-typescript-core 需要什么版本的 Node.js?

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

@barney-media/crap-typescript-core 可以商用吗?

@barney-media/crap-typescript-core 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@barney-media/crap-typescript-core 的下载量有多少?

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

@barney-media/crap-typescript-core 还在维护吗?

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