@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,75 @@
1
+ /**
2
+ * `nifra explain NF-xxxx`: a rule code answered from the catalog, with no project read. The catalog
3
+ * loads only when a code is asked for, so the explain command's other paths never pay for it.
4
+ */
5
+
6
+ /** The shape of a built-in rule code. A positional that matches it is a code, not an error message. */
7
+ export const RULE_CODE_SHAPE = /^NF-[A-Z]\d{3}$/
8
+
9
+ const DOCS_ORIGIN = "https://nifra.dev"
10
+
11
+ export interface RuleExplainOutput {
12
+ readonly kind: "rule"
13
+ readonly code: string
14
+ readonly title: string
15
+ /** The `nifra review` check it reports under. */
16
+ readonly category: string
17
+ /** What the rule reports and why it matters. */
18
+ readonly message: string
19
+ readonly fix: string
20
+ readonly command?: string
21
+ readonly docs: string
22
+ }
23
+
24
+ export interface UnknownRuleOutput {
25
+ readonly code: "NIFRA_UNKNOWN_RULE"
26
+ readonly message: string
27
+ }
28
+
29
+ export async function explainRule(code: string): Promise<RuleExplainOutput | UnknownRuleOutput> {
30
+ const [{ isBuiltInCode, RETIRED_CODES, RULE_CODES }, { RULE_EXPLANATIONS }, { CHECK_CODES }] =
31
+ await Promise.all([
32
+ import("./rules/codes.ts"),
33
+ import("./rules/explanations.ts"),
34
+ import("./review.ts"),
35
+ ])
36
+ if (RETIRED_CODES.has(code))
37
+ return {
38
+ code: "NIFRA_UNKNOWN_RULE",
39
+ message: `${code} is retired: no rule reports it any more, and the code is never reused.`,
40
+ }
41
+ if (!isBuiltInCode(code) || !Object.hasOwn(RULE_CODES, code))
42
+ return {
43
+ code: "NIFRA_UNKNOWN_RULE",
44
+ message: `${code} is not a nifra rule code. \`nifra check --json\` lists each finding's code; \`nifra explain "<message>"\` explains an error instead.`,
45
+ }
46
+ const explanation = RULE_EXPLANATIONS[code]
47
+ const out: RuleExplainOutput = {
48
+ kind: "rule",
49
+ code,
50
+ title: RULE_CODES[code],
51
+ category: CHECK_CODES[code] ?? "configuration",
52
+ message: explanation.why,
53
+ fix: explanation.fix,
54
+ docs: `${DOCS_ORIGIN}${explanation.docs}`,
55
+ }
56
+ return explanation.command === undefined ? out : { ...out, command: explanation.command }
57
+ }
58
+
59
+ export const isRuleExplanation = (value: unknown): value is RuleExplainOutput =>
60
+ typeof value === "object" &&
61
+ value !== null &&
62
+ Reflect.get(value, "kind") === "rule" &&
63
+ ["code", "title", "category", "message", "fix", "docs"].every(
64
+ (key) => typeof Reflect.get(value, key) === "string",
65
+ )
66
+
67
+ export function renderRuleExplanation(out: RuleExplainOutput): string[] {
68
+ return [
69
+ `${out.code} ${out.title} (${out.category})`,
70
+ ` ${out.message}`,
71
+ ` fix: ${out.fix}`,
72
+ ...(out.command === undefined ? [] : [` run: ${out.command}`]),
73
+ ` docs: ${out.docs}`,
74
+ ]
75
+ }
@@ -31,18 +31,22 @@ export const RULE_CODES = Object.freeze({
31
31
  "NF-C032": "Credential in browser code",
32
32
  "NF-C033": "Generated output schema out of date",
33
33
  "NF-C034": "Root status page wraps the root layout itself",
34
- "NF-C035": "Backend mounted at the implicit /api",
34
+ "NF-C035": "Backend not served over HTTP",
35
35
  "NF-C036": "Route path the static scan cannot read",
36
36
  "NF-C037": "API-only declaration over a web app",
37
37
  "NF-C038": "Retired root layout file",
38
38
  "NF-C039": "Implemented contract operation the backend does not serve",
39
39
  "NF-C040": "Layout meta reads data its layout never loads",
40
+ "NF-C041": "Server-only export in a route frontend file",
41
+ "NF-C042": "Layout loader refuses access without a gate",
42
+ "NF-C043": "Merged routes skip the parent's authentication",
40
43
  "NF-C020": "Island enhancer cleanup check",
41
44
  "NF-C021": "nano binding cleanup check",
42
45
  "NF-C022": "nano bindList key check",
43
46
  "NF-C023": "nano computed deps check",
44
47
  "NF-D001": "Doctor check",
45
48
  "NF-D002": "Mixed nifra versions",
49
+ "NF-D003": "Stale file: dependencies in bun.lock",
46
50
  "NF-A001": "Assurance bundle check",
47
51
  "NF-H001": "Hydration mismatch",
48
52
  "NF-H002": "Duplicate framework runtime",
@@ -5,6 +5,8 @@
5
5
  *
6
6
  * The served routes come from the backend the contract-lock check already loaded; this rule runs no
7
7
  * project code of its own. Without a lock the backend is not loaded and the comparison is skipped.
8
+ * A contract imported from a workspace package is read through that package's `exports`.
9
+ * Re-exports on the way to a contract, and `...operations` spreads inside one, are followed.
8
10
  */
9
11
  import { existsSync, readFileSync, realpathSync } from "node:fs"
10
12
  import { join, relative } from "node:path"
@@ -13,6 +15,7 @@ import { BACKEND_APP_FILE } from "../app-files.ts"
13
15
  import { type Diagnostic, diagnostic } from "../diagnostics.ts"
14
16
  import { createSpecifierResolver, type SpecifierResolver } from "../internal/project-specifiers.ts"
15
17
  import { scriptKindOf } from "../internal/script-kind.ts"
18
+ import { createExportedFileResolver } from "../internal/workspace-packages.ts"
16
19
  import type { CheckRule } from "./index.ts"
17
20
  import { loadRuleTypeScript } from "./typescript.ts"
18
21
 
@@ -126,14 +129,31 @@ function calls(
126
129
  )
127
130
  }
128
131
 
129
- /** The operations an object literal of a contract declares, and what could not be read. */
132
+ interface OperationsRead {
133
+ readonly operations: readonly Operation[]
134
+ readonly unreadable: readonly string[]
135
+ }
136
+
137
+ /** The operations an object literal of a contract declares, and what could not be read. `spreadOf`
138
+ * reads a `...name` spread of another operations object. */
130
139
  function operationsOf(
131
140
  ts: typeof TSApi,
132
141
  literal: TSApi.ObjectLiteralExpression,
133
- ): { operations: Operation[]; unreadable: string[] } {
134
- const operations: Operation[] = []
142
+ spreadOf: (name: string) => OperationsRead | undefined,
143
+ ): OperationsRead {
144
+ // A later key replaces an earlier one, as in the object the page builds at runtime.
145
+ const operations = new Map<string, Operation>()
135
146
  const unreadable: string[] = []
136
147
  for (const property of literal.properties) {
148
+ if (ts.isSpreadAssignment(property)) {
149
+ const spread = unwrap(ts, property.expression)
150
+ const read = ts.isIdentifier(spread) ? spreadOf(spread.text) : undefined
151
+ if (read !== undefined) {
152
+ for (const op of read.operations) operations.set(op.name, op)
153
+ unreadable.push(...read.unreadable)
154
+ continue
155
+ }
156
+ }
137
157
  if (!ts.isPropertyAssignment(property)) {
138
158
  unreadable.push("an operation spread or written as a method")
139
159
  continue
@@ -156,9 +176,9 @@ function operationsOf(
156
176
  const path = field("path")
157
177
  if (method === undefined || path === undefined)
158
178
  unreadable.push(`operation "${name}" has a method or path that is not a string literal`)
159
- else operations.push({ name, method: method.toUpperCase(), path })
179
+ else operations.set(name, { name, method: method.toUpperCase(), path })
160
180
  }
161
- return { operations, unreadable }
181
+ return { operations: [...operations.values()], unreadable }
162
182
  }
163
183
 
164
184
  interface Parsed {
@@ -166,6 +186,13 @@ interface Parsed {
166
186
  readonly bound: Bindings
167
187
  }
168
188
 
189
+ /** An operations object literal and the file it is written in, which its spreads resolve against. */
190
+ interface Located {
191
+ readonly literal: TSApi.ObjectLiteralExpression
192
+ readonly file: string
193
+ readonly parsed: Parsed
194
+ }
195
+
169
196
  export const contractVisibilityRule: CheckRule = {
170
197
  code: "NF-C039",
171
198
  title: "Implemented contract operation the backend does not serve",
@@ -192,7 +219,19 @@ export const contractVisibilityRule: CheckRule = {
192
219
  const loaded = await loadRuleTypeScript(ctx)
193
220
  const ts = loaded.compiler
194
221
  if (ts === undefined) return []
195
- const resolve = createSpecifierResolver()
222
+ const projectResolve = createSpecifierResolver()
223
+ const packageEntry = await createExportedFileResolver(ctx.root)
224
+ /** Why a package specifier could not be read, by importing file and specifier. */
225
+ const packageReasons = new Map<string, string>()
226
+ const resolve: SpecifierResolver = (from, specifier) => {
227
+ const local = projectResolve(from, specifier)
228
+ if (local !== undefined) return local
229
+ const entry = packageEntry(from, specifier)
230
+ if (entry === undefined) return undefined
231
+ if ("path" in entry) return entry.path
232
+ packageReasons.set(`${from}\0${specifier}`, entry.reason)
233
+ return undefined
234
+ }
196
235
  // The resolver answers with real paths, so the root is read as one to name files project-relative.
197
236
  const realRoot = (() => {
198
237
  try {
@@ -217,20 +256,21 @@ export const contractVisibilityRule: CheckRule = {
217
256
  return parsed
218
257
  }
219
258
  /** The object literal `defineContract({...})` gives the const `name` in `file`, followed one hop
220
- * into a project module it is imported from. */
259
+ * into a project module it is imported from, and through any re-exports on the way. */
221
260
  const contractLiteral = (
222
261
  file: string,
223
262
  parsed: Parsed,
224
263
  name: string,
225
264
  hops = 0,
226
- ): TSApi.ObjectLiteralExpression | undefined => {
265
+ seen = new Set<string>(),
266
+ ): Located | undefined => {
227
267
  for (const statement of parsed.tree.statements) {
228
268
  if (!ts.isVariableStatement(statement)) continue
229
269
  for (const declaration of statement.declarationList.declarations) {
230
270
  if (!ts.isIdentifier(declaration.name) || declaration.name.text !== name) continue
231
271
  if (declaration.initializer === undefined) return undefined
232
272
  const init = unwrap(ts, declaration.initializer)
233
- if (ts.isObjectLiteralExpression(init)) return init
273
+ if (ts.isObjectLiteralExpression(init)) return { literal: init, file, parsed }
234
274
  if (
235
275
  !ts.isCallExpression(init) ||
236
276
  !calls(ts, init.expression, parsed.bound, "defineContract")
@@ -239,19 +279,66 @@ export const contractVisibilityRule: CheckRule = {
239
279
  const arg = init.arguments[0]
240
280
  const literal = arg === undefined ? undefined : unwrap(ts, arg)
241
281
  return literal !== undefined && ts.isObjectLiteralExpression(literal)
242
- ? literal
282
+ ? { literal, file, parsed }
243
283
  : undefined
244
284
  }
245
285
  }
246
286
  const from = parsed.bound.imported.get(name)
247
- if (from === undefined || hops > 0) return undefined
248
- const target = resolve(join(ctx.root, file), from.specifier)
287
+ if (from !== undefined)
288
+ return hops > 0 ? undefined : follow(file, from.specifier, from.name, hops + 1, seen)
289
+ // A re-export declares nothing, so passing through one does not spend the hop.
290
+ for (const statement of parsed.tree.statements) {
291
+ if (
292
+ !ts.isExportDeclaration(statement) ||
293
+ statement.isTypeOnly ||
294
+ statement.moduleSpecifier === undefined ||
295
+ !ts.isStringLiteral(statement.moduleSpecifier)
296
+ )
297
+ continue
298
+ const clause = statement.exportClause
299
+ if (clause === undefined) {
300
+ const found = follow(file, statement.moduleSpecifier.text, name, hops, seen)
301
+ if (found !== undefined) return found
302
+ continue
303
+ }
304
+ if (!ts.isNamedExports(clause)) continue
305
+ const element = clause.elements.find((e) => !e.isTypeOnly && e.name.text === name)
306
+ if (element !== undefined)
307
+ return follow(
308
+ file,
309
+ statement.moduleSpecifier.text,
310
+ (element.propertyName ?? element.name).text,
311
+ hops,
312
+ seen,
313
+ )
314
+ }
315
+ return undefined
316
+ }
317
+ const follow = (
318
+ file: string,
319
+ specifier: string,
320
+ name: string,
321
+ hops: number,
322
+ seen: Set<string>,
323
+ ): Located | undefined => {
324
+ const target = resolve(join(ctx.root, file), specifier)
249
325
  if (target === undefined) return undefined
250
326
  const rel = relative(realRoot, target).replaceAll("\\", "/")
327
+ const key = `${rel}\0${name}`
328
+ if (seen.has(key)) return undefined
329
+ seen.add(key)
251
330
  const next = parse(rel, readText(target))
252
- return next === undefined ? undefined : contractLiteral(rel, next, from.name, hops + 1)
331
+ return next === undefined ? undefined : contractLiteral(rel, next, name, hops, seen)
253
332
  }
254
333
 
334
+ // Each spread is looked up afresh from the file it is written in; the depth only stops a cycle.
335
+ const readOperations = (found: Located, depth = 0): OperationsRead =>
336
+ operationsOf(ts, found.literal, (name) => {
337
+ if (depth >= 8) return undefined
338
+ const inner = contractLiteral(found.file, found.parsed, name)
339
+ return inner === undefined ? undefined : readOperations(inner, depth + 1)
340
+ })
341
+
255
342
  const implementations: Implementation[] = []
256
343
  for (const file of candidates) {
257
344
  const parsed = parse(file, ctx.sources.read(file))
@@ -264,12 +351,12 @@ export const contractVisibilityRule: CheckRule = {
264
351
  if (ts.isCallExpression(node) && calls(ts, node.expression, parsed.bound, "implement")) {
265
352
  const line = parsed.tree.getLineAndCharacterOfPosition(node.getStart()).line + 1
266
353
  const arg = node.arguments[0] === undefined ? undefined : unwrap(ts, node.arguments[0])
267
- let literal: TSApi.ObjectLiteralExpression | undefined
354
+ let located: Located | undefined
268
355
  let contract = arg?.getText() ?? "(none)"
269
356
  if (arg !== undefined && ts.isIdentifier(arg))
270
- literal = contractLiteral(file, parsed, arg.text)
357
+ located = contractLiteral(file, parsed, arg.text)
271
358
  else if (arg !== undefined && ts.isObjectLiteralExpression(arg)) {
272
- literal = arg
359
+ located = { literal: arg, file, parsed }
273
360
  contract = "an inline contract"
274
361
  } else if (
275
362
  arg !== undefined &&
@@ -277,20 +364,31 @@ export const contractVisibilityRule: CheckRule = {
277
364
  calls(ts, arg.expression, parsed.bound, "defineContract")
278
365
  ) {
279
366
  const inner = arg.arguments[0] === undefined ? undefined : unwrap(ts, arg.arguments[0])
280
- if (inner !== undefined && ts.isObjectLiteralExpression(inner)) literal = inner
367
+ if (inner !== undefined && ts.isObjectLiteralExpression(inner))
368
+ located = { literal: inner, file, parsed }
281
369
  contract = "an inline contract"
282
370
  }
283
- if (literal === undefined) {
371
+ if (located === undefined) {
372
+ const from =
373
+ arg !== undefined && ts.isIdentifier(arg)
374
+ ? parsed.bound.imported.get(arg.text)?.specifier
375
+ : undefined
376
+ const reason =
377
+ from === undefined
378
+ ? undefined
379
+ : packageReasons.get(`${join(ctx.root, file)}\0${from}`)
284
380
  implementations.push({
285
381
  file,
286
382
  line,
287
383
  contract,
288
384
  operations: [],
289
385
  unreadable: [
290
- `the contract ${contract} is not a defineContract({...}) the scan can read`,
386
+ reason === undefined
387
+ ? `the contract ${contract} is not a defineContract({...}) the scan can read`
388
+ : `the contract ${contract} is imported from "${from}", which the scan cannot read: ${reason}`,
291
389
  ],
292
390
  })
293
- } else implementations.push({ file, line, contract, ...operationsOf(ts, literal) })
391
+ } else implementations.push({ file, line, contract, ...readOperations(located) })
294
392
  }
295
393
  ts.forEachChild(node, visit)
296
394
  }
@@ -264,6 +264,32 @@ export function missingOutputSchemas(content: string): MissingOutputSchema[] {
264
264
  return missing
265
265
  }
266
266
 
267
+ const GENERATED_LINE = /^[ \t]*\/\/ nifra:generated [0-9a-f]{8}\b/
268
+ const OPEN_DRAFT = /\bt\.json\(\)\s*\/\*\s*(any|unknown) in the source type\s*\*\//g
269
+
270
+ /** Where a generated output schema (the export under a `nifra:generated` line) admits any value: the
271
+ * note `nifra schemas` writes beside a `t.json()` drafted from `any` or `unknown`. */
272
+ export function openDraftLines(content: string): Array<{ line: number; type: string }> {
273
+ const found: Array<{ line: number; type: string }> = []
274
+ let marked = false
275
+ let generated = false
276
+ for (const [index, text] of content.split("\n").entries()) {
277
+ if (GENERATED_LINE.test(text)) {
278
+ marked = true
279
+ continue
280
+ }
281
+ if (/^export\s/.test(text)) {
282
+ generated = marked
283
+ marked = false
284
+ }
285
+ if (!generated) continue
286
+ for (const match of text.matchAll(OPEN_DRAFT)) {
287
+ found.push({ line: index + 1, type: match[1] ?? "any" })
288
+ }
289
+ }
290
+ return found
291
+ }
292
+
267
293
  export const missingOutputRule: CheckRule = {
268
294
  code: "NF-C030",
269
295
  title: "Route data without an output schema",
@@ -289,10 +315,24 @@ export const missingOutputRule: CheckRule = {
289
315
  `missing: ${schema}`,
290
316
  ...(proven ? ["returns: a data literal"] : []),
291
317
  ],
318
+ fix: { recipe: "schemas.draft", command: "nifra fix --code NF-C030" },
292
319
  verify: "nifra check",
293
320
  }),
294
321
  )
295
322
  }
323
+ for (const { line, type } of openDraftLines(content)) {
324
+ findings.push(
325
+ diagnostic({
326
+ code: "NF-C030",
327
+ severity: "warn",
328
+ file,
329
+ line,
330
+ message: `${file}'s generated output schema admits any value here, because the type it was drafted from is ${type} at this point: the output guard drops nothing below it. Give that value a type, then run \`nifra schemas --write\`.`,
331
+ evidence: [`draft: t.json() from ${type}`],
332
+ verify: "nifra schemas --check",
333
+ }),
334
+ )
335
+ }
296
336
  }
297
337
  return findings.sort(byPlace)
298
338
  },