Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | /**
* @module Scripts/StripInMethodComments
* @description Removes leading, trailing and free-standing comments that live
* **inside** function/method/arrow/constructor/accessor bodies, while preserving
* comments that document declarations and any directive comments needed by the
* tooling chain (ESLint, TypeScript, coverage, bundlers, license headers).
*
* Rules:
* - Strip block and line comments whose range falls inside
* any function/method/constructor/get/set/arrow body block.
* - Preserve directive comments anywhere:
* `eslint-`, `@ts-`, `prettier-ignore`, `c8 ignore`, `istanbul ignore`,
* `webpackIgnore`, `vite-ignore`, `__PURE__`, `@vite-`, `@license`,
* `@preserve`, `@copyright`, `@cc_on`.
* - Always preserve top-level JSDoc anywhere — they document
* declarations, never live inside method bodies under our codebase rules.
*
* Usage:
* npx tsx scripts/strip-in-method-comments.ts [--check] [--quiet] [paths…]
*
* With no paths, defaults to `src/browser` and `scripts` (recursively),
* skipping tests, generated artefacts, vendor `js/lib`, dashboard stubs and
* declaration files.
*/
import { readFileSync, writeFileSync, statSync, readdirSync } from 'node:fs';
import { join, resolve, relative, extname } from 'node:path';
import ts from 'typescript';
const REPO_ROOT = resolve(new URL('..', import.meta.url).pathname);
const DEFAULT_ROOTS = ['src/browser', 'scripts'];
const SKIP_PATH_PATTERNS = [
/\bnode_modules\b/,
/\bdist\b/,
/\bbuilds\b/,
/\bcoverage\b/,
/\bapi\b\//,
/\.test\.[mc]?[jt]sx?$/,
/\.spec\.[mc]?[jt]sx?$/,
/\.d\.ts$/,
/\bjs\/lib\b/,
/scripts\/coalition-dashboard(\.|\/)/,
/scripts\/committees-dashboard(\.|\/)/,
/scripts\/back-to-top\.ts$/,
/scripts\/strip-in-method-comments\.ts$/,
/scripts\/generate-types-from-cia-schemas\.ts$/,
/scripts\/generate-article-types-doc\.ts$/,
];
const PRESERVE_DIRECTIVES = [
'eslint-',
'@ts-',
'prettier-ignore',
'c8 ignore',
'istanbul ignore',
'webpackIgnore',
'vite-ignore',
'__PURE__',
'@vite-',
'@license',
'@preserve',
'@copyright',
'@cc_on',
];
interface BodyRange {
readonly start: number;
readonly end: number;
}
/**
* Determine if a comment must be preserved regardless of its position.
*
* @param text - Full comment text including delimiters.
* @returns `true` when the comment is a JSDoc block, a license header or a
* recognised tooling directive.
*/
function shouldPreserveComment(text: string): boolean {
if (text.startsWith('/**')) return true;
for (const marker of PRESERVE_DIRECTIVES) {
if (text.includes(marker)) return true;
}
return false;
}
/**
* Collect the byte ranges of every function/method/arrow/constructor/accessor
* body in a source file.
*
* @param source - Parsed TypeScript source file.
* @returns Sorted, possibly overlapping body ranges.
*/
function collectBodyRanges(source: ts.SourceFile): BodyRange[] {
const ranges: BodyRange[] = [];
const visit = (node: ts.Node): void => {
let body: ts.Node | undefined;
if (
ts.isFunctionDeclaration(node) ||
ts.isFunctionExpression(node) ||
ts.isMethodDeclaration(node) ||
ts.isConstructorDeclaration(node) ||
ts.isGetAccessorDeclaration(node) ||
ts.isSetAccessorDeclaration(node)
) {
body = node.body;
} else if (ts.isArrowFunction(node)) {
body = ts.isBlock(node.body) ? node.body : undefined;
}
if (body && ts.isBlock(body)) {
ranges.push({ start: body.getStart(source) + 1, end: body.getEnd() - 1 });
}
ts.forEachChild(node, visit);
};
visit(source);
return ranges;
}
/**
* Check whether a comment range is covered by any method body range.
*
* @param ranges - Body ranges from `collectBodyRanges`.
* @param pos - Comment start offset.
* @param end - Comment end offset.
* @returns `true` when the comment lies fully inside at least one body range.
*/
function isInsideBody(ranges: readonly BodyRange[], pos: number, end: number): boolean {
for (const r of ranges) {
if (pos >= r.start && end <= r.end) return true;
}
return false;
}
interface CommentToRemove {
readonly pos: number;
readonly end: number;
readonly kind: ts.CommentKind;
}
/**
* Walk every token-leading and token-trailing comment in the file and
* collect those that should be removed.
*
* @param source - Parsed TypeScript source file.
* @param bodyRanges - Body ranges where comments are considered in-method.
* @returns Comment ranges sorted descending so removals from the source string
* do not shift the indices of pending removals.
*/
function collectInMethodComments(source: ts.SourceFile, bodyRanges: readonly BodyRange[]): CommentToRemove[] {
const text = source.text;
const seen = new Set<string>();
const removals: CommentToRemove[] = [];
const handle = (pos: number, end: number, kind: ts.CommentKind): void => {
const key = `${pos}:${end}`;
if (seen.has(key)) return;
seen.add(key);
if (!isInsideBody(bodyRanges, pos, end)) return;
const commentText = text.slice(pos, end);
if (shouldPreserveComment(commentText)) return;
removals.push({ pos, end, kind });
};
const visit = (node: ts.Node): void => {
const leading = ts.getLeadingCommentRanges(text, node.getFullStart()) ?? [];
for (const c of leading) handle(c.pos, c.end, c.kind);
const trailing = ts.getTrailingCommentRanges(text, node.getEnd()) ?? [];
for (const c of trailing) handle(c.pos, c.end, c.kind);
if (ts.isBlock(node) && node.statements.length > 0) {
const tail = ts.getLeadingCommentRanges(text, node.statements.end) ?? [];
for (const c of tail) handle(c.pos, c.end, c.kind);
}
ts.forEachChild(node, visit);
};
visit(source);
removals.sort((a, b) => b.pos - a.pos);
return removals;
}
/**
* Remove a single comment from the source text. Strips the trailing newline of
* line comments and any leading whitespace/newline-only run preceding a
* stand-alone block comment so the file does not accumulate blank lines.
*/
function spliceComment(text: string, removal: CommentToRemove): string {
let end = removal.end;
let start = removal.pos;
let lineStart = start;
while (lineStart > 0 && text[lineStart - 1] !== '\n') {
const ch = text[lineStart - 1];
if (ch !== ' ' && ch !== '\t') break;
lineStart -= 1;
}
const onOwnLine = lineStart === 0 || text[lineStart - 1] === '\n';
if (onOwnLine) {
start = lineStart;
if (text[end] === '\n') end += 1;
} else if (removal.kind === ts.SyntaxKind.SingleLineCommentTrivia) {
// Trailing line-comment on a code line — strip preceding spaces/tabs so the
// surviving line does not have dangling whitespace, but keep the newline.
while (start > lineStart && (text[start - 1] === ' ' || text[start - 1] === '\t')) {
start -= 1;
}
}
return text.slice(0, start) + text.slice(end);
}
/**
* Process a single file: parse, collect in-method comments, splice them out,
* collapse runs of blank lines and write the result if changed.
*
* @returns `true` when the file content changed.
*/
function processFile(filePath: string, opts: { check: boolean; quiet: boolean }): boolean {
const original = readFileSync(filePath, 'utf8');
const source = ts.createSourceFile(filePath, original, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS);
const bodyRanges = collectBodyRanges(source);
if (bodyRanges.length === 0) return false;
const removals = collectInMethodComments(source, bodyRanges);
if (removals.length === 0) return false;
let updated = original;
for (const r of removals) updated = spliceComment(updated, r);
updated = updated.replace(/\n{3,}/g, '\n\n');
if (updated === original) return false;
if (!opts.check) writeFileSync(filePath, updated, 'utf8');
if (!opts.quiet) {
const rel = relative(REPO_ROOT, filePath);
console.log(`${opts.check ? '[would-strip]' : '[stripped]'} ${rel} (${removals.length} comment(s))`);
}
return true;
}
/**
* Recursively gather candidate `.ts` / `.js` files for stripping.
*/
function* walk(dir: string): Iterable<string> {
let entries: string[];
try {
entries = readdirSync(dir);
} catch {
return;
}
for (const name of entries) {
const full = join(dir, name);
const stat = statSync(full);
if (stat.isDirectory()) {
if (SKIP_PATH_PATTERNS.some((re) => re.test(full))) continue;
yield* walk(full);
continue;
}
const ext = extname(full);
if (ext !== '.ts' && ext !== '.js' && ext !== '.mjs' && ext !== '.cjs') continue;
if (SKIP_PATH_PATTERNS.some((re) => re.test(full))) continue;
yield full;
}
}
/**
* CLI entry point.
*/
function main(): void {
const argv = process.argv.slice(2);
const opts = {
check: argv.includes('--check'),
quiet: argv.includes('--quiet'),
};
const explicit = argv.filter((a) => !a.startsWith('--'));
const roots = explicit.length > 0 ? explicit : DEFAULT_ROOTS;
let total = 0;
let changed = 0;
for (const root of roots) {
for (const file of walk(resolve(REPO_ROOT, root))) {
total += 1;
if (processFile(file, opts)) changed += 1;
}
}
if (!opts.quiet) {
console.log(`\nScanned ${total} file(s); ${opts.check ? 'would change' : 'changed'} ${changed}.`);
}
if (opts.check && changed > 0) process.exit(1);
}
main();
|