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>
This commit is contained in:
6 files changed
+100
No files matched your search
@@ -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
|
||||
Executable
+17
@@ -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
|
||||
@@ -10,3 +10,7 @@ coverage/
|
||||
# Env
|
||||
.env
|
||||
.env.*
|
||||
|
||||
# Astro (docs site)
|
||||
.astro/
|
||||
website/dist/
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"printWidth": 120,
|
||||
"tabWidth": 2,
|
||||
"singleQuote": true,
|
||||
"semi": true,
|
||||
"trailingComma": "all",
|
||||
"arrowParens": "always"
|
||||
}
|
||||
@@ -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' },
|
||||
],
|
||||
},
|
||||
},
|
||||
);
|
||||
Executable
+42
@@ -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}`);
|
||||
Reference in new issue
Block a user