@nifrajs/cli 5.0.0 → 5.2.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 (296) hide show
  1. package/CHANGELOG.md +3211 -0
  2. package/README.md +10 -2
  3. package/dist/agent-files.d.ts +19 -11
  4. package/dist/agent-files.d.ts.map +1 -1
  5. package/dist/agent-files.js +26 -18
  6. package/dist/agent-files.js.map +1 -1
  7. package/dist/assure-hydration.d.ts.map +1 -1
  8. package/dist/assure-hydration.js +5 -2
  9. package/dist/assure-hydration.js.map +1 -1
  10. package/dist/assure.d.ts +12 -2
  11. package/dist/assure.d.ts.map +1 -1
  12. package/dist/assure.js +73 -9
  13. package/dist/assure.js.map +1 -1
  14. package/dist/capabilities-tool.d.ts.map +1 -1
  15. package/dist/capabilities-tool.js +152 -49
  16. package/dist/capabilities-tool.js.map +1 -1
  17. package/dist/check-diagnostics.d.ts +7 -0
  18. package/dist/check-diagnostics.d.ts.map +1 -1
  19. package/dist/check-diagnostics.js +3 -0
  20. package/dist/check-diagnostics.js.map +1 -1
  21. package/dist/check-scan.d.ts +8 -2
  22. package/dist/check-scan.d.ts.map +1 -1
  23. package/dist/check-scan.js +49 -13
  24. package/dist/check-scan.js.map +1 -1
  25. package/dist/check.d.ts.map +1 -1
  26. package/dist/check.js +91 -45
  27. package/dist/check.js.map +1 -1
  28. package/dist/cli.d.ts +2 -0
  29. package/dist/cli.d.ts.map +1 -1
  30. package/dist/cli.js +128 -59
  31. package/dist/cli.js.map +1 -1
  32. package/dist/client-module.d.ts +5 -3
  33. package/dist/client-module.d.ts.map +1 -1
  34. package/dist/client-module.js +34 -10
  35. package/dist/client-module.js.map +1 -1
  36. package/dist/command-catalog.d.ts +9 -1
  37. package/dist/command-catalog.d.ts.map +1 -1
  38. package/dist/command-catalog.js +75 -26
  39. package/dist/command-catalog.js.map +1 -1
  40. package/dist/dev-bun-config.d.ts +6 -3
  41. package/dist/dev-bun-config.d.ts.map +1 -1
  42. package/dist/dev-bun-config.js +13 -10
  43. package/dist/dev-bun-config.js.map +1 -1
  44. package/dist/dev-feed-tool.d.ts +19 -1
  45. package/dist/dev-feed-tool.d.ts.map +1 -1
  46. package/dist/dev-feed-tool.js +114 -0
  47. package/dist/dev-feed-tool.js.map +1 -1
  48. package/dist/doctor.d.ts +15 -1
  49. package/dist/doctor.d.ts.map +1 -1
  50. package/dist/doctor.js +79 -61
  51. package/dist/doctor.js.map +1 -1
  52. package/dist/fix-recipes.d.ts +2 -0
  53. package/dist/fix-recipes.d.ts.map +1 -1
  54. package/dist/fix-recipes.js +93 -12
  55. package/dist/fix-recipes.js.map +1 -1
  56. package/dist/i18n-check.d.ts +11 -1
  57. package/dist/i18n-check.d.ts.map +1 -1
  58. package/dist/i18n-check.js +170 -17
  59. package/dist/i18n-check.js.map +1 -1
  60. package/dist/init-agents.d.ts +60 -4
  61. package/dist/init-agents.d.ts.map +1 -1
  62. package/dist/init-agents.js +196 -33
  63. package/dist/init-agents.js.map +1 -1
  64. package/dist/internal/config-paths.d.ts +16 -0
  65. package/dist/internal/config-paths.d.ts.map +1 -0
  66. package/dist/internal/config-paths.js +124 -0
  67. package/dist/internal/config-paths.js.map +1 -0
  68. package/dist/internal/installed-package.d.ts +4 -0
  69. package/dist/internal/installed-package.d.ts.map +1 -0
  70. package/dist/internal/installed-package.js +17 -0
  71. package/dist/internal/installed-package.js.map +1 -0
  72. package/dist/internal/project-specifiers.d.ts +14 -1
  73. package/dist/internal/project-specifiers.d.ts.map +1 -1
  74. package/dist/internal/project-specifiers.js +63 -16
  75. package/dist/internal/project-specifiers.js.map +1 -1
  76. package/dist/internal/source-facts.d.ts +12 -1
  77. package/dist/internal/source-facts.d.ts.map +1 -1
  78. package/dist/internal/source-facts.js +117 -5
  79. package/dist/internal/source-facts.js.map +1 -1
  80. package/dist/internal/typescript-import.d.ts.map +1 -1
  81. package/dist/internal/typescript-import.js +5 -3
  82. package/dist/internal/typescript-import.js.map +1 -1
  83. package/dist/internal/workspace-packages.d.ts +22 -0
  84. package/dist/internal/workspace-packages.d.ts.map +1 -0
  85. package/dist/internal/workspace-packages.js +215 -0
  86. package/dist/internal/workspace-packages.js.map +1 -0
  87. package/dist/introspect.d.ts +9 -0
  88. package/dist/introspect.d.ts.map +1 -1
  89. package/dist/introspect.js +32 -1
  90. package/dist/introspect.js.map +1 -1
  91. package/dist/levels-tool.d.ts.map +1 -1
  92. package/dist/levels-tool.js +5 -8
  93. package/dist/levels-tool.js.map +1 -1
  94. package/dist/load.d.ts +9 -4
  95. package/dist/load.d.ts.map +1 -1
  96. package/dist/load.js +32 -3
  97. package/dist/load.js.map +1 -1
  98. package/dist/lock-links.d.ts +42 -0
  99. package/dist/lock-links.d.ts.map +1 -0
  100. package/dist/lock-links.js +144 -0
  101. package/dist/lock-links.js.map +1 -0
  102. package/dist/mcp-context.d.ts.map +1 -1
  103. package/dist/mcp-context.js +6 -21
  104. package/dist/mcp-context.js.map +1 -1
  105. package/dist/mcp-delegate.d.ts +2 -2
  106. package/dist/mcp-delegate.d.ts.map +1 -1
  107. package/dist/mcp-delegate.js +19 -7
  108. package/dist/mcp-delegate.js.map +1 -1
  109. package/dist/mcp-exec.d.ts +1 -0
  110. package/dist/mcp-exec.d.ts.map +1 -1
  111. package/dist/mcp-exec.js +13 -5
  112. package/dist/mcp-exec.js.map +1 -1
  113. package/dist/mcp-http.js +1 -1
  114. package/dist/mcp-io.d.ts +13 -0
  115. package/dist/mcp-io.d.ts.map +1 -1
  116. package/dist/mcp-io.js +23 -0
  117. package/dist/mcp-io.js.map +1 -1
  118. package/dist/mcp-path.d.ts +2 -1
  119. package/dist/mcp-path.d.ts.map +1 -1
  120. package/dist/mcp-path.js +9 -24
  121. package/dist/mcp-path.js.map +1 -1
  122. package/dist/mcp-root.d.ts +6 -4
  123. package/dist/mcp-root.d.ts.map +1 -1
  124. package/dist/mcp-root.js +12 -8
  125. package/dist/mcp-root.js.map +1 -1
  126. package/dist/mcp.d.ts.map +1 -1
  127. package/dist/mcp.js +40 -17
  128. package/dist/mcp.js.map +1 -1
  129. package/dist/migrate-layout.d.ts +41 -1
  130. package/dist/migrate-layout.d.ts.map +1 -1
  131. package/dist/migrate-layout.js +1729 -236
  132. package/dist/migrate-layout.js.map +1 -1
  133. package/dist/output-schemas.d.ts +34 -0
  134. package/dist/output-schemas.d.ts.map +1 -1
  135. package/dist/output-schemas.js +132 -21
  136. package/dist/output-schemas.js.map +1 -1
  137. package/dist/project-path.d.ts +6 -0
  138. package/dist/project-path.d.ts.map +1 -1
  139. package/dist/project-path.js +48 -14
  140. package/dist/project-path.js.map +1 -1
  141. package/dist/project-site.d.ts +7 -0
  142. package/dist/project-site.d.ts.map +1 -0
  143. package/dist/project-site.js +19 -0
  144. package/dist/project-site.js.map +1 -0
  145. package/dist/recipes/4.0.0.d.ts.map +1 -1
  146. package/dist/recipes/4.0.0.js +2 -0
  147. package/dist/recipes/4.0.0.js.map +1 -1
  148. package/dist/recipes/5.1.0.d.ts +9 -0
  149. package/dist/recipes/5.1.0.d.ts.map +1 -0
  150. package/dist/recipes/5.1.0.js +22 -0
  151. package/dist/recipes/5.1.0.js.map +1 -0
  152. package/dist/recipes/5.2.0.d.ts +10 -0
  153. package/dist/recipes/5.2.0.d.ts.map +1 -0
  154. package/dist/recipes/5.2.0.js +32 -0
  155. package/dist/recipes/5.2.0.js.map +1 -0
  156. package/dist/recipes/index.d.ts +3 -0
  157. package/dist/recipes/index.d.ts.map +1 -1
  158. package/dist/recipes/index.js +7 -0
  159. package/dist/recipes/index.js.map +1 -1
  160. package/dist/release-verification.d.ts +5 -0
  161. package/dist/release-verification.d.ts.map +1 -1
  162. package/dist/release-verification.js +14 -0
  163. package/dist/release-verification.js.map +1 -1
  164. package/dist/review.d.ts.map +1 -1
  165. package/dist/review.js +7 -0
  166. package/dist/review.js.map +1 -1
  167. package/dist/rule-explain.d.ts +26 -0
  168. package/dist/rule-explain.d.ts.map +1 -0
  169. package/dist/rule-explain.js +49 -0
  170. package/dist/rule-explain.js.map +1 -0
  171. package/dist/rules/codes.d.ts +5 -1
  172. package/dist/rules/codes.d.ts.map +1 -1
  173. package/dist/rules/codes.js +5 -1
  174. package/dist/rules/codes.js.map +1 -1
  175. package/dist/rules/contract-visibility.d.ts.map +1 -1
  176. package/dist/rules/contract-visibility.js +93 -21
  177. package/dist/rules/contract-visibility.js.map +1 -1
  178. package/dist/rules/data-guard.d.ts +6 -0
  179. package/dist/rules/data-guard.d.ts.map +1 -1
  180. package/dist/rules/data-guard.js +37 -0
  181. package/dist/rules/data-guard.js.map +1 -1
  182. package/dist/rules/explanations.d.ts +14 -0
  183. package/dist/rules/explanations.d.ts.map +1 -0
  184. package/dist/rules/explanations.js +316 -0
  185. package/dist/rules/explanations.js.map +1 -0
  186. package/dist/rules/legacy.d.ts.map +1 -1
  187. package/dist/rules/legacy.js +44 -2
  188. package/dist/rules/legacy.js.map +1 -1
  189. package/dist/rules/merge-auth.d.ts +12 -0
  190. package/dist/rules/merge-auth.d.ts.map +1 -0
  191. package/dist/rules/merge-auth.js +52 -0
  192. package/dist/rules/merge-auth.js.map +1 -0
  193. package/dist/rules/pages.d.ts +56 -3
  194. package/dist/rules/pages.d.ts.map +1 -1
  195. package/dist/rules/pages.js +412 -23
  196. package/dist/rules/pages.js.map +1 -1
  197. package/dist/rules/routes.d.ts +3 -2
  198. package/dist/rules/routes.d.ts.map +1 -1
  199. package/dist/rules/routes.js +4 -3
  200. package/dist/rules/routes.js.map +1 -1
  201. package/dist/rules/security.d.ts +3 -2
  202. package/dist/rules/security.d.ts.map +1 -1
  203. package/dist/rules/security.js +42 -11
  204. package/dist/rules/security.js.map +1 -1
  205. package/dist/rules/zones.d.ts.map +1 -1
  206. package/dist/rules/zones.js +34 -11
  207. package/dist/rules/zones.js.map +1 -1
  208. package/dist/stylex-migrate.d.ts.map +1 -1
  209. package/dist/stylex-migrate.js +127 -17
  210. package/dist/stylex-migrate.js.map +1 -1
  211. package/dist/test-tool.d.ts.map +1 -1
  212. package/dist/test-tool.js +3 -9
  213. package/dist/test-tool.js.map +1 -1
  214. package/dist/typecheck-coverage.d.ts +11 -0
  215. package/dist/typecheck-coverage.d.ts.map +1 -1
  216. package/dist/typecheck-coverage.js +50 -7
  217. package/dist/typecheck-coverage.js.map +1 -1
  218. package/dist/upgrade.d.ts +57 -6
  219. package/dist/upgrade.d.ts.map +1 -1
  220. package/dist/upgrade.js +247 -19
  221. package/dist/upgrade.js.map +1 -1
  222. package/dist/verification-plan.d.ts.map +1 -1
  223. package/dist/verification-plan.js +7 -2
  224. package/dist/verification-plan.js.map +1 -1
  225. package/dist/version-drift.d.ts.map +1 -1
  226. package/dist/version-drift.js +66 -3
  227. package/dist/version-drift.js.map +1 -1
  228. package/dist/web-app-options.d.ts +1 -1
  229. package/dist/web-app-options.d.ts.map +1 -1
  230. package/dist/web-app-options.js.map +1 -1
  231. package/docs/examples.json +21 -3
  232. package/docs/llms-full.txt +1083 -168
  233. package/docs/types.json +535 -112
  234. package/package.json +13 -12
  235. package/src/agent-files.ts +27 -17
  236. package/src/assure-hydration.ts +5 -1
  237. package/src/assure.ts +99 -11
  238. package/src/capabilities-tool.ts +188 -53
  239. package/src/check-diagnostics.ts +10 -0
  240. package/src/check-scan.ts +65 -12
  241. package/src/check.ts +122 -51
  242. package/src/cli.ts +127 -45
  243. package/src/client-module.ts +45 -9
  244. package/src/command-catalog.ts +92 -28
  245. package/src/dev-bun-config.ts +13 -9
  246. package/src/dev-feed-tool.ts +146 -1
  247. package/src/doctor.ts +102 -66
  248. package/src/fix-recipes.ts +101 -10
  249. package/src/i18n-check.ts +254 -18
  250. package/src/init-agents.ts +225 -44
  251. package/src/internal/config-paths.ts +154 -0
  252. package/src/internal/installed-package.ts +15 -0
  253. package/src/internal/project-specifiers.ts +78 -17
  254. package/src/internal/source-facts.ts +147 -5
  255. package/src/internal/typescript-import.ts +5 -3
  256. package/src/internal/workspace-packages.ts +218 -0
  257. package/src/introspect.ts +39 -1
  258. package/src/levels-tool.ts +7 -9
  259. package/src/load.ts +46 -4
  260. package/src/lock-links.ts +173 -0
  261. package/src/mcp-context.ts +5 -18
  262. package/src/mcp-delegate.ts +22 -10
  263. package/src/mcp-exec.ts +14 -4
  264. package/src/mcp-http.ts +1 -1
  265. package/src/mcp-io.ts +33 -0
  266. package/src/mcp-path.ts +8 -22
  267. package/src/mcp-root.ts +14 -8
  268. package/src/mcp.ts +48 -19
  269. package/src/migrate-layout.ts +1968 -245
  270. package/src/output-schemas.ts +179 -23
  271. package/src/project-path.ts +44 -13
  272. package/src/project-site.ts +18 -0
  273. package/src/recipes/4.0.0.ts +2 -0
  274. package/src/recipes/5.1.0.ts +23 -0
  275. package/src/recipes/5.2.0.ts +33 -0
  276. package/src/recipes/index.ts +7 -0
  277. package/src/release-verification.ts +20 -0
  278. package/src/review.ts +7 -0
  279. package/src/rule-explain.ts +75 -0
  280. package/src/rules/codes.ts +5 -1
  281. package/src/rules/contract-visibility.ts +118 -20
  282. package/src/rules/data-guard.ts +40 -0
  283. package/src/rules/explanations.ts +329 -0
  284. package/src/rules/legacy.ts +46 -2
  285. package/src/rules/merge-auth.ts +55 -0
  286. package/src/rules/pages.ts +486 -25
  287. package/src/rules/routes.ts +4 -3
  288. package/src/rules/security.ts +49 -11
  289. package/src/rules/zones.ts +33 -9
  290. package/src/stylex-migrate.ts +117 -17
  291. package/src/test-tool.ts +3 -9
  292. package/src/typecheck-coverage.ts +51 -6
  293. package/src/upgrade.ts +312 -19
  294. package/src/verification-plan.ts +15 -2
  295. package/src/version-drift.ts +68 -2
  296. package/src/web-app-options.ts +9 -1
package/src/check-scan.ts CHANGED
@@ -352,15 +352,59 @@ export function parseSimpleFetchCall(snippet: string): SimpleFetchCall | undefin
352
352
  return { path, method, start: match.index, end: close + 1 }
353
353
  }
354
354
 
355
- function isPotentialBackendSource(code: string): boolean {
355
+ const SERVER_CONSTRUCTION = /(?<![.\w])server\s*\(|new\s+Hono(?:<[^>]+>)?\s*\(/
356
+
357
+ function isPotentialBackendSource(
358
+ code: string,
359
+ file?: string,
360
+ project?: ReadonlyMap<string, string>,
361
+ ): boolean {
356
362
  return (
357
363
  code.includes("@nifrajs/core") ||
358
- /(?<![.\w])server\s*\(/.test(code) ||
364
+ SERVER_CONSTRUCTION.test(code) ||
359
365
  /(?:from\s*|import\s*)["']hono["']/.test(code) ||
360
- /new\s+Hono(?:<[^>]+>)?\s*\(/.test(code)
366
+ (file !== undefined && project !== undefined && callsLocalServerFactory(code, file, project))
361
367
  )
362
368
  }
363
369
 
370
+ /**
371
+ * A module that builds its server through a local helper - `newServer().get(...)` with `newServer`
372
+ * imported from a project file that calls `server()` - names neither `@nifrajs/core` nor `server(`
373
+ * itself. One import hop is followed: the imported module must construct a server, so a helper module
374
+ * that merely mentions `@nifrajs/core` (a schema re-export) does not turn every importer into a
375
+ * backend and its `client.get("/x")` calls into routes.
376
+ */
377
+ function callsLocalServerFactory(
378
+ code: string,
379
+ file: string,
380
+ project: ReadonlyMap<string, string>,
381
+ ): boolean {
382
+ for (const [local, binding] of namedImports(code)) {
383
+ if (!new RegExp(`(?<![.\\w$])${local.replaceAll("$", "\\$")}\\s*\\(`).test(code)) continue
384
+ const target = projectModule(project, file, binding.specifier)
385
+ if (target !== undefined && SERVER_CONSTRUCTION.test(stripComments(project.get(target) ?? "")))
386
+ return true
387
+ }
388
+ return false
389
+ }
390
+
391
+ /**
392
+ * Literal route registrations in a file the backend-source gate turned away, so a route the scan
393
+ * could not cover can say where it probably lives. Nothing here is treated as a route.
394
+ */
395
+ export function scanSkippedRouteText(
396
+ file: string,
397
+ content: string,
398
+ project?: ReadonlyMap<string, string>,
399
+ ): StaticRouteFinding[] {
400
+ const code = stripComments(content)
401
+ if (isPotentialBackendSource(code, file, project)) return []
402
+ return [
403
+ ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_DQ),
404
+ ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_SQ),
405
+ ]
406
+ }
407
+
364
408
  function scanRoutePattern(
365
409
  file: string,
366
410
  content: string,
@@ -727,7 +771,7 @@ export function scanStaticRouteText(
727
771
  project?: ReadonlyMap<string, string>,
728
772
  ): StaticRouteFinding[] {
729
773
  const code = stripComments(content)
730
- if (!isPotentialBackendSource(code)) return []
774
+ if (!isPotentialBackendSource(code, file, project)) return []
731
775
  return [
732
776
  ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_DQ, facts),
733
777
  ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_SQ, facts),
@@ -849,7 +893,7 @@ function scanRouteArguments(
849
893
  const routes: StaticRouteFinding[] = []
850
894
  const unresolved: UnresolvedRoutePathFinding[] = []
851
895
  const code = stripComments(content)
852
- if (!isPotentialBackendSource(code)) return { routes, unresolved }
896
+ if (!isPotentialBackendSource(code, file, project)) return { routes, unresolved }
853
897
  const lines = content.split("\n")
854
898
  const constants = moduleConstants(code, content)
855
899
  const imports = namedImports(code)
@@ -922,7 +966,7 @@ export function scanStaticSocketText(
922
966
  project?: ReadonlyMap<string, string>,
923
967
  ): StaticRouteFinding[] {
924
968
  const code = stripComments(content)
925
- if (!isPotentialBackendSource(code)) return []
969
+ if (!isPotentialBackendSource(code, file, project)) return []
926
970
  return [
927
971
  ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_DQ, facts, SOCKET_VERB),
928
972
  ...scanRoutePattern(file, content, code, ROUTE_REGISTRATION_SQ, facts, SOCKET_VERB),
@@ -2939,7 +2983,8 @@ function staticImportEdges(
2939
2983
  for (let m = re.exec(code); m !== null; m = re.exec(code)) {
2940
2984
  if (positions[m.index] === " ") continue
2941
2985
  if (m[1] === undefined) continue
2942
- if (facts !== undefined && file !== undefined) {
2986
+ // Only a `type` modifier can erase a clause the pattern matched, so other clauses skip the parse.
2987
+ if (facts !== undefined && file !== undefined && /\btype\b/.test(m[0])) {
2943
2988
  const source = facts.parse(file, content)
2944
2989
  if (source !== undefined && facts.isValueImportAt(source, m.index, m[1]) === false) continue
2945
2990
  }
@@ -3020,12 +3065,14 @@ function importEdges(
3020
3065
  return edges.sort((a, b) => a.index - b.index)
3021
3066
  }
3022
3067
 
3023
- /** The runtime imports of a module with the line each sits on, in source order. Pure. */
3068
+ /** The runtime imports of a module with the line each sits on, in source order. With `facts`, an
3069
+ * import the compiler erases (`import { type X }`) is none. Pure. */
3024
3070
  export function importSites(
3025
3071
  content: string,
3026
3072
  file?: string,
3073
+ facts?: SourceFacts,
3027
3074
  ): Array<{ readonly specifier: string; readonly line: number }> {
3028
- return importEdges(content, undefined, file).map(({ specifier, index }) => ({
3075
+ return importEdges(content, facts, file).map(({ specifier, index }) => ({
3029
3076
  specifier,
3030
3077
  line: lineAt(content, index),
3031
3078
  }))
@@ -3334,9 +3381,15 @@ export function scanResponseRoutes(
3334
3381
  for (let m = RESPONSE_RETURN.exec(code); m !== null; m = RESPONSE_RETURN.exec(code)) {
3335
3382
  if (facts !== undefined) {
3336
3383
  const source = facts.parse(file, content)
3337
- // A parseable AST rejects `return new Response(` text that only lives in a normal string. On
3338
- // malformed source we keep the lexical candidate; the typecheck gate will report the syntax error.
3339
- if (source !== undefined && facts.isResponseSyntaxAt(source, m.index) !== true) continue
3384
+ // A parseable AST rejects `return new Response(` text that only lives in a normal string, and
3385
+ // one outside a route handler (an auth stage's `rejected` helper, say). On malformed source we
3386
+ // keep the lexical candidate; the typecheck gate will report the syntax error.
3387
+ if (
3388
+ source !== undefined &&
3389
+ (facts.isResponseSyntaxAt(source, m.index) !== true ||
3390
+ !facts.isRouteHandlerAt(source, m.index))
3391
+ )
3392
+ continue
3340
3393
  }
3341
3394
  const line = lineAt(content, m.index)
3342
3395
  // A `// nifra-expect NF-C007` comment (or the `raw-response` slug) marks an intentional raw Response
package/src/check.ts CHANGED
@@ -19,7 +19,7 @@
19
19
  import { existsSync, mkdtempSync, readFileSync, realpathSync, rmdirSync, rmSync } from "node:fs"
20
20
  import { tmpdir } from "node:os"
21
21
  import { dirname, isAbsolute, join, relative, resolve } from "node:path"
22
- import { specifierPackage } from "@nifrajs/web/zones"
22
+ import { declaredSide, specifierPackage } from "@nifrajs/web/zones"
23
23
  import { BACKEND_APP_FILE } from "./app-files.ts"
24
24
  import {
25
25
  type CheckAssuranceContext,
@@ -33,6 +33,7 @@ import {
33
33
  import { appDirs, clientModuleRoot } from "./client-module.ts"
34
34
  import { checkContractsLock, DEFAULT_CONTRACTS_LOCK } from "./contracts.ts"
35
35
  import { type Diagnostic, normalizeSeverity, toSarifLog } from "./diagnostics.ts"
36
+ import { installedManifest } from "./internal/installed-package.ts"
36
37
  import { createApiOnlyProbe, createNifraProjectProbe } from "./internal/nifra-project.ts"
37
38
  import { createSpecifierResolver } from "./internal/project-specifiers.ts"
38
39
  import { createSourceFacts } from "./internal/source-facts.ts"
@@ -43,7 +44,7 @@ import { forwardTerminalSignals, OWN_PROCESS_GROUP, signalProcessGroup } from ".
43
44
  import type { ContractCheckFacts, ProjectFactsSeed } from "./project-facts.ts"
44
45
  import { RULE_CODES } from "./rules/codes.ts"
45
46
  import { sourceIndex } from "./rules/index.ts"
46
- import { filesOutsideProgram, splitListedFiles } from "./typecheck-coverage.ts"
47
+ import { filesOutsideProgram, splitListedFiles, typecheckProjects } from "./typecheck-coverage.ts"
47
48
 
48
49
  export type {
49
50
  CheckAnalysisInput,
@@ -95,6 +96,10 @@ interface TypecheckResult {
95
96
  /** tsconfig.json exists but no `typescript` install was found walking up from cwd. The contract
96
97
  * gate could not run - the caller surfaces this as a FAILING diagnostic, never a silent skip. */
97
98
  readonly missingTypeScript?: boolean
99
+ /** No tsconfig.json at the project root. Fails the check only when TypeScript sources exist. */
100
+ readonly missingTsconfig?: boolean
101
+ /** TypeScript sources the scan found with no tsconfig.json to typecheck them. */
102
+ readonly sourcesWithoutTsconfig?: number
98
103
  /** Every file in the compiler's program, absolute. Absent when the compiler listed none. */
99
104
  readonly programFiles?: readonly string[]
100
105
  /** Zoned TypeScript files outside the program, project-relative: the typecheck never read them. */
@@ -119,11 +124,25 @@ function resolveTscBin(cwd: string): string | undefined {
119
124
  }
120
125
  }
121
126
 
122
- /** Run the project's own `tsc --noEmit`, if TypeScript + a tsconfig are present. Never auto-installs. */
123
- async function typecheck(cwd: string, signal?: AbortSignal): Promise<TypecheckResult> {
124
- const tsconfig = join(cwd, "tsconfig.json")
125
- if (!(await Bun.file(tsconfig).exists()))
126
- return { ran: false, ok: true, note: "no tsconfig.json" }
127
+ /**
128
+ * Run the project's own `tsc --noEmit` over each tsconfig `typecheckProjects` names, one program at a
129
+ * time, if TypeScript and a tsconfig are present. Never auto-installs.
130
+ */
131
+ async function typecheck(
132
+ cwd: string,
133
+ signal?: AbortSignal,
134
+ configured?: readonly string[],
135
+ ): Promise<TypecheckResult> {
136
+ const projects = typecheckProjects(cwd, configured)
137
+ if (projects.missing.length > 0) {
138
+ return {
139
+ ran: true,
140
+ ok: false,
141
+ output: `nifra.check.json "tsconfigs" names ${projects.missing.map((path) => `"${path}"`).join(", ")}, which does not exist`,
142
+ }
143
+ }
144
+ if (projects.paths.length === 0)
145
+ return { ran: false, ok: true, missingTsconfig: true, note: "no tsconfig.json" }
127
146
  // Routes import their generated `./+types` modules; bring them up to date before tsc reads them.
128
147
  const { refreshRouteTypes } = await import("./route-types.ts")
129
148
  refreshRouteTypes(cwd, () => {})
@@ -136,11 +155,28 @@ async function typecheck(cwd: string, signal?: AbortSignal): Promise<TypecheckRe
136
155
  note: "typescript not installed (run: bun add -d typescript)",
137
156
  }
138
157
  }
139
- const first = await runTsc(cwd, tscBin, tsconfig, true, signal)
140
- // A compiler that rejects `--listFiles` still gets its typecheck; only the coverage check is lost.
141
- if (!first.ok && !first.cancelled && /TS50(?:23|25)\b[^\n]*listFiles/.test(first.output ?? ""))
142
- return runTsc(cwd, tscBin, tsconfig, false, signal)
143
- return first
158
+ const runs: TypecheckResult[] = []
159
+ for (const tsconfig of projects.paths) {
160
+ let run = await runTsc(cwd, tscBin, tsconfig, true, signal)
161
+ // A compiler that rejects `--listFiles` still gets its typecheck; only the coverage check is lost.
162
+ if (!run.ok && !run.cancelled && /TS50(?:23|25)\b[^\n]*listFiles/.test(run.output ?? ""))
163
+ run = await runTsc(cwd, tscBin, tsconfig, false, signal)
164
+ if (run.cancelled === true) return run
165
+ runs.push(run)
166
+ }
167
+ const [only] = runs
168
+ if (runs.length === 1 && only !== undefined) return only
169
+ // A file two programs both hold reports each error twice; keep one copy of each diagnostic block.
170
+ const blocks = new Set(
171
+ runs.flatMap((run) => (run.output ?? "").split(/\n(?=\S)/).filter((block) => block !== "")),
172
+ )
173
+ const programFiles = [...new Set(runs.flatMap((run) => run.programFiles ?? []))]
174
+ const merged: TypecheckResult = {
175
+ ran: true,
176
+ ok: runs.every((run) => run.ok),
177
+ output: [...blocks].join("\n"),
178
+ }
179
+ return programFiles.length > 0 ? { ...merged, programFiles } : merged
144
180
  }
145
181
 
146
182
  async function runTsc(
@@ -216,6 +252,7 @@ async function loadCheckConfig(
216
252
  const parsed = JSON.parse(await Bun.file(path).text()) as {
217
253
  externalMounts?: unknown
218
254
  rules?: unknown
255
+ tsconfigs?: unknown
219
256
  }
220
257
  const raw = Array.isArray(parsed.externalMounts) ? parsed.externalMounts : []
221
258
  const externalMounts = raw
@@ -268,7 +305,25 @@ async function loadCheckConfig(
268
305
  }
269
306
  }
270
307
  }
271
- return { config: { externalMounts, rules }, warnings }
308
+ let tsconfigs: readonly string[] | undefined
309
+ if (parsed.tsconfigs !== undefined) {
310
+ if (
311
+ Array.isArray(parsed.tsconfigs) &&
312
+ parsed.tsconfigs.length > 0 &&
313
+ parsed.tsconfigs.every(
314
+ (entry): entry is string =>
315
+ typeof entry === "string" && entry.endsWith(".json") && !isAbsolute(entry),
316
+ )
317
+ )
318
+ tsconfigs = parsed.tsconfigs
319
+ else
320
+ warnings.push(
321
+ '`tsconfigs` must be a non-empty array of tsconfig paths relative to the project, such as ["tsconfig.json"] - ignored',
322
+ )
323
+ }
324
+ const config: CheckConfig =
325
+ tsconfigs === undefined ? { externalMounts, rules } : { externalMounts, rules, tsconfigs }
326
+ return { config, warnings }
272
327
  } catch (error) {
273
328
  return {
274
329
  config: empty,
@@ -309,6 +364,16 @@ async function collectContractFacts(cwd: string): Promise<ContractCheckFacts> {
309
364
  }
310
365
  }
311
366
 
367
+ /** A project whose TypeScript no tsconfig.json covers has no typecheck to pass: the gate fails. */
368
+ function withoutTsconfig(
369
+ result: TypecheckResult,
370
+ sourceFiles: ReadonlyArray<{ readonly file: string }>,
371
+ ): TypecheckResult {
372
+ if (result.missingTsconfig !== true) return result
373
+ const sources = sourceFiles.filter(({ file }) => !/\.d\.[cm]?ts$/.test(file)).length
374
+ return sources === 0 ? result : { ...result, ok: false, sourcesWithoutTsconfig: sources }
375
+ }
376
+
312
377
  /** Record the zoned files the compiler's program left out, minus the typecheck rule's `ignore` globs. */
313
378
  function withProgramCoverage(
314
379
  cwd: string,
@@ -354,7 +419,7 @@ async function buildProjectScan(
354
419
  const [typecheckRun, _, doctor, manifestDrift, contracts] = await Promise.all([
355
420
  opts.lintsOnly
356
421
  ? Promise.resolve<TypecheckResult>({ ran: false, ok: true, note: "lints-only mode" })
357
- : typecheck(cwd, opts.signal),
422
+ : typecheck(cwd, opts.signal, checkConfig.tsconfigs),
358
423
  walkSource(
359
424
  cwd,
360
425
  (rel, content) => {
@@ -376,11 +441,10 @@ async function buildProjectScan(
376
441
 
377
442
  // tsc compiles TypeScript sources only; the JavaScript and component files the scan reads are no
378
443
  // program's to cover.
379
- const typecheckResult = withProgramCoverage(
380
- cwd,
381
- typecheckRun,
382
- sourceFiles.filter(({ file }) => /\.[cm]?tsx?$/.test(file)),
383
- checkConfig,
444
+ const typeScriptSources = sourceFiles.filter(({ file }) => /\.[cm]?tsx?$/.test(file))
445
+ const typecheckResult = withoutTsconfig(
446
+ withProgramCoverage(cwd, typecheckRun, typeScriptSources, checkConfig),
447
+ typeScriptSources,
384
448
  )
385
449
 
386
450
  // TypeScript 7's unstable API materializes source files through one project session. Build the
@@ -390,7 +454,24 @@ async function buildProjectScan(
390
454
  const loadedTypeScript = await loadProjectTypeScript(cwd, opts.loadTypeScript, source)
391
455
  try {
392
456
  const sqlCompiler = loadedTypeScript.compiler
393
- const sourceFacts = sqlCompiler === undefined ? undefined : createSourceFacts(sqlCompiler)
457
+ // Resolved modules stay project-relative, as the scanned files are: the zone of a path is read
458
+ // from its folders, and a checkout under a folder named `backend/` must not make it all backend.
459
+ // The resolver answers with real paths, so the project root is read as one too.
460
+ const realCwd = (() => {
461
+ try {
462
+ return realpathSync(cwd)
463
+ } catch {
464
+ return cwd
465
+ }
466
+ })()
467
+ const resolveSpecifier = createSpecifierResolver()
468
+ const sourceFacts =
469
+ sqlCompiler === undefined
470
+ ? undefined
471
+ : createSourceFacts(sqlCompiler, {
472
+ keepsTypeOnlyImports: (file) =>
473
+ resolveSpecifier.keepsTypeOnlyImports(isAbsolute(file) ? file : join(realCwd, file)),
474
+ })
394
475
  const sqlImports = sqlCompiler === undefined ? undefined : createProjectSqlImports(cwd)
395
476
  const project = new Map(
396
477
  sourceFiles.map(({ file, content }) => [file.replaceAll("\\", "/"), content]),
@@ -403,17 +484,6 @@ async function buildProjectScan(
403
484
  interpolatedSql.push(...scanSqlSites(file, content, sqlCompiler, sqlImports))
404
485
  }
405
486
 
406
- // Resolved modules stay project-relative, as the scanned files are: the zone of a path is read
407
- // from its folders, and a checkout under a folder named `backend/` must not make it all backend.
408
- // The resolver answers with real paths, so the project root is read as one too.
409
- const realCwd = (() => {
410
- try {
411
- return realpathSync(cwd)
412
- } catch {
413
- return cwd
414
- }
415
- })()
416
- const resolveSpecifier = createSpecifierResolver()
417
487
  const projectPath = (path: string): string => {
418
488
  const rel = relative(realCwd, path).replaceAll("\\", "/")
419
489
  return rel.startsWith("../") || isAbsolute(rel) ? path : rel
@@ -439,7 +509,7 @@ async function buildProjectScan(
439
509
  // An app's `clientModule` is browser code wherever it lives: the generated client entry imports it.
440
510
  const browserRoots = new Set<string>()
441
511
  for (const app of appDirs(source.files)) {
442
- const entry = clientModuleRoot(cwd, app, resolveSpecifier)
512
+ const entry = clientModuleRoot(cwd, app, resolveSpecifier, sqlCompiler)
443
513
  if (entry === undefined) continue
444
514
  const rel = projectPath(entry.file)
445
515
  if (isBrowserSource(rel) || browserRoots.has(rel)) continue
@@ -448,28 +518,24 @@ async function buildProjectScan(
448
518
  browserRoots.add(rel)
449
519
  browserModules.push({ rel, content })
450
520
  }
451
- // Node's lookup: the nearest `node_modules/<name>` at or above the importing file.
521
+ // The side the package gives the entry the import names: package-wide, or per entry.
452
522
  const backendPackage = (fromFile: string, specifier: string): boolean => {
453
523
  const name = specifierPackage(specifier)
454
524
  if (name === undefined) return false
455
- let dir = dirname(isAbsolute(fromFile) ? fromFile : join(realCwd, fromFile))
456
- for (;;) {
457
- const manifest = join(dir, "node_modules", name, "package.json")
458
- if (existsSync(manifest)) {
459
- try {
460
- const nifra: unknown = JSON.parse(readFileSync(manifest, "utf8"))?.nifra
461
- return (
462
- typeof nifra === "object" &&
463
- nifra !== null &&
464
- Reflect.get(nifra, "environment") === "backend"
465
- )
466
- } catch {
467
- return false
468
- }
469
- }
470
- const parent = dirname(dir)
471
- if (parent === dir) return false
472
- dir = parent
525
+ const manifest = installedManifest(
526
+ isAbsolute(fromFile) ? fromFile : join(realCwd, fromFile),
527
+ name,
528
+ )
529
+ if (manifest === undefined) return false
530
+ try {
531
+ const nifra: unknown = JSON.parse(readFileSync(manifest, "utf8"))?.nifra
532
+ return (
533
+ typeof nifra === "object" &&
534
+ nifra !== null &&
535
+ declaredSide(Reflect.get(nifra, "environment"), specifier) === "backend"
536
+ )
537
+ } catch {
538
+ return false
473
539
  }
474
540
  }
475
541
  const serverOnlyChains = createServerOnlyChains({
@@ -591,6 +657,11 @@ export function renderCheckReport(result: CheckResult): string[] {
591
657
  `• intentional external mounts (not typed-client checked): ${result.externalMounts.join(", ")}`,
592
658
  )
593
659
  }
660
+ if (result.tsconfigs !== undefined) {
661
+ lines.push(
662
+ `• typecheck programs (nifra.check.json "tsconfigs"): ${result.tsconfigs.join(", ")}`,
663
+ )
664
+ }
594
665
  for (const gap of result.knownGaps ?? []) {
595
666
  lines.push(`• known gap, not capability-analyzed: ${gap.path} - ${gap.reason}`)
596
667
  }