Add code quality gates

Add ESLint/quality gates, per-file ratchet budgets, and regression tests for code quality checks.
This commit is contained in:
Eyejoker
2026-04-28 01:19:06 +09:00
committed by GitHub
parent e2dd12a235
commit 79efa6b37c
8 changed files with 888 additions and 6 deletions

View File

@@ -0,0 +1,132 @@
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { relative, resolve } from 'node:path';
import {
analyzeCodeQuality,
evaluateCodeQuality,
type CodeQualityBudget,
type CodeQualityFinding,
type CodeQualityMetrics,
} from './quality/code-quality.js';
interface QualityBudgetConfig {
defaults: CodeQualityBudget;
overrides?: Record<string, CodeQualityBudget>;
testDefaults?: CodeQualityBudget;
}
const ROOT = resolve(import.meta.dir, '..');
const CONFIG_PATH = resolve(ROOT, 'quality/code-quality-budgets.json');
const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.css']);
const IGNORED_PARTS = new Set([
'dist',
'node_modules',
'coverage',
'store',
'data',
]);
const config = JSON.parse(
readFileSync(CONFIG_PATH, 'utf8'),
) as QualityBudgetConfig;
const reportOnly = process.argv.includes('--report');
const metrics = collectMetrics();
const findings = metrics.flatMap((item) =>
evaluateCodeQuality(item, budgetForFile(item.filePath, config)),
);
if (reportOnly) {
for (const item of metrics
.filter((metric) => config.overrides?.[metric.filePath])
.sort((a, b) => b.lineCount - a.lineCount)) {
console.log(
[
item.filePath,
`lines=${item.lineCount}`,
`fnLines=${item.maxFunctionLines}`,
`complexity=${item.maxComplexity}`,
`nesting=${item.maxNesting}`,
].join(' '),
);
}
}
if (findings.length > 0) {
console.error(formatFindings(findings));
process.exit(1);
}
console.log(
`code quality OK (${metrics.length} files, ${Object.keys(config.overrides ?? {}).length} ratcheted hotspots)`,
);
function collectMetrics(): CodeQualityMetrics[] {
return trackedFiles()
.filter((filePath) => shouldAnalyze(filePath))
.map((filePath) =>
analyzeCodeQuality(
filePath,
readFileSync(resolve(ROOT, filePath), 'utf8'),
),
);
}
function trackedFiles(): string[] {
return execFileSync('git', ['ls-files'], {
cwd: ROOT,
encoding: 'utf8',
})
.split('\n')
.filter(Boolean);
}
function shouldAnalyze(filePath: string): boolean {
if (filePath.endsWith('.d.ts')) return false;
const parts = filePath.split('/');
if (parts.some((part) => IGNORED_PARTS.has(part))) return false;
return SOURCE_EXTENSIONS.has(extensionFor(filePath));
}
function extensionFor(filePath: string): string {
const match = /\.[^.]+$/.exec(filePath);
return match?.[0] ?? '';
}
function budgetForFile(
filePath: string,
budgetConfig: QualityBudgetConfig,
): CodeQualityBudget {
const base = isTestFile(filePath)
? (budgetConfig.testDefaults ?? budgetConfig.defaults)
: budgetConfig.defaults;
return {
...base,
...budgetConfig.overrides?.[filePath],
};
}
function isTestFile(filePath: string): boolean {
return (
filePath.includes('/test/') ||
filePath.endsWith('.test.ts') ||
filePath.endsWith('.test.tsx') ||
filePath.endsWith('.spec.ts') ||
filePath.endsWith('.spec.tsx')
);
}
function formatFindings(findings: CodeQualityFinding[]): string {
const lines = findings.map((finding) => {
const label = finding.owner
? `${finding.filePath} (${finding.owner})`
: finding.filePath;
return `- ${label}: ${finding.metric} ${finding.actual} > ${finding.budget}`;
});
return [
'code quality budget failed:',
...lines,
'',
`Update the code to reduce the metric, or adjust ${relative(ROOT, CONFIG_PATH)} only when intentionally ratcheting a new baseline.`,
].join('\n');
}

View File

@@ -0,0 +1,64 @@
import { describe, expect, it } from 'vitest';
import { analyzeCodeQuality, evaluateCodeQuality } from './code-quality.js';
describe('code quality evaluator', () => {
it('measures line count and function metrics from TypeScript source', () => {
const metrics = analyzeCodeQuality(
'sample.ts',
[
'export function choose(value: number) {',
' if (value > 10) {',
' return value;',
' }',
' return value > 0 ? value : 0;',
'}',
].join('\n'),
);
expect(metrics.lineCount).toBe(6);
expect(metrics.nonEmptyLineCount).toBe(6);
expect(metrics.maxFunctionLines).toBe(6);
expect(metrics.maxComplexity).toBeGreaterThanOrEqual(3);
expect(metrics.maxNesting).toBeGreaterThanOrEqual(1);
});
it('does not let nested functions inflate the parent function budget', () => {
const metrics = analyzeCodeQuality(
'nested.ts',
[
'export function outer() {',
' const inner = () => {',
' if (true) return 1;',
' return 0;',
' };',
' return inner();',
'}',
].join('\n'),
);
expect(metrics.maxComplexity).toBe(2);
});
it('returns budget findings for metrics over the configured limit', () => {
const metrics = analyzeCodeQuality(
'oversized.ts',
['export function f() {', ' return 1;', '}'].join('\n'),
);
expect(
evaluateCodeQuality(metrics, {
maxComplexity: 1,
maxFunctionLines: 2,
maxLines: 2,
maxNesting: 0,
owner: 'test',
}),
).toEqual(
expect.arrayContaining([
expect.objectContaining({ metric: 'lineCount' }),
expect.objectContaining({ metric: 'maxFunctionLines' }),
]),
);
});
});

View File

@@ -0,0 +1,202 @@
import ts from 'typescript';
export interface CodeQualityBudget {
maxComplexity?: number;
maxFunctionLines?: number;
maxLines?: number;
maxNesting?: number;
owner?: string;
}
export interface CodeQualityMetrics {
filePath: string;
lineCount: number;
maxComplexity: number;
maxFunctionLines: number;
maxNesting: number;
nonEmptyLineCount: number;
}
export interface CodeQualityFinding {
actual: number;
budget: number;
filePath: string;
metric: keyof Pick<
CodeQualityMetrics,
'lineCount' | 'maxComplexity' | 'maxFunctionLines' | 'maxNesting'
>;
owner?: string;
}
interface FunctionMetrics {
complexity: number;
lines: number;
nesting: number;
}
export function analyzeCodeQuality(
filePath: string,
sourceText: string,
): CodeQualityMetrics {
const lines = sourceText.split(/\r?\n/);
const textMetrics = {
filePath,
lineCount: lines.length,
nonEmptyLineCount: lines.filter((line) => line.trim().length > 0).length,
};
if (!/\.(?:c|m)?[jt]sx?$/.test(filePath)) {
return {
...textMetrics,
maxComplexity: 0,
maxFunctionLines: 0,
maxNesting: 0,
};
}
const sourceFile = ts.createSourceFile(
filePath,
sourceText,
ts.ScriptTarget.Latest,
true,
scriptKindForPath(filePath),
);
const functions: FunctionMetrics[] = [];
const visit = (node: ts.Node) => {
if (isFunctionLike(node)) {
functions.push(analyzeFunction(sourceFile, node));
}
ts.forEachChild(node, visit);
};
ts.forEachChild(sourceFile, visit);
return {
...textMetrics,
maxComplexity: maxOf(functions.map((fn) => fn.complexity)),
maxFunctionLines: maxOf(functions.map((fn) => fn.lines)),
maxNesting: maxOf(functions.map((fn) => fn.nesting)),
};
}
export function evaluateCodeQuality(
metrics: CodeQualityMetrics,
budget: CodeQualityBudget,
): CodeQualityFinding[] {
const findings: CodeQualityFinding[] = [];
addFinding(findings, metrics, budget, 'lineCount', budget.maxLines);
addFinding(
findings,
metrics,
budget,
'maxFunctionLines',
budget.maxFunctionLines,
);
addFinding(findings, metrics, budget, 'maxComplexity', budget.maxComplexity);
addFinding(findings, metrics, budget, 'maxNesting', budget.maxNesting);
return findings;
}
function addFinding(
findings: CodeQualityFinding[],
metrics: CodeQualityMetrics,
budget: CodeQualityBudget,
metric: CodeQualityFinding['metric'],
limit: number | undefined,
) {
if (limit === undefined) return;
const actual = metrics[metric];
if (actual <= limit) return;
findings.push({
actual,
budget: limit,
filePath: metrics.filePath,
metric,
owner: budget.owner,
});
}
function analyzeFunction(
sourceFile: ts.SourceFile,
node: ts.SignatureDeclaration,
): FunctionMetrics {
const startLine = sourceFile.getLineAndCharacterOfPosition(
node.getStart(sourceFile),
).line;
const endLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line;
let complexity = 1;
let maxNesting = 0;
const walk = (current: ts.Node, nesting: number) => {
if (current !== node && isFunctionLike(current)) return;
if (isDecisionNode(current)) {
complexity += 1;
const nextNesting = nesting + 1;
maxNesting = Math.max(maxNesting, nextNesting);
ts.forEachChild(current, (child) => walk(child, nextNesting));
return;
}
if (
ts.isBinaryExpression(current) &&
isLogicalOperator(current.operatorToken.kind)
) {
complexity += 1;
}
ts.forEachChild(current, (child) => walk(child, nesting));
};
walk(node, 0);
return {
complexity,
lines: endLine - startLine + 1,
nesting: maxNesting,
};
}
function isDecisionNode(node: ts.Node): boolean {
return (
ts.isIfStatement(node) ||
ts.isForStatement(node) ||
ts.isForInStatement(node) ||
ts.isForOfStatement(node) ||
ts.isWhileStatement(node) ||
ts.isDoStatement(node) ||
ts.isCaseClause(node) ||
ts.isCatchClause(node) ||
ts.isConditionalExpression(node)
);
}
function isFunctionLike(node: ts.Node): node is ts.SignatureDeclaration {
return (
ts.isFunctionDeclaration(node) ||
ts.isFunctionExpression(node) ||
ts.isArrowFunction(node) ||
ts.isMethodDeclaration(node) ||
ts.isConstructorDeclaration(node) ||
ts.isGetAccessorDeclaration(node) ||
ts.isSetAccessorDeclaration(node)
);
}
function isLogicalOperator(kind: ts.SyntaxKind): boolean {
return (
kind === ts.SyntaxKind.AmpersandAmpersandToken ||
kind === ts.SyntaxKind.BarBarToken ||
kind === ts.SyntaxKind.QuestionQuestionToken
);
}
function maxOf(values: number[]): number {
return values.length === 0 ? 0 : Math.max(...values);
}
function scriptKindForPath(filePath: string): ts.ScriptKind {
if (filePath.endsWith('.tsx')) return ts.ScriptKind.TSX;
if (filePath.endsWith('.jsx')) return ts.ScriptKind.JSX;
if (filePath.endsWith('.js')) return ts.ScriptKind.JS;
return ts.ScriptKind.TS;
}