@nifrajs/cli 5.5.1 → 5.7.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 +314 -0
- package/dist/cdn-check.d.ts +7 -6
- package/dist/cdn-check.d.ts.map +1 -1
- package/dist/cdn-check.js +13 -1
- package/dist/cdn-check.js.map +1 -1
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +9 -3
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +6 -0
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +30 -32
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +35 -12
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +4 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +66 -9
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +19 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +111 -6
- package/dist/command-catalog.js.map +1 -1
- package/dist/contracts.d.ts +42 -4
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +288 -9
- package/dist/contracts.js.map +1 -1
- package/dist/doctor.d.ts +12 -2
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +67 -1
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +27 -0
- package/dist/fix-recipes.js.map +1 -1
- package/dist/frontend-guidance.d.ts +1 -1
- package/dist/frontend-guidance.d.ts.map +1 -1
- package/dist/frontend-guidance.js +17 -8
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/git-ignored.d.ts +12 -0
- package/dist/internal/git-ignored.d.ts.map +1 -0
- package/dist/internal/git-ignored.js +49 -0
- package/dist/internal/git-ignored.js.map +1 -0
- package/dist/internal/source-facts.d.ts +29 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +160 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/introspect.js +1 -1
- package/dist/introspect.js.map +1 -1
- package/dist/load.d.ts +7 -1
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +18 -0
- package/dist/load.js.map +1 -1
- package/dist/local-cli.d.ts +15 -0
- package/dist/local-cli.d.ts.map +1 -0
- package/dist/local-cli.js +74 -0
- package/dist/local-cli.js.map +1 -0
- package/dist/lock-links.d.ts +0 -7
- package/dist/lock-links.d.ts.map +1 -1
- package/dist/lock-links.js +5 -44
- package/dist/lock-links.js.map +1 -1
- package/dist/mcp-delegate.d.ts +13 -5
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +24 -8
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +57 -3
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.d.ts +2 -1
- package/dist/mcp-http.d.ts.map +1 -1
- package/dist/mcp-http.js +3 -2
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts +21 -0
- package/dist/mcp-tool-meta.d.ts.map +1 -0
- package/dist/mcp-tool-meta.js +246 -0
- package/dist/mcp-tool-meta.js.map +1 -0
- package/dist/mcp.d.ts +3 -2
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +36 -98
- package/dist/mcp.js.map +1 -1
- package/dist/openapi-types.d.ts.map +1 -1
- package/dist/openapi-types.js +45 -2
- package/dist/openapi-types.js.map +1 -1
- package/dist/output-schemas.d.ts +8 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +14 -0
- package/dist/output-schemas.js.map +1 -1
- package/dist/ppr-read-fix.d.ts +8 -0
- package/dist/ppr-read-fix.d.ts.map +1 -0
- package/dist/ppr-read-fix.js +356 -0
- package/dist/ppr-read-fix.js.map +1 -0
- package/dist/recipes/5.6.0.d.ts +10 -0
- package/dist/recipes/5.6.0.d.ts.map +1 -0
- package/dist/recipes/5.6.0.js +68 -0
- package/dist/recipes/5.6.0.js.map +1 -0
- package/dist/recipes/5.7.0.d.ts +12 -0
- package/dist/recipes/5.7.0.d.ts.map +1 -0
- package/dist/recipes/5.7.0.js +70 -0
- package/dist/recipes/5.7.0.js.map +1 -0
- package/dist/recipes/index.d.ts +13 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +4 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +9 -2
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +10 -3
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +11 -4
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +52 -16
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/islands.d.ts +91 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +757 -3
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/legacy.js +1 -1
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/ppr.d.ts +16 -0
- package/dist/rules/ppr.d.ts.map +1 -0
- package/dist/rules/ppr.js +114 -0
- package/dist/rules/ppr.js.map +1 -0
- package/dist/rules/swap.d.ts +7 -0
- package/dist/rules/swap.d.ts.map +1 -0
- package/dist/rules/swap.js +338 -0
- package/dist/rules/swap.js.map +1 -0
- package/dist/rules/text-slot-boolean.d.ts +8 -0
- package/dist/rules/text-slot-boolean.d.ts.map +1 -0
- package/dist/rules/text-slot-boolean.js +57 -0
- package/dist/rules/text-slot-boolean.js.map +1 -0
- package/dist/rules/undeclared-response.d.ts +9 -0
- package/dist/rules/undeclared-response.d.ts.map +1 -0
- package/dist/rules/undeclared-response.js +93 -0
- package/dist/rules/undeclared-response.js.map +1 -0
- package/dist/scaffold-hole.d.ts +105 -0
- package/dist/scaffold-hole.d.ts.map +1 -0
- package/dist/scaffold-hole.js +688 -0
- package/dist/scaffold-hole.js.map +1 -0
- package/dist/scaffold.d.ts +5 -3
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +56 -46
- package/dist/scaffold.js.map +1 -1
- package/dist/upgrade.d.ts +34 -8
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +197 -25
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +12 -1
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.js +1 -1
- 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 +70 -10
- package/docs/llms-full.txt +3162 -1196
- package/docs/types.json +1473 -322
- package/package.json +11 -11
- package/src/cdn-check.ts +21 -7
- package/src/check-diagnostics.ts +9 -3
- package/src/check-scan.ts +35 -32
- package/src/check.ts +32 -10
- package/src/cli.ts +71 -9
- package/src/command-catalog.ts +149 -6
- package/src/contracts.ts +348 -14
- package/src/doctor.ts +62 -3
- package/src/fix-recipes.ts +28 -0
- package/src/frontend-guidance.ts +20 -8
- package/src/internal/git-ignored.ts +47 -0
- package/src/internal/source-facts.ts +198 -0
- package/src/introspect.ts +1 -1
- package/src/load.ts +29 -0
- package/src/local-cli.ts +83 -0
- package/src/lock-links.ts +5 -43
- package/src/mcp-delegate.ts +30 -9
- package/src/mcp-exec.ts +74 -3
- package/src/mcp-http.ts +3 -2
- package/src/mcp-tool-meta.ts +268 -0
- package/src/mcp.ts +35 -99
- package/src/openapi-types.ts +43 -2
- package/src/output-schemas.ts +28 -0
- package/src/ppr-read-fix.ts +426 -0
- package/src/recipes/5.6.0.ts +69 -0
- package/src/recipes/5.7.0.ts +72 -0
- package/src/recipes/index.ts +18 -0
- package/src/review.ts +9 -2
- package/src/rules/codes.ts +11 -4
- package/src/rules/explanations.ts +52 -16
- package/src/rules/islands.ts +875 -3
- package/src/rules/legacy.ts +1 -1
- package/src/rules/ppr.ts +143 -0
- package/src/rules/swap.ts +402 -0
- package/src/rules/text-slot-boolean.ts +56 -0
- package/src/rules/undeclared-response.ts +92 -0
- package/src/scaffold-hole.ts +880 -0
- package/src/scaffold.ts +60 -49
- package/src/upgrade.ts +229 -33
- package/src/verification-plan.ts +20 -1
- package/src/version-drift.ts +1 -1
- package/src/web-app-options.ts +2 -0
- package/dist/rules/nano.d.ts +0 -6
- package/dist/rules/nano.d.ts.map +0 -1
- package/dist/rules/nano.js +0 -273
- package/dist/rules/nano.js.map +0 -1
- package/src/rules/nano.ts +0 -307
package/src/rules/legacy.ts
CHANGED
|
@@ -104,7 +104,7 @@ const STREAM_HINT =
|
|
|
104
104
|
const SERVER_IMPORT_HINT =
|
|
105
105
|
"server-only code in browser code (a route's frontend half, frontend/ or shared/) - reach it from the route's backend half (x.backend.ts) or a *.fn.ts server function; a dynamic import() is bundled too"
|
|
106
106
|
const RESPONSE_ROUTE_HINT =
|
|
107
|
-
"route handler returns a raw Response - the typed client infers `data: never`, so drift detection is lost for this route. Return a plain object (it's serialized for you); for a stream use a typed SSE route (`app.sse(...)`), which keeps typed events; or,
|
|
107
|
+
"route handler returns a raw Response - the typed client infers `data: never`, so drift detection is lost for this route. Return a plain object (it's serialized for you); for a stream use a typed SSE route (`app.sse(...)`), which keeps typed events; for a file or other binary body return `bytes(body, { type, filename })` from @nifrajs/core/binary (the client reads it as a Blob); for a Response you must build yourself, wrap it as `raw<T>(response)` from the same module to keep `data` typed. For a redirect, add `{ response: t.… }` or a `// nifra-expect NF-C007: <reason>` comment to mark it and silence this"
|
|
108
108
|
const PIPELINE_DOC_HINT =
|
|
109
109
|
"nifra runs one bundler per phase - `vitePlugins` feed Vite, `clientPlugins`/`serverPlugins` feed Bun, and the file `nifra build` imports the adapter from is bundled into the server. See the Gotchas section of the Dev & HMR guide."
|
|
110
110
|
const UNDECLARED_DEP_HINT =
|
package/src/rules/ppr.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { discoverRoutes } from "@nifrajs/web/fs"
|
|
4
|
+
import type * as TSApi from "typescript"
|
|
5
|
+
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
6
|
+
import { codeUnitOrder } from "../internal/code-unit-order.ts"
|
|
7
|
+
import { scriptKindOf } from "../internal/script-kind.ts"
|
|
8
|
+
import type { CheckRule } from "./index.ts"
|
|
9
|
+
import { lineOf } from "./islands.ts"
|
|
10
|
+
import { loadRuleTypeScript } from "./typescript.ts"
|
|
11
|
+
|
|
12
|
+
/** The module whose calls read the visitor's request from async context. */
|
|
13
|
+
export const REQUEST_READ_MODULE = "@nifrajs/middleware/context-storage"
|
|
14
|
+
const REQUEST_READS: ReadonlySet<string> = new Set(["getContext", "tryGetContext"])
|
|
15
|
+
const SCRIPT_PAGE = /\.[cm]?[jt]sx?$/
|
|
16
|
+
|
|
17
|
+
/** Each local name `tree` imports a request read under, mapped to the export it names. */
|
|
18
|
+
export function requestReadImports(
|
|
19
|
+
ts: typeof TSApi,
|
|
20
|
+
tree: TSApi.SourceFile,
|
|
21
|
+
): ReadonlyMap<string, string> {
|
|
22
|
+
const names = new Map<string, string>()
|
|
23
|
+
for (const statement of tree.statements) {
|
|
24
|
+
if (
|
|
25
|
+
!ts.isImportDeclaration(statement) ||
|
|
26
|
+
!ts.isStringLiteral(statement.moduleSpecifier) ||
|
|
27
|
+
statement.moduleSpecifier.text !== REQUEST_READ_MODULE
|
|
28
|
+
)
|
|
29
|
+
continue
|
|
30
|
+
const bindings = statement.importClause?.namedBindings
|
|
31
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue
|
|
32
|
+
for (const element of bindings.elements) {
|
|
33
|
+
const imported = (element.propertyName ?? element.name).text
|
|
34
|
+
if (REQUEST_READS.has(imported)) names.set(element.name.text, imported)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return names
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Each call in `tree` of a name `reads` holds. */
|
|
41
|
+
export function requestReadCalls(
|
|
42
|
+
ts: typeof TSApi,
|
|
43
|
+
tree: TSApi.SourceFile,
|
|
44
|
+
reads: ReadonlyMap<string, string>,
|
|
45
|
+
): TSApi.CallExpression[] {
|
|
46
|
+
const calls: TSApi.CallExpression[] = []
|
|
47
|
+
if (reads.size === 0) return calls
|
|
48
|
+
const visit = (node: TSApi.Node): void => {
|
|
49
|
+
if (
|
|
50
|
+
ts.isCallExpression(node) &&
|
|
51
|
+
ts.isIdentifier(node.expression) &&
|
|
52
|
+
reads.has(node.expression.text)
|
|
53
|
+
)
|
|
54
|
+
calls.push(node)
|
|
55
|
+
ts.forEachChild(node, visit)
|
|
56
|
+
}
|
|
57
|
+
visit(tree)
|
|
58
|
+
return calls
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const exportsShellLoader = (ts: typeof TSApi, tree: TSApi.SourceFile): boolean =>
|
|
62
|
+
tree.statements.some((statement) => {
|
|
63
|
+
if (ts.isExportDeclaration(statement))
|
|
64
|
+
return (
|
|
65
|
+
statement.exportClause !== undefined &&
|
|
66
|
+
ts.isNamedExports(statement.exportClause) &&
|
|
67
|
+
statement.exportClause.elements.some((element) => element.name.text === "shellLoader")
|
|
68
|
+
)
|
|
69
|
+
const exported = (node: TSApi.FunctionDeclaration | TSApi.VariableStatement): boolean =>
|
|
70
|
+
node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) === true
|
|
71
|
+
if (ts.isFunctionDeclaration(statement))
|
|
72
|
+
return exported(statement) && statement.name?.text === "shellLoader"
|
|
73
|
+
return (
|
|
74
|
+
ts.isVariableStatement(statement) &&
|
|
75
|
+
exported(statement) &&
|
|
76
|
+
statement.declarationList.declarations.some(
|
|
77
|
+
(declaration) =>
|
|
78
|
+
ts.isIdentifier(declaration.name) && declaration.name.text === "shellLoader",
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* NF-C053: a partially prerendered page (its backend half exports `shellLoader`) reads the visitor's
|
|
85
|
+
* request through `getContext()` or `tryGetContext()`. Its shell is built once and served to every
|
|
86
|
+
* visitor, so the shell check refuses it; this reports each read before a build runs that check.
|
|
87
|
+
*/
|
|
88
|
+
export const shellRequestReadRule: CheckRule = {
|
|
89
|
+
code: "NF-C053",
|
|
90
|
+
title: "Partially prerendered page reads the request",
|
|
91
|
+
async scan(ctx) {
|
|
92
|
+
const routesDir = join(ctx.root, "routes")
|
|
93
|
+
if (!existsSync(routesDir)) return []
|
|
94
|
+
let manifest: ReturnType<typeof discoverRoutes>
|
|
95
|
+
try {
|
|
96
|
+
manifest = discoverRoutes(routesDir)
|
|
97
|
+
} catch {
|
|
98
|
+
return []
|
|
99
|
+
}
|
|
100
|
+
const routes = manifest.routes.filter(
|
|
101
|
+
(route) => route.backend !== undefined && SCRIPT_PAGE.test(route.file),
|
|
102
|
+
)
|
|
103
|
+
if (routes.length === 0) return []
|
|
104
|
+
const loaded = await loadRuleTypeScript(ctx)
|
|
105
|
+
const ts = loaded.compiler
|
|
106
|
+
if (ts === undefined) return []
|
|
107
|
+
const parse = (file: string): TSApi.SourceFile =>
|
|
108
|
+
ts.createSourceFile(
|
|
109
|
+
file,
|
|
110
|
+
readFileSync(join(routesDir, file), "utf8"),
|
|
111
|
+
ts.ScriptTarget.Latest,
|
|
112
|
+
true,
|
|
113
|
+
scriptKindOf(ts, file),
|
|
114
|
+
)
|
|
115
|
+
const findings: Diagnostic[] = []
|
|
116
|
+
for (const route of routes.sort((a, b) => codeUnitOrder(a.file, b.file))) {
|
|
117
|
+
const backend = route.backend
|
|
118
|
+
if (backend === undefined) continue
|
|
119
|
+
const page = parse(route.file)
|
|
120
|
+
const reads = requestReadImports(ts, page)
|
|
121
|
+
const calls = requestReadCalls(ts, page, reads)
|
|
122
|
+
if (calls.length === 0 || !exportsShellLoader(ts, parse(backend))) continue
|
|
123
|
+
for (const call of calls) {
|
|
124
|
+
const name = call.expression.getText(page)
|
|
125
|
+
findings.push(
|
|
126
|
+
diagnostic({
|
|
127
|
+
code: "NF-C053",
|
|
128
|
+
severity: "error",
|
|
129
|
+
file: `routes/${route.file}`,
|
|
130
|
+
line: lineOf(page, call),
|
|
131
|
+
message: `routes/${route.file} calls ${name}() in a partially prerendered page. Its shell is built once and served to every visitor, so the shell check refuses a page that reads the request. Read it in a dynamic hole's loader, and render what uses it in that hole.`,
|
|
132
|
+
evidence: [`read: ${name}()`, `shell: routes/${backend} exports shellLoader`],
|
|
133
|
+
fix: { recipe: "ppr.read-to-hole", command: "nifra fix --code NF-C053" },
|
|
134
|
+
verify: "nifra check",
|
|
135
|
+
}),
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return findings
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export const pprRules = Object.freeze([shellRequestReadRule])
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type * as TSApi from "typescript"
|
|
2
|
+
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
3
|
+
import type { CheckRule } from "./index.ts"
|
|
4
|
+
import {
|
|
5
|
+
type FileMarkup,
|
|
6
|
+
HOLE,
|
|
7
|
+
lineOf,
|
|
8
|
+
projectMarkup,
|
|
9
|
+
type SlotCall,
|
|
10
|
+
type SwapMarkup,
|
|
11
|
+
UNKNOWN,
|
|
12
|
+
} from "./islands.ts"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* NF-C049 to NF-C052 - the slot swap lints. A `data-swap` link or form (`@nifrajs/web/islands/swap`)
|
|
16
|
+
* refreshes one `data-swap-slot` from the server's answer for that slot, which `slot()` in
|
|
17
|
+
* `@nifrajs/web-vanilla` renders. Every way it can go wrong falls back to a normal page load, so
|
|
18
|
+
* nothing breaks loudly: these lints say where a swap can never happen, or loses content when it does.
|
|
19
|
+
* Like the binding lints, a value that is not literal is not judged.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
// The client and server accept these and nothing else.
|
|
23
|
+
const MODE = /^(replace|append):([A-Za-z][\w-]{0,63})$/
|
|
24
|
+
const SLOT_NAME = /^[A-Za-z][\w-]{0,63}$/
|
|
25
|
+
|
|
26
|
+
const fix = { recipe: "islands.swap", command: "nifra_docs islands" }
|
|
27
|
+
const verify = "nifra check --lints-only"
|
|
28
|
+
|
|
29
|
+
function swapUses(
|
|
30
|
+
files: readonly FileMarkup[],
|
|
31
|
+
): Array<{ readonly file: FileMarkup; readonly use: SwapMarkup }> {
|
|
32
|
+
return files.flatMap((file) => file.swaps.map((use) => ({ file, use })))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Every slot name the project's markup declares, or UNKNOWN when one is computed and could be any. */
|
|
36
|
+
function declaredSlots(uses: ReturnType<typeof swapUses>): Set<string> | typeof UNKNOWN {
|
|
37
|
+
const declared = new Set<string>()
|
|
38
|
+
for (const { use } of uses) {
|
|
39
|
+
if (use.kind !== "slot") continue
|
|
40
|
+
if (use.value === UNKNOWN) return UNKNOWN
|
|
41
|
+
declared.add(use.value)
|
|
42
|
+
}
|
|
43
|
+
return declared
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const swapAttributeRule: CheckRule = {
|
|
47
|
+
code: "NF-C049",
|
|
48
|
+
title: "Swap link or form misdeclared",
|
|
49
|
+
async scan(ctx) {
|
|
50
|
+
const { diagnostics, markup } = await projectMarkup(
|
|
51
|
+
ctx,
|
|
52
|
+
"NF-C049",
|
|
53
|
+
"Swap link or form misdeclared",
|
|
54
|
+
)
|
|
55
|
+
if (diagnostics !== undefined) return diagnostics
|
|
56
|
+
const findings: Diagnostic[] = []
|
|
57
|
+
for (const { file, use } of swapUses(markup.files)) {
|
|
58
|
+
if (use.kind !== "swap") continue
|
|
59
|
+
let message: string | undefined
|
|
60
|
+
if (use.jsx)
|
|
61
|
+
message =
|
|
62
|
+
"data-swap in JSX never swaps: only a @nifrajs/web-vanilla page answers a slot swap, so the link or form loads the page normally"
|
|
63
|
+
else if (use.tag !== "a" && use.tag !== "form")
|
|
64
|
+
message = `data-swap on <${use.tag}> never swaps: it works on <a href> and <form> only`
|
|
65
|
+
else if (use.tag === "a" && use.url === undefined)
|
|
66
|
+
message = "data-swap on an <a> without href never swaps: there is no URL to fetch"
|
|
67
|
+
else if (typeof use.value === "string" && !MODE.test(use.value))
|
|
68
|
+
message = `data-swap="${use.value}" never swaps: write replace:<slot> or append:<slot>, the slot a letter then up to 63 letters, digits, _ or -`
|
|
69
|
+
if (message === undefined) continue
|
|
70
|
+
findings.push(
|
|
71
|
+
diagnostic({
|
|
72
|
+
code: "NF-C049",
|
|
73
|
+
severity: "warn",
|
|
74
|
+
file: file.file,
|
|
75
|
+
line: use.line ?? lineOf(file.tree, use.node),
|
|
76
|
+
message,
|
|
77
|
+
evidence: [`data-swap=${typeof use.value === "string" ? use.value : "(interpolated)"}`],
|
|
78
|
+
fix,
|
|
79
|
+
verify,
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
return findings
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const swapTargetRule: CheckRule = {
|
|
88
|
+
code: "NF-C050",
|
|
89
|
+
title: "Swap names a slot nothing declares",
|
|
90
|
+
async scan(ctx) {
|
|
91
|
+
const { diagnostics, markup } = await projectMarkup(
|
|
92
|
+
ctx,
|
|
93
|
+
"NF-C050",
|
|
94
|
+
"Swap names a slot nothing declares",
|
|
95
|
+
)
|
|
96
|
+
if (diagnostics !== undefined) return diagnostics
|
|
97
|
+
const uses = swapUses(markup.files)
|
|
98
|
+
const declared = declaredSlots(uses)
|
|
99
|
+
if (declared === UNKNOWN) return []
|
|
100
|
+
const findings: Diagnostic[] = []
|
|
101
|
+
for (const { file, use } of uses) {
|
|
102
|
+
if (use.kind !== "swap" || use.jsx || typeof use.value !== "string") continue
|
|
103
|
+
const name = MODE.exec(use.value)?.[2]
|
|
104
|
+
if (name === undefined || declared.has(name)) continue
|
|
105
|
+
findings.push(
|
|
106
|
+
diagnostic({
|
|
107
|
+
code: "NF-C050",
|
|
108
|
+
severity: "warn",
|
|
109
|
+
file: file.file,
|
|
110
|
+
line: use.line ?? lineOf(file.tree, use.node),
|
|
111
|
+
message: `data-swap="${use.value}" names slot "${name}", which no data-swap-slot in the project declares, so the ${use.tag === "form" ? "form submits" : "link loads"} normally`,
|
|
112
|
+
evidence: [`data-swap-slot="${name}" not found`],
|
|
113
|
+
fix,
|
|
114
|
+
verify,
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
return findings
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Whether `node` is one of the file's `slot()` calls, and which. */
|
|
123
|
+
function slotCallOf(file: FileMarkup, node: TSApi.Node): SlotCall | undefined {
|
|
124
|
+
return file.slotCalls.find((call) => call.node === node)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export const slotPlacementRule: CheckRule = {
|
|
128
|
+
code: "NF-C051",
|
|
129
|
+
title: "Slot content is not its slot() alone",
|
|
130
|
+
async scan(ctx) {
|
|
131
|
+
const { diagnostics, ts, markup } = await projectMarkup(
|
|
132
|
+
ctx,
|
|
133
|
+
"NF-C051",
|
|
134
|
+
"Slot content is not its slot() alone",
|
|
135
|
+
)
|
|
136
|
+
if (diagnostics !== undefined || ts === undefined) return diagnostics ?? []
|
|
137
|
+
const findings: Diagnostic[] = []
|
|
138
|
+
const report = (file: FileMarkup, line: number, message: string, evidence: string): void => {
|
|
139
|
+
findings.push(
|
|
140
|
+
diagnostic({
|
|
141
|
+
code: "NF-C051",
|
|
142
|
+
severity: "warn",
|
|
143
|
+
file: file.file,
|
|
144
|
+
line,
|
|
145
|
+
message,
|
|
146
|
+
evidence: [evidence],
|
|
147
|
+
fix,
|
|
148
|
+
verify,
|
|
149
|
+
}),
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
for (const file of markup.files) {
|
|
153
|
+
const seen = new Map<TSApi.Node, Set<string>>()
|
|
154
|
+
for (const use of file.swaps) {
|
|
155
|
+
if (use.kind !== "slot" || use.jsx || typeof use.value !== "string") continue
|
|
156
|
+
const name = use.value
|
|
157
|
+
const evidence = `data-swap-slot="${name}"`
|
|
158
|
+
if (!SLOT_NAME.test(name)) {
|
|
159
|
+
report(
|
|
160
|
+
file,
|
|
161
|
+
use.line ?? lineOf(file.tree, use.node),
|
|
162
|
+
`data-swap-slot="${name}" is never swapped: a slot name is a letter then up to 63 letters, digits, _ or -`,
|
|
163
|
+
evidence,
|
|
164
|
+
)
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
const names = seen.get(use.node) ?? new Set<string>()
|
|
168
|
+
seen.set(use.node, names)
|
|
169
|
+
if (names.has(name)) {
|
|
170
|
+
report(
|
|
171
|
+
file,
|
|
172
|
+
use.line ?? lineOf(file.tree, use.node),
|
|
173
|
+
`a second data-swap-slot="${name}" in this markup is never swapped: a swap answers and fills the first slot of a name`,
|
|
174
|
+
evidence,
|
|
175
|
+
)
|
|
176
|
+
continue
|
|
177
|
+
}
|
|
178
|
+
names.add(name)
|
|
179
|
+
const content = use.content
|
|
180
|
+
if (content === undefined) continue
|
|
181
|
+
const [only] = content.holes
|
|
182
|
+
if (content.text.trim() === HOLE && only !== undefined) {
|
|
183
|
+
const call = slotCallOf(file, only)
|
|
184
|
+
// Anything but a slot() call may be a component that renders one.
|
|
185
|
+
if (call === undefined || call.name === UNKNOWN || call.name === name) continue
|
|
186
|
+
report(
|
|
187
|
+
file,
|
|
188
|
+
use.line ?? lineOf(file.tree, use.node),
|
|
189
|
+
`data-swap-slot="${name}" holds slot("${call.name}", ...): the server answers a swap of "${name}" from a slot() of that name, so this one is never swapped`,
|
|
190
|
+
evidence,
|
|
191
|
+
)
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
const holds = content.holes.some((hole) => slotCallOf(file, hole)?.name === name)
|
|
195
|
+
report(
|
|
196
|
+
file,
|
|
197
|
+
use.line ?? lineOf(file.tree, use.node),
|
|
198
|
+
holds
|
|
199
|
+
? `data-swap-slot="${name}" holds content beside slot("${name}", ...): a swap replaces it all with what slot() renders, so that content is lost. Move it out of the slot element`
|
|
200
|
+
: `data-swap-slot="${name}" holds no slot("${name}", ...): the server has nothing to answer a swap of it with, so every swap loads the page normally. Make slot("${name}", content) its only content`,
|
|
201
|
+
evidence,
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
for (const call of file.slotCalls) {
|
|
205
|
+
if (call.name === UNKNOWN) continue
|
|
206
|
+
// Only a call placed straight into a template is judged; a returned one renders elsewhere.
|
|
207
|
+
const parent = call.node.parent
|
|
208
|
+
if (!ts.isTemplateSpan(parent)) continue
|
|
209
|
+
const placed = file.placement.get(call.node)
|
|
210
|
+
if (placed === undefined || placed.slot !== null) continue
|
|
211
|
+
report(
|
|
212
|
+
file,
|
|
213
|
+
lineOf(file.tree, call.node),
|
|
214
|
+
`slot("${call.name}", ...) is not the content of a data-swap-slot element, so nothing on the page is swapped with it. Wrap it: <div data-swap-slot="${call.name}">\${slot("${call.name}", ...)}</div>`,
|
|
215
|
+
`slot("${call.name}")`,
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return findings
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Bare imports that look like path aliases may hold the app's own components.
|
|
224
|
+
const ALIAS = /^(?:~|@\/|#)/
|
|
225
|
+
// A stylesheet, an image, a font, data or another framework's component never calls slot(). Any other
|
|
226
|
+
// dotted name may be a module (`./cart.client`), so it is resolved like one.
|
|
227
|
+
const NOT_SCRIPT =
|
|
228
|
+
/\.(?:css|scss|sass|less|styl|pcss|svg|png|jpe?g|gif|webp|avif|ico|bmp|woff2?|ttf|otf|eot|json|txt|md|html|wasm|mp3|mp4|webm|ogg|wav|svelte|vue)(?:\?.*)?$/i
|
|
229
|
+
const PAGE_FILE = /^routes\/(.+\.(?:tsx|jsx|svelte|vue|mdx))$/
|
|
230
|
+
|
|
231
|
+
/** Resolve a relative import of `from` among the project's files. */
|
|
232
|
+
function resolveImport(
|
|
233
|
+
from: string,
|
|
234
|
+
specifier: string,
|
|
235
|
+
files: ReadonlySet<string>,
|
|
236
|
+
): string | undefined {
|
|
237
|
+
const base = from.slice(0, from.lastIndexOf("/") + 1)
|
|
238
|
+
const parts: string[] = []
|
|
239
|
+
for (const part of `${base}${specifier}`.split("/")) {
|
|
240
|
+
if (part === "..") parts.pop()
|
|
241
|
+
else if (part !== "." && part !== "") parts.push(part)
|
|
242
|
+
}
|
|
243
|
+
const path = parts.join("/")
|
|
244
|
+
const stem = path.replace(/\.(?:js|jsx|mjs)$/, "")
|
|
245
|
+
for (const candidate of [
|
|
246
|
+
path,
|
|
247
|
+
`${stem}.ts`,
|
|
248
|
+
`${stem}.tsx`,
|
|
249
|
+
`${stem}.js`,
|
|
250
|
+
`${stem}.jsx`,
|
|
251
|
+
`${path}/index.ts`,
|
|
252
|
+
`${path}/index.tsx`,
|
|
253
|
+
])
|
|
254
|
+
if (files.has(candidate)) return candidate
|
|
255
|
+
return undefined
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The slot names `entries` and the project files they import render, or UNKNOWN when an import
|
|
259
|
+
* cannot be followed. */
|
|
260
|
+
function renderedSlots(
|
|
261
|
+
ts: typeof TSApi,
|
|
262
|
+
entries: readonly string[],
|
|
263
|
+
byFile: ReadonlyMap<string, FileMarkup>,
|
|
264
|
+
): Set<string> | typeof UNKNOWN {
|
|
265
|
+
const names = new Set<string>()
|
|
266
|
+
const files = new Set(byFile.keys())
|
|
267
|
+
const pending = [...entries]
|
|
268
|
+
const visited = new Set<string>()
|
|
269
|
+
while (pending.length > 0) {
|
|
270
|
+
const path = pending.pop()
|
|
271
|
+
if (path === undefined || visited.has(path)) continue
|
|
272
|
+
visited.add(path)
|
|
273
|
+
const file = byFile.get(path)
|
|
274
|
+
if (file === undefined) return UNKNOWN
|
|
275
|
+
for (const call of file.slotCalls) {
|
|
276
|
+
if (call.name === UNKNOWN) return UNKNOWN
|
|
277
|
+
names.add(call.name)
|
|
278
|
+
}
|
|
279
|
+
for (const statement of file.tree.statements) {
|
|
280
|
+
const specifier =
|
|
281
|
+
(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) &&
|
|
282
|
+
statement.moduleSpecifier !== undefined &&
|
|
283
|
+
ts.isStringLiteral(statement.moduleSpecifier)
|
|
284
|
+
? statement.moduleSpecifier.text
|
|
285
|
+
: undefined
|
|
286
|
+
if (specifier === undefined || NOT_SCRIPT.test(specifier)) continue
|
|
287
|
+
if (specifier.startsWith(".")) {
|
|
288
|
+
const target = resolveImport(path, specifier, files)
|
|
289
|
+
if (target === undefined) return UNKNOWN
|
|
290
|
+
pending.push(target)
|
|
291
|
+
} else if (ALIAS.test(specifier)) return UNKNOWN
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return names
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** The path part of a link's href or a form's action, when it is literal. */
|
|
298
|
+
function literalPath(url: string): string | undefined {
|
|
299
|
+
const end = url.search(/[?#]/)
|
|
300
|
+
const path = end < 0 ? url : url.slice(0, end)
|
|
301
|
+
return path.includes(HOLE) ? undefined : path
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export const swapRouteRule: CheckRule = {
|
|
305
|
+
code: "NF-C052",
|
|
306
|
+
title: "Swap URL's page renders no such slot",
|
|
307
|
+
async scan(ctx) {
|
|
308
|
+
const { diagnostics, ts, markup } = await projectMarkup(
|
|
309
|
+
ctx,
|
|
310
|
+
"NF-C052",
|
|
311
|
+
"Swap URL's page renders no such slot",
|
|
312
|
+
)
|
|
313
|
+
if (diagnostics !== undefined || ts === undefined) return diagnostics ?? []
|
|
314
|
+
const uses = swapUses(markup.files)
|
|
315
|
+
const declared = declaredSlots(uses)
|
|
316
|
+
const targets: Array<{
|
|
317
|
+
file: FileMarkup
|
|
318
|
+
use: SwapMarkup
|
|
319
|
+
value: string
|
|
320
|
+
name: string
|
|
321
|
+
path: string
|
|
322
|
+
}> = []
|
|
323
|
+
for (const { file, use } of uses) {
|
|
324
|
+
if (use.kind !== "swap" || use.jsx || typeof use.value !== "string") continue
|
|
325
|
+
const name = MODE.exec(use.value)?.[2]
|
|
326
|
+
// A name no slot declares is NF-C050's finding.
|
|
327
|
+
if (
|
|
328
|
+
name === undefined ||
|
|
329
|
+
use.url === UNKNOWN ||
|
|
330
|
+
(declared !== UNKNOWN && !declared.has(name))
|
|
331
|
+
)
|
|
332
|
+
continue
|
|
333
|
+
const path = literalPath(use.url ?? "")
|
|
334
|
+
if (path !== undefined) targets.push({ file, use, value: use.value, name, path })
|
|
335
|
+
}
|
|
336
|
+
const pages = markup.files.flatMap((file) => {
|
|
337
|
+
const match = PAGE_FILE.exec(file.file)
|
|
338
|
+
return match?.[1] === undefined ? [] : [match[1]]
|
|
339
|
+
})
|
|
340
|
+
if (targets.length === 0 || pages.length === 0) return []
|
|
341
|
+
const { buildManifest } = await import("@nifrajs/web")
|
|
342
|
+
const { compileRoutePattern, matchRoutePattern, sortRoutesBySpecificity } = await import(
|
|
343
|
+
"@nifrajs/core/pattern"
|
|
344
|
+
)
|
|
345
|
+
let manifest: ReturnType<typeof buildManifest>
|
|
346
|
+
try {
|
|
347
|
+
manifest = buildManifest(pages, () => async () => ({ default: null }))
|
|
348
|
+
} catch {
|
|
349
|
+
return []
|
|
350
|
+
}
|
|
351
|
+
const routes = sortRoutesBySpecificity(
|
|
352
|
+
manifest.routes.flatMap((route) => {
|
|
353
|
+
try {
|
|
354
|
+
return [{ route, pattern: compileRoutePattern(route.pattern) }]
|
|
355
|
+
} catch {
|
|
356
|
+
return []
|
|
357
|
+
}
|
|
358
|
+
}),
|
|
359
|
+
)
|
|
360
|
+
const byFile = new Map(markup.files.map((file) => [file.file, file]))
|
|
361
|
+
const findings: Diagnostic[] = []
|
|
362
|
+
for (const { file, use, value, name, path } of targets) {
|
|
363
|
+
// An empty or query-only URL is the page's own; elsewhere only a root-relative path is judged.
|
|
364
|
+
const route =
|
|
365
|
+
path === ""
|
|
366
|
+
? routes.find((entry) => `routes/${entry.route.file}` === file.file)?.route
|
|
367
|
+
: path.startsWith("/")
|
|
368
|
+
? routes.find((entry) => matchRoutePattern(entry.pattern, path).matched)?.route
|
|
369
|
+
: undefined
|
|
370
|
+
if (route === undefined) continue
|
|
371
|
+
const chain = [
|
|
372
|
+
`routes/${route.file}`,
|
|
373
|
+
...route.layoutIds.flatMap((id) => {
|
|
374
|
+
const layout = manifest.layouts[id]
|
|
375
|
+
return layout === undefined ? [] : [`routes/${layout.file}`]
|
|
376
|
+
}),
|
|
377
|
+
]
|
|
378
|
+
const rendered = renderedSlots(ts, chain, byFile)
|
|
379
|
+
if (rendered === UNKNOWN || rendered.has(name)) continue
|
|
380
|
+
findings.push(
|
|
381
|
+
diagnostic({
|
|
382
|
+
code: "NF-C052",
|
|
383
|
+
severity: "warn",
|
|
384
|
+
file: file.file,
|
|
385
|
+
line: use.line ?? lineOf(file.tree, use.node),
|
|
386
|
+
message: `data-swap="${value}" goes to ${route.pattern}, whose page and layouts render no slot("${name}", ...), so every swap loads the page normally`,
|
|
387
|
+
evidence: [`${route.pattern} -> ${chain.join(", ")}`],
|
|
388
|
+
fix,
|
|
389
|
+
verify,
|
|
390
|
+
}),
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
return findings
|
|
394
|
+
},
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export const swapRules = Object.freeze([
|
|
398
|
+
swapAttributeRule,
|
|
399
|
+
swapTargetRule,
|
|
400
|
+
slotPlacementRule,
|
|
401
|
+
swapRouteRule,
|
|
402
|
+
])
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
2
|
+
import { createSourceFacts } from "../internal/source-facts.ts"
|
|
3
|
+
import { commentBlockExpectation } from "./comment-markers.ts"
|
|
4
|
+
import type { CheckRule } from "./index.ts"
|
|
5
|
+
import { loadRuleTypeScript } from "./typescript.ts"
|
|
6
|
+
|
|
7
|
+
const TEXT_SLOT_BOOLEAN_EXPECTATION = ["NF-C055", "text-slot-boolean"]
|
|
8
|
+
const ZOD_IMPORT = /from\s+["']zod(?:\/v4)?(?:\/mini)?["']/
|
|
9
|
+
const VALUE_IN = { query: "query", headers: "header", cookies: "cookie" } as const
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* NF-C055: a zod `z.coerce.boolean()` in a route's `query`, `headers` or `cookies` schema. It is
|
|
13
|
+
* `Boolean(value)`, and those values always arrive as text, so every non-empty value reads as `true`:
|
|
14
|
+
* `?archived=false` and `?archived=0` both do. `z.stringbool()` reads the words a request sends.
|
|
15
|
+
*/
|
|
16
|
+
export const textSlotBooleanRule: CheckRule = {
|
|
17
|
+
code: "NF-C055",
|
|
18
|
+
title: "zod coerce.boolean() reads request text",
|
|
19
|
+
async scan(ctx) {
|
|
20
|
+
const loaded = await loadRuleTypeScript(ctx)
|
|
21
|
+
if (loaded.diagnostics !== undefined) return loaded.diagnostics
|
|
22
|
+
const ts = loaded.compiler
|
|
23
|
+
if (ts === undefined) return []
|
|
24
|
+
const facts = createSourceFacts(ts)
|
|
25
|
+
const findings: Diagnostic[] = []
|
|
26
|
+
for (const file of ctx.project.source.files) {
|
|
27
|
+
if (!/\.[cm]?tsx?$/.test(file)) continue
|
|
28
|
+
const text = ctx.project.source.read(file)
|
|
29
|
+
if (text === undefined || !text.includes("coerce") || !ZOD_IMPORT.test(text)) continue
|
|
30
|
+
const tree = facts.parse(file, text)
|
|
31
|
+
if (tree === undefined) continue
|
|
32
|
+
const lines = text.split("\n")
|
|
33
|
+
for (const found of facts.textSlotBooleanCoercions(tree)) {
|
|
34
|
+
const line = tree.getLineAndCharacterOfPosition(found.position).line + 1
|
|
35
|
+
if (commentBlockExpectation(lines, line, TEXT_SLOT_BOOLEAN_EXPECTATION) !== undefined)
|
|
36
|
+
continue
|
|
37
|
+
findings.push(
|
|
38
|
+
diagnostic({
|
|
39
|
+
code: "NF-C055",
|
|
40
|
+
severity: "warn",
|
|
41
|
+
file,
|
|
42
|
+
line,
|
|
43
|
+
message: `\`coerce.boolean()\` in the \`${found.slot}\` schema of ${found.method} ${found.path} reads every non-empty value as true, "false" and "0" included: a ${VALUE_IN[found.slot]} value is always text. Use \`z.stringbool()\`, which reads "false", "0", "no" and "off" as false and refuses text it does not know.`,
|
|
44
|
+
evidence: [
|
|
45
|
+
"zod's coerce.boolean() is Boolean(value): any non-empty string is true",
|
|
46
|
+
`${found.slot} values arrive as text, so "false" never reaches the schema as false`,
|
|
47
|
+
"a use kept on purpose: `// nifra-expect NF-C055: <reason>` above the call",
|
|
48
|
+
],
|
|
49
|
+
verify: "nifra check",
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return findings
|
|
55
|
+
},
|
|
56
|
+
}
|