@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
@@ -0,0 +1,52 @@
1
+ import { diagnostic } from "../diagnostics.js";
2
+ import { projectSite } from "../project-site.js";
3
+ import { commentBlockExpectation } from "./comment-markers.js";
4
+ const MERGE_AUTH_EXPECTATION = ["NF-C043", "merge-auth"];
5
+ const SHOWN_ROUTES = 5;
6
+ /**
7
+ * NF-C043: a `merge()` under a parent whose order-scoped auth - an `authenticate()` stage, or
8
+ * `authenticated` evidence scoped to subsequent routes - covers the parent's own routes, while the
9
+ * merged server authenticates none of its routes. A merged route keeps the chain of the server that
10
+ * declared it, so the parent's auth never runs for it. Read from the app `nifra.assurance.ts` loads
11
+ * (core records each such merge in a development load). A server that authenticates any route of its
12
+ * own is taken to have chosen its own auth and is not reported. `// nifra-expect NF-C043: <reason>` on
13
+ * the merge line accepts a module that is public on purpose.
14
+ */
15
+ export const mergeAuthGapRule = {
16
+ code: "NF-C043",
17
+ title: "Merged routes skip the parent's authentication",
18
+ async scan(ctx) {
19
+ const assurance = ctx.project.policies.assurance;
20
+ if (assurance === undefined || assurance.error !== undefined)
21
+ return [];
22
+ const source = assurance.config?.source;
23
+ if (source === undefined)
24
+ return [];
25
+ const { reflectMergeAuthGaps } = await import("@nifrajs/core/reflection");
26
+ const findings = [];
27
+ for (const gap of reflectMergeAuthGaps(source)) {
28
+ const at = projectSite(ctx.root, gap.site);
29
+ if (at !== undefined) {
30
+ const lines = (ctx.project.source.read(at.file) ?? "").split("\n");
31
+ if (commentBlockExpectation(lines, at.line, MERGE_AUTH_EXPECTATION)?.reason !== undefined)
32
+ continue;
33
+ }
34
+ const shown = gap.routes.slice(0, SHOWN_ROUTES).join(", ");
35
+ const more = gap.routes.length > SHOWN_ROUTES ? ` and ${gap.routes.length - SHOWN_ROUTES} more` : "";
36
+ findings.push(diagnostic({
37
+ code: "NF-C043",
38
+ severity: "warn",
39
+ ...at,
40
+ message: `merge() adds routes that run none of this server's authentication (${gap.auth.join(", ")}): ${shown}${more}. A merged route keeps the hooks of the server that declared it, and that server authenticates none of its routes.`,
41
+ evidence: [
42
+ "merged routes keep the derive/authenticate/beforeHandle chain of the server that declared them",
43
+ "apply the auth plugin on the merged server, or declare its routes in a group() of this one",
44
+ "a module that is public on purpose: `// nifra-expect NF-C043: <reason>` on the merge line",
45
+ ],
46
+ verify: "nifra check",
47
+ }));
48
+ }
49
+ return findings;
50
+ },
51
+ };
52
+ //# sourceMappingURL=merge-auth.js.map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IAAI,CAAC,CAAA;gBAClE,IAAI,uBAAuB,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,EAAE,sBAAsB,CAAC,EAAE,MAAM,KAAK,SAAS;oBACvF,SAAQ;YACZ,CAAC;YACD,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YAC1D,MAAM,IAAI,GACR,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,YAAY,OAAO,CAAC,CAAC,CAAC,EAAE,CAAA;YACzF,QAAQ,CAAC,IAAI,CACX,UAAU,CAAC;gBACT,IAAI,EAAE,SAAS;gBACf,QAAQ,EAAE,MAAM;gBAChB,GAAG,EAAE;gBACL,OAAO,EAAE,sEAAsE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,oHAAoH;gBACxO,QAAQ,EAAE;oBACR,gGAAgG;oBAChG,4FAA4F;oBAC5F,2FAA2F;iBAC5F;gBACD,MAAM,EAAE,aAAa;aACtB,CAAC,CACH,CAAA;QACH,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;CACF,CAAA"}
@@ -18,14 +18,51 @@ export declare const shadowedPageRule: CheckRule;
18
18
  * A page that names `inLayouts` at all has decided, so it is not reported.
19
19
  */
20
20
  export declare const statusLayoutRule: CheckRule;
21
+ /** The two framework configs' sources, `undefined` for one the app lacks. */
22
+ export interface ApiPrefixSources {
23
+ readonly framework: string | undefined;
24
+ readonly config: string | undefined;
25
+ }
26
+ /** What a `createWebApp` call passes as `apiPrefix`: a value, the framework config's own, or neither. */
27
+ type EntryApiPrefix = {
28
+ readonly kind: "literal";
29
+ readonly value: string;
30
+ readonly unset: boolean;
31
+ } | {
32
+ readonly kind: "config";
33
+ } | {
34
+ readonly kind: "unreadable";
35
+ };
36
+ /** A `createWebApp` call that mounts a backend (`api`), with the `apiPrefix` it passes. */
37
+ export interface EntryApiMount {
38
+ readonly line: number;
39
+ readonly apiPrefix: EntryApiPrefix;
40
+ }
41
+ /**
42
+ * Every `createWebApp({ api, ... })` call in a module, `createWebApp` imported from `@nifrajs/web`.
43
+ * `apiPrefix` reads as a string literal, a same-module string `const`, or the framework config's own
44
+ * export (`import { apiPrefix } from "./backend/framework"`); a call whose options come from a spread
45
+ * or a variable is unreadable. A call with no `api` mounts nothing and is left out. Pure.
46
+ */
47
+ export declare function entryApiMounts(ts: typeof TSApi, tree: TSApi.SourceFile, file: string): EntryApiMount[];
21
48
  /**
22
- * NF-C035: `backend/app.ts` exports a backend and the framework config sets no `apiPrefix`, so the
23
- * backend is not served over HTTP - a browser call to `/api` reaches page routing. Loader-only is a
24
- * valid choice; the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's to report.
49
+ * NF-C035: `backend/app.ts` exports a backend that is not served over HTTP, so a browser call to `/api`
50
+ * reaches page routing. The framework config sets no `apiPrefix`; loader-only is a valid choice, and
51
+ * the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's.
52
+ *
53
+ * An app module that calls `createWebApp` itself is checked against the same value, since only the
54
+ * framework config reaches `nifra dev`: a backend it mounts at another `apiPrefix`, or with none, is
55
+ * served in production where dev never served it.
25
56
  */
26
57
  export declare const implicitApiMountRule: CheckRule;
27
58
  /** Append `export const apiPrefix = "/api"` to a framework config that sets none, creating it if needed. */
28
59
  export declare function withDefaultApiPrefix(source: string | undefined): string | undefined;
60
+ /**
61
+ * The edit that serves the backend at `/api`: the mount in backend/framework.ts, where the server
62
+ * entry reads it, or in nifra.config.ts when it is the only config. A file that needs no edit comes
63
+ * back `undefined`; an `apiPrefix` either file sets is never replaced.
64
+ */
65
+ export declare function apiPrefixFix(sources: ApiPrefixSources): ApiPrefixSources;
29
66
  /** Where an exported function `meta` reads its args' `data`: a destructured `data`, or `args.data`. */
30
67
  export declare function metaDataReads(ts: typeof TSApi, tree: TSApi.SourceFile): number[];
31
68
  /**
@@ -35,5 +72,21 @@ export declare function metaDataReads(ts: typeof TSApi, tree: TSApi.SourceFile):
35
72
  * a `meta` was written against. A layout with a loader reading `data` reads its own, so it is not reported.
36
73
  */
37
74
  export declare const layoutMetaDataRule: CheckRule;
75
+ /** Lines where an exported `loader` refuses access: `redirect()`, `statusPage(401|403)`, `new Response()`. */
76
+ export declare function loaderRefusals(ts: typeof TSApi, tree: TSApi.SourceFile): number[];
77
+ /**
78
+ * NF-C042: a layout loader that refuses access - a redirect, a 401 or 403, a `Response` - in a layout
79
+ * that is not a gate and exports no middleware. A client navigation that keeps the layout skips that
80
+ * loader, and an action below it runs without it, so the check reads as a guard and is not one. A
81
+ * refusal inside a helper the loader calls is not seen; the development warning covers that.
82
+ */
83
+ export declare const ungatedLayoutRefusalRule: CheckRule;
84
+ /**
85
+ * NF-C041: a route's frontend file (`x.tsx`, a layout, a status or error page) exports a name only
86
+ * the server reads, such as `loader` or `action`. The frontend file ships to the browser whole, so
87
+ * `nifra build` and the server refuse it; this reports it before either runs.
88
+ */
89
+ export declare const frontendServerExportRule: CheckRule;
38
90
  export declare const pageRules: readonly CheckRule[];
91
+ export {};
39
92
  //# sourceMappingURL=pages.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pages.d.ts","sourceRoot":"","sources":["../../src/rules/pages.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,KAAK,KAAK,MAAM,YAAY,CAAA;AAOxC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAG3C,uEAAuE;AACvE,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;CAAE,GAC5B;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACpD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAA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E,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,CAAC,UAAU,GAAG,MAAM,EAAE,CA4BjF;AAED;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,EAAE,SA2DtC,CAAA;AA0CD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,EAAE,SAyDtC,CAAA;AAED,eAAO,MAAM,SAAS,sBAOpB,CAAA"}
@@ -12,9 +12,9 @@
12
12
  * startup and build checks.
13
13
  */
14
14
  import { existsSync, readFileSync } from "node:fs";
15
- import { join } from "node:path";
15
+ import { join, posix } from "node:path";
16
16
  import { discoverRoutes } from "@nifrajs/web/fs";
17
- import { shadowedPages } from "@nifrajs/web/route-manifest";
17
+ import { BACKEND_ROUTE_EXPORTS, shadowedPages } from "@nifrajs/web/route-manifest";
18
18
  import { BACKEND_APP_FILE, CONFIG_FILE, FRAMEWORK_FILE } from "../app-files.js";
19
19
  import { stripComments } from "../check-scan.js";
20
20
  import { diagnostic } from "../diagnostics.js";
@@ -144,10 +144,182 @@ export const statusLayoutRule = {
144
144
  return findings;
145
145
  },
146
146
  };
147
+ /** `apiPrefix` as the app runs it: nifra.config.ts's own, else backend/framework.ts's, which every
148
+ * command takes for an option nifra.config.ts leaves unset. */
149
+ function effectiveApiPrefix({ framework, config }) {
150
+ const own = config === undefined ? { kind: "default" } : readStaticApiPrefix(config);
151
+ return own.kind !== "default" || framework === undefined ? own : readStaticApiPrefix(framework);
152
+ }
153
+ const SCRIPT_SOURCE = /\.[cm]?[jt]sx?$/;
154
+ const WEB_PACKAGE = "@nifrajs/web";
155
+ const CONFIG_MODULES = new Set([FRAMEWORK_FILE, CONFIG_FILE].map((file) => file.replace(SCRIPT_SOURCE, "")));
156
+ /** Whether a relative specifier in `file` names backend/framework.ts or nifra.config.ts. */
157
+ const namesFrameworkConfig = (file, specifier) => specifier.startsWith(".") &&
158
+ CONFIG_MODULES.has(posix.join(posix.dirname(file), specifier).replace(SCRIPT_SOURCE, ""));
159
+ const unwrapExpression = (ts, node) => {
160
+ let expr = node;
161
+ while (ts.isParenthesizedExpression(expr) ||
162
+ ts.isAsExpression(expr) ||
163
+ ts.isSatisfiesExpression(expr)) {
164
+ expr = expr.expression;
165
+ }
166
+ return expr;
167
+ };
147
168
  /**
148
- * NF-C035: `backend/app.ts` exports a backend and the framework config sets no `apiPrefix`, so the
149
- * backend is not served over HTTP - a browser call to `/api` reaches page routing. Loader-only is a
150
- * valid choice; the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's to report.
169
+ * Every `createWebApp({ api, ... })` call in a module, `createWebApp` imported from `@nifrajs/web`.
170
+ * `apiPrefix` reads as a string literal, a same-module string `const`, or the framework config's own
171
+ * export (`import { apiPrefix } from "./backend/framework"`); a call whose options come from a spread
172
+ * or a variable is unreadable. A call with no `api` mounts nothing and is left out. Pure.
173
+ */
174
+ export function entryApiMounts(ts, tree, file) {
175
+ const webNames = new Set();
176
+ const webNamespaces = new Set();
177
+ const configNames = new Set();
178
+ const configNamespaces = new Set();
179
+ const strings = new Map();
180
+ for (const statement of tree.statements) {
181
+ if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
182
+ const from = statement.moduleSpecifier.text;
183
+ const bindings = statement.importClause?.namedBindings;
184
+ const isWeb = from === WEB_PACKAGE;
185
+ const isConfig = namesFrameworkConfig(file, from);
186
+ if (bindings === undefined || (!isWeb && !isConfig))
187
+ continue;
188
+ if (ts.isNamespaceImport(bindings)) {
189
+ ;
190
+ (isWeb ? webNamespaces : configNamespaces).add(bindings.name.text);
191
+ continue;
192
+ }
193
+ for (const element of bindings.elements) {
194
+ const imported = (element.propertyName ?? element.name).text;
195
+ if (isWeb && imported === "createWebApp")
196
+ webNames.add(element.name.text);
197
+ if (isConfig && imported === "apiPrefix")
198
+ configNames.add(element.name.text);
199
+ }
200
+ continue;
201
+ }
202
+ if (!ts.isVariableStatement(statement) ||
203
+ (statement.declarationList.flags & ts.NodeFlags.Const) === 0)
204
+ continue;
205
+ for (const declaration of statement.declarationList.declarations) {
206
+ const init = declaration.initializer === undefined
207
+ ? undefined
208
+ : unwrapExpression(ts, declaration.initializer);
209
+ if (ts.isIdentifier(declaration.name) &&
210
+ init !== undefined &&
211
+ (ts.isStringLiteral(init) || ts.isNoSubstitutionTemplateLiteral(init)))
212
+ strings.set(declaration.name.text, init.text);
213
+ }
214
+ }
215
+ if (webNames.size === 0 && webNamespaces.size === 0)
216
+ return [];
217
+ const readPrefix = (node) => {
218
+ const expr = unwrapExpression(ts, node);
219
+ if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
220
+ return { kind: "literal", value: expr.text, unset: false };
221
+ if (ts.isIdentifier(expr)) {
222
+ if (configNames.has(expr.text))
223
+ return { kind: "config" };
224
+ const value = strings.get(expr.text);
225
+ return value === undefined ? { kind: "unreadable" } : { kind: "literal", value, unset: false };
226
+ }
227
+ if (ts.isPropertyAccessExpression(expr) &&
228
+ ts.isIdentifier(expr.expression) &&
229
+ configNamespaces.has(expr.expression.text) &&
230
+ expr.name.text === "apiPrefix")
231
+ return { kind: "config" };
232
+ return { kind: "unreadable" };
233
+ };
234
+ const isCreateWebApp = (callee) => ts.isIdentifier(callee)
235
+ ? webNames.has(callee.text)
236
+ : ts.isPropertyAccessExpression(callee) &&
237
+ ts.isIdentifier(callee.expression) &&
238
+ webNamespaces.has(callee.expression.text) &&
239
+ callee.name.text === "createWebApp";
240
+ const propertyName = (property) => property.name !== undefined &&
241
+ (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))
242
+ ? property.name.text
243
+ : undefined;
244
+ const mounts = [];
245
+ const visit = (node) => {
246
+ const arg = ts.isCallExpression(node) ? node.arguments[0] : undefined;
247
+ const options = arg === undefined ? undefined : unwrapExpression(ts, arg);
248
+ if (ts.isCallExpression(node) &&
249
+ isCreateWebApp(node.expression) &&
250
+ options !== undefined &&
251
+ ts.isObjectLiteralExpression(options) &&
252
+ options.properties.some((property) => propertyName(property) === "api")) {
253
+ // The last of an `apiPrefix` property and a spread decides, as it does at runtime.
254
+ let apiPrefix = { kind: "literal", value: "", unset: true };
255
+ for (const property of options.properties) {
256
+ if (ts.isSpreadAssignment(property))
257
+ apiPrefix = { kind: "unreadable" };
258
+ else if (propertyName(property) !== "apiPrefix")
259
+ continue;
260
+ else if (ts.isPropertyAssignment(property))
261
+ apiPrefix = readPrefix(property.initializer);
262
+ else if (ts.isShorthandPropertyAssignment(property))
263
+ apiPrefix = readPrefix(property.name);
264
+ else
265
+ apiPrefix = { kind: "unreadable" };
266
+ }
267
+ const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1;
268
+ mounts.push({ line, apiPrefix });
269
+ }
270
+ ts.forEachChild(node, visit);
271
+ };
272
+ visit(tree);
273
+ return mounts;
274
+ }
275
+ const servedAt = (prefix) => (prefix === "" ? "nowhere" : `at ${prefix}`);
276
+ /**
277
+ * The NF-C035 findings for app modules that start their own server: a `createWebApp` call that
278
+ * mounts a backend at a different `apiPrefix` than the one `nifra dev` reads, or with none, so
279
+ * production serves the backend somewhere dev and e2e never exercised.
280
+ */
281
+ async function entryApiPrefixFindings(ctx, expected, origin) {
282
+ const candidates = ctx.sources.files.filter((file) => SCRIPT_SOURCE.test(file) && ctx.sources.read(file)?.includes("createWebApp") === true);
283
+ if (candidates.length === 0)
284
+ return [];
285
+ const ts = (await loadRuleTypeScript(ctx)).compiler;
286
+ if (ts === undefined)
287
+ return [];
288
+ const findings = [];
289
+ for (const file of [...candidates].sort(codeUnitOrder)) {
290
+ const text = ctx.sources.read(file) ?? "";
291
+ const tree = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file));
292
+ for (const { line, apiPrefix } of entryApiMounts(ts, tree, file)) {
293
+ if (apiPrefix.kind !== "literal" || apiPrefix.value === expected)
294
+ continue;
295
+ const specifier = posix.relative(posix.dirname(file), origin.replace(SCRIPT_SOURCE, ""));
296
+ const passed = apiPrefix.unset
297
+ ? "no apiPrefix"
298
+ : `apiPrefix ${JSON.stringify(apiPrefix.value)}`;
299
+ findings.push(diagnostic({
300
+ code: "NF-C035",
301
+ severity: "warn",
302
+ file,
303
+ line,
304
+ message: `${file} calls createWebApp with a backend and ${passed}, so the server it starts serves the backend ${servedAt(apiPrefix.value)}, while \`nifra dev\` serves it ${servedAt(expected)} (${origin}). Pass the framework config's value: \`import { apiPrefix } from "${specifier.startsWith(".") ? specifier : `./${specifier}`}"\` and \`createWebApp({ ..., apiPrefix })\`.`,
305
+ evidence: [
306
+ `createWebApp apiPrefix: ${apiPrefix.unset ? "unset" : JSON.stringify(apiPrefix.value)}`,
307
+ `${origin} apiPrefix: ${JSON.stringify(expected)}`,
308
+ ],
309
+ verify: "nifra check",
310
+ }));
311
+ }
312
+ }
313
+ return findings;
314
+ }
315
+ /**
316
+ * NF-C035: `backend/app.ts` exports a backend that is not served over HTTP, so a browser call to `/api`
317
+ * reaches page routing. The framework config sets no `apiPrefix`; loader-only is a valid choice, and
318
+ * the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's.
319
+ *
320
+ * An app module that calls `createWebApp` itself is checked against the same value, since only the
321
+ * framework config reaches `nifra dev`: a backend it mounts at another `apiPrefix`, or with none, is
322
+ * served in production where dev never served it.
151
323
  */
152
324
  export const implicitApiMountRule = {
153
325
  code: "NF-C035",
@@ -159,47 +331,71 @@ export const implicitApiMountRule = {
159
331
  return [];
160
332
  if (!BACKEND_EXPORT.test(stripComments(readFileSync(backendFile, "utf8"))))
161
333
  return [];
162
- const configFile = CONFIG_FILES.find((file) => existsSync(join(ctx.root, file)));
163
- if (configFile !== undefined &&
164
- readStaticApiPrefix(readFileSync(join(ctx.root, configFile), "utf8")).kind !== "default") {
334
+ const read = (file) => existsSync(join(ctx.root, file)) ? readFileSync(join(ctx.root, file), "utf8") : undefined;
335
+ const sources = { framework: read(FRAMEWORK_FILE), config: read(CONFIG_FILE) };
336
+ if (sources.framework === undefined && sources.config === undefined)
165
337
  return [];
166
- }
167
- const file = configFile ?? FRAMEWORK_FILE;
338
+ const effective = effectiveApiPrefix(sources);
339
+ if (effective.kind === "unreadable")
340
+ return [];
341
+ const configOwn = sources.config === undefined ? undefined : readStaticApiPrefix(sources.config);
342
+ const origin = configOwn !== undefined && (configOwn.kind !== "default" || sources.framework === undefined)
343
+ ? CONFIG_FILE
344
+ : FRAMEWORK_FILE;
345
+ const entries = await entryApiPrefixFindings(ctx, effective.kind === "literal" ? effective.value : "", origin);
346
+ if (effective.kind === "literal")
347
+ return entries;
348
+ const file = sources.framework === undefined ? CONFIG_FILE : FRAMEWORK_FILE;
168
349
  return [
169
350
  diagnostic({
170
351
  code: "NF-C035",
171
- severity: "info",
352
+ severity: "warn",
172
353
  file,
173
354
  message: `${BACKEND_APP_FILE}'s backend is not served over HTTP, because ${file} sets no apiPrefix: a browser call to /api reaches page routing instead. Add \`export const apiPrefix = "/api"\` to ${file} to serve it there, as 4.x did by default, or \`export const apiPrefix = ""\` to keep it loader-only.`,
174
355
  fix: { recipe: "framework.api-prefix", command: "nifra fix --code NF-C035" },
175
356
  verify: "nifra check",
176
357
  }),
358
+ ...entries,
177
359
  ];
178
360
  },
179
361
  };
362
+ const appendLine = (source, line) => source === "" ? line : `${source}${source.endsWith("\n") ? "" : "\n"}${line}`;
180
363
  /** Append `export const apiPrefix = "/api"` to a framework config that sets none, creating it if needed. */
181
364
  export function withDefaultApiPrefix(source) {
182
365
  if (source !== undefined && readStaticApiPrefix(source).kind !== "default")
183
366
  return undefined;
184
- const line = 'export const apiPrefix = "/api"\n';
185
- if (source === undefined || source === "")
186
- return line;
187
- return `${source}${source.endsWith("\n") ? "" : "\n"}${line}`;
367
+ return appendLine(source ?? "", 'export const apiPrefix = "/api"\n');
368
+ }
369
+ /**
370
+ * The edit that serves the backend at `/api`: the mount in backend/framework.ts, where the server
371
+ * entry reads it, or in nifra.config.ts when it is the only config. A file that needs no edit comes
372
+ * back `undefined`; an `apiPrefix` either file sets is never replaced.
373
+ */
374
+ export function apiPrefixFix(sources) {
375
+ if (effectiveApiPrefix(sources).kind !== "default")
376
+ return { framework: undefined, config: undefined };
377
+ return sources.framework === undefined
378
+ ? { framework: undefined, config: withDefaultApiPrefix(sources.config) }
379
+ : { framework: withDefaultApiPrefix(sources.framework), config: undefined };
188
380
  }
189
381
  const LOADER_EXPORT = /\bexport\s+(?:async\s+)?(?:const|let|var|function\*?)\s+loader\b|\bexport\s*\{[^}]*\bloader\b[^}]*\}/;
190
382
  const SCRIPT_LAYOUT = /\.[cm]?[jt]sx?$/;
191
- const hasModifier = (ts, node, kind) => ts.canHaveModifiers(node) && (ts.getModifiers(node) ?? []).some((m) => m.kind === kind);
192
- /** The function an exported `meta` names: a declaration, or a const initialized with one. */
193
- function exportedMetaFunction(ts, tree) {
383
+ // Read from the node, not `ts.getModifiers`: TypeScript 7's API has no getModifiers or canHaveModifiers.
384
+ const hasModifier = (node, kind) => node.modifiers?.some((m) => m.kind === kind) === true;
385
+ /** The function an exported `name` names: a declaration, or a const initialized with one. */
386
+ function exportedFunction(ts, tree, name) {
194
387
  for (const statement of tree.statements) {
195
- if (!hasModifier(ts, statement, ts.SyntaxKind.ExportKeyword))
388
+ if (ts.isFunctionDeclaration(statement)) {
389
+ if (statement.name?.text === name && hasModifier(statement, ts.SyntaxKind.ExportKeyword))
390
+ return statement;
196
391
  continue;
197
- if (ts.isFunctionDeclaration(statement) && statement.name?.text === "meta")
198
- return statement;
392
+ }
199
393
  if (!ts.isVariableStatement(statement))
200
394
  continue;
395
+ if (!hasModifier(statement, ts.SyntaxKind.ExportKeyword))
396
+ continue;
201
397
  for (const declaration of statement.declarationList.declarations) {
202
- if (!ts.isIdentifier(declaration.name) || declaration.name.text !== "meta")
398
+ if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name)
203
399
  continue;
204
400
  let init = declaration.initializer;
205
401
  while (init !== undefined &&
@@ -216,7 +412,7 @@ function exportedMetaFunction(ts, tree) {
216
412
  }
217
413
  /** Where an exported function `meta` reads its args' `data`: a destructured `data`, or `args.data`. */
218
414
  export function metaDataReads(ts, tree) {
219
- const meta = exportedMetaFunction(ts, tree);
415
+ const meta = exportedFunction(ts, tree, "meta");
220
416
  const param = meta?.parameters[0];
221
417
  if (param === undefined)
222
418
  return [];
@@ -297,10 +493,203 @@ export const layoutMetaDataRule = {
297
493
  return findings;
298
494
  },
299
495
  };
496
+ const GATE_EXPORT = /\bexport\s+const\s+gate\s*(?::\s*boolean\s*)?=\s*true\b/;
497
+ const MIDDLEWARE_EXPORT = /\bexport\s+(?:async\s+)?(?:const|let|var|function\*?)\s+middleware\b|\bexport\s*\{[^}]*\bmiddleware\b[^}]*\}/;
498
+ /** Lines where an exported `loader` refuses access: `redirect()`, `statusPage(401|403)`, `new Response()`. */
499
+ export function loaderRefusals(ts, tree) {
500
+ const loader = exportedFunction(ts, tree, "loader");
501
+ if (loader?.body === undefined)
502
+ return [];
503
+ const lines = [];
504
+ const lineOf = (node) => tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1;
505
+ const visit = (node) => {
506
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
507
+ const status = node.arguments[0];
508
+ if (node.expression.text === "redirect" ||
509
+ (node.expression.text === "statusPage" &&
510
+ status !== undefined &&
511
+ ts.isNumericLiteral(status) &&
512
+ (status.text === "401" || status.text === "403")))
513
+ lines.push(lineOf(node));
514
+ }
515
+ else if (ts.isNewExpression(node) &&
516
+ ts.isIdentifier(node.expression) &&
517
+ node.expression.text === "Response") {
518
+ lines.push(lineOf(node));
519
+ }
520
+ ts.forEachChild(node, visit);
521
+ };
522
+ visit(loader.body);
523
+ return lines;
524
+ }
525
+ /**
526
+ * NF-C042: a layout loader that refuses access - a redirect, a 401 or 403, a `Response` - in a layout
527
+ * that is not a gate and exports no middleware. A client navigation that keeps the layout skips that
528
+ * loader, and an action below it runs without it, so the check reads as a guard and is not one. A
529
+ * refusal inside a helper the loader calls is not seen; the development warning covers that.
530
+ */
531
+ export const ungatedLayoutRefusalRule = {
532
+ code: "NF-C042",
533
+ title: "Layout loader refuses access without a gate",
534
+ async scan(ctx) {
535
+ const routesDir = join(ctx.root, "routes");
536
+ if (!existsSync(routesDir))
537
+ return [];
538
+ let manifest;
539
+ try {
540
+ manifest = discoverRoutes(routesDir);
541
+ }
542
+ catch {
543
+ return [];
544
+ }
545
+ const layouts = Object.values(manifest.layouts)
546
+ .filter((layout) => SCRIPT_LAYOUT.test(layout.file))
547
+ .sort((a, b) => codeUnitOrder(a.file, b.file));
548
+ if (layouts.length === 0)
549
+ return [];
550
+ const sources = layouts.map((layout) => [layout.file, ...(layout.backend === undefined ? [] : [layout.backend])].map((file) => ({
551
+ file,
552
+ text: readFileSync(join(routesDir, file), "utf8"),
553
+ })));
554
+ if (!sources.flat().some((half) => LOADER_EXPORT.test(stripComments(half.text))))
555
+ return [];
556
+ const loaded = await loadRuleTypeScript(ctx);
557
+ const ts = loaded.compiler;
558
+ if (ts === undefined)
559
+ return [];
560
+ const findings = [];
561
+ for (const halves of sources) {
562
+ const code = halves.map((half) => stripComments(half.text));
563
+ if (code.some((text) => GATE_EXPORT.test(text) || MIDDLEWARE_EXPORT.test(text)))
564
+ continue;
565
+ for (const half of halves) {
566
+ if (!LOADER_EXPORT.test(stripComments(half.text)))
567
+ continue;
568
+ const tree = ts.createSourceFile(half.file, half.text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, half.file));
569
+ const [line] = loaderRefusals(ts, tree);
570
+ if (line === undefined)
571
+ continue;
572
+ findings.push(diagnostic({
573
+ code: "NF-C042",
574
+ severity: "warn",
575
+ file: `routes/${half.file}`,
576
+ line,
577
+ message: `routes/${half.file}'s loader refuses access, but the layout is not a gate. A client navigation that keeps the layout skips this loader, and an action below it runs without it, so it does not protect the pages under it. Export \`gate = true\` from this file, or move the check into its \`middleware\`.`,
578
+ evidence: [
579
+ "gate = true: the loader runs on every request and before every action, ahead of the page loader",
580
+ "middleware: runs before every page and action below the directory",
581
+ ],
582
+ verify: "nifra check",
583
+ }));
584
+ }
585
+ }
586
+ return findings;
587
+ },
588
+ };
589
+ /** The names a module exports at runtime, each with its line: declarations and `export { ... }` lists.
590
+ * `export *` names nothing it can read, and type-only exports carry no value. */
591
+ function runtimeExports(ts, tree) {
592
+ const found = [];
593
+ const add = (name, node) => {
594
+ found.push({ name, line: tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1 });
595
+ };
596
+ for (const statement of tree.statements) {
597
+ if (ts.isExportDeclaration(statement)) {
598
+ if (statement.isTypeOnly || statement.exportClause === undefined)
599
+ continue;
600
+ if (!ts.isNamedExports(statement.exportClause))
601
+ continue;
602
+ for (const element of statement.exportClause.elements) {
603
+ if (!element.isTypeOnly)
604
+ add(element.name.text, element);
605
+ }
606
+ continue;
607
+ }
608
+ if (!(ts.isVariableStatement(statement) ||
609
+ ts.isFunctionDeclaration(statement) ||
610
+ ts.isClassDeclaration(statement)) ||
611
+ !hasModifier(statement, ts.SyntaxKind.ExportKeyword) ||
612
+ hasModifier(statement, ts.SyntaxKind.DeclareKeyword))
613
+ continue;
614
+ if (ts.isVariableStatement(statement)) {
615
+ for (const declaration of statement.declarationList.declarations) {
616
+ if (ts.isIdentifier(declaration.name))
617
+ add(declaration.name.text, declaration);
618
+ }
619
+ }
620
+ else if (statement.name !== undefined) {
621
+ add(statement.name.text, statement);
622
+ }
623
+ }
624
+ return found;
625
+ }
626
+ /**
627
+ * NF-C041: a route's frontend file (`x.tsx`, a layout, a status or error page) exports a name only
628
+ * the server reads, such as `loader` or `action`. The frontend file ships to the browser whole, so
629
+ * `nifra build` and the server refuse it; this reports it before either runs.
630
+ */
631
+ export const frontendServerExportRule = {
632
+ code: "NF-C041",
633
+ title: "Server-only export in a route frontend file",
634
+ async scan(ctx) {
635
+ const routesDir = join(ctx.root, "routes");
636
+ if (!existsSync(routesDir))
637
+ return [];
638
+ let manifest;
639
+ try {
640
+ manifest = discoverRoutes(routesDir);
641
+ }
642
+ catch {
643
+ return [];
644
+ }
645
+ const files = new Set();
646
+ for (const entry of [
647
+ ...manifest.routes,
648
+ ...Object.values(manifest.layouts),
649
+ ...Object.values(manifest.errors ?? {}),
650
+ ...(manifest.notFound === undefined ? [] : [manifest.notFound]),
651
+ ...Object.values(manifest.notFounds ?? {}),
652
+ ...Object.values(manifest.statusPages ?? {}),
653
+ ...Object.values(manifest.loadings ?? {}),
654
+ ]) {
655
+ if (SCRIPT_LAYOUT.test(entry.file))
656
+ files.add(entry.file);
657
+ }
658
+ if (files.size === 0)
659
+ return [];
660
+ const loaded = await loadRuleTypeScript(ctx);
661
+ const ts = loaded.compiler;
662
+ if (ts === undefined)
663
+ return [];
664
+ const findings = [];
665
+ for (const file of [...files].sort(codeUnitOrder)) {
666
+ const text = readFileSync(join(routesDir, file), "utf8");
667
+ const tree = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file));
668
+ for (const { name, line } of runtimeExports(ts, tree)) {
669
+ if (!BACKEND_ROUTE_EXPORTS.has(name))
670
+ continue;
671
+ const backend = `routes/${file.replace(SCRIPT_LAYOUT, ".backend.ts")}`;
672
+ findings.push(diagnostic({
673
+ code: "NF-C041",
674
+ severity: "error",
675
+ file: `routes/${file}`,
676
+ line,
677
+ message: `routes/${file} exports "${name}", which runs on the server only, and a route's frontend file ships to the browser whole, so nifra build and the server refuse it. Move it to ${backend}.`,
678
+ evidence: [`export: ${name}`, `backend half: ${backend}`],
679
+ fix: { recipe: "route.split", command: "nifra fix --code NF-C041" },
680
+ verify: "nifra check",
681
+ }));
682
+ }
683
+ }
684
+ return findings;
685
+ },
686
+ };
300
687
  export const pageRules = Object.freeze([
301
688
  shadowedPageRule,
302
689
  statusLayoutRule,
303
690
  implicitApiMountRule,
304
691
  layoutMetaDataRule,
692
+ frontendServerExportRule,
693
+ ungatedLayoutRefusalRule,
305
694
  ]);
306
695
  //# sourceMappingURL=pages.js.map