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}`);