hullproof 0.0.0-stage → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (363) hide show
  1. package/LICENSE +222 -0
  2. package/NOTICE +32 -0
  3. package/README.md +313 -2
  4. package/checks/db.firebase-rules-not-open.json +24 -0
  5. package/checks/db.rls-every-table.json +25 -0
  6. package/checks/log.no-credential-in-logs.json +30 -0
  7. package/checks/secrets.no-env-file-committed.json +31 -0
  8. package/checks/secrets.no-secret-in-public-env.json +37 -0
  9. package/checks/storage.firebase-rules-not-open.json +22 -0
  10. package/checks/supply.no-untrusted-event-in-run.json +23 -0
  11. package/checks/web.cors-no-reflected-origin.json +31 -0
  12. package/detectors/ai.json +25 -0
  13. package/detectors/auth.json +27 -0
  14. package/detectors/ci.json +19 -0
  15. package/detectors/containers.json +8 -0
  16. package/detectors/data_stores.json +38 -0
  17. package/detectors/frameworks.json +94 -0
  18. package/detectors/hosting.json +44 -0
  19. package/detectors/iac.json +43 -0
  20. package/detectors/languages.json +48 -0
  21. package/detectors/mobile.json +22 -0
  22. package/detectors/package_managers.json +39 -0
  23. package/detectors/payments.json +19 -0
  24. package/detectors/runtimes.json +17 -0
  25. package/dist/checks/code/admin-role.js +46 -0
  26. package/dist/checks/code/owner-filter.js +91 -0
  27. package/dist/checks/code/shared.js +84 -0
  28. package/dist/checks/code/webhook-verify.js +77 -0
  29. package/dist/checks/context.js +64 -0
  30. package/dist/checks/map/config-checks.js +74 -0
  31. package/dist/checks/map/entry-checks.js +97 -0
  32. package/dist/checks/map/shared.js +44 -0
  33. package/dist/checks/ops.js +307 -0
  34. package/dist/checks/pro-map.js +1 -0
  35. package/dist/checks/run.js +92 -0
  36. package/dist/checks/types.js +1 -0
  37. package/dist/ci/comment.js +44 -0
  38. package/dist/ci/sarif-check.js +157 -0
  39. package/dist/ci/sarif.js +61 -0
  40. package/dist/cli.js +358 -0
  41. package/dist/commands/ci.js +115 -0
  42. package/dist/commands/git.js +58 -0
  43. package/dist/commands/hooks.js +30 -0
  44. package/dist/commands/hunt-pro.js +1 -0
  45. package/dist/commands/hunt.js +86 -0
  46. package/dist/commands/map.js +33 -0
  47. package/dist/commands/mcp.js +17 -0
  48. package/dist/commands/pack.js +48 -0
  49. package/dist/commands/prove-pro.js +1 -0
  50. package/dist/commands/prove.js +173 -0
  51. package/dist/commands/report-gate.js +92 -0
  52. package/dist/commands/scan.js +158 -0
  53. package/dist/confirm/agreement.js +18 -0
  54. package/dist/confirm/chain.js +84 -0
  55. package/dist/confirm/confirm.js +112 -0
  56. package/dist/confirm/honesty.js +75 -0
  57. package/dist/confirm/lead.js +92 -0
  58. package/dist/confirm/merge.js +88 -0
  59. package/dist/confirm/query.js +145 -0
  60. package/dist/confirm/rule.js +86 -0
  61. package/dist/confirm/store.js +42 -0
  62. package/dist/confirm/summary.js +6 -0
  63. package/dist/confirm/synthesis.js +28 -0
  64. package/dist/discover/detectors.js +50 -0
  65. package/dist/discover/discover.js +270 -0
  66. package/dist/discover/extract.js +282 -0
  67. package/dist/discover/git.js +40 -0
  68. package/dist/discover/kit-files.js +47 -0
  69. package/dist/discover/walk.js +110 -0
  70. package/dist/env.js +57 -0
  71. package/dist/gate/gate.js +173 -0
  72. package/dist/hooks/install.js +153 -0
  73. package/dist/hooks/run.js +112 -0
  74. package/dist/map/analyze.js +201 -0
  75. package/dist/map/build.js +253 -0
  76. package/dist/map/candidates.js +49 -0
  77. package/dist/map/cblocks.js +77 -0
  78. package/dist/map/config.js +291 -0
  79. package/dist/map/dotnet.js +242 -0
  80. package/dist/map/entry.js +78 -0
  81. package/dist/map/expo.js +190 -0
  82. package/dist/map/go.js +292 -0
  83. package/dist/map/js-express.js +143 -0
  84. package/dist/map/js-misc.js +295 -0
  85. package/dist/map/js-next.js +292 -0
  86. package/dist/map/laravel.js +389 -0
  87. package/dist/map/lex-more.js +305 -0
  88. package/dist/map/lex.js +425 -0
  89. package/dist/map/light-index.js +191 -0
  90. package/dist/map/light-profiles.js +324 -0
  91. package/dist/map/light.js +574 -0
  92. package/dist/map/local-vocab.js +27 -0
  93. package/dist/map/mobile.js +326 -0
  94. package/dist/map/py-django.js +281 -0
  95. package/dist/map/py.js +391 -0
  96. package/dist/map/rails.js +391 -0
  97. package/dist/map/repo.js +178 -0
  98. package/dist/map/rust.js +312 -0
  99. package/dist/map/spring.js +207 -0
  100. package/dist/map/taint.js +96 -0
  101. package/dist/map/touches.js +355 -0
  102. package/dist/map/vocab.js +88 -0
  103. package/dist/mapping/controls.js +34 -0
  104. package/dist/mapping/dependency-policy.js +44 -0
  105. package/dist/mapping/map.js +44 -0
  106. package/dist/mapping/maps/gitleaks.json +12 -0
  107. package/dist/mapping/maps/osv-scanner.json +7 -0
  108. package/dist/mapping/maps/trivy.json +13 -0
  109. package/dist/mcp/confine.js +59 -0
  110. package/dist/mcp/protocol.js +140 -0
  111. package/dist/mcp/schema.js +43 -0
  112. package/dist/mcp/server.js +37 -0
  113. package/dist/mcp/tools.js +238 -0
  114. package/dist/packs/coverage.js +79 -0
  115. package/dist/packs/manifest.js +36 -0
  116. package/dist/packs/validate.js +271 -0
  117. package/dist/prove/container.js +276 -0
  118. package/dist/prove/exec-main.js +20 -0
  119. package/dist/prove/executor.js +149 -0
  120. package/dist/prove/local.js +131 -0
  121. package/dist/prove/probes.js +65 -0
  122. package/dist/prove/recipe.js +278 -0
  123. package/dist/prove/run.js +136 -0
  124. package/dist/prove/runner.js +1 -0
  125. package/dist/prove/safety.js +126 -0
  126. package/dist/prove/spec.js +357 -0
  127. package/dist/prove/store.js +65 -0
  128. package/dist/report/build.js +297 -0
  129. package/dist/report/entry-points.js +75 -0
  130. package/dist/report/hunt-section.js +71 -0
  131. package/dist/report/kit.js +41 -0
  132. package/dist/report/load.js +35 -0
  133. package/dist/report/messages.js +273 -0
  134. package/dist/report/prove-section.js +65 -0
  135. package/dist/report/report.js +156 -0
  136. package/dist/report/surface.js +21 -0
  137. package/dist/report/test-code.js +51 -0
  138. package/dist/report/tiers.js +44 -0
  139. package/dist/scanners/common.js +162 -0
  140. package/dist/scanners/gitleaks.js +90 -0
  141. package/dist/scanners/index.js +113 -0
  142. package/dist/scanners/osv-scanner.js +77 -0
  143. package/dist/scanners/run-targets.js +34 -0
  144. package/dist/scanners/secret-noise.js +89 -0
  145. package/dist/scanners/semgrep.js +142 -0
  146. package/dist/scanners/trivy.js +65 -0
  147. package/dist/scanners/types.js +1 -0
  148. package/dist/types/check.js +1 -0
  149. package/dist/types/codemap.js +1 -0
  150. package/dist/types/findings.js +5 -0
  151. package/dist/types/hunt.js +1 -0
  152. package/dist/types/index.js +1 -0
  153. package/dist/types/prove.js +1 -0
  154. package/dist/types/results.js +2 -0
  155. package/dist/types/scan.js +1 -0
  156. package/dist/types/stack.js +5 -0
  157. package/dist/util.js +71 -0
  158. package/dist/version.js +2 -0
  159. package/package.json +46 -4
  160. package/packs/android/fixture-result.json +43 -0
  161. package/packs/android/pack.json +56 -0
  162. package/packs/dotnet/fixture-result.json +62 -0
  163. package/packs/dotnet/pack.json +88 -0
  164. package/packs/expo/answer-key.json +51 -0
  165. package/packs/expo/fixture-result.json +62 -0
  166. package/packs/expo/pack.json +79 -0
  167. package/packs/firebase/answer-key.json +27 -0
  168. package/packs/firebase/fixture-result.json +43 -0
  169. package/packs/firebase/pack.json +58 -0
  170. package/packs/go/answer-key.json +101 -0
  171. package/packs/go/fixture-result.json +64 -0
  172. package/packs/go/pack.json +99 -0
  173. package/packs/ios/fixture-result.json +43 -0
  174. package/packs/ios/pack.json +55 -0
  175. package/packs/laravel/answer-key.json +84 -0
  176. package/packs/laravel/fixture-result.json +60 -0
  177. package/packs/laravel/pack.json +91 -0
  178. package/packs/node-ts/answer-key.json +220 -0
  179. package/packs/node-ts/fixture-result.json +114 -0
  180. package/packs/node-ts/pack.json +134 -0
  181. package/packs/python/answer-key.json +195 -0
  182. package/packs/python/fixture-result.json +119 -0
  183. package/packs/python/pack.json +121 -0
  184. package/packs/rails/answer-key.json +84 -0
  185. package/packs/rails/fixture-result.json +60 -0
  186. package/packs/rails/pack.json +78 -0
  187. package/packs/rust/fixture-result.json +43 -0
  188. package/packs/rust/pack.json +69 -0
  189. package/packs/spring/fixture-result.json +63 -0
  190. package/packs/spring/pack.json +103 -0
  191. package/packs/supabase/answer-key.json +52 -0
  192. package/packs/supabase/fixture-result.json +58 -0
  193. package/packs/supabase/pack.json +60 -0
  194. package/rules/gitleaks.toml +256 -0
  195. package/rules/semgrep/api-route-no-auth.yaml +31 -0
  196. package/rules/semgrep/cors-reflect-origin.yaml +48 -0
  197. package/rules/semgrep/eval-or-new-function.yaml +185 -0
  198. package/rules/semgrep/log-session-token.yaml +48 -0
  199. package/rules/semgrep/math-random-tokens.yaml +24 -0
  200. package/rules/semgrep/next-public-secret-name.yaml +32 -0
  201. package/rules/semgrep/postgrest-filter-injection.yaml +78 -0
  202. package/rules/semgrep/raw-sql-concat.yaml +42 -0
  203. package/rules/semgrep/read-by-id-no-owner.yaml +49 -0
  204. package/rules/semgrep/server-action-no-auth.yaml +26 -0
  205. package/rules/semgrep/sql-variants.yaml +99 -0
  206. package/rules/semgrep/ssrf-unguarded-url.yaml +137 -0
  207. package/rules/semgrep/subprocess-injection.yaml +163 -0
  208. package/rules/semgrep/taint-go.yaml +333 -0
  209. package/rules/semgrep/taint-java.yaml +317 -0
  210. package/rules/semgrep/taint-javascript.yaml +412 -0
  211. package/rules/semgrep/taint-php.yaml +361 -0
  212. package/rules/semgrep/taint-python.yaml +414 -0
  213. package/rules/semgrep/tool-endpoint-admin-client.yaml +86 -0
  214. package/rules/semgrep/user-metadata-authz.yaml +22 -0
  215. package/rules/semgrep/verify-result-unused.yaml +62 -0
  216. package/rules/semgrep/webhook-parse-before-verify.yaml +29 -0
  217. package/rules/test-artifact-ids.json +4 -0
  218. package/rules/tiers.json +427 -0
  219. package/schemas/check-results.schema.json +55 -0
  220. package/schemas/check.schema.json +41 -0
  221. package/schemas/codemap.schema.json +216 -0
  222. package/schemas/findings.schema.json +1048 -0
  223. package/schemas/hunt-output.schema.json +46 -0
  224. package/schemas/hunt-result.schema.json +58 -0
  225. package/schemas/results.schema.json +17 -0
  226. package/schemas/scan-hits.schema.json +32 -0
  227. package/schemas/scan-meta.schema.json +25 -0
  228. package/schemas/stack.schema.json +93 -0
  229. package/src/checks/README.md +32 -0
  230. package/src/checks/code/admin-role.ts +49 -0
  231. package/src/checks/code/owner-filter.ts +95 -0
  232. package/src/checks/code/shared.ts +66 -0
  233. package/src/checks/code/webhook-verify.ts +75 -0
  234. package/src/checks/context.ts +72 -0
  235. package/src/checks/map/config-checks.ts +79 -0
  236. package/src/checks/map/entry-checks.ts +116 -0
  237. package/src/checks/map/shared.ts +63 -0
  238. package/src/checks/ops.ts +280 -0
  239. package/src/checks/pro-map.ts +4 -0
  240. package/src/checks/run.ts +80 -0
  241. package/src/checks/types.ts +12 -0
  242. package/src/ci/comment.ts +52 -0
  243. package/src/ci/sarif-check.ts +100 -0
  244. package/src/ci/sarif.ts +72 -0
  245. package/src/cli.ts +273 -0
  246. package/src/commands/ci.ts +104 -0
  247. package/src/commands/git.ts +59 -0
  248. package/src/commands/hooks.ts +27 -0
  249. package/src/commands/hunt-pro.ts +4 -0
  250. package/src/commands/hunt.ts +68 -0
  251. package/src/commands/map.ts +27 -0
  252. package/src/commands/mcp.ts +18 -0
  253. package/src/commands/pack.ts +34 -0
  254. package/src/commands/prove-pro.ts +4 -0
  255. package/src/commands/prove.ts +126 -0
  256. package/src/commands/report-gate.ts +75 -0
  257. package/src/commands/scan.ts +156 -0
  258. package/src/confirm/agreement.ts +23 -0
  259. package/src/confirm/chain.ts +67 -0
  260. package/src/confirm/confirm.ts +107 -0
  261. package/src/confirm/honesty.ts +65 -0
  262. package/src/confirm/lead.ts +76 -0
  263. package/src/confirm/merge.ts +89 -0
  264. package/src/confirm/query.ts +126 -0
  265. package/src/confirm/rule.ts +76 -0
  266. package/src/confirm/store.ts +42 -0
  267. package/src/confirm/summary.ts +9 -0
  268. package/src/confirm/synthesis.ts +33 -0
  269. package/src/discover/detectors.ts +71 -0
  270. package/src/discover/discover.ts +253 -0
  271. package/src/discover/extract.ts +235 -0
  272. package/src/discover/git.ts +31 -0
  273. package/src/discover/kit-files.ts +37 -0
  274. package/src/discover/walk.ts +88 -0
  275. package/src/env.ts +51 -0
  276. package/src/gate/gate.ts +169 -0
  277. package/src/hooks/install.ts +142 -0
  278. package/src/hooks/run.ts +121 -0
  279. package/src/map/analyze.ts +219 -0
  280. package/src/map/build.ts +206 -0
  281. package/src/map/candidates.ts +47 -0
  282. package/src/map/cblocks.ts +55 -0
  283. package/src/map/config.ts +207 -0
  284. package/src/map/dotnet.ts +205 -0
  285. package/src/map/entry.ts +80 -0
  286. package/src/map/expo.ts +171 -0
  287. package/src/map/go.ts +271 -0
  288. package/src/map/js-express.ts +135 -0
  289. package/src/map/js-misc.ts +221 -0
  290. package/src/map/js-next.ts +282 -0
  291. package/src/map/laravel.ts +327 -0
  292. package/src/map/lex-more.ts +152 -0
  293. package/src/map/lex.ts +274 -0
  294. package/src/map/light-index.ts +134 -0
  295. package/src/map/light-profiles.ts +363 -0
  296. package/src/map/light.ts +484 -0
  297. package/src/map/local-vocab.ts +30 -0
  298. package/src/map/mobile.ts +263 -0
  299. package/src/map/py-django.ts +239 -0
  300. package/src/map/py.ts +329 -0
  301. package/src/map/rails.ts +291 -0
  302. package/src/map/repo.ts +150 -0
  303. package/src/map/rust.ts +256 -0
  304. package/src/map/spring.ts +185 -0
  305. package/src/map/taint.ts +86 -0
  306. package/src/map/touches.ts +296 -0
  307. package/src/map/vocab.ts +106 -0
  308. package/src/mapping/controls.ts +47 -0
  309. package/src/mapping/dependency-policy.ts +55 -0
  310. package/src/mapping/map.ts +59 -0
  311. package/src/mapping/maps/gitleaks.json +12 -0
  312. package/src/mapping/maps/osv-scanner.json +7 -0
  313. package/src/mapping/maps/trivy.json +13 -0
  314. package/src/mcp/confine.ts +44 -0
  315. package/src/mcp/protocol.ts +144 -0
  316. package/src/mcp/schema.ts +35 -0
  317. package/src/mcp/server.ts +32 -0
  318. package/src/mcp/tools.ts +235 -0
  319. package/src/packs/coverage.ts +98 -0
  320. package/src/packs/manifest.ts +97 -0
  321. package/src/packs/validate.ts +204 -0
  322. package/src/prove/container.ts +237 -0
  323. package/src/prove/exec-main.ts +16 -0
  324. package/src/prove/executor.ts +130 -0
  325. package/src/prove/local.ts +100 -0
  326. package/src/prove/probes.ts +68 -0
  327. package/src/prove/recipe.ts +215 -0
  328. package/src/prove/run.ts +124 -0
  329. package/src/prove/runner.ts +28 -0
  330. package/src/prove/safety.ts +98 -0
  331. package/src/prove/spec.ts +256 -0
  332. package/src/prove/store.ts +48 -0
  333. package/src/report/build.ts +304 -0
  334. package/src/report/entry-points.ts +78 -0
  335. package/src/report/hunt-section.ts +67 -0
  336. package/src/report/kit.ts +59 -0
  337. package/src/report/load.ts +49 -0
  338. package/src/report/messages.ts +270 -0
  339. package/src/report/prove-section.ts +61 -0
  340. package/src/report/report.ts +160 -0
  341. package/src/report/surface.ts +32 -0
  342. package/src/report/test-code.ts +56 -0
  343. package/src/report/tiers.ts +50 -0
  344. package/src/scanners/common.ts +149 -0
  345. package/src/scanners/gitleaks.ts +89 -0
  346. package/src/scanners/index.ts +103 -0
  347. package/src/scanners/osv-scanner.ts +90 -0
  348. package/src/scanners/run-targets.ts +35 -0
  349. package/src/scanners/secret-noise.ts +80 -0
  350. package/src/scanners/semgrep.ts +139 -0
  351. package/src/scanners/trivy.ts +66 -0
  352. package/src/scanners/types.ts +98 -0
  353. package/src/types/check.ts +52 -0
  354. package/src/types/codemap.ts +142 -0
  355. package/src/types/findings.ts +107 -0
  356. package/src/types/hunt.ts +97 -0
  357. package/src/types/index.ts +10 -0
  358. package/src/types/prove.ts +83 -0
  359. package/src/types/results.ts +15 -0
  360. package/src/types/scan.ts +63 -0
  361. package/src/types/stack.ts +61 -0
  362. package/src/util.ts +59 -0
  363. package/src/version.ts +2 -0
@@ -0,0 +1,391 @@
1
+ import { empty, joinPath, makeEntry } from './entry.js';
2
+ import { analyzeLight } from './light.js';
3
+ const CRUD = [
4
+ ['index', 'GET', '', false], ['create', 'POST', '', false], ['new', 'GET', '/new', false], ['show', 'GET', '/:id', true], ['edit', 'GET', '/:id/edit', true],
5
+ ['update', 'PATCH', '/:id', true], ['update', 'PUT', '/:id', true], ['destroy', 'DELETE', '/:id', true],
6
+ ];
7
+ const sym = (s) => (/^%[iw]/.test(s) ? s.replace(/^%[iw]\[|\]$/g, '').split(/\s+/).filter(Boolean) : [...s.matchAll(/:(\w+)|['"](\w+)['"]/g)].map((m) => m[1] ?? m[2]));
8
+ const listOpt = (line, key) => {
9
+ const m = new RegExp(`\\b${key}:\\s*(%[iw]\\[[^\\]]*\\]|\\[[^\\]]*\\]|:\\w+|['"]\\w+['"])`).exec(line);
10
+ return m ? sym(m[1]) : null;
11
+ };
12
+ const strOpt = (line, key) => new RegExp(`\\b${key}:\\s*(?::(\\w+)|['"]([^'"]*)['"])`).exec(line)?.slice(1).find((x) => x !== undefined) ?? null;
13
+ const singular = (s) => (/ies$/.test(s) ? s.replace(/ies$/, 'y') : /ses$/.test(s) ? s.replace(/es$/, '') : s.replace(/s$/, ''));
14
+ export function railsFiles(repo) {
15
+ return {
16
+ routes: repo.filesOf('rb').filter((f) => /(?:^|\/)config\/routes(?:\/[^/]+)?\.rb$/.test(f)),
17
+ controllers: repo.filesOf('rb').filter((f) => /(?:^|\/)app\/controllers\/.*\.rb$|(?:^|\/)app\/controllers\/concerns\/.*\.rb$/.test(f)),
18
+ };
19
+ }
20
+ function parseRoutes(v) {
21
+ const out = [];
22
+ const lines = v.masked.split('\n');
23
+ const orig = v.text.split('\n');
24
+ const stack = [{ kind: 'root', path: '', module: '', resource: null, auth: [], controller: null, only: null, except: null }];
25
+ const top = () => stack[stack.length - 1];
26
+ const authFacts = (ln, text) => (/\b(?:authenticate|authenticated)\b/.test(text) ? [{ kind: 'session_read', scope: 'router', file: v.file, line: ln, via: 'route authenticate block', enforced: true, confidence: 'high' }] : []);
27
+ const roleFacts = (ln, text) => (/\b(?:admin|role|staff|superuser|can\?)/i.test(text) ? [{ kind: 'role_check', scope: 'router', file: v.file, line: ln, via: 'route constraint', enforced: true, confidence: 'medium' }] : []);
28
+ const push = (verb, path, controller, action, ln, f, mount) => {
29
+ out.push({ verb, path: joinPath(f.path, path), controller, action, line: ln, auth: stack.flatMap((s) => s.auth), decoLine: ln, mount });
30
+ };
31
+ const ctrlOf = (f, name) => (name.includes('/') ? name : `${f.module}${name}`);
32
+ const nestedPrefix = (f, member) => {
33
+ const r = f.resource;
34
+ if (!r)
35
+ return '';
36
+ return `/${r.name}${r.singular || !member ? '' : '/:id'}`;
37
+ };
38
+ for (let i = 0; i < lines.length; i++) {
39
+ const raw = lines[i];
40
+ const l = raw.trim();
41
+ if (!l)
42
+ continue;
43
+ const ln = i + 1;
44
+ const o = orig[i] ?? '';
45
+ const f = top();
46
+ // Multi line route calls: join following lines while the line ends with a comma.
47
+ let full = o.trim();
48
+ let j = i;
49
+ while (/,\s*$/.test(lines[j]) && j + 1 < lines.length && j < i + 6) {
50
+ j++;
51
+ full += ' ' + (orig[j] ?? '').trim();
52
+ }
53
+ const opens = /\bdo(?:\s*\|[^|]*\|)?\s*$/.test(lines[j].trim());
54
+ const blockHead = (kind, fr) => { stack.push({ ...f, kind, auth: [], only: null, except: null, ...fr }); };
55
+ if (/^end\b/.test(l)) {
56
+ if (stack.length > 1)
57
+ stack.pop();
58
+ continue;
59
+ }
60
+ if (/^(?:if|unless|case|begin|while)\b/.test(l) && !/\bend\b/.test(l)) {
61
+ stack.push({ ...f, kind: 'other', auth: [] });
62
+ continue;
63
+ }
64
+ if (/^else\b|^elsif\b|^when\b/.test(l))
65
+ continue;
66
+ let m;
67
+ if ((m = /^namespace\s+(?::(\w+)|['"]([\w/]+)['"])/.exec(full))) {
68
+ const n = m[1] ?? m[2];
69
+ const p = strOpt(full, 'path');
70
+ if (opens)
71
+ blockHead('namespace', { path: joinPath(f.path, p ?? n), module: `${f.module}${n}/` });
72
+ i = j;
73
+ continue;
74
+ }
75
+ if ((m = /^scope\b(.*)$/.exec(full))) {
76
+ const rest = m[1];
77
+ const pos = /^\s*\(?\s*(?::(\w+)|['"]([^'"]*)['"])/.exec(rest);
78
+ const p = strOpt(rest, 'path') ?? (pos ? pos[1] ?? pos[2] : null);
79
+ const mod = strOpt(rest, 'module');
80
+ if (opens)
81
+ blockHead('scope', { path: p ? joinPath(f.path, p) : f.path, module: mod ? `${f.module}${mod}/` : f.module });
82
+ i = j;
83
+ continue;
84
+ }
85
+ if ((m = /^(authenticate|authenticated)\b(.*)$/.exec(full))) {
86
+ if (opens)
87
+ blockHead('authenticate', { auth: authFacts(ln, full) });
88
+ i = j;
89
+ continue;
90
+ }
91
+ if ((m = /^constraints\b(.*)$/.exec(full))) {
92
+ if (opens)
93
+ blockHead('constraints', { auth: [...authFacts(ln, full), ...roleFacts(ln, full)] });
94
+ i = j;
95
+ continue;
96
+ }
97
+ if ((m = /^controller\s+(?::(\w+)|['"]([\w/]+)['"])/.exec(full))) {
98
+ if (opens)
99
+ blockHead('controller', { controller: m[1] ?? m[2] });
100
+ i = j;
101
+ continue;
102
+ }
103
+ if ((m = /^(member|collection)\s+do\b/.exec(full))) {
104
+ blockHead(m[1], {});
105
+ continue;
106
+ }
107
+ if (/^(?:concern|devise_scope|defaults|with_options|resources_for_api|direct|draw)\b/.test(l) && opens) {
108
+ stack.push({ ...f, kind: 'concern', auth: [], controller: '\u0000skip' });
109
+ i = j;
110
+ continue;
111
+ }
112
+ if ((m = /^(resources|resource)\s+(.*)$/.exec(full))) {
113
+ const singularRes = m[1] === 'resource';
114
+ const names = [...m[2].split(/\b(?:only|except|controller|path|param|as|module|shallow|constraints|defaults|format)\s*:/)[0].matchAll(/:(\w+)/g)].map((x) => x[1]);
115
+ const only = listOpt(m[2], 'only');
116
+ const except = listOpt(m[2], 'except');
117
+ const ctrlOpt = strOpt(m[2], 'controller');
118
+ const pathOpt = strOpt(m[2], 'path');
119
+ for (const name of names) {
120
+ const base = f.resource ? `${nestedPrefix(f, true).replace(/:id$/, `:${singular(f.resource.name)}_id`)}` : '';
121
+ const urlName = pathOpt ?? name;
122
+ const controller = ctrlOf(f, ctrlOpt ?? name);
123
+ const acts = singularRes ? CRUD.filter(([a]) => a !== 'index') : CRUD;
124
+ for (const [act, verb, suffix, member] of acts) {
125
+ if (only && !only.includes(act))
126
+ continue;
127
+ if (except && except.includes(act))
128
+ continue;
129
+ const sfx = singularRes ? suffix.replace(/^\/:id/, '') : suffix;
130
+ if (singularRes && act === 'index')
131
+ continue;
132
+ push(verb, `${base}/${urlName}${sfx}`, controller, act, ln, f);
133
+ }
134
+ if (opens)
135
+ stack.push({ ...f, kind: 'resources', path: f.path, resource: { name: urlName, singular: singularRes }, auth: [], controller: controller });
136
+ }
137
+ if (opens && !names.length)
138
+ stack.push({ ...f, kind: 'other', auth: [] });
139
+ i = j;
140
+ continue;
141
+ }
142
+ if ((m = /^(get|post|put|patch|delete|match)\s+(.*)$/.exec(full)) && !/^(?:get|post|put|patch|delete|match)\s*=/.test(full)) {
143
+ const verbWord = m[1];
144
+ const rest = m[2];
145
+ const first = /^\(?\s*(?::(\w+)|['"]([^'"]*)['"])/.exec(rest);
146
+ let target = strOpt(rest, 'to') ?? /=>\s*['"]([\w/]+#\w+)['"]/.exec(rest)?.[1] ?? null;
147
+ if (/\bto:\s*(?:redirect|->|lambda|proc)\b|=>\s*redirect\b/.test(rest) || /\bto:\s*[A-Z]/.test(rest)) {
148
+ if (opens)
149
+ stack.push({ ...f, kind: 'other', auth: [] });
150
+ i = j;
151
+ continue;
152
+ }
153
+ let action = strOpt(rest, 'action');
154
+ let controller = null;
155
+ if (target && target.includes('#')) {
156
+ const [c, a] = target.split('#');
157
+ controller = ctrlOf(f, c);
158
+ action = a;
159
+ }
160
+ else if (!target && first && /=>\s*['"]?[\w/]+#\w+/.test(rest)) {
161
+ const t = /=>\s*['"]?([\w/]+)#(\w+)/.exec(rest);
162
+ controller = ctrlOf(f, t[1]);
163
+ action = t[2];
164
+ }
165
+ let path = first ? (first[1] ?? first[2]) : null;
166
+ if (!controller) {
167
+ const ctrlName = f.controller && f.controller !== '\u0000skip' ? f.controller : null;
168
+ if (ctrlName)
169
+ controller = ctrlOf(f, ctrlName);
170
+ if (!action && path)
171
+ action = path.replace(/^\//, '').split('/')[0].replace(/\W+/g, '_');
172
+ if (!controller && /^['"][\w/]+#\w+['"]$/.test(rest.split(',')[0].trim())) {
173
+ const t = /['"]([\w/]+)#(\w+)['"]/.exec(rest);
174
+ controller = ctrlOf(f, t[1]);
175
+ action = t[2];
176
+ }
177
+ }
178
+ if (f.controller === '\u0000skip') {
179
+ if (opens)
180
+ stack.push({ ...f, kind: 'other', auth: [] });
181
+ i = j;
182
+ continue;
183
+ }
184
+ if (!controller || !action || path === null) {
185
+ if (opens)
186
+ stack.push({ ...f, kind: 'other', auth: [] });
187
+ i = j;
188
+ continue;
189
+ }
190
+ const on = strOpt(rest, 'on');
191
+ const inMember = f.kind === 'member' || on === 'member';
192
+ const inCollection = f.kind === 'collection' || on === 'collection';
193
+ let prefix = '';
194
+ if (f.resource && (inMember || inCollection))
195
+ prefix = nestedPrefix(f, inMember);
196
+ else if (f.resource && f.kind === 'resources')
197
+ prefix = nestedPrefix(f, true).replace(/:id$/, `:${singular(f.resource.name)}_id`);
198
+ const verbs = verbWord === 'match' ? (listOpt(rest, 'via') ?? ['any']).map((x) => (x === 'all' || x === 'any' ? 'ANY' : x.toUpperCase())) : [verbWord.toUpperCase()];
199
+ for (const verb of verbs)
200
+ push(verb === 'ANY' ? 'ANY' : verb, `${prefix}/${path.replace(/^\//, '')}`.replace(/\(\.:format\)/g, ''), controller, action, ln, f);
201
+ if (opens)
202
+ stack.push({ ...f, kind: 'other', auth: [] });
203
+ i = j;
204
+ continue;
205
+ }
206
+ if ((m = /^root\s+(.*)$/.exec(full))) {
207
+ const t = /['"]([\w/]+)#(\w+)['"]/.exec(m[1]);
208
+ if (t)
209
+ push('GET', '/', ctrlOf(f, t[1]), t[2], ln, f);
210
+ i = j;
211
+ continue;
212
+ }
213
+ if ((m = /^mount\s+(.*)$/.exec(full))) {
214
+ const t = /^([\w:]+)\s*(?:=>|,\s*at:)\s*['"]([^'"]+)['"]/.exec(m[1]);
215
+ if (t)
216
+ push('ANY', t[2], t[1], '\u0000mount', ln, f, t[1]);
217
+ i = j;
218
+ continue;
219
+ }
220
+ if (opens)
221
+ stack.push({ ...f, kind: 'other', auth: [] });
222
+ i = j;
223
+ }
224
+ return out.filter((r) => r.path !== undefined);
225
+ }
226
+ const snakeCase = (s) => s.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toLowerCase();
227
+ // app/controllers/admin/users_controller.rb -> admin/users ; app/controllers/concerns/authentication.rb -> concerns/authentication
228
+ const keyOf = (file) => (/app\/controllers\/(.*)\.rb$/.exec(file)?.[1] ?? file).replace(/_controller$/, '');
229
+ function parseControllers(repo, files) {
230
+ const ctrls = new Map();
231
+ const concerns = new Map();
232
+ for (const file of files) {
233
+ const v = repo.view(file);
234
+ if (!v)
235
+ continue;
236
+ const isConcern = /\/concerns\//.test(file);
237
+ const cl = /^[ \t]*class\s+([A-Z][\w:]*)(?:\s*<\s*([\w:]+))?/m.exec(v.masked);
238
+ const head = isConcern ? /^[ \t]*module\s+([A-Z][\w:]*)/m.exec(v.masked) : cl;
239
+ if (!head)
240
+ continue;
241
+ const info = { key: keyOf(file), name: head[1].split('::').pop(), file, view: v, parent: !isConcern && cl?.[2] ? cl[2] : null, includes: [...v.masked.matchAll(/^[ \t]*include\s+([\w:]+)/gm)].map((x) => x[1].split('::').pop()), filters: [] };
242
+ const lines = v.masked.split('\n');
243
+ const orig = v.text.split('\n');
244
+ let defIndent = -1;
245
+ for (let i = 0; i < lines.length; i++) {
246
+ const l = lines[i].trim();
247
+ const indent = /^[ \t]*/.exec(lines[i])[0].length;
248
+ // A filter inside a method or a class_methods block is a macro definition, not a filter of this class.
249
+ if (defIndent >= 0) {
250
+ if (indent <= defIndent && /^end\b/.test(l))
251
+ defIndent = -1;
252
+ continue;
253
+ }
254
+ if (/^(?:def\b|class_methods\b)/.test(l) && !/\bend\s*$/.test(l)) {
255
+ defIndent = indent;
256
+ continue;
257
+ }
258
+ const m = /^(before_action|before_filter|prepend_before_action|append_before_action|skip_before_action|skip_before_filter|skip_forgery_protection|load_and_authorize_resource|authorize_resource|load_resource|rate_limit|allow_unauthenticated_access|require_authentication)\b/.exec(l);
259
+ if (!m)
260
+ continue;
261
+ let full = orig[i] ?? '';
262
+ let j = i;
263
+ while (/,\s*$/.test(lines[j]) && j + 1 < lines.length && j < i + 5) {
264
+ j++;
265
+ full += ' ' + (orig[j] ?? '').trim();
266
+ }
267
+ const only = listOpt(full, 'only');
268
+ const except = listOpt(full, 'except');
269
+ const kw = m[1];
270
+ const names = [...full.replace(/\b(?:only|except|if|unless|prepend|to|within|by|with)\s*:.*$/, '').matchAll(/:(\w+[?!]?)/g)].map((x) => x[1]);
271
+ const start = v.lines.start(i + 1);
272
+ let end = Math.min(v.lines.start(j + 2) - 1, v.text.length);
273
+ // The options (only, except, if, unless) name actions and methods, not filters: leave them out of the text that is read.
274
+ const cut = v.text.slice(start, end).search(/\b(?:only|except|if|unless)\s*:/);
275
+ if (cut > 0)
276
+ end = start + cut;
277
+ const add = (name, kind) => { info.filters.push({ name, line: i + 1, view: v, start, end, only, except, kind, text: full }); };
278
+ if (/^skip_/.test(kw))
279
+ for (const n of names)
280
+ add(n, 'skip');
281
+ else if (kw === 'allow_unauthenticated_access')
282
+ add('require_authentication', 'skip');
283
+ else if (/before_action|before_filter/.test(kw)) {
284
+ if (names.length)
285
+ for (const n of names)
286
+ add(n, 'add');
287
+ else
288
+ add('(block)', 'add');
289
+ }
290
+ else
291
+ add(kw, 'add');
292
+ i = j;
293
+ }
294
+ if (isConcern)
295
+ concerns.set(info.name, info);
296
+ else
297
+ ctrls.set(info.key, info);
298
+ }
299
+ return { ctrls, concerns };
300
+ }
301
+ export function extractRailsAll(repo, fn, local) {
302
+ const out = empty();
303
+ const { routes, controllers } = railsFiles(repo);
304
+ if (!routes.length)
305
+ return out;
306
+ const { ctrls, concerns } = parseControllers(repo, controllers);
307
+ const parentOf = (c) => {
308
+ if (!c.parent)
309
+ return undefined;
310
+ const dir = c.key.includes('/') ? c.key.slice(0, c.key.lastIndexOf('/')) : '';
311
+ const bare = c.parent.split('::').map(snakeCase).join('/').replace(/_controller$/, '');
312
+ return ctrls.get(dir ? `${dir}/${bare}` : bare) ?? ctrls.get(bare);
313
+ };
314
+ for (const rf of routes) {
315
+ const rv = repo.view(rf);
316
+ if (!rv)
317
+ continue;
318
+ for (const r of parseRoutes(rv)) {
319
+ if (r.action === '\u0000mount') {
320
+ const a = analyzeLight(rv, { start: rv.lines.start(r.line), end: rv.lines.start(r.line + 1) - 1, seeds: [], decorators: [], extra: r.auth, local });
321
+ out.entries.push(makeEntry(repo, { kind: 'http_route', framework: 'rails-mount', file: rf, line: r.line, method: 'ANY', path: r.path, name: r.mount ?? null, confidence: 'medium', handlerView: rv, spec: { start: rv.lines.start(r.line), end: rv.lines.start(r.line + 1) - 1, seeds: [] }, analysis: a }));
322
+ continue;
323
+ }
324
+ const ci = ctrls.get(r.controller);
325
+ const chain = [];
326
+ for (let cur = ci, n = 0; cur && n < 8; n++) {
327
+ chain.unshift(cur);
328
+ cur = parentOf(cur);
329
+ }
330
+ // Filters in the order they run: each ancestor's included concerns, then its own lines.
331
+ const active = [];
332
+ const applies = (fl) => (!fl.only || fl.only.includes(r.action)) && (!fl.except || !fl.except.includes(r.action));
333
+ for (const c of chain) {
334
+ const all = [...c.includes.flatMap((i) => concerns.get(i)?.filters ?? []), ...c.filters];
335
+ for (const fl of all) {
336
+ if (!applies(fl))
337
+ continue;
338
+ if (fl.kind === 'skip') {
339
+ for (let k = active.length - 1; k >= 0; k--)
340
+ if (active[k].name === fl.name)
341
+ active.splice(k, 1);
342
+ continue;
343
+ }
344
+ active.push(fl);
345
+ }
346
+ }
347
+ // A filter named authenticate_user may only look the user up (@user = ...) and let a guest through, as in Lobsters. It protects nothing then.
348
+ const softLookup = (fl) => {
349
+ if (!/^authenticate_(?:user|member|account)$/.test(fl.name))
350
+ return false;
351
+ const d = fn.defs(fl.name).find((x) => chain.some((c) => c.file === x.view.file) || concerns.size > 0);
352
+ if (!d)
353
+ return false;
354
+ const body = d.view.masked.slice(d.start, d.end);
355
+ return /@(?:user|current_user)\s*(?:\|\|)?=/.test(body) && !/\b(?:redirect_to|redirect_back|render|head|raise|halt|deny_access|forbidden|unauthorized)\b|\bauthenticate!?\b(?!_user)|\brequire_\w+/.test(body);
356
+ };
357
+ const decorators = active.filter((fl) => !softLookup(fl)).map((fl) => ({ view: fl.view, start: fl.start, end: fl.end, scope: 'router' }));
358
+ const policyScoped = active.some((fl) => /load_and_authorize_resource|authorize_resource/.test(fl.name));
359
+ // The action itself.
360
+ let hv = rv;
361
+ let start = rv.lines.start(r.line);
362
+ let end = rv.lines.start(r.line + 1) - 1;
363
+ let found = false;
364
+ if (ci) {
365
+ const d = fn.defs(r.action).find((x) => x.view.file === ci.file);
366
+ if (d) {
367
+ hv = d.view;
368
+ start = d.start;
369
+ end = d.end;
370
+ found = true;
371
+ }
372
+ }
373
+ // A filter such as set_article loads the record before the action runs: @article = Article.find(params[:id]).
374
+ const bound = [];
375
+ for (const fl of active) {
376
+ const d = fn.defs(fl.name).find((x) => chain.some((c) => c.file === x.view.file));
377
+ if (!d)
378
+ continue;
379
+ const body = d.view.text.slice(d.start, d.end);
380
+ for (const m of body.matchAll(/(@\w+)\s*=\s*(?:(@?current_\w+)\.(\w+)\.|([A-Z][\w:]*)\.)(?:find|find_by!?)\s*\(\s*(?:params\[\s*:\w+\s*\]|id:\s*params)/g))
381
+ bound.push({ name: m[1], target: m[4] ?? m[3], owner: m[2] !== undefined });
382
+ }
383
+ const a = analyzeLight(hv, { start, end, seeds: [], decorators, extra: r.auth.slice(), lookup: fn.find, local, policyScoped, bound });
384
+ out.entries.push(makeEntry(repo, {
385
+ kind: 'http_route', framework: 'rails', file: rf, line: r.line, method: r.verb, path: r.path.replace(/\(\.:format\)/g, '') || '/', name: `${r.controller}#${r.action}`,
386
+ confidence: found ? 'high' : 'low', handlerView: hv, spec: { start, end, seeds: [] }, analysis: a,
387
+ }));
388
+ }
389
+ }
390
+ return out;
391
+ }
@@ -0,0 +1,178 @@
1
+ import { cmp } from '../util.js';
2
+ import { jsFunction, Lines, mask, pyBlock } from './lex.js';
3
+ import { loadLocalVocab } from './local-vocab.js';
4
+ export const JS_EXT = /\.(?:[cm]?[jt]sx?)$/;
5
+ export const PY_EXT = /\.py$/;
6
+ export const langOf = (file) => (JS_EXT.test(file) && !/\.d\.ts$/.test(file) ? 'js' : PY_EXT.test(file) ? 'py' : /\.go$/.test(file) ? 'go' : /\.rb$/.test(file) ? 'rb' : /\.php$/.test(file) ? 'php' : /\.java$/.test(file) ? 'java' : /\.cs$/.test(file) ? 'cs' : /\.kts?$/.test(file) ? 'kt' : /\.swift$/.test(file) ? 'swift' : /\.rs$/.test(file) ? 'rs' : null);
7
+ // The lines of a Go, Ruby or PHP file that name what it uses: import blocks, require lines, use statements.
8
+ const IMPORTS_MORE = {
9
+ go: /^[ \t]*import\s*(?:\([\s\S]*?\)|(?:\w+\s+)?"[^"]*")/gm,
10
+ rb: /^[ \t]*(?:require(?:_relative)?|gem|include|extend)\s[^\n]*/gm,
11
+ php: /^[ \t]*(?:use|namespace)\s+[^;\n]*;/gm,
12
+ java: /^[ \t]*(?:import|package)\s+[^;\n]*;/gm,
13
+ cs: /^[ \t]*(?:global\s+)?(?:using|namespace)\s+[^;\n{]*[;{]?/gm,
14
+ kt: /^[ \t]*(?:import|package)\s+[^\n]*/gm,
15
+ swift: /^[ \t]*(?:@testable\s+)?import\s+[^\n]*/gm,
16
+ rs: /^[ \t]*(?:use|extern\s+crate|mod)\s+[^;\n]*;/gm,
17
+ };
18
+ // A file that is browser code or generated: no server entry points are looked for in it.
19
+ const CLIENT_FILE = /^\s*["']use client["']|@Component\s*\(|@NgModule\s*\(|from\s+["']@angular\/|\.min\.[cm]?js$/;
20
+ export class Repo {
21
+ views = new Map();
22
+ indexes = new Map();
23
+ fileSet;
24
+ ctx;
25
+ local;
26
+ constructor(ctx) { this.ctx = ctx; this.fileSet = new Set(ctx.files); this.local = loadLocalVocab(ctx.repo); }
27
+ view(file) {
28
+ if (this.views.has(file))
29
+ return this.views.get(file);
30
+ const lang = langOf(file);
31
+ let v = null;
32
+ if (lang) {
33
+ const text = this.ctx.text(file);
34
+ if (text) {
35
+ const masked = mask(text, lang);
36
+ const imports = lang !== 'js' && lang !== 'py' ? (text.match(IMPORTS_MORE[lang]) ?? []).join('\n') : (text.match(/\bimport\s[^;]*?from\s+['"][^'"]+['"]|\bimport\s+['"][^'"]+['"]|\b(?:const|let|var)\s+[^=;\n]+=\s*require\(\s*['"][^'"]+['"]\s*\)|^[ \t]*(?:from\s+\S+\s+import\b[^\n]*|import\s+\S+[^\n]*)/gm) ?? []).join('\n');
37
+ v = { file, text, masked, lang, lines: new Lines(text), imports };
38
+ }
39
+ }
40
+ this.views.set(file, v);
41
+ return v;
42
+ }
43
+ // Local name -> import specifier, for the default, named and namespace imports and require calls of a JavaScript file.
44
+ importMap(v) {
45
+ const out = new Map();
46
+ for (const m of v.imports.matchAll(/\bimport\s+(?:type\s+)?([^'"]*?)\s+from\s+['"]([^'"]+)['"]/g)) {
47
+ const clause = m[1];
48
+ const named = /\{([^}]*)\}/.exec(clause)?.[1] ?? '';
49
+ for (const part of named.split(',')) {
50
+ const n = part.trim().split(/\s+as\s+/).pop()?.trim();
51
+ if (n)
52
+ out.set(n, m[2]);
53
+ }
54
+ const def = clause.replace(/\{[^}]*\}/, '').replace(/\*\s+as\s+/, '').split(',').map((x) => x.trim()).filter(Boolean);
55
+ for (const d of def)
56
+ out.set(d, m[2]);
57
+ }
58
+ for (const m of v.imports.matchAll(/\b(?:const|let|var)\s+(\{[^}]*\}|[\w$]+)\s*=\s*require\(\s*['"]([^'"]+)['"]/g)) {
59
+ if (m[1].startsWith('{')) {
60
+ for (const part of m[1].slice(1, -1).split(',')) {
61
+ const n = part.trim().split(/\s*:\s*/).pop()?.trim();
62
+ if (n)
63
+ out.set(n, m[2]);
64
+ }
65
+ }
66
+ else
67
+ out.set(m[1], m[2]);
68
+ }
69
+ return out;
70
+ }
71
+ isClient(v) { return CLIENT_FILE.test(v.text.slice(0, 4000)) || (v.lang === 'js' && /\.[jt]sx$/.test(v.file) && /<\w+[^>]*>/.test(v.masked.slice(0, 20000)) && /\buseState\b|\buseEffect\b/.test(v.masked)); }
72
+ filesOf(lang) { return this.ctx.files.filter((f) => langOf(f) === lang); }
73
+ // Resolve a relative (or "@/" aliased) import specifier from a file to a repository file.
74
+ resolveImport(from, spec) {
75
+ let base;
76
+ if (spec.startsWith('.')) {
77
+ const parts = from.split('/').slice(0, -1);
78
+ for (const seg of spec.split('/')) {
79
+ if (seg === '..')
80
+ parts.pop();
81
+ else if (seg !== '.')
82
+ parts.push(seg);
83
+ }
84
+ base = parts.join('/');
85
+ }
86
+ else if (spec.startsWith('@/') || spec.startsWith('~/')) {
87
+ const rest = spec.slice(2);
88
+ for (const root of ['src/', '']) {
89
+ const r = this.tryFile(root + rest);
90
+ if (r)
91
+ return r;
92
+ }
93
+ return null;
94
+ }
95
+ else
96
+ return null;
97
+ return this.tryFile(base);
98
+ }
99
+ tryFile(base) {
100
+ for (const ext of ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.py', '/index.ts', '/index.js', '/index.mjs', '/__init__.py'])
101
+ if (this.fileSet.has(base + ext))
102
+ return base + ext;
103
+ return null;
104
+ }
105
+ index(lang) {
106
+ let idx = this.indexes.get(lang);
107
+ if (idx)
108
+ return idx;
109
+ idx = new Map();
110
+ if (lang !== 'js' && lang !== 'py') {
111
+ this.indexes.set(lang, idx);
112
+ return idx;
113
+ } // the other languages keep their own indexes (light-index.ts)
114
+ const put = (name, ref) => { const l = idx.get(name); if (l)
115
+ l.push(ref);
116
+ else
117
+ idx.set(name, [ref]); };
118
+ for (const file of this.filesOf(lang)) {
119
+ const v = this.view(file);
120
+ if (!v || this.isClient(v))
121
+ continue;
122
+ if (lang === 'js') {
123
+ const m = v.masked;
124
+ for (const x of m.matchAll(/(?<![.\w$])function\s*\*?\s*([A-Za-z_$][\w$]*)\s*[(<]/g)) {
125
+ const at = /async\s+$/.test(m.slice(Math.max(0, x.index - 7), x.index)) ? x.index - 6 : x.index;
126
+ const f = jsFunction(m, at);
127
+ if (f)
128
+ put(x[1], { view: v, start: f.start, end: f.end });
129
+ }
130
+ for (const x of m.matchAll(/\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?=\s*/g)) {
131
+ const f = jsFunction(m, x.index + x[0].length);
132
+ if (f)
133
+ put(x[1], { view: v, start: f.start, end: f.end });
134
+ }
135
+ for (const x of m.matchAll(/\bexports\.([A-Za-z_$][\w$]*)\s*=\s*/g)) {
136
+ const f = jsFunction(m, x.index + x[0].length);
137
+ if (f)
138
+ put(x[1], { view: v, start: f.start, end: f.end });
139
+ }
140
+ for (const x of m.matchAll(/(?:^|[{,\n])\s*([A-Za-z_$][\w$]*)\s*:\s*(?=async\b|function\b|\(|[A-Za-z_$][\w$]*\s*=>)/g)) {
141
+ const f = jsFunction(m, x.index + x[0].length);
142
+ if (f)
143
+ put(x[1], { view: v, start: f.start, end: f.end });
144
+ }
145
+ }
146
+ else {
147
+ for (const x of v.masked.matchAll(/^([ \t]*)(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(/gm)) {
148
+ const b = pyBlock(v.masked, v.lines, x.index);
149
+ put(x[2], { view: v, start: x.index, end: b.end });
150
+ }
151
+ }
152
+ }
153
+ this.indexes.set(lang, idx);
154
+ return idx;
155
+ }
156
+ // Which repository file defines a function of this name: unique across the repository, else the one in the caller's file.
157
+ helpers(lang) {
158
+ return {
159
+ local: this.local,
160
+ find: (name, fromFile) => {
161
+ const refs = this.index(lang).get(name);
162
+ if (!refs)
163
+ return null;
164
+ if (refs.length === 1)
165
+ return refs[0];
166
+ const same = refs.filter((r) => r.view.file === fromFile);
167
+ if (same.length === 1)
168
+ return same[0];
169
+ const sorted = refs.slice().sort((a, b) => cmp(a.view.file, b.view.file) || a.start - b.start);
170
+ return fromFile === undefined ? sorted[0] : null;
171
+ },
172
+ };
173
+ }
174
+ // Function definitions of a name, all of them (for resolving a handler reference).
175
+ defs(lang, name) {
176
+ return (this.index(lang).get(name) ?? []).slice().sort((a, b) => cmp(a.view.file, b.view.file) || a.start - b.start);
177
+ }
178
+ }