strip-comments.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /**
  2. * Per-language comment stripper for framework route extractors.
  3. *
  4. * Replaces comment characters and string-literal contents that hide
  5. * routing-shaped text with spaces (NOT removal) so that source offsets
  6. * are preserved. This means `match.index` from a regex run on the
  7. * stripped output still maps to the same line in the original source.
  8. *
  9. * Example:
  10. * Input: "x = 1 # path('/fake/', V)\n real = 2"
  11. * Output: "x = 1 \n real = 2"
  12. *
  13. * Why strip strings/docstrings as well as comments? Python module/class
  14. * docstrings are a common source of false positives — they often contain
  15. * `path('/example/', View)` examples in usage docs. We treat triple-quoted
  16. * strings the same as comments. Single-line strings stay intact (a `#`
  17. * inside a Python string is NOT a comment).
  18. *
  19. * Scope: this is a pragmatic, regex-supporting helper, not a full parser.
  20. * It does NOT try to detect JS regex literals, Python f-string expressions,
  21. * or shell-style heredocs. Those edge cases are not load-bearing for the
  22. * `path(...)`, `Route::get(...)`, `app.get(...)` style patterns that
  23. * framework extractors scan for.
  24. */
  25. /**
  26. * Blank string contents while preserving quotes and offsets. Template
  27. * interpolations are blanked too; callers checking executable expressions
  28. * must conservatively inspect those expressions in the original source.
  29. */
  30. export function blankStringContents(text: string): string {
  31. const out = text.split('');
  32. let i = 0;
  33. const n = text.length;
  34. while (i < n) {
  35. const c = text[i]!;
  36. // A quote inside a JS regex is data, not the beginning of a string.
  37. // Expression-start punctuation and keywords distinguish these from division.
  38. if (c === '/' && /(?:^|[=(:,)!&|?;{}\[\]+*%~^<>-]|\b(?:return|throw|case|yield|await|else|do|typeof|void|delete|new|in|of|instanceof))\s*$/.test(text.slice(Math.max(0, i - 32), i))) {
  39. let end = i + 1;
  40. let inClass = false;
  41. for (; end < n && text[end] !== '\n'; end++) {
  42. if (text[end] === '\\') { end++; continue; }
  43. if (text[end] === '[') inClass = true;
  44. if (text[end] === ']') inClass = false;
  45. if (text[end] === '/' && !inClass) break;
  46. }
  47. if (end < n && text[end] === '/') { i = end + 1; continue; }
  48. }
  49. if (c === '"' || c === "'" || c === '`') {
  50. const quote = c;
  51. i++;
  52. while (i < n && text[i] !== quote) {
  53. if (text[i] === '\\' && i + 1 < n) {
  54. out[i] = ' ';
  55. out[i + 1] = ' ';
  56. i += 2;
  57. continue;
  58. }
  59. if (quote !== '`' && text[i] === '\n') break;
  60. if (text[i] !== '\n') out[i] = ' ';
  61. i++;
  62. }
  63. if (i < n && text[i] === quote) i++;
  64. continue;
  65. }
  66. i++;
  67. }
  68. return out.join('');
  69. }
  70. export type CommentLang =
  71. | 'python'
  72. | 'javascript'
  73. | 'typescript'
  74. | 'php'
  75. | 'ruby'
  76. | 'java'
  77. | 'csharp'
  78. | 'swift'
  79. | 'go'
  80. | 'rust'
  81. | 'c'
  82. | 'cpp'
  83. | 'erlang';
  84. export function stripCommentsForRegex(content: string, lang: CommentLang): string {
  85. switch (lang) {
  86. case 'python':
  87. return stripPython(content);
  88. case 'ruby':
  89. return stripRuby(content);
  90. case 'rust':
  91. return stripRust(content);
  92. case 'erlang':
  93. return stripErlang(content);
  94. case 'php':
  95. return stripPhp(content);
  96. case 'go':
  97. return stripGo(content);
  98. case 'javascript':
  99. case 'typescript':
  100. case 'java':
  101. case 'csharp':
  102. case 'swift':
  103. case 'c':
  104. case 'cpp':
  105. return stripCStyle(content, /* allowSingleQuoteStrings */ lang === 'javascript' || lang === 'typescript');
  106. default:
  107. return content;
  108. }
  109. }
  110. /**
  111. * Replace every char in a slice with spaces, but keep newlines so line
  112. * numbers computed downstream remain valid.
  113. */
  114. function blankRange(buf: string[], start: number, end: number, src: string): void {
  115. for (let i = start; i < end; i++) {
  116. buf[i] = src[i] === '\n' ? '\n' : ' ';
  117. }
  118. }
  119. // ---------- Python ----------
  120. function stripPython(src: string): string {
  121. const out = src.split('');
  122. let i = 0;
  123. const n = src.length;
  124. while (i < n) {
  125. const c = src[i]!;
  126. const c2 = src[i + 1] ?? '';
  127. const c3 = src[i + 2] ?? '';
  128. // Triple-quoted string: """...""" or '''...'''
  129. if ((c === '"' || c === "'") && c2 === c && c3 === c) {
  130. const quote = c;
  131. const start = i;
  132. i += 3;
  133. while (i < n) {
  134. if (src[i] === '\\' && i + 1 < n) {
  135. i += 2;
  136. continue;
  137. }
  138. if (src[i] === quote && src[i + 1] === quote && src[i + 2] === quote) {
  139. i += 3;
  140. break;
  141. }
  142. i++;
  143. }
  144. blankRange(out, start, i, src);
  145. continue;
  146. }
  147. // Single-line string: '...' or "..."
  148. if (c === '"' || c === "'") {
  149. const quote = c;
  150. i++;
  151. while (i < n && src[i] !== quote) {
  152. if (src[i] === '\\' && i + 1 < n) {
  153. i += 2;
  154. continue;
  155. }
  156. if (src[i] === '\n') break; // unterminated
  157. i++;
  158. }
  159. if (i < n && src[i] === quote) i++;
  160. continue;
  161. }
  162. // Line comment
  163. if (c === '#') {
  164. const start = i;
  165. while (i < n && src[i] !== '\n') i++;
  166. blankRange(out, start, i, src);
  167. continue;
  168. }
  169. i++;
  170. }
  171. return out.join('');
  172. }
  173. // ---------- Ruby ----------
  174. function stripRuby(src: string): string {
  175. const out = src.split('');
  176. let i = 0;
  177. const n = src.length;
  178. let atLineStart = true;
  179. while (i < n) {
  180. const c = src[i]!;
  181. // =begin / =end block comments must be at start of line (after optional whitespace)
  182. if (atLineStart && c === '=' && src.startsWith('=begin', i)) {
  183. const start = i;
  184. // consume to matching =end at line start
  185. i += '=begin'.length;
  186. while (i < n) {
  187. if (src[i] === '\n') {
  188. // check next line for =end
  189. let j = i + 1;
  190. while (j < n && (src[j] === ' ' || src[j] === '\t')) j++;
  191. if (src.startsWith('=end', j)) {
  192. i = j + '=end'.length;
  193. // consume rest of that line
  194. while (i < n && src[i] !== '\n') i++;
  195. break;
  196. }
  197. }
  198. i++;
  199. }
  200. blankRange(out, start, i, src);
  201. atLineStart = i > 0 && src[i - 1] === '\n';
  202. continue;
  203. }
  204. // String literals
  205. if (c === '"' || c === "'") {
  206. const quote = c;
  207. i++;
  208. while (i < n && src[i] !== quote) {
  209. if (src[i] === '\\' && i + 1 < n) {
  210. i += 2;
  211. continue;
  212. }
  213. if (src[i] === '\n') break;
  214. i++;
  215. }
  216. if (i < n && src[i] === quote) i++;
  217. atLineStart = false;
  218. continue;
  219. }
  220. // Line comment
  221. if (c === '#') {
  222. const start = i;
  223. while (i < n && src[i] !== '\n') i++;
  224. blankRange(out, start, i, src);
  225. atLineStart = false;
  226. continue;
  227. }
  228. if (c === '\n') {
  229. atLineStart = true;
  230. i++;
  231. continue;
  232. }
  233. if (c === ' ' || c === '\t') {
  234. // whitespace doesn't change atLineStart
  235. i++;
  236. continue;
  237. }
  238. atLineStart = false;
  239. i++;
  240. }
  241. return out.join('');
  242. }
  243. // ---------- C-style (JS/TS/Java/C#/Swift) ----------
  244. function stripCStyle(src: string, allowSingleQuoteStrings: boolean): string {
  245. const out = src.split('');
  246. let i = 0;
  247. const n = src.length;
  248. while (i < n) {
  249. const c = src[i]!;
  250. const c2 = src[i + 1] ?? '';
  251. // Block comment
  252. if (c === '/' && c2 === '*') {
  253. const start = i;
  254. i += 2;
  255. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  256. if (i < n) i += 2;
  257. blankRange(out, start, i, src);
  258. continue;
  259. }
  260. // Line comment
  261. if (c === '/' && c2 === '/') {
  262. const start = i;
  263. while (i < n && src[i] !== '\n') i++;
  264. blankRange(out, start, i, src);
  265. continue;
  266. }
  267. // String literals
  268. if (c === '"' || (allowSingleQuoteStrings && c === "'") || c === '`') {
  269. const quote = c;
  270. i++;
  271. while (i < n && src[i] !== quote) {
  272. if (src[i] === '\\' && i + 1 < n) {
  273. i += 2;
  274. continue;
  275. }
  276. // Template literal can span lines; regular strings break on newline (treat as unterminated)
  277. if (quote !== '`' && src[i] === '\n') break;
  278. i++;
  279. }
  280. if (i < n && src[i] === quote) i++;
  281. continue;
  282. }
  283. i++;
  284. }
  285. return out.join('');
  286. }
  287. // ---------- PHP ----------
  288. function stripPhp(src: string): string {
  289. const out = src.split('');
  290. let i = 0;
  291. const n = src.length;
  292. while (i < n) {
  293. const c = src[i]!;
  294. const c2 = src[i + 1] ?? '';
  295. // Block comment
  296. if (c === '/' && c2 === '*') {
  297. const start = i;
  298. i += 2;
  299. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  300. if (i < n) i += 2;
  301. blankRange(out, start, i, src);
  302. continue;
  303. }
  304. // // line comment
  305. if (c === '/' && c2 === '/') {
  306. const start = i;
  307. while (i < n && src[i] !== '\n') i++;
  308. blankRange(out, start, i, src);
  309. continue;
  310. }
  311. // # line comment (PHP supports both)
  312. if (c === '#') {
  313. const start = i;
  314. while (i < n && src[i] !== '\n') i++;
  315. blankRange(out, start, i, src);
  316. continue;
  317. }
  318. // String literals: ', ", ` (PHP doesn't really use backticks for strings,
  319. // but it does have shell-exec backticks; treating as a string is fine here)
  320. if (c === '"' || c === "'" || c === '`') {
  321. const quote = c;
  322. i++;
  323. while (i < n && src[i] !== quote) {
  324. if (src[i] === '\\' && i + 1 < n) {
  325. i += 2;
  326. continue;
  327. }
  328. if (src[i] === '\n') break;
  329. i++;
  330. }
  331. if (i < n && src[i] === quote) i++;
  332. continue;
  333. }
  334. i++;
  335. }
  336. return out.join('');
  337. }
  338. // ---------- Go ----------
  339. function stripGo(src: string): string {
  340. const out = src.split('');
  341. let i = 0;
  342. const n = src.length;
  343. while (i < n) {
  344. const c = src[i]!;
  345. const c2 = src[i + 1] ?? '';
  346. // Block comment
  347. if (c === '/' && c2 === '*') {
  348. const start = i;
  349. i += 2;
  350. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  351. if (i < n) i += 2;
  352. blankRange(out, start, i, src);
  353. continue;
  354. }
  355. // Line comment
  356. if (c === '/' && c2 === '/') {
  357. const start = i;
  358. while (i < n && src[i] !== '\n') i++;
  359. blankRange(out, start, i, src);
  360. continue;
  361. }
  362. // Raw string with backticks (no escapes, can span lines)
  363. if (c === '`') {
  364. i++;
  365. while (i < n && src[i] !== '`') i++;
  366. if (i < n) i++;
  367. continue;
  368. }
  369. // Interpreted string with double quotes
  370. if (c === '"') {
  371. i++;
  372. while (i < n && src[i] !== '"') {
  373. if (src[i] === '\\' && i + 1 < n) {
  374. i += 2;
  375. continue;
  376. }
  377. if (src[i] === '\n') break;
  378. i++;
  379. }
  380. if (i < n && src[i] === '"') i++;
  381. continue;
  382. }
  383. // Rune literal with single quotes (handle as a tiny string)
  384. if (c === "'") {
  385. i++;
  386. while (i < n && src[i] !== "'") {
  387. if (src[i] === '\\' && i + 1 < n) {
  388. i += 2;
  389. continue;
  390. }
  391. if (src[i] === '\n') break;
  392. i++;
  393. }
  394. if (i < n && src[i] === "'") i++;
  395. continue;
  396. }
  397. i++;
  398. }
  399. return out.join('');
  400. }
  401. // ---------- Rust ----------
  402. function stripRust(src: string): string {
  403. const out = src.split('');
  404. let i = 0;
  405. const n = src.length;
  406. while (i < n) {
  407. const c = src[i]!;
  408. const c2 = src[i + 1] ?? '';
  409. // Nested block comment /* ... /* ... */ ... */
  410. if (c === '/' && c2 === '*') {
  411. const start = i;
  412. i += 2;
  413. let depth = 1;
  414. while (i < n && depth > 0) {
  415. if (src[i] === '/' && src[i + 1] === '*') {
  416. depth++;
  417. i += 2;
  418. } else if (src[i] === '*' && src[i + 1] === '/') {
  419. depth--;
  420. i += 2;
  421. } else {
  422. i++;
  423. }
  424. }
  425. blankRange(out, start, i, src);
  426. continue;
  427. }
  428. // Line comment
  429. if (c === '/' && c2 === '/') {
  430. const start = i;
  431. while (i < n && src[i] !== '\n') i++;
  432. blankRange(out, start, i, src);
  433. continue;
  434. }
  435. // String literals
  436. if (c === '"') {
  437. i++;
  438. while (i < n && src[i] !== '"') {
  439. if (src[i] === '\\' && i + 1 < n) {
  440. i += 2;
  441. continue;
  442. }
  443. i++;
  444. }
  445. if (i < n && src[i] === '"') i++;
  446. continue;
  447. }
  448. // Char literal — keep simple: skip 'x' or '\x'
  449. if (c === "'") {
  450. // Could be a lifetime, e.g. 'a, but those don't contain routing text
  451. i++;
  452. while (i < n && src[i] !== "'") {
  453. if (src[i] === '\\' && i + 1 < n) {
  454. i += 2;
  455. continue;
  456. }
  457. if (src[i] === '\n') break;
  458. i++;
  459. }
  460. if (i < n && src[i] === "'") i++;
  461. continue;
  462. }
  463. i++;
  464. }
  465. return out.join('');
  466. }
  467. // ---------- Erlang ----------
  468. /**
  469. * Erlang: `%` starts a line comment unless it sits inside a `"string"`, a
  470. * `'quoted atom'`, or is the character literal `$%`. Strings and quoted atoms
  471. * are left intact (a behaviour callback name can be a quoted atom); only the
  472. * comment text is blanked.
  473. */
  474. function stripErlang(src: string): string {
  475. const out = src.split('');
  476. let i = 0;
  477. const n = src.length;
  478. while (i < n) {
  479. const c = src[i];
  480. if (c === '"' || c === "'") {
  481. const quote = c;
  482. i++;
  483. while (i < n && src[i] !== quote) {
  484. if (src[i] === '\\' && i + 1 < n) {
  485. i += 2;
  486. continue;
  487. }
  488. i++;
  489. }
  490. if (i < n) i++;
  491. continue;
  492. }
  493. // Character literal: `$x`, `$\n`, `$%` — the next char (or escape) is data.
  494. if (c === '$') {
  495. i++;
  496. if (i < n && src[i] === '\\') i++;
  497. i++;
  498. continue;
  499. }
  500. if (c === '%') {
  501. let end = i;
  502. while (end < n && src[end] !== '\n') end++;
  503. blankRange(out, i, end, src);
  504. i = end;
  505. continue;
  506. }
  507. i++;
  508. }
  509. return out.join('');
  510. }