Files
core/scripts/check-complexity.mjs
chrnx 3ca4c8680e chore(qa): add ESLint, Prettier, and a cognitive-complexity gate
The project shipped no linting or formatting; add both ahead of the first
npm publish. ESLint (flat config, typescript-eslint) + Prettier enforce a
consistent style, and a cognitive-complexity gate (ccts, threshold 25)
keeps any function from regressing into the unreadable.

Both run in CI and in a pre-commit hook (.githooks, enabled via
core.hooksPath + the `prepare` script). Cyclomatic complexity stays
advisory-only — its ??-chain false positives make it a poor gate.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-07-07 11:59:42 -05:00

43 lines
1.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Cognitive-complexity gate. Fails (exit 1) if any named function in src/ exceeds
// the threshold, per cognitive-complexity-ts. Cyclomatic complexity is intentionally
// NOT gated here — its `??`-chain false positives make it advisory only (npm run complexity:cyclomatic).
import { execSync } from 'node:child_process';
const THRESHOLD = Number(process.env.COMPLEXITY_MAX ?? 25);
let raw = '';
try {
raw = execSync('npx ccts-json src', { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
} catch (error) {
raw = error.stdout?.toString() ?? '';
}
if (!raw.trim()) {
console.error('check-complexity: no output from ccts-json');
process.exit(2);
}
const offenders = [];
function walk(node) {
if (!node || typeof node !== 'object') return;
if (node.kind === 'function' && node.name && node.score > THRESHOLD) {
offenders.push({ score: node.score, name: node.name });
}
for (const [key, value] of Object.entries(node)) {
if (key === 'inner' && Array.isArray(value)) value.forEach(walk);
else if (value && typeof value === 'object') walk(value);
}
}
walk(JSON.parse(raw));
if (offenders.length) {
offenders.sort((a, b) => b.score - a.score);
console.error(`\n✖ cognitive complexity: ${offenders.length} function(s) over ${THRESHOLD}\n`);
for (const o of offenders) console.error(` ${String(o.score).padStart(4)} ${o.name}`);
console.error('\nExtract helpers to bring them under the threshold (or raise COMPLEXITY_MAX).\n');
process.exit(1);
}
console.log(`✓ cognitive complexity: every function ≤ ${THRESHOLD}`);