// analyzer-linting/claude-md-extractor.ts — CLAUDE.md linting rule extraction (SPEC-025 tech-debt)
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
/** Known ESLint rule name patterns to detect in CLAUDE.md text */
const KNOWN_RULE_PATTERNS = [
    { pattern: /no-explicit-any/i, reason: 'Prevents unsafe `any` usage' },
    {
        pattern: /explicit-function-return-type/i,
        reason: 'Enforces explicit return types on functions',
    },
    { pattern: /no-unused-vars/i, reason: 'Prevents unused variable declarations' },
    { pattern: /no-unused-locals/i, reason: 'Prevents unused local declarations (TypeScript)' },
    { pattern: /no-unused-parameters/i, reason: 'Prevents unused parameters (TypeScript)' },
    { pattern: /no-floating-promises/i, reason: 'Ensures Promises are awaited or returned' },
    { pattern: /restrict-template-expressions/i, reason: 'Limits types usable in template literals' },
    { pattern: /consistent-type-imports/i, reason: 'Enforces `import type` for type-only imports' },
    { pattern: /no-restricted-imports/i, reason: 'Restricts which modules can be imported' },
    { pattern: /no-restricted-syntax/i, reason: 'Restricts specific syntax patterns' },
    { pattern: /max-lines\b/i, reason: 'Enforces maximum file length' },
    { pattern: /max-lines-per-function/i, reason: 'Enforces maximum function length' },
    { pattern: /complexity\b/i, reason: 'Enforces cyclomatic complexity limit' },
    { pattern: /no-console\b/i, reason: 'Disallows console statements in production code' },
    { pattern: /eqeqeq\b/i, reason: 'Requires strict equality operators' },
];
/** Section headers that indicate linting/code-style content */
const LINTING_SECTION_PATTERNS = [
    /##\s+eslint/i,
    /##\s+coding\s+rules/i,
    /##\s+code\s+style/i,
    /##\s+lint/i,
    /##\s+typescript\s+(strict|config)/i,
];
/**
 * Reads CLAUDE.md in the given project path (if it exists) and extracts
 * any ESLint rule names that appear in linting/code-style sections.
 * Returns an empty array when CLAUDE.md is absent.
 */
export async function extractClaudeMdRules(projectPath) {
    let content;
    try {
        content = await readFile(join(projectPath, 'CLAUDE.md'), 'utf-8');
    }
    catch {
        return [];
    }
    const lines = content.split('\n');
    const rules = [];
    const seenRuleNames = new Set();
    let insideLintingSection = false;
    for (const line of lines) {
        if (/^##\s+/.test(line)) {
            insideLintingSection = LINTING_SECTION_PATTERNS.some((p) => p.test(line));
        }
        const isRelevantLine = insideLintingSection ||
            /eslint|linting|code\s+quality|no-explicit-any|explicit-function-return-type/i.test(line);
        if (!isRelevantLine) {
            continue;
        }
        for (const { pattern, reason } of KNOWN_RULE_PATTERNS) {
            if (pattern.test(line)) {
                const ruleNameMatch = pattern.source.replace(/\\b|\\i|\/i$/g, '').replace(/\\\./g, '-');
                const ruleName = ruleNameMatch
                    .replace(/[()[\]\\^$*+?{}|]/g, '')
                    .trim()
                    .toLowerCase();
                if (!seenRuleNames.has(ruleName)) {
                    seenRuleNames.add(ruleName);
                    rules.push({ ruleName, suggestAdding: true, reason });
                }
            }
        }
    }
    return rules;
}
//# sourceMappingURL=claude-md-extractor.js.map