@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
@@ -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 type * as TSApi from "typescript"
19
19
  import { BACKEND_APP_FILE, CONFIG_FILE, FRAMEWORK_FILE } from "../app-files.ts"
20
20
  import { stripComments } from "../check-scan.ts"
@@ -159,10 +159,230 @@ export const statusLayoutRule: CheckRule = {
159
159
  },
160
160
  }
161
161
 
162
+ /** The two framework configs' sources, `undefined` for one the app lacks. */
163
+ export interface ApiPrefixSources {
164
+ readonly framework: string | undefined
165
+ readonly config: string | undefined
166
+ }
167
+
168
+ /** `apiPrefix` as the app runs it: nifra.config.ts's own, else backend/framework.ts's, which every
169
+ * command takes for an option nifra.config.ts leaves unset. */
170
+ function effectiveApiPrefix({ framework, config }: ApiPrefixSources): StaticApiPrefix {
171
+ const own: StaticApiPrefix =
172
+ config === undefined ? { kind: "default" } : readStaticApiPrefix(config)
173
+ return own.kind !== "default" || framework === undefined ? own : readStaticApiPrefix(framework)
174
+ }
175
+
176
+ /** What a `createWebApp` call passes as `apiPrefix`: a value, the framework config's own, or neither. */
177
+ type EntryApiPrefix =
178
+ | { readonly kind: "literal"; readonly value: string; readonly unset: boolean }
179
+ | { readonly kind: "config" }
180
+ | { readonly kind: "unreadable" }
181
+
182
+ const SCRIPT_SOURCE = /\.[cm]?[jt]sx?$/
183
+ const WEB_PACKAGE = "@nifrajs/web"
184
+ const CONFIG_MODULES: ReadonlySet<string> = new Set(
185
+ [FRAMEWORK_FILE, CONFIG_FILE].map((file) => file.replace(SCRIPT_SOURCE, "")),
186
+ )
187
+
188
+ /** Whether a relative specifier in `file` names backend/framework.ts or nifra.config.ts. */
189
+ const namesFrameworkConfig = (file: string, specifier: string): boolean =>
190
+ specifier.startsWith(".") &&
191
+ CONFIG_MODULES.has(posix.join(posix.dirname(file), specifier).replace(SCRIPT_SOURCE, ""))
192
+
193
+ const unwrapExpression = (ts: typeof TSApi, node: TSApi.Expression): TSApi.Expression => {
194
+ let expr = node
195
+ while (
196
+ ts.isParenthesizedExpression(expr) ||
197
+ ts.isAsExpression(expr) ||
198
+ ts.isSatisfiesExpression(expr)
199
+ ) {
200
+ expr = expr.expression
201
+ }
202
+ return expr
203
+ }
204
+
205
+ /** A `createWebApp` call that mounts a backend (`api`), with the `apiPrefix` it passes. */
206
+ export interface EntryApiMount {
207
+ readonly line: number
208
+ readonly apiPrefix: EntryApiPrefix
209
+ }
210
+
211
+ /**
212
+ * Every `createWebApp({ api, ... })` call in a module, `createWebApp` imported from `@nifrajs/web`.
213
+ * `apiPrefix` reads as a string literal, a same-module string `const`, or the framework config's own
214
+ * export (`import { apiPrefix } from "./backend/framework"`); a call whose options come from a spread
215
+ * or a variable is unreadable. A call with no `api` mounts nothing and is left out. Pure.
216
+ */
217
+ export function entryApiMounts(
218
+ ts: typeof TSApi,
219
+ tree: TSApi.SourceFile,
220
+ file: string,
221
+ ): EntryApiMount[] {
222
+ const webNames = new Set<string>()
223
+ const webNamespaces = new Set<string>()
224
+ const configNames = new Set<string>()
225
+ const configNamespaces = new Set<string>()
226
+ const strings = new Map<string, string>()
227
+ for (const statement of tree.statements) {
228
+ if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
229
+ const from = statement.moduleSpecifier.text
230
+ const bindings = statement.importClause?.namedBindings
231
+ const isWeb = from === WEB_PACKAGE
232
+ const isConfig = namesFrameworkConfig(file, from)
233
+ if (bindings === undefined || (!isWeb && !isConfig)) continue
234
+ if (ts.isNamespaceImport(bindings)) {
235
+ ;(isWeb ? webNamespaces : configNamespaces).add(bindings.name.text)
236
+ continue
237
+ }
238
+ for (const element of bindings.elements) {
239
+ const imported = (element.propertyName ?? element.name).text
240
+ if (isWeb && imported === "createWebApp") webNames.add(element.name.text)
241
+ if (isConfig && imported === "apiPrefix") configNames.add(element.name.text)
242
+ }
243
+ continue
244
+ }
245
+ if (
246
+ !ts.isVariableStatement(statement) ||
247
+ (statement.declarationList.flags & ts.NodeFlags.Const) === 0
248
+ )
249
+ continue
250
+ for (const declaration of statement.declarationList.declarations) {
251
+ const init =
252
+ declaration.initializer === undefined
253
+ ? undefined
254
+ : unwrapExpression(ts, declaration.initializer)
255
+ if (
256
+ ts.isIdentifier(declaration.name) &&
257
+ init !== undefined &&
258
+ (ts.isStringLiteral(init) || ts.isNoSubstitutionTemplateLiteral(init))
259
+ )
260
+ strings.set(declaration.name.text, init.text)
261
+ }
262
+ }
263
+ if (webNames.size === 0 && webNamespaces.size === 0) return []
264
+
265
+ const readPrefix = (node: TSApi.Expression): EntryApiPrefix => {
266
+ const expr = unwrapExpression(ts, node)
267
+ if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
268
+ return { kind: "literal", value: expr.text, unset: false }
269
+ if (ts.isIdentifier(expr)) {
270
+ if (configNames.has(expr.text)) return { kind: "config" }
271
+ const value = strings.get(expr.text)
272
+ return value === undefined ? { kind: "unreadable" } : { kind: "literal", value, unset: false }
273
+ }
274
+ if (
275
+ ts.isPropertyAccessExpression(expr) &&
276
+ ts.isIdentifier(expr.expression) &&
277
+ configNamespaces.has(expr.expression.text) &&
278
+ expr.name.text === "apiPrefix"
279
+ )
280
+ return { kind: "config" }
281
+ return { kind: "unreadable" }
282
+ }
283
+ const isCreateWebApp = (callee: TSApi.Expression): boolean =>
284
+ ts.isIdentifier(callee)
285
+ ? webNames.has(callee.text)
286
+ : ts.isPropertyAccessExpression(callee) &&
287
+ ts.isIdentifier(callee.expression) &&
288
+ webNamespaces.has(callee.expression.text) &&
289
+ callee.name.text === "createWebApp"
290
+ const propertyName = (property: TSApi.ObjectLiteralElementLike): string | undefined =>
291
+ property.name !== undefined &&
292
+ (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))
293
+ ? property.name.text
294
+ : undefined
295
+
296
+ const mounts: EntryApiMount[] = []
297
+ const visit = (node: TSApi.Node): void => {
298
+ const arg = ts.isCallExpression(node) ? node.arguments[0] : undefined
299
+ const options = arg === undefined ? undefined : unwrapExpression(ts, arg)
300
+ if (
301
+ ts.isCallExpression(node) &&
302
+ isCreateWebApp(node.expression) &&
303
+ options !== undefined &&
304
+ ts.isObjectLiteralExpression(options) &&
305
+ options.properties.some((property) => propertyName(property) === "api")
306
+ ) {
307
+ // The last of an `apiPrefix` property and a spread decides, as it does at runtime.
308
+ let apiPrefix: EntryApiPrefix = { kind: "literal", value: "", unset: true }
309
+ for (const property of options.properties) {
310
+ if (ts.isSpreadAssignment(property)) apiPrefix = { kind: "unreadable" }
311
+ else if (propertyName(property) !== "apiPrefix") continue
312
+ else if (ts.isPropertyAssignment(property)) apiPrefix = readPrefix(property.initializer)
313
+ else if (ts.isShorthandPropertyAssignment(property)) apiPrefix = readPrefix(property.name)
314
+ else apiPrefix = { kind: "unreadable" }
315
+ }
316
+ const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
317
+ mounts.push({ line, apiPrefix })
318
+ }
319
+ ts.forEachChild(node, visit)
320
+ }
321
+ visit(tree)
322
+ return mounts
323
+ }
324
+
325
+ const servedAt = (prefix: string): string => (prefix === "" ? "nowhere" : `at ${prefix}`)
326
+
327
+ /**
328
+ * The NF-C035 findings for app modules that start their own server: a `createWebApp` call that
329
+ * mounts a backend at a different `apiPrefix` than the one `nifra dev` reads, or with none, so
330
+ * production serves the backend somewhere dev and e2e never exercised.
331
+ */
332
+ async function entryApiPrefixFindings(
333
+ ctx: Parameters<CheckRule["scan"]>[0],
334
+ expected: string,
335
+ origin: string,
336
+ ): Promise<Diagnostic[]> {
337
+ const candidates = ctx.sources.files.filter(
338
+ (file) => SCRIPT_SOURCE.test(file) && ctx.sources.read(file)?.includes("createWebApp") === true,
339
+ )
340
+ if (candidates.length === 0) return []
341
+ const ts = (await loadRuleTypeScript(ctx)).compiler
342
+ if (ts === undefined) return []
343
+ const findings: Diagnostic[] = []
344
+ for (const file of [...candidates].sort(codeUnitOrder)) {
345
+ const text = ctx.sources.read(file) ?? ""
346
+ const tree = ts.createSourceFile(
347
+ file,
348
+ text,
349
+ ts.ScriptTarget.Latest,
350
+ true,
351
+ scriptKindOf(ts, file),
352
+ )
353
+ for (const { line, apiPrefix } of entryApiMounts(ts, tree, file)) {
354
+ if (apiPrefix.kind !== "literal" || apiPrefix.value === expected) continue
355
+ const specifier = posix.relative(posix.dirname(file), origin.replace(SCRIPT_SOURCE, ""))
356
+ const passed = apiPrefix.unset
357
+ ? "no apiPrefix"
358
+ : `apiPrefix ${JSON.stringify(apiPrefix.value)}`
359
+ findings.push(
360
+ diagnostic({
361
+ code: "NF-C035",
362
+ severity: "warn",
363
+ file,
364
+ line,
365
+ 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 })\`.`,
366
+ evidence: [
367
+ `createWebApp apiPrefix: ${apiPrefix.unset ? "unset" : JSON.stringify(apiPrefix.value)}`,
368
+ `${origin} apiPrefix: ${JSON.stringify(expected)}`,
369
+ ],
370
+ verify: "nifra check",
371
+ }),
372
+ )
373
+ }
374
+ }
375
+ return findings
376
+ }
377
+
162
378
  /**
163
- * NF-C035: `backend/app.ts` exports a backend and the framework config sets no `apiPrefix`, so the
164
- * backend is not served over HTTP - a browser call to `/api` reaches page routing. Loader-only is a
165
- * valid choice; the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's to report.
379
+ * NF-C035: `backend/app.ts` exports a backend that is not served over HTTP, so a browser call to `/api`
380
+ * reaches page routing. The framework config sets no `apiPrefix`; loader-only is a valid choice, and
381
+ * the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's.
382
+ *
383
+ * An app module that calls `createWebApp` itself is checked against the same value, since only the
384
+ * framework config reaches `nifra dev`: a backend it mounts at another `apiPrefix`, or with none, is
385
+ * served in production where dev never served it.
166
386
  */
167
387
  export const implicitApiMountRule: CheckRule = {
168
388
  code: "NF-C035",
@@ -172,53 +392,86 @@ export const implicitApiMountRule: CheckRule = {
172
392
  // An API-only app serves its backend itself; there is no page routing to fall into.
173
393
  if (!existsSync(backendFile) || !hasWebConfig(ctx.root, ".")) return []
174
394
  if (!BACKEND_EXPORT.test(stripComments(readFileSync(backendFile, "utf8")))) return []
175
- const configFile = CONFIG_FILES.find((file) => existsSync(join(ctx.root, file)))
176
- if (
177
- configFile !== undefined &&
178
- readStaticApiPrefix(readFileSync(join(ctx.root, configFile), "utf8")).kind !== "default"
179
- ) {
180
- return []
181
- }
182
- const file = configFile ?? FRAMEWORK_FILE
395
+ const read = (file: string): string | undefined =>
396
+ existsSync(join(ctx.root, file)) ? readFileSync(join(ctx.root, file), "utf8") : undefined
397
+ const sources = { framework: read(FRAMEWORK_FILE), config: read(CONFIG_FILE) }
398
+ if (sources.framework === undefined && sources.config === undefined) return []
399
+ const effective = effectiveApiPrefix(sources)
400
+ if (effective.kind === "unreadable") return []
401
+ const configOwn = sources.config === undefined ? undefined : readStaticApiPrefix(sources.config)
402
+ const origin =
403
+ configOwn !== undefined && (configOwn.kind !== "default" || sources.framework === undefined)
404
+ ? CONFIG_FILE
405
+ : FRAMEWORK_FILE
406
+ const entries = await entryApiPrefixFindings(
407
+ ctx,
408
+ effective.kind === "literal" ? effective.value : "",
409
+ origin,
410
+ )
411
+ if (effective.kind === "literal") return entries
412
+ const file = sources.framework === undefined ? CONFIG_FILE : FRAMEWORK_FILE
183
413
  return [
184
414
  diagnostic({
185
415
  code: "NF-C035",
186
- severity: "info",
416
+ severity: "warn",
187
417
  file,
188
418
  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.`,
189
419
  fix: { recipe: "framework.api-prefix", command: "nifra fix --code NF-C035" },
190
420
  verify: "nifra check",
191
421
  }),
422
+ ...entries,
192
423
  ]
193
424
  },
194
425
  }
195
426
 
427
+ const appendLine = (source: string, line: string): string =>
428
+ source === "" ? line : `${source}${source.endsWith("\n") ? "" : "\n"}${line}`
429
+
196
430
  /** Append `export const apiPrefix = "/api"` to a framework config that sets none, creating it if needed. */
197
431
  export function withDefaultApiPrefix(source: string | undefined): string | undefined {
198
432
  if (source !== undefined && readStaticApiPrefix(source).kind !== "default") return undefined
199
- const line = 'export const apiPrefix = "/api"\n'
200
- if (source === undefined || source === "") return line
201
- return `${source}${source.endsWith("\n") ? "" : "\n"}${line}`
433
+ return appendLine(source ?? "", 'export const apiPrefix = "/api"\n')
434
+ }
435
+
436
+ /**
437
+ * The edit that serves the backend at `/api`: the mount in backend/framework.ts, where the server
438
+ * entry reads it, or in nifra.config.ts when it is the only config. A file that needs no edit comes
439
+ * back `undefined`; an `apiPrefix` either file sets is never replaced.
440
+ */
441
+ export function apiPrefixFix(sources: ApiPrefixSources): ApiPrefixSources {
442
+ if (effectiveApiPrefix(sources).kind !== "default")
443
+ return { framework: undefined, config: undefined }
444
+ return sources.framework === undefined
445
+ ? { framework: undefined, config: withDefaultApiPrefix(sources.config) }
446
+ : { framework: withDefaultApiPrefix(sources.framework), config: undefined }
202
447
  }
203
448
 
204
449
  const LOADER_EXPORT =
205
450
  /\bexport\s+(?:async\s+)?(?:const|let|var|function\*?)\s+loader\b|\bexport\s*\{[^}]*\bloader\b[^}]*\}/
206
451
  const SCRIPT_LAYOUT = /\.[cm]?[jt]sx?$/
207
452
 
208
- const hasModifier = (ts: typeof TSApi, node: TSApi.Node, kind: TSApi.SyntaxKind): boolean =>
209
- ts.canHaveModifiers(node) && (ts.getModifiers(node) ?? []).some((m) => m.kind === kind)
453
+ // Read from the node, not `ts.getModifiers`: TypeScript 7's API has no getModifiers or canHaveModifiers.
454
+ const hasModifier = (
455
+ node: TSApi.VariableStatement | TSApi.FunctionDeclaration | TSApi.ClassDeclaration,
456
+ kind: TSApi.SyntaxKind,
457
+ ): boolean => node.modifiers?.some((m) => m.kind === kind) === true
210
458
 
211
- /** The function an exported `meta` names: a declaration, or a const initialized with one. */
212
- function exportedMetaFunction(
459
+ /** The function an exported `name` names: a declaration, or a const initialized with one. */
460
+ function exportedFunction(
213
461
  ts: typeof TSApi,
214
462
  tree: TSApi.SourceFile,
463
+ name: string,
215
464
  ): TSApi.FunctionDeclaration | TSApi.ArrowFunction | TSApi.FunctionExpression | undefined {
216
465
  for (const statement of tree.statements) {
217
- if (!hasModifier(ts, statement, ts.SyntaxKind.ExportKeyword)) continue
218
- if (ts.isFunctionDeclaration(statement) && statement.name?.text === "meta") return statement
466
+ if (ts.isFunctionDeclaration(statement)) {
467
+ if (statement.name?.text === name && hasModifier(statement, ts.SyntaxKind.ExportKeyword))
468
+ return statement
469
+ continue
470
+ }
219
471
  if (!ts.isVariableStatement(statement)) continue
472
+ if (!hasModifier(statement, ts.SyntaxKind.ExportKeyword)) continue
220
473
  for (const declaration of statement.declarationList.declarations) {
221
- if (!ts.isIdentifier(declaration.name) || declaration.name.text !== "meta") continue
474
+ if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name) continue
222
475
  let init = declaration.initializer
223
476
  while (
224
477
  init !== undefined &&
@@ -237,7 +490,7 @@ function exportedMetaFunction(
237
490
 
238
491
  /** Where an exported function `meta` reads its args' `data`: a destructured `data`, or `args.data`. */
239
492
  export function metaDataReads(ts: typeof TSApi, tree: TSApi.SourceFile): number[] {
240
- const meta = exportedMetaFunction(ts, tree)
493
+ const meta = exportedFunction(ts, tree, "meta")
241
494
  const param = meta?.parameters[0]
242
495
  if (param === undefined) return []
243
496
  const lineOf = (node: TSApi.Node) =>
@@ -326,9 +579,217 @@ export const layoutMetaDataRule: CheckRule = {
326
579
  },
327
580
  }
328
581
 
582
+ const GATE_EXPORT = /\bexport\s+const\s+gate\s*(?::\s*boolean\s*)?=\s*true\b/
583
+ const MIDDLEWARE_EXPORT =
584
+ /\bexport\s+(?:async\s+)?(?:const|let|var|function\*?)\s+middleware\b|\bexport\s*\{[^}]*\bmiddleware\b[^}]*\}/
585
+
586
+ /** Lines where an exported `loader` refuses access: `redirect()`, `statusPage(401|403)`, `new Response()`. */
587
+ export function loaderRefusals(ts: typeof TSApi, tree: TSApi.SourceFile): number[] {
588
+ const loader = exportedFunction(ts, tree, "loader")
589
+ if (loader?.body === undefined) return []
590
+ const lines: number[] = []
591
+ const lineOf = (node: TSApi.Node): number =>
592
+ tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
593
+ const visit = (node: TSApi.Node): void => {
594
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
595
+ const status = node.arguments[0]
596
+ if (
597
+ node.expression.text === "redirect" ||
598
+ (node.expression.text === "statusPage" &&
599
+ status !== undefined &&
600
+ ts.isNumericLiteral(status) &&
601
+ (status.text === "401" || status.text === "403"))
602
+ )
603
+ lines.push(lineOf(node))
604
+ } else if (
605
+ ts.isNewExpression(node) &&
606
+ ts.isIdentifier(node.expression) &&
607
+ node.expression.text === "Response"
608
+ ) {
609
+ lines.push(lineOf(node))
610
+ }
611
+ ts.forEachChild(node, visit)
612
+ }
613
+ visit(loader.body)
614
+ return lines
615
+ }
616
+
617
+ /**
618
+ * NF-C042: a layout loader that refuses access - a redirect, a 401 or 403, a `Response` - in a layout
619
+ * that is not a gate and exports no middleware. A client navigation that keeps the layout skips that
620
+ * loader, and an action below it runs without it, so the check reads as a guard and is not one. A
621
+ * refusal inside a helper the loader calls is not seen; the development warning covers that.
622
+ */
623
+ export const ungatedLayoutRefusalRule: CheckRule = {
624
+ code: "NF-C042",
625
+ title: "Layout loader refuses access without a gate",
626
+ async scan(ctx) {
627
+ const routesDir = join(ctx.root, "routes")
628
+ if (!existsSync(routesDir)) return []
629
+ let manifest: ReturnType<typeof discoverRoutes>
630
+ try {
631
+ manifest = discoverRoutes(routesDir)
632
+ } catch {
633
+ return []
634
+ }
635
+ const layouts = Object.values(manifest.layouts)
636
+ .filter((layout) => SCRIPT_LAYOUT.test(layout.file))
637
+ .sort((a, b) => codeUnitOrder(a.file, b.file))
638
+ if (layouts.length === 0) return []
639
+ const sources = layouts.map((layout) =>
640
+ [layout.file, ...(layout.backend === undefined ? [] : [layout.backend])].map((file) => ({
641
+ file,
642
+ text: readFileSync(join(routesDir, file), "utf8"),
643
+ })),
644
+ )
645
+ if (!sources.flat().some((half) => LOADER_EXPORT.test(stripComments(half.text)))) return []
646
+ const loaded = await loadRuleTypeScript(ctx)
647
+ const ts = loaded.compiler
648
+ if (ts === undefined) return []
649
+ const findings: Diagnostic[] = []
650
+ for (const halves of sources) {
651
+ const code = halves.map((half) => stripComments(half.text))
652
+ if (code.some((text) => GATE_EXPORT.test(text) || MIDDLEWARE_EXPORT.test(text))) continue
653
+ for (const half of halves) {
654
+ if (!LOADER_EXPORT.test(stripComments(half.text))) continue
655
+ const tree = ts.createSourceFile(
656
+ half.file,
657
+ half.text,
658
+ ts.ScriptTarget.Latest,
659
+ true,
660
+ scriptKindOf(ts, half.file),
661
+ )
662
+ const [line] = loaderRefusals(ts, tree)
663
+ if (line === undefined) continue
664
+ findings.push(
665
+ diagnostic({
666
+ code: "NF-C042",
667
+ severity: "warn",
668
+ file: `routes/${half.file}`,
669
+ line,
670
+ 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\`.`,
671
+ evidence: [
672
+ "gate = true: the loader runs on every request and before every action, ahead of the page loader",
673
+ "middleware: runs before every page and action below the directory",
674
+ ],
675
+ verify: "nifra check",
676
+ }),
677
+ )
678
+ }
679
+ }
680
+ return findings
681
+ },
682
+ }
683
+
684
+ /** The names a module exports at runtime, each with its line: declarations and `export { ... }` lists.
685
+ * `export *` names nothing it can read, and type-only exports carry no value. */
686
+ function runtimeExports(
687
+ ts: typeof TSApi,
688
+ tree: TSApi.SourceFile,
689
+ ): Array<{ readonly name: string; readonly line: number }> {
690
+ const found: Array<{ name: string; line: number }> = []
691
+ const add = (name: string, node: TSApi.Node): void => {
692
+ found.push({ name, line: tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1 })
693
+ }
694
+ for (const statement of tree.statements) {
695
+ if (ts.isExportDeclaration(statement)) {
696
+ if (statement.isTypeOnly || statement.exportClause === undefined) continue
697
+ if (!ts.isNamedExports(statement.exportClause)) continue
698
+ for (const element of statement.exportClause.elements) {
699
+ if (!element.isTypeOnly) add(element.name.text, element)
700
+ }
701
+ continue
702
+ }
703
+ if (
704
+ !(
705
+ ts.isVariableStatement(statement) ||
706
+ ts.isFunctionDeclaration(statement) ||
707
+ ts.isClassDeclaration(statement)
708
+ ) ||
709
+ !hasModifier(statement, ts.SyntaxKind.ExportKeyword) ||
710
+ hasModifier(statement, ts.SyntaxKind.DeclareKeyword)
711
+ )
712
+ continue
713
+ if (ts.isVariableStatement(statement)) {
714
+ for (const declaration of statement.declarationList.declarations) {
715
+ if (ts.isIdentifier(declaration.name)) add(declaration.name.text, declaration)
716
+ }
717
+ } else if (statement.name !== undefined) {
718
+ add(statement.name.text, statement)
719
+ }
720
+ }
721
+ return found
722
+ }
723
+
724
+ /**
725
+ * NF-C041: a route's frontend file (`x.tsx`, a layout, a status or error page) exports a name only
726
+ * the server reads, such as `loader` or `action`. The frontend file ships to the browser whole, so
727
+ * `nifra build` and the server refuse it; this reports it before either runs.
728
+ */
729
+ export const frontendServerExportRule: CheckRule = {
730
+ code: "NF-C041",
731
+ title: "Server-only export in a route frontend file",
732
+ async scan(ctx) {
733
+ const routesDir = join(ctx.root, "routes")
734
+ if (!existsSync(routesDir)) return []
735
+ let manifest: ReturnType<typeof discoverRoutes>
736
+ try {
737
+ manifest = discoverRoutes(routesDir)
738
+ } catch {
739
+ return []
740
+ }
741
+ const files = new Set<string>()
742
+ for (const entry of [
743
+ ...manifest.routes,
744
+ ...Object.values(manifest.layouts),
745
+ ...Object.values(manifest.errors ?? {}),
746
+ ...(manifest.notFound === undefined ? [] : [manifest.notFound]),
747
+ ...Object.values(manifest.notFounds ?? {}),
748
+ ...Object.values(manifest.statusPages ?? {}),
749
+ ...Object.values(manifest.loadings ?? {}),
750
+ ]) {
751
+ if (SCRIPT_LAYOUT.test(entry.file)) files.add(entry.file)
752
+ }
753
+ if (files.size === 0) return []
754
+ const loaded = await loadRuleTypeScript(ctx)
755
+ const ts = loaded.compiler
756
+ if (ts === undefined) return []
757
+ const findings: Diagnostic[] = []
758
+ for (const file of [...files].sort(codeUnitOrder)) {
759
+ const text = readFileSync(join(routesDir, file), "utf8")
760
+ const tree = ts.createSourceFile(
761
+ file,
762
+ text,
763
+ ts.ScriptTarget.Latest,
764
+ true,
765
+ scriptKindOf(ts, file),
766
+ )
767
+ for (const { name, line } of runtimeExports(ts, tree)) {
768
+ if (!BACKEND_ROUTE_EXPORTS.has(name)) continue
769
+ const backend = `routes/${file.replace(SCRIPT_LAYOUT, ".backend.ts")}`
770
+ findings.push(
771
+ diagnostic({
772
+ code: "NF-C041",
773
+ severity: "error",
774
+ file: `routes/${file}`,
775
+ line,
776
+ 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}.`,
777
+ evidence: [`export: ${name}`, `backend half: ${backend}`],
778
+ fix: { recipe: "route.split", command: "nifra fix --code NF-C041" },
779
+ verify: "nifra check",
780
+ }),
781
+ )
782
+ }
783
+ }
784
+ return findings
785
+ },
786
+ }
787
+
329
788
  export const pageRules = Object.freeze([
330
789
  shadowedPageRule,
331
790
  statusLayoutRule,
332
791
  implicitApiMountRule,
333
792
  layoutMetaDataRule,
793
+ frontendServerExportRule,
794
+ ungatedLayoutRefusalRule,
334
795
  ])
@@ -388,8 +388,9 @@ export const paramModifierRule: CheckRule = {
388
388
 
389
389
  /**
390
390
  * NF-C036: a registration whose path the static scan cannot read. The route still serves; what it
391
- * loses is every source-level analysis keyed by path - automatic capability provenance and the route
392
- * lints above - so it is reported as information, naming the expression.
391
+ * loses is every source-level analysis keyed by path - the route lints above, and the scan's half of
392
+ * capability provenance (the other half is the registering module recorded when the app loads) - so
393
+ * it is reported as information, naming the expression.
393
394
  */
394
395
  export const unresolvedRoutePathRule: CheckRule = {
395
396
  code: "NF-C036",
@@ -411,7 +412,7 @@ export const unresolvedRoutePathRule: CheckRule = {
411
412
  severity: "info",
412
413
  file: route.file,
413
414
  line: route.line,
414
- message: `${route.method} ${route.expression} - the static scan cannot read this route path, so automatic capability provenance and the route lints do not see the route. Write the path as a string literal, or as a module-level \`const\` (optionally \`as const\`) in this file or exported from a project file it imports; mark a deliberately computed path with a \`// nifra-expect NF-C036: <reason>\` comment above the registration`,
415
+ message: `${route.method} ${route.expression} - the static scan cannot read this route path, so the route lints do not see the route, and capability provenance finds its module only from the app \`nifra.assurance.ts\` loads. Write the path as a string literal, or as a module-level \`const\` (optionally \`as const\`) in this file or exported from a project file it imports; mark a deliberately computed path with a \`// nifra-expect NF-C036: <reason>\` comment above the registration`,
415
416
  evidence: [`${route.method} ${route.expression}`],
416
417
  verify: "nifra check --lints-only",
417
418
  }),