#!/usr/bin/env node /** * Purpose: scan YumiNail business source files and report declaration-level * comment coverage for classes, methods, fields, top-level constants, and mini * program Page data/method entries. * Usage: run `rtk node scripts/comment-coverage.mjs` from the repository root; * pass `--json` to emit machine-readable output or `--fix` to insert comments * for declarations that are safe to annotate mechanically. */ import fs from 'node:fs/promises' import path from 'node:path' import process from 'node:process' /** * Purpose: absolute repository root used to resolve all scan targets. * Usage: keep this derived from the current working directory so the script can * be launched through `rtk node scripts/comment-coverage.mjs`. */ const repoRoot = process.cwd() /** * Purpose: source roots that contain hand-written business code. * Usage: add new first-party source roots here when they should be part of the * declaration coverage budget. */ const SOURCE_ROOTS = [ 'YumiNail-Core/src/main/java', 'YumiNail-Frontend/src', 'WeChatProgram', 'YumiNail-Admin-Program' ] /** * Purpose: directory or file fragments that are generated, configuration-only, * binary, or otherwise unsuitable for declaration comment coverage. * Usage: keep exclusions narrow so real business code stays covered. */ const EXCLUDED_SEGMENTS = [ 'node_modules', 'target', 'dist', '.git', '.idea', 'logs', 'assets/', 'assets\\', 'public/assets', 'public\\assets', 'components.d.ts', 'auto-import.d.ts', 'package-lock.json', 'project.config.json', 'project.private.config.json', 'sitemap.json' ] /** * Purpose: file extensions whose declarations can be statically detected by * this lightweight scanner. * Usage: update when introducing another hand-written source language. */ const INCLUDED_EXTENSIONS = new Set(['.java', '.ts', '.vue', '.js', '.wxml', '.wxss']) /** * Purpose: tiny names that represent framework keywords rather than business * declarations. * Usage: ignore these when scanning object methods in JavaScript-like files. */ const JS_KEYWORD_NAMES = new Set([ 'if', 'for', 'while', 'switch', 'catch', 'function', 'return', 'else', 'do' ]) /** * Purpose: read command line output mode. * Usage: `--json` prints the same coverage data without prose formatting. */ const outputJson = process.argv.includes('--json') /** * Purpose: read command line mutation mode. * Usage: `--fix` inserts declaration comments, then the script can be run again * without flags to verify the final percentage. */ const fixMode = process.argv.includes('--fix') /** * Purpose: determine whether a path should be skipped entirely. * Usage: call before recursing into directories or scanning files. */ function isExcluded(relativePath) { const normalized = relativePath.replaceAll('\\', '/') return EXCLUDED_SEGMENTS.some(segment => normalized.includes(segment.replaceAll('\\', '/'))) } /** * Purpose: detect whether a file extension is part of the coverage scan. * Usage: call for each discovered file before reading its content. */ function hasIncludedExtension(filePath) { return INCLUDED_EXTENSIONS.has(path.extname(filePath)) } /** * Purpose: recursively list all source files under a directory. * Usage: returns repository-relative paths using forward slashes for stable * reports across Windows and Unix shells. */ async function listSourceFiles(relativeDir) { const absoluteDir = path.join(repoRoot, relativeDir) const results = [] async function walk(currentDir) { const entries = await fs.readdir(currentDir, { withFileTypes: true }).catch(() => []) for (const entry of entries) { const absolutePath = path.join(currentDir, entry.name) const relativePath = path.relative(repoRoot, absolutePath).replaceAll('\\', '/') if (isExcluded(relativePath)) { continue } if (entry.isDirectory()) { await walk(absolutePath) } else if (entry.isFile() && hasIncludedExtension(relativePath)) { results.push(relativePath) } } } await walk(absoluteDir) return results } /** * Purpose: decide whether a declaration has a nearby leading comment. * Usage: pass the full source lines and the zero-based declaration line index. */ function hasLeadingComment(lines, lineIndex) { for (let cursor = lineIndex - 1; cursor >= 0; cursor -= 1) { const text = lines[cursor].trim() if (!text) { continue } if (text.startsWith('@')) { continue } if (text.endsWith('*/') || text.startsWith('*') || text.startsWith('//') || text.includes('