strip-comments.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. export type CommentLang =
  26. | 'python'
  27. | 'javascript'
  28. | 'typescript'
  29. | 'php'
  30. | 'ruby'
  31. | 'java'
  32. | 'csharp'
  33. | 'swift'
  34. | 'go'
  35. | 'rust';
  36. export function stripCommentsForRegex(content: string, lang: CommentLang): string {
  37. switch (lang) {
  38. case 'python':
  39. return stripPython(content);
  40. case 'ruby':
  41. return stripRuby(content);
  42. case 'rust':
  43. return stripRust(content);
  44. case 'php':
  45. return stripPhp(content);
  46. case 'go':
  47. return stripGo(content);
  48. case 'javascript':
  49. case 'typescript':
  50. case 'java':
  51. case 'csharp':
  52. case 'swift':
  53. return stripCStyle(content, /* allowSingleQuoteStrings */ lang === 'javascript' || lang === 'typescript');
  54. default:
  55. return content;
  56. }
  57. }
  58. /**
  59. * Replace every char in a slice with spaces, but keep newlines so line
  60. * numbers computed downstream remain valid.
  61. */
  62. function blankRange(buf: string[], start: number, end: number, src: string): void {
  63. for (let i = start; i < end; i++) {
  64. buf[i] = src[i] === '\n' ? '\n' : ' ';
  65. }
  66. }
  67. // ---------- Python ----------
  68. function stripPython(src: string): string {
  69. const out = src.split('');
  70. let i = 0;
  71. const n = src.length;
  72. while (i < n) {
  73. const c = src[i]!;
  74. const c2 = src[i + 1] ?? '';
  75. const c3 = src[i + 2] ?? '';
  76. // Triple-quoted string: """...""" or '''...'''
  77. if ((c === '"' || c === "'") && c2 === c && c3 === c) {
  78. const quote = c;
  79. const start = i;
  80. i += 3;
  81. while (i < n) {
  82. if (src[i] === '\\' && i + 1 < n) {
  83. i += 2;
  84. continue;
  85. }
  86. if (src[i] === quote && src[i + 1] === quote && src[i + 2] === quote) {
  87. i += 3;
  88. break;
  89. }
  90. i++;
  91. }
  92. blankRange(out, start, i, src);
  93. continue;
  94. }
  95. // Single-line string: '...' or "..."
  96. if (c === '"' || c === "'") {
  97. const quote = c;
  98. i++;
  99. while (i < n && src[i] !== quote) {
  100. if (src[i] === '\\' && i + 1 < n) {
  101. i += 2;
  102. continue;
  103. }
  104. if (src[i] === '\n') break; // unterminated
  105. i++;
  106. }
  107. if (i < n && src[i] === quote) i++;
  108. continue;
  109. }
  110. // Line comment
  111. if (c === '#') {
  112. const start = i;
  113. while (i < n && src[i] !== '\n') i++;
  114. blankRange(out, start, i, src);
  115. continue;
  116. }
  117. i++;
  118. }
  119. return out.join('');
  120. }
  121. // ---------- Ruby ----------
  122. function stripRuby(src: string): string {
  123. const out = src.split('');
  124. let i = 0;
  125. const n = src.length;
  126. let atLineStart = true;
  127. while (i < n) {
  128. const c = src[i]!;
  129. // =begin / =end block comments must be at start of line (after optional whitespace)
  130. if (atLineStart && c === '=' && src.startsWith('=begin', i)) {
  131. const start = i;
  132. // consume to matching =end at line start
  133. i += '=begin'.length;
  134. while (i < n) {
  135. if (src[i] === '\n') {
  136. // check next line for =end
  137. let j = i + 1;
  138. while (j < n && (src[j] === ' ' || src[j] === '\t')) j++;
  139. if (src.startsWith('=end', j)) {
  140. i = j + '=end'.length;
  141. // consume rest of that line
  142. while (i < n && src[i] !== '\n') i++;
  143. break;
  144. }
  145. }
  146. i++;
  147. }
  148. blankRange(out, start, i, src);
  149. atLineStart = i > 0 && src[i - 1] === '\n';
  150. continue;
  151. }
  152. // String literals
  153. if (c === '"' || c === "'") {
  154. const quote = c;
  155. i++;
  156. while (i < n && src[i] !== quote) {
  157. if (src[i] === '\\' && i + 1 < n) {
  158. i += 2;
  159. continue;
  160. }
  161. if (src[i] === '\n') break;
  162. i++;
  163. }
  164. if (i < n && src[i] === quote) i++;
  165. atLineStart = false;
  166. continue;
  167. }
  168. // Line comment
  169. if (c === '#') {
  170. const start = i;
  171. while (i < n && src[i] !== '\n') i++;
  172. blankRange(out, start, i, src);
  173. atLineStart = false;
  174. continue;
  175. }
  176. if (c === '\n') {
  177. atLineStart = true;
  178. i++;
  179. continue;
  180. }
  181. if (c === ' ' || c === '\t') {
  182. // whitespace doesn't change atLineStart
  183. i++;
  184. continue;
  185. }
  186. atLineStart = false;
  187. i++;
  188. }
  189. return out.join('');
  190. }
  191. // ---------- C-style (JS/TS/Java/C#/Swift) ----------
  192. function stripCStyle(src: string, allowSingleQuoteStrings: boolean): string {
  193. const out = src.split('');
  194. let i = 0;
  195. const n = src.length;
  196. while (i < n) {
  197. const c = src[i]!;
  198. const c2 = src[i + 1] ?? '';
  199. // Block comment
  200. if (c === '/' && c2 === '*') {
  201. const start = i;
  202. i += 2;
  203. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  204. if (i < n) i += 2;
  205. blankRange(out, start, i, src);
  206. continue;
  207. }
  208. // Line comment
  209. if (c === '/' && c2 === '/') {
  210. const start = i;
  211. while (i < n && src[i] !== '\n') i++;
  212. blankRange(out, start, i, src);
  213. continue;
  214. }
  215. // String literals
  216. if (c === '"' || (allowSingleQuoteStrings && c === "'") || c === '`') {
  217. const quote = c;
  218. i++;
  219. while (i < n && src[i] !== quote) {
  220. if (src[i] === '\\' && i + 1 < n) {
  221. i += 2;
  222. continue;
  223. }
  224. // Template literal can span lines; regular strings break on newline (treat as unterminated)
  225. if (quote !== '`' && src[i] === '\n') break;
  226. i++;
  227. }
  228. if (i < n && src[i] === quote) i++;
  229. continue;
  230. }
  231. i++;
  232. }
  233. return out.join('');
  234. }
  235. // ---------- PHP ----------
  236. function stripPhp(src: string): string {
  237. const out = src.split('');
  238. let i = 0;
  239. const n = src.length;
  240. while (i < n) {
  241. const c = src[i]!;
  242. const c2 = src[i + 1] ?? '';
  243. // Block comment
  244. if (c === '/' && c2 === '*') {
  245. const start = i;
  246. i += 2;
  247. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  248. if (i < n) i += 2;
  249. blankRange(out, start, i, src);
  250. continue;
  251. }
  252. // // line comment
  253. if (c === '/' && c2 === '/') {
  254. const start = i;
  255. while (i < n && src[i] !== '\n') i++;
  256. blankRange(out, start, i, src);
  257. continue;
  258. }
  259. // # line comment (PHP supports both)
  260. if (c === '#') {
  261. const start = i;
  262. while (i < n && src[i] !== '\n') i++;
  263. blankRange(out, start, i, src);
  264. continue;
  265. }
  266. // String literals: ', ", ` (PHP doesn't really use backticks for strings,
  267. // but it does have shell-exec backticks; treating as a string is fine here)
  268. if (c === '"' || 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. if (src[i] === '\n') break;
  277. i++;
  278. }
  279. if (i < n && src[i] === quote) i++;
  280. continue;
  281. }
  282. i++;
  283. }
  284. return out.join('');
  285. }
  286. // ---------- Go ----------
  287. function stripGo(src: string): string {
  288. const out = src.split('');
  289. let i = 0;
  290. const n = src.length;
  291. while (i < n) {
  292. const c = src[i]!;
  293. const c2 = src[i + 1] ?? '';
  294. // Block comment
  295. if (c === '/' && c2 === '*') {
  296. const start = i;
  297. i += 2;
  298. while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++;
  299. if (i < n) i += 2;
  300. blankRange(out, start, i, src);
  301. continue;
  302. }
  303. // Line comment
  304. if (c === '/' && c2 === '/') {
  305. const start = i;
  306. while (i < n && src[i] !== '\n') i++;
  307. blankRange(out, start, i, src);
  308. continue;
  309. }
  310. // Raw string with backticks (no escapes, can span lines)
  311. if (c === '`') {
  312. i++;
  313. while (i < n && src[i] !== '`') i++;
  314. if (i < n) i++;
  315. continue;
  316. }
  317. // Interpreted string with double quotes
  318. if (c === '"') {
  319. i++;
  320. while (i < n && src[i] !== '"') {
  321. if (src[i] === '\\' && i + 1 < n) {
  322. i += 2;
  323. continue;
  324. }
  325. if (src[i] === '\n') break;
  326. i++;
  327. }
  328. if (i < n && src[i] === '"') i++;
  329. continue;
  330. }
  331. // Rune literal with single quotes (handle as a tiny string)
  332. if (c === "'") {
  333. i++;
  334. while (i < n && src[i] !== "'") {
  335. if (src[i] === '\\' && i + 1 < n) {
  336. i += 2;
  337. continue;
  338. }
  339. if (src[i] === '\n') break;
  340. i++;
  341. }
  342. if (i < n && src[i] === "'") i++;
  343. continue;
  344. }
  345. i++;
  346. }
  347. return out.join('');
  348. }
  349. // ---------- Rust ----------
  350. function stripRust(src: string): string {
  351. const out = src.split('');
  352. let i = 0;
  353. const n = src.length;
  354. while (i < n) {
  355. const c = src[i]!;
  356. const c2 = src[i + 1] ?? '';
  357. // Nested block comment /* ... /* ... */ ... */
  358. if (c === '/' && c2 === '*') {
  359. const start = i;
  360. i += 2;
  361. let depth = 1;
  362. while (i < n && depth > 0) {
  363. if (src[i] === '/' && src[i + 1] === '*') {
  364. depth++;
  365. i += 2;
  366. } else if (src[i] === '*' && src[i + 1] === '/') {
  367. depth--;
  368. i += 2;
  369. } else {
  370. i++;
  371. }
  372. }
  373. blankRange(out, start, i, src);
  374. continue;
  375. }
  376. // Line comment
  377. if (c === '/' && c2 === '/') {
  378. const start = i;
  379. while (i < n && src[i] !== '\n') i++;
  380. blankRange(out, start, i, src);
  381. continue;
  382. }
  383. // String literals
  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. i++;
  392. }
  393. if (i < n && src[i] === '"') i++;
  394. continue;
  395. }
  396. // Char literal — keep simple: skip 'x' or '\x'
  397. if (c === "'") {
  398. // Could be a lifetime, e.g. 'a, but those don't contain routing text
  399. i++;
  400. while (i < n && src[i] !== "'") {
  401. if (src[i] === '\\' && i + 1 < n) {
  402. i += 2;
  403. continue;
  404. }
  405. if (src[i] === '\n') break;
  406. i++;
  407. }
  408. if (i < n && src[i] === "'") i++;
  409. continue;
  410. }
  411. i++;
  412. }
  413. return out.join('');
  414. }