From 3ca4c8680ebb9c784462fe7000211f17bdb1d9e5 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Tue, 7 Jul 2026 11:59:42 -0500 Subject: [PATCH] chore(qa): add ESLint, Prettier, and a cognitive-complexity gate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .gitea/workflows/ci.yml | 3 +++ .githooks/pre-commit | 17 +++++++++++++++ .gitignore | 4 ++++ .prettierrc.json | 8 +++++++ eslint.config.mjs | 26 ++++++++++++++++++++++ scripts/check-complexity.mjs | 42 ++++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+) create mode 100755 .githooks/pre-commit create mode 100644 .prettierrc.json create mode 100644 eslint.config.mjs create mode 100755 scripts/check-complexity.mjs diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index ba0e6fd..6ce8237 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -13,6 +13,9 @@ jobs: with: node-version: '18' - run: npm ci + - run: npm run lint + - run: npm run format:check + - run: npm run complexity:check - run: npm run typecheck - run: npm test - run: npm run build diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..cbeda7b --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,17 @@ +#!/bin/sh +# Pre-commit gate. Auto-fixes formatting/lint on staged source, then enforces +# lint + cognitive-complexity. Enabled via `git config core.hooksPath .githooks` +# (run automatically by the `prepare` npm script on install). +set -e + +# Auto-fix format + lint on staged TypeScript sources, then re-stage them. +staged=$(git diff --cached --name-only --diff-filter=ACM -- 'src/*.ts' 'src/**/*.ts') +if [ -n "$staged" ]; then + echo "$staged" | xargs npx prettier --write + echo "$staged" | xargs npx eslint --fix + echo "$staged" | xargs git add +fi + +# Enforce (no auto-fix): lint must be clean, and no function over the complexity gate. +npm run lint +npm run complexity:check diff --git a/.gitignore b/.gitignore index a64bd69..32206f8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ coverage/ # Env .env .env.* + +# Astro (docs site) +.astro/ +website/dist/ diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..f730360 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,8 @@ +{ + "printWidth": 120, + "tabWidth": 2, + "singleQuote": true, + "semi": true, + "trailingComma": "all", + "arrowParens": "always" +} diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..565e909 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,26 @@ +import js from '@eslint/js'; +import tseslint from 'typescript-eslint'; +import stylistic from '@stylistic/eslint-plugin'; + +export default tseslint.config( + { ignores: ['dist', 'node_modules', 'bench', 'coverage'] }, + js.configs.recommended, + ...tseslint.configs.recommended, + { + plugins: { '@stylistic': stylistic }, + rules: { + // The request pipeline accumulates a dynamic context object by design; + // `any` there is load-bearing, not laziness. Flag it as a warning so new + // uses are visible without failing the build. + '@typescript-eslint/no-explicit-any': 'warn', + '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }], + // Breathing room around multi-line blocks (for/while/if/function/class) so + // consecutive blocks don't read as one wall of code. Auto-fixable. + '@stylistic/padding-line-between-statements': [ + 'error', + { blankLine: 'always', prev: 'multiline-block-like', next: '*' }, + { blankLine: 'always', prev: '*', next: 'multiline-block-like' }, + ], + }, + }, +); diff --git a/scripts/check-complexity.mjs b/scripts/check-complexity.mjs new file mode 100755 index 0000000..e66958b --- /dev/null +++ b/scripts/check-complexity.mjs @@ -0,0 +1,42 @@ +#!/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}`);