@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.
- package/CHANGELOG.md +3211 -0
- package/README.md +10 -2
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +5 -2
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts +12 -2
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +73 -9
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +152 -49
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +8 -2
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +49 -13
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +91 -45
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +128 -59
- package/dist/cli.js.map +1 -1
- package/dist/client-module.d.ts +5 -3
- package/dist/client-module.d.ts.map +1 -1
- package/dist/client-module.js +34 -10
- package/dist/client-module.js.map +1 -1
- package/dist/command-catalog.d.ts +9 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +75 -26
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +19 -1
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +114 -0
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts +2 -0
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +93 -12
- package/dist/fix-recipes.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +60 -4
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +196 -33
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/config-paths.d.ts +16 -0
- package/dist/internal/config-paths.d.ts.map +1 -0
- package/dist/internal/config-paths.js +124 -0
- package/dist/internal/config-paths.js.map +1 -0
- package/dist/internal/installed-package.d.ts +4 -0
- package/dist/internal/installed-package.d.ts.map +1 -0
- package/dist/internal/installed-package.js +17 -0
- package/dist/internal/installed-package.js.map +1 -0
- package/dist/internal/project-specifiers.d.ts +14 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +63 -16
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +12 -1
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +117 -5
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +5 -3
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/introspect.d.ts +9 -0
- package/dist/introspect.d.ts.map +1 -1
- package/dist/introspect.js +32 -1
- package/dist/introspect.js.map +1 -1
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +5 -8
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +9 -4
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +32 -3
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-context.d.ts.map +1 -1
- package/dist/mcp-context.js +6 -21
- package/dist/mcp-context.js.map +1 -1
- package/dist/mcp-delegate.d.ts +2 -2
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +19 -7
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-exec.d.ts +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +13 -5
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts +13 -0
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +23 -0
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-path.d.ts +2 -1
- package/dist/mcp-path.d.ts.map +1 -1
- package/dist/mcp-path.js +9 -24
- package/dist/mcp-path.js.map +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +40 -17
- package/dist/mcp.js.map +1 -1
- package/dist/migrate-layout.d.ts +41 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +1729 -236
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts +34 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +132 -21
- package/dist/output-schemas.js.map +1 -1
- package/dist/project-path.d.ts +6 -0
- package/dist/project-path.d.ts.map +1 -1
- package/dist/project-path.js +48 -14
- package/dist/project-path.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +7 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/release-verification.d.ts +5 -0
- package/dist/release-verification.d.ts.map +1 -1
- package/dist/release-verification.js +14 -0
- package/dist/release-verification.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +7 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +5 -1
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +5 -1
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.d.ts +6 -0
- package/dist/rules/data-guard.d.ts.map +1 -1
- package/dist/rules/data-guard.js +37 -0
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +316 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +44 -2
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +56 -3
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +412 -23
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/security.d.ts +3 -2
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +42 -11
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +34 -11
- package/dist/rules/zones.js.map +1 -1
- package/dist/stylex-migrate.d.ts.map +1 -1
- package/dist/stylex-migrate.js +127 -17
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/test-tool.d.ts.map +1 -1
- package/dist/test-tool.js +3 -9
- package/dist/test-tool.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +57 -6
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +247 -19
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +7 -2
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +66 -3
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +21 -3
- package/docs/llms-full.txt +1083 -168
- package/docs/types.json +535 -112
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +5 -1
- package/src/assure.ts +99 -11
- package/src/capabilities-tool.ts +188 -53
- package/src/check-diagnostics.ts +10 -0
- package/src/check-scan.ts +65 -12
- package/src/check.ts +122 -51
- package/src/cli.ts +127 -45
- package/src/client-module.ts +45 -9
- package/src/command-catalog.ts +92 -28
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +146 -1
- package/src/doctor.ts +102 -66
- package/src/fix-recipes.ts +101 -10
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +225 -44
- package/src/internal/config-paths.ts +154 -0
- package/src/internal/installed-package.ts +15 -0
- package/src/internal/project-specifiers.ts +78 -17
- package/src/internal/source-facts.ts +147 -5
- package/src/internal/typescript-import.ts +5 -3
- package/src/internal/workspace-packages.ts +218 -0
- package/src/introspect.ts +39 -1
- package/src/levels-tool.ts +7 -9
- package/src/load.ts +46 -4
- package/src/lock-links.ts +173 -0
- package/src/mcp-context.ts +5 -18
- package/src/mcp-delegate.ts +22 -10
- package/src/mcp-exec.ts +14 -4
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +33 -0
- package/src/mcp-path.ts +8 -22
- package/src/mcp-root.ts +14 -8
- package/src/mcp.ts +48 -19
- package/src/migrate-layout.ts +1968 -245
- package/src/output-schemas.ts +179 -23
- package/src/project-path.ts +44 -13
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/index.ts +7 -0
- package/src/release-verification.ts +20 -0
- package/src/review.ts +7 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +5 -1
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/data-guard.ts +40 -0
- package/src/rules/explanations.ts +329 -0
- package/src/rules/legacy.ts +46 -2
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +486 -25
- package/src/rules/routes.ts +4 -3
- package/src/rules/security.ts +49 -11
- package/src/rules/zones.ts +33 -9
- package/src/stylex-migrate.ts +117 -17
- package/src/test-tool.ts +3 -9
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +312 -19
- package/src/verification-plan.ts +15 -2
- package/src/version-drift.ts +68 -2
- 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
|
+
}
|
package/src/rules/codes.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
134
|
-
|
|
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.
|
|
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
|
|
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
|
-
|
|
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
|
|
248
|
-
|
|
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,
|
|
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
|
|
354
|
+
let located: Located | undefined
|
|
268
355
|
let contract = arg?.getText() ?? "(none)"
|
|
269
356
|
if (arg !== undefined && ts.isIdentifier(arg))
|
|
270
|
-
|
|
357
|
+
located = contractLiteral(file, parsed, arg.text)
|
|
271
358
|
else if (arg !== undefined && ts.isObjectLiteralExpression(arg)) {
|
|
272
|
-
|
|
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))
|
|
367
|
+
if (inner !== undefined && ts.isObjectLiteralExpression(inner))
|
|
368
|
+
located = { literal: inner, file, parsed }
|
|
281
369
|
contract = "an inline contract"
|
|
282
370
|
}
|
|
283
|
-
if (
|
|
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
|
-
|
|
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, ...
|
|
391
|
+
} else implementations.push({ file, line, contract, ...readOperations(located) })
|
|
294
392
|
}
|
|
295
393
|
ts.forEachChild(node, visit)
|
|
296
394
|
}
|
package/src/rules/data-guard.ts
CHANGED
|
@@ -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
|
},
|