@nifrajs/cli 5.8.0 → 5.10.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 +191 -0
- package/dist/agent-files.d.ts +0 -22
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +3 -1
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +2 -10
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +3 -3
- package/dist/assure.js.map +1 -1
- package/dist/check-diagnostics.d.ts +1 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +4 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +3 -0
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +62 -87
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts +3 -0
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +27 -25
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +53 -20
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +1 -5
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +20 -49
- package/dist/command-catalog.js.map +1 -1
- package/dist/config-target.js +1 -1
- package/dist/config-target.js.map +1 -1
- package/dist/db-child.js +3 -10
- package/dist/db-child.js.map +1 -1
- package/dist/db-config.d.ts +2 -0
- package/dist/db-config.d.ts.map +1 -1
- package/dist/db-config.js +2 -0
- package/dist/db-config.js.map +1 -1
- package/dist/doctor.d.ts +14 -3
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +91 -6
- package/dist/doctor.js.map +1 -1
- package/dist/frontend-guidance.d.ts +19 -0
- package/dist/frontend-guidance.d.ts.map +1 -1
- package/dist/frontend-guidance.js +267 -23
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/hand-built-manifest.d.ts.map +1 -1
- package/dist/hand-built-manifest.js +1 -1
- package/dist/hand-built-manifest.js.map +1 -1
- package/dist/internal/control-character.d.ts +3 -0
- package/dist/internal/control-character.d.ts.map +1 -0
- package/dist/internal/control-character.js +10 -0
- package/dist/internal/control-character.js.map +1 -0
- package/dist/internal/escape-regexp.d.ts +3 -0
- package/dist/internal/escape-regexp.d.ts.map +1 -0
- package/dist/internal/escape-regexp.js +5 -0
- package/dist/internal/escape-regexp.js.map +1 -0
- package/dist/internal/line-at.d.ts +3 -0
- package/dist/internal/line-at.d.ts.map +1 -0
- package/dist/internal/line-at.js +5 -0
- package/dist/internal/line-at.js.map +1 -0
- package/dist/internal/nifra-project.d.ts.map +1 -1
- package/dist/internal/nifra-project.js +35 -41
- package/dist/internal/nifra-project.js.map +1 -1
- package/dist/internal/posix-path.d.ts +3 -0
- package/dist/internal/posix-path.d.ts.map +1 -0
- package/dist/internal/posix-path.js +5 -0
- package/dist/internal/posix-path.js.map +1 -0
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +3 -10
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/realpath-or-self.d.ts +3 -0
- package/dist/internal/realpath-or-self.d.ts.map +1 -0
- package/dist/internal/realpath-or-self.js +11 -0
- package/dist/internal/realpath-or-self.js.map +1 -0
- package/dist/internal/record.d.ts +3 -0
- package/dist/internal/record.d.ts.map +1 -0
- package/dist/internal/record.js +5 -0
- package/dist/internal/record.js.map +1 -0
- package/dist/internal/source-facts.d.ts +30 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +155 -25
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/introspect.d.ts +0 -22
- package/dist/introspect.d.ts.map +1 -1
- package/dist/introspect.js +11 -151
- package/dist/introspect.js.map +1 -1
- package/dist/levels-tool.d.ts +2 -0
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +54 -64
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +8 -1
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +5 -0
- package/dist/load.js.map +1 -1
- package/dist/local-cli.d.ts +7 -0
- package/dist/local-cli.d.ts.map +1 -1
- package/dist/local-cli.js +13 -0
- package/dist/local-cli.js.map +1 -1
- package/dist/mcp-docs-tools.d.ts +1 -1
- package/dist/mcp-docs-tools.d.ts.map +1 -1
- package/dist/mcp-docs-tools.js +46 -3
- package/dist/mcp-docs-tools.js.map +1 -1
- package/dist/mcp-http.d.ts +1 -1
- package/dist/mcp-http.js +2 -2
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-io.d.ts +16 -0
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +63 -0
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-isolate.d.ts.map +1 -1
- package/dist/mcp-isolate.js +2 -1
- package/dist/mcp-isolate.js.map +1 -1
- package/dist/mcp-project-child.d.ts.map +1 -1
- package/dist/mcp-project-child.js +5 -12
- package/dist/mcp-project-child.js.map +1 -1
- package/dist/mcp-render.d.ts.map +1 -1
- package/dist/mcp-render.js +9 -51
- package/dist/mcp-render.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-run.d.ts.map +1 -1
- package/dist/mcp-run.js +3 -46
- package/dist/mcp-run.js.map +1 -1
- package/dist/mcp-tool-meta.d.ts.map +1 -1
- package/dist/mcp-tool-meta.js +0 -1
- package/dist/mcp-tool-meta.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +2 -18
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +36 -57
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +7 -7
- package/dist/output-schemas.js.map +1 -1
- package/dist/port.d.ts.map +1 -1
- package/dist/port.js +1 -2
- package/dist/port.js.map +1 -1
- package/dist/ppr-read-fix.js +2 -2
- package/dist/ppr-read-fix.js.map +1 -1
- package/dist/recipes/5.10.0.d.ts +12 -0
- package/dist/recipes/5.10.0.d.ts.map +1 -0
- package/dist/recipes/5.10.0.js +44 -0
- package/dist/recipes/5.10.0.js.map +1 -0
- package/dist/recipes/5.9.0.d.ts +10 -0
- package/dist/recipes/5.9.0.d.ts.map +1 -0
- package/dist/recipes/5.9.0.js +41 -0
- package/dist/recipes/5.9.0.js.map +1 -0
- package/dist/recipes/index.d.ts +5 -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 +12 -13
- package/dist/review.js.map +1 -1
- package/dist/rules/codes.d.ts +3 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +3 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +3 -9
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.d.ts.map +1 -1
- package/dist/rules/data-guard.js +3 -10
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +15 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/internal-links.d.ts +5 -0
- package/dist/rules/internal-links.d.ts.map +1 -0
- package/dist/rules/internal-links.js +418 -0
- package/dist/rules/internal-links.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +20 -0
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +41 -58
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +20 -27
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +206 -313
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/server-fn-namespace.d.ts +8 -0
- package/dist/rules/server-fn-namespace.d.ts.map +1 -0
- package/dist/rules/server-fn-namespace.js +43 -0
- package/dist/rules/server-fn-namespace.js.map +1 -0
- package/dist/rules/source-scan.d.ts +29 -0
- package/dist/rules/source-scan.d.ts.map +1 -0
- package/dist/rules/source-scan.js +38 -0
- package/dist/rules/source-scan.js.map +1 -0
- package/dist/rules/text-slot-boolean.d.ts.map +1 -1
- package/dist/rules/text-slot-boolean.js +21 -43
- package/dist/rules/text-slot-boolean.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +3 -10
- package/dist/rules/zones.js.map +1 -1
- package/dist/runtime-differences.d.ts +62 -0
- package/dist/runtime-differences.d.ts.map +1 -0
- package/dist/runtime-differences.js +222 -0
- package/dist/runtime-differences.js.map +1 -0
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +43 -43
- package/dist/sdk.js.map +1 -1
- package/dist/smoke.d.ts.map +1 -1
- package/dist/smoke.js +4 -12
- package/dist/smoke.js.map +1 -1
- package/dist/source-rewrites.d.ts +27 -0
- package/dist/source-rewrites.d.ts.map +1 -0
- package/dist/source-rewrites.js +517 -0
- package/dist/source-rewrites.js.map +1 -0
- package/dist/stylex-migrate.d.ts.map +1 -1
- package/dist/stylex-migrate.js +1 -1
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/symptom-search.d.ts +5 -0
- package/dist/symptom-search.d.ts.map +1 -0
- package/dist/symptom-search.js +21 -0
- package/dist/symptom-search.js.map +1 -0
- package/dist/typecheck-coverage.d.ts +9 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +16 -0
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +28 -3
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +153 -64
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +5 -3
- package/dist/verification-plan.js.map +1 -1
- package/docs/examples.json +51 -9
- package/docs/llms-full.txt +1296 -287
- package/docs/types.json +854 -170
- package/package.json +11 -11
- package/src/agent-files.ts +4 -1
- package/src/assure-hydration.ts +2 -9
- package/src/assure.ts +3 -4
- package/src/check-diagnostics.ts +5 -0
- package/src/check-scan.ts +74 -83
- package/src/check.ts +40 -26
- package/src/cli.ts +76 -20
- package/src/command-catalog.ts +22 -56
- package/src/config-target.ts +1 -1
- package/src/db-child.ts +3 -10
- package/src/db-config.ts +4 -0
- package/src/doctor.ts +128 -12
- package/src/frontend-guidance.ts +344 -25
- package/src/hand-built-manifest.ts +1 -2
- package/src/internal/control-character.ts +8 -0
- package/src/internal/escape-regexp.ts +4 -0
- package/src/internal/line-at.ts +4 -0
- package/src/internal/nifra-project.ts +39 -35
- package/src/internal/posix-path.ts +4 -0
- package/src/internal/project-specifiers.ts +3 -10
- package/src/internal/realpath-or-self.ts +10 -0
- package/src/internal/record.ts +4 -0
- package/src/internal/source-facts.ts +194 -29
- package/src/introspect.ts +16 -156
- package/src/levels-tool.ts +71 -78
- package/src/load.ts +15 -1
- package/src/local-cli.ts +12 -0
- package/src/mcp-docs-tools.ts +51 -3
- package/src/mcp-http.ts +2 -2
- package/src/mcp-io.ts +77 -0
- package/src/mcp-isolate.ts +2 -3
- package/src/mcp-project-child.ts +5 -14
- package/src/mcp-render.ts +11 -59
- package/src/mcp-root.ts +1 -1
- package/src/mcp-run.ts +5 -54
- package/src/mcp-tool-meta.ts +0 -1
- package/src/mcp-ws.ts +2 -19
- package/src/migrate-layout.ts +52 -72
- package/src/output-schemas.ts +7 -8
- package/src/port.ts +1 -4
- package/src/ppr-read-fix.ts +2 -2
- package/src/recipes/5.10.0.ts +45 -0
- package/src/recipes/5.9.0.ts +42 -0
- package/src/recipes/index.ts +9 -0
- package/src/review.ts +15 -15
- package/src/rules/codes.ts +3 -0
- package/src/rules/contract-visibility.ts +3 -8
- package/src/rules/data-guard.ts +3 -10
- package/src/rules/explanations.ts +15 -0
- package/src/rules/internal-links.ts +490 -0
- package/src/rules/legacy.ts +21 -0
- package/src/rules/pages.ts +47 -58
- package/src/rules/routes.ts +21 -27
- package/src/rules/security.ts +224 -255
- package/src/rules/server-fn-namespace.ts +47 -0
- package/src/rules/source-scan.ts +66 -0
- package/src/rules/text-slot-boolean.ts +26 -42
- package/src/rules/zones.ts +3 -10
- package/src/runtime-differences.ts +302 -0
- package/src/sdk.ts +55 -58
- package/src/smoke.ts +4 -13
- package/src/source-rewrites.ts +592 -0
- package/src/stylex-migrate.ts +1 -3
- package/src/symptom-search.ts +27 -0
- package/src/typecheck-coverage.ts +23 -0
- package/src/upgrade.ts +194 -74
- package/src/verification-plan.ts +10 -4
- package/dist/sync-routes.d.ts +0 -29
- package/dist/sync-routes.d.ts.map +0 -1
- package/dist/sync-routes.js +0 -56
- package/dist/sync-routes.js.map +0 -1
- package/src/sync-routes.ts +0 -70
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
import { Glob } from "bun"
|
|
4
|
+
import { CONFIG_FILE, FRAMEWORK_FILE } from "../app-files.ts"
|
|
5
|
+
import { IGNORED, stripComments } from "../check-scan.ts"
|
|
6
|
+
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
7
|
+
import { codeUnitOrder } from "../internal/code-unit-order.ts"
|
|
8
|
+
import { toPosix } from "../internal/posix-path.ts"
|
|
9
|
+
import { ANCHOR_HOLE } from "../internal/source-facts.ts"
|
|
10
|
+
import { buildRouteTable } from "../introspect.ts"
|
|
11
|
+
import { commentBlockExpectation } from "./comment-markers.ts"
|
|
12
|
+
import type { CheckRule, RuleContext } from "./index.ts"
|
|
13
|
+
import { readStaticApiPrefix } from "./pages.ts"
|
|
14
|
+
import { scanSources } from "./source-scan.ts"
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* NF-C057 and NF-C058 - an `<a href="/path">` written as a string. `href()` checks a link against the
|
|
18
|
+
* app's routes, so a renamed or removed page is a compile error; a path string is invisible to the types.
|
|
19
|
+
* The two codes split what the route table can tell apart, so each takes its own severity:
|
|
20
|
+
*
|
|
21
|
+
* - NF-C057 (warn): the path matches no page, no backend route and no file: the link is a 404.
|
|
22
|
+
* - NF-C058 (info): the path is a page of the app, so it works today but nothing keeps it working.
|
|
23
|
+
*
|
|
24
|
+
* A template literal (`/users/${id}`) is judged by its shape only, and never reported as dead: the
|
|
25
|
+
* route it builds may live outside the file routes.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const TEST_DIR = /(?:^|\/)(?:tests?|__tests__)\//
|
|
29
|
+
const FILE_EXTENSION = /\.[A-Za-z0-9]{1,8}$/
|
|
30
|
+
// One attribute of a tag, matched where the previous one ended (sticky), so a tag is read in one pass:
|
|
31
|
+
// `href="/x"`, `href='/x'`, `href={"/x"}` or `` href={`/x`} `` (Svelte), and Vue's `:href="'/x'"`. Any
|
|
32
|
+
// other `{...}` value is an expression and names no path.
|
|
33
|
+
const ATTRIBUTE =
|
|
34
|
+
/\s+([^\s=>/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*(?:"([^"]*)"|'([^']*)'|`([^`]*)`)\s*\}|\{[^}]*\}))?/y
|
|
35
|
+
const BOUND_LITERAL = /^\s*(?:'([^']*)'|"([^"]*)"|`([^`$]*)`)\s*$/
|
|
36
|
+
|
|
37
|
+
interface Judged {
|
|
38
|
+
readonly pattern: string
|
|
39
|
+
readonly params: ReadonlyMap<string, string>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface PageRoutes {
|
|
43
|
+
/** The page route an anchor path resolves to, most specific first. */
|
|
44
|
+
match(path: string): Judged | undefined
|
|
45
|
+
/** Whether something other than a page serves `path`: a backend route or a file in `public/`. */
|
|
46
|
+
servedElsewhere(path: string): boolean
|
|
47
|
+
/** The static page pattern closest to `path`, when it is a typo away. */
|
|
48
|
+
nearest(path: string): string | undefined
|
|
49
|
+
/** Whether a backend route path the scan could not read leaves the dead-link verdict open. */
|
|
50
|
+
readonly backendUnknown: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const cache = new WeakMap<RuleContext, Promise<PageRoutes | undefined>>()
|
|
54
|
+
|
|
55
|
+
function distance(a: string, b: string): number {
|
|
56
|
+
let row = Array.from({ length: b.length + 1 }, (_, i) => i)
|
|
57
|
+
for (let i = 1; i <= a.length; i++) {
|
|
58
|
+
const next = [i]
|
|
59
|
+
for (let j = 1; j <= b.length; j++) {
|
|
60
|
+
next[j] = Math.min(
|
|
61
|
+
(row[j] ?? 0) + 1,
|
|
62
|
+
(next[j - 1] ?? 0) + 1,
|
|
63
|
+
(row[j - 1] ?? 0) + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
row = next
|
|
67
|
+
}
|
|
68
|
+
return row[b.length] ?? 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Read only literal mount exports, never import/evaluate the app's config. An unreadable field
|
|
72
|
+
* leaves the dead-link verdict open, just like an unreadable backend registration. */
|
|
73
|
+
function backendMount(
|
|
74
|
+
ctx: RuleContext,
|
|
75
|
+
): { readonly prefix: string | undefined; readonly strip: boolean } | undefined {
|
|
76
|
+
let prefix: string | undefined
|
|
77
|
+
let strip = false
|
|
78
|
+
let prefixUnknown = false
|
|
79
|
+
let stripUnknown = false
|
|
80
|
+
// nifra.config.ts overrides individual fields inherited from backend/framework.ts.
|
|
81
|
+
for (const file of [FRAMEWORK_FILE, CONFIG_FILE]) {
|
|
82
|
+
const path = join(ctx.root, file)
|
|
83
|
+
if (!existsSync(path)) continue
|
|
84
|
+
const source = stripComments(readFileSync(path, "utf8"))
|
|
85
|
+
const read = readStaticApiPrefix(source)
|
|
86
|
+
if (read.kind !== "default") {
|
|
87
|
+
prefixUnknown = read.kind === "unreadable"
|
|
88
|
+
if (read.kind === "literal") prefix = read.value
|
|
89
|
+
}
|
|
90
|
+
if (/\bapiStrip\b/.test(source)) {
|
|
91
|
+
const literal =
|
|
92
|
+
/\bexport\s+const\s+apiStrip\s*(?::\s*boolean\s*)?=\s*(true|false)\s*(?:as\s+const\s*)?(?:;|\r?\n|$)/.exec(
|
|
93
|
+
source,
|
|
94
|
+
)
|
|
95
|
+
stripUnknown = literal === null
|
|
96
|
+
if (literal !== null) strip = literal[1] === "true"
|
|
97
|
+
}
|
|
98
|
+
// A star re-export may supply either field; it cannot prove an absent/default mount.
|
|
99
|
+
if (/\bexport\s*\*/.test(source)) {
|
|
100
|
+
prefixUnknown = true
|
|
101
|
+
stripUnknown = true
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return prefixUnknown || stripUnknown ? undefined : { prefix, strip }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function pageRoutes(ctx: RuleContext): Promise<PageRoutes | undefined> {
|
|
108
|
+
let loaded = cache.get(ctx)
|
|
109
|
+
if (loaded === undefined) {
|
|
110
|
+
loaded = loadPageRoutes(ctx)
|
|
111
|
+
cache.set(ctx, loaded)
|
|
112
|
+
}
|
|
113
|
+
return loaded
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function loadPageRoutes(ctx: RuleContext): Promise<PageRoutes | undefined> {
|
|
117
|
+
// The pages come from the file system, not the scanned sources: a `.vue`, `.svelte` or `.mdx` page is
|
|
118
|
+
// not in the source index, and a missed page would turn its links into false 404s.
|
|
119
|
+
const routesDir = join(ctx.root, "routes")
|
|
120
|
+
if (!existsSync(routesDir)) return undefined
|
|
121
|
+
const { discoverRoutes } = await import("@nifrajs/web/fs")
|
|
122
|
+
const { compileRoutePattern, matchRoutePattern, sortRoutesBySpecificity } = await import(
|
|
123
|
+
"@nifrajs/core/pattern"
|
|
124
|
+
)
|
|
125
|
+
let patterns: string[]
|
|
126
|
+
try {
|
|
127
|
+
patterns = discoverRoutes(routesDir).routes.map((route) => route.pattern)
|
|
128
|
+
} catch {
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
if (patterns.length === 0) return undefined
|
|
132
|
+
// `sortRoutesBySpecificity` reads the compiled pattern from `pattern`, and carries the rest along.
|
|
133
|
+
const compile = (route: string) => {
|
|
134
|
+
try {
|
|
135
|
+
return [{ route, pattern: compileRoutePattern(route) }]
|
|
136
|
+
} catch {
|
|
137
|
+
return []
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const pageEntries = sortRoutesBySpecificity(patterns.flatMap(compile))
|
|
141
|
+
const mount = backendMount(ctx)
|
|
142
|
+
const backendEntries = buildRouteTable({
|
|
143
|
+
pages: [],
|
|
144
|
+
api: ctx.project.routes,
|
|
145
|
+
apiPrefix: mount?.prefix ?? "",
|
|
146
|
+
apiStrip: mount?.strip ?? false,
|
|
147
|
+
})
|
|
148
|
+
// Without a configured mount, preserve the scan's allowance for independently served backends.
|
|
149
|
+
.filter((route) => mount?.prefix === undefined || route.autoMounted)
|
|
150
|
+
.flatMap((route) => compile(route.path))
|
|
151
|
+
// Code-unit order, so which of two equally close pages a diagnostic names does not depend on the
|
|
152
|
+
// directory order of the machine that ran the check.
|
|
153
|
+
const staticPatterns = patterns.filter((pattern) => !/[:*]/.test(pattern)).sort(codeUnitOrder)
|
|
154
|
+
return {
|
|
155
|
+
backendUnknown: mount === undefined || ctx.project.sourceFindings.routePaths.length > 0,
|
|
156
|
+
match(path) {
|
|
157
|
+
for (const entry of pageEntries) {
|
|
158
|
+
const found = matchRoutePattern(entry.pattern, path)
|
|
159
|
+
if (found.matched)
|
|
160
|
+
return {
|
|
161
|
+
pattern: entry.route,
|
|
162
|
+
params: new Map(Object.entries(found.params).map(([k, v]) => [k, String(v)])),
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return undefined
|
|
166
|
+
},
|
|
167
|
+
servedElsewhere(path) {
|
|
168
|
+
if (backendEntries.some((entry) => matchRoutePattern(entry.pattern, path).matched))
|
|
169
|
+
return true
|
|
170
|
+
if (FILE_EXTENSION.test(path) || path.startsWith("/_")) return true
|
|
171
|
+
try {
|
|
172
|
+
return existsSync(join(ctx.root, "public", decodeURI(path)))
|
|
173
|
+
} catch {
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
nearest(path) {
|
|
178
|
+
let best: { pattern: string; at: number } | undefined
|
|
179
|
+
for (const pattern of staticPatterns) {
|
|
180
|
+
const at = distance(path, pattern)
|
|
181
|
+
if (at <= 2 && (best === undefined || at < best.at)) best = { pattern, at }
|
|
182
|
+
}
|
|
183
|
+
return best?.pattern
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface Verdict {
|
|
189
|
+
readonly kind: "dead" | "unchecked"
|
|
190
|
+
readonly shown: string
|
|
191
|
+
readonly message: string
|
|
192
|
+
readonly evidence: readonly string[]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const spell = (path: string): string => path.replaceAll(ANCHOR_HOLE, `\${…}`)
|
|
196
|
+
|
|
197
|
+
/** Judge one anchor path (a hole stands for a `${...}`), or undefined when it is not this rule's business. */
|
|
198
|
+
function judge(routes: PageRoutes, written: string): Verdict | undefined {
|
|
199
|
+
const cut = written.search(/[?#]/)
|
|
200
|
+
const path = cut < 0 ? written : written.slice(0, cut)
|
|
201
|
+
const rest = cut < 0 ? "" : written.slice(cut)
|
|
202
|
+
const dynamic = path.includes(ANCHOR_HOLE)
|
|
203
|
+
const found = routes.match(path)
|
|
204
|
+
const shown = spell(written)
|
|
205
|
+
if (found !== undefined) {
|
|
206
|
+
const hash = rest.includes("#") ? rest.slice(rest.indexOf("#") + 1) : undefined
|
|
207
|
+
const query = rest.startsWith("?")
|
|
208
|
+
const entries = [...found.params]
|
|
209
|
+
const params =
|
|
210
|
+
entries.length === 0
|
|
211
|
+
? ""
|
|
212
|
+
: `, params: { ${entries.map(([name, value]) => `${name}: ${dynamic ? "…" : JSON.stringify(value)}`).join(", ")} }`
|
|
213
|
+
const call = `href({ to: ${JSON.stringify(found.pattern)}${params}${query ? ", search: { … }" : ""}${hash !== undefined && hash !== "" && !hash.includes(ANCHOR_HOLE) ? `, hash: ${JSON.stringify(hash)}` : ""} })`
|
|
214
|
+
return {
|
|
215
|
+
kind: "unchecked",
|
|
216
|
+
shown,
|
|
217
|
+
message: `\`<a href="${shown}">\` writes a page's path by hand, so renaming or removing ${found.pattern} leaves it dead and nothing fails. Build it with \`${call}\` (\`href\` is exported by \`@nifrajs/web-<framework>/router\`), which checks the route and its params against the app.`,
|
|
218
|
+
evidence: [
|
|
219
|
+
`${shown} resolves to the page route ${found.pattern}`,
|
|
220
|
+
"a path built elsewhere, such as a CMS link, takes `rawHref(path)`",
|
|
221
|
+
],
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (dynamic || routes.backendUnknown || routes.servedElsewhere(path)) return undefined
|
|
225
|
+
const near = routes.nearest(path)
|
|
226
|
+
return {
|
|
227
|
+
kind: "dead",
|
|
228
|
+
shown,
|
|
229
|
+
message: `\`<a href="${shown}">\` links to a path no page of this app serves, so following it is a 404.${near === undefined ? "" : ` Did you mean ${near}?`} Use \`href({ to })\` with a route of the app, which fails to compile for one that does not exist.`,
|
|
230
|
+
evidence: [
|
|
231
|
+
`${path} matches no page route, no backend route and no file in public/`,
|
|
232
|
+
"a path another server answers takes `rawHref(path)`",
|
|
233
|
+
],
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const adviceFor = (code: string, template: boolean): string =>
|
|
238
|
+
template
|
|
239
|
+
? `a link kept as written, on purpose: \`<!-- nifra-expect ${code}: <reason> -->\` above the anchor`
|
|
240
|
+
: `a link kept as written, on purpose: \`// nifra-expect ${code}: <reason>\` above the attribute`
|
|
241
|
+
|
|
242
|
+
function report(
|
|
243
|
+
code: string,
|
|
244
|
+
severity: "warn" | "info",
|
|
245
|
+
file: string,
|
|
246
|
+
line: number,
|
|
247
|
+
verdict: Verdict,
|
|
248
|
+
note: string | undefined,
|
|
249
|
+
template = false,
|
|
250
|
+
): Diagnostic {
|
|
251
|
+
return diagnostic({
|
|
252
|
+
code,
|
|
253
|
+
severity,
|
|
254
|
+
file,
|
|
255
|
+
line,
|
|
256
|
+
message: note === undefined ? verdict.message : `${verdict.message} ${note}`,
|
|
257
|
+
evidence: [...verdict.evidence, adviceFor(code, template)],
|
|
258
|
+
verify: "nifra check",
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
interface HtmlComment {
|
|
263
|
+
readonly start: number
|
|
264
|
+
/** Where the text after `<!--` stops: before the closing delimiter. */
|
|
265
|
+
readonly bodyEnd: number
|
|
266
|
+
readonly end: number
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// A comment ends at `-->` or `--!>`.
|
|
270
|
+
const COMMENT_END = /--!?>/g
|
|
271
|
+
|
|
272
|
+
/** The comment opened at `start`, which HTML ends at `-->` or `--!>`; `<!-->` and `<!--->` are empty. */
|
|
273
|
+
function commentAt(text: string, start: number): HtmlComment {
|
|
274
|
+
const body = start + 4
|
|
275
|
+
if (text.startsWith(">", body)) return { start, bodyEnd: body, end: body + 1 }
|
|
276
|
+
if (text.startsWith("->", body)) return { start, bodyEnd: body, end: body + 2 }
|
|
277
|
+
COMMENT_END.lastIndex = body
|
|
278
|
+
const close = COMMENT_END.exec(text)
|
|
279
|
+
return close === null
|
|
280
|
+
? { start, bodyEnd: text.length, end: text.length }
|
|
281
|
+
: { start, bodyEnd: close.index, end: close.index + close[0].length }
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The end of a markup tag or `{expression}`, ignoring `>`/braces inside quoted values. */
|
|
285
|
+
function markupEnd(text: string, start: number, expression = false): number {
|
|
286
|
+
let quote: string | undefined
|
|
287
|
+
let braces = expression ? 1 : 0
|
|
288
|
+
for (let at = start; at < text.length; at++) {
|
|
289
|
+
const char = text[at]
|
|
290
|
+
if (quote !== undefined) {
|
|
291
|
+
if (braces > 0 && char === "\\") at++
|
|
292
|
+
else if (char === quote) quote = undefined
|
|
293
|
+
} else if (char === '"' || char === "'" || (braces > 0 && char === "`")) quote = char
|
|
294
|
+
else if (char === "{") braces++
|
|
295
|
+
else if (char === "}") {
|
|
296
|
+
braces--
|
|
297
|
+
if (expression && braces === 0) return at + 1
|
|
298
|
+
} else if (!expression && braces === 0 && char === ">") return at + 1
|
|
299
|
+
}
|
|
300
|
+
return text.length
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Scan markup, not script strings, HTML comments or other tags' quoted attributes. The existing
|
|
304
|
+
* componentScripts scanner only extracts scripts and cannot distinguish these template regions. */
|
|
305
|
+
function templateHrefs(
|
|
306
|
+
text: string,
|
|
307
|
+
vue: boolean,
|
|
308
|
+
): {
|
|
309
|
+
readonly hrefs: Array<{ readonly at: number; readonly tagAt: number; readonly value: string }>
|
|
310
|
+
readonly expectationLines: readonly string[]
|
|
311
|
+
} {
|
|
312
|
+
const hrefs: Array<{ at: number; tagAt: number; value: string }> = []
|
|
313
|
+
const comments: HtmlComment[] = []
|
|
314
|
+
let templateDepth = 0
|
|
315
|
+
for (let at = 0; at < text.length; ) {
|
|
316
|
+
if (text.startsWith("<!--", at)) {
|
|
317
|
+
const comment = commentAt(text, at)
|
|
318
|
+
comments.push(comment)
|
|
319
|
+
at = comment.end
|
|
320
|
+
continue
|
|
321
|
+
}
|
|
322
|
+
if (text[at] === "{" && (!vue || text.startsWith("{{", at))) {
|
|
323
|
+
at = markupEnd(text, at + 1, true)
|
|
324
|
+
continue
|
|
325
|
+
}
|
|
326
|
+
if (text[at] !== "<") {
|
|
327
|
+
at++
|
|
328
|
+
continue
|
|
329
|
+
}
|
|
330
|
+
const tag = /^<(\/?)([\w-]+)(?=[\s/>])/.exec(text.slice(at))
|
|
331
|
+
if (tag === null) {
|
|
332
|
+
at++
|
|
333
|
+
continue
|
|
334
|
+
}
|
|
335
|
+
const tagAt = at
|
|
336
|
+
const end = markupEnd(text, at + tag[0].length)
|
|
337
|
+
const name = tag[2]?.toLowerCase()
|
|
338
|
+
const closing = tag[1] === "/"
|
|
339
|
+
at = end
|
|
340
|
+
if (!closing && (name === "script" || name === "style")) {
|
|
341
|
+
const close = name === "script" ? /<\/script\s*>/gi : /<\/style\s*>/gi
|
|
342
|
+
close.lastIndex = end
|
|
343
|
+
const match = close.exec(text)
|
|
344
|
+
at = match === null ? text.length : close.lastIndex
|
|
345
|
+
continue
|
|
346
|
+
}
|
|
347
|
+
if (name === "template") templateDepth += closing ? -1 : 1
|
|
348
|
+
if (closing || name !== "a" || (vue && templateDepth <= 0)) continue
|
|
349
|
+
ATTRIBUTE.lastIndex = tagAt + tag[0].length
|
|
350
|
+
for (let match = ATTRIBUTE.exec(text); match !== null; match = ATTRIBUTE.exec(text)) {
|
|
351
|
+
const [whole, attribute, doubled, single, braceDouble, braceSingle, braceTemplate] = match
|
|
352
|
+
if (attribute !== "href" && attribute !== ":href" && attribute !== "v-bind:href") continue
|
|
353
|
+
const raw = doubled ?? single
|
|
354
|
+
const value =
|
|
355
|
+
attribute === "href"
|
|
356
|
+
? (raw ?? braceDouble ?? braceSingle ?? braceTemplate)
|
|
357
|
+
: raw
|
|
358
|
+
?.match(BOUND_LITERAL)
|
|
359
|
+
?.slice(1)
|
|
360
|
+
.find((part) => part !== undefined)
|
|
361
|
+
if (value !== undefined)
|
|
362
|
+
hrefs.push({
|
|
363
|
+
at: ATTRIBUTE.lastIndex - whole.length + whole.indexOf(attribute),
|
|
364
|
+
tagAt,
|
|
365
|
+
value,
|
|
366
|
+
})
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// Adapt only real HTML comments to the shared directive parser. Non-comment code stays a block
|
|
370
|
+
// boundary; quoted marker text cannot suppress anything. Line counts are unchanged.
|
|
371
|
+
const code = (part: string): string => part.replace(/[^\s]/g, "x")
|
|
372
|
+
let expectations = ""
|
|
373
|
+
let previous = 0
|
|
374
|
+
for (const comment of comments) {
|
|
375
|
+
expectations += code(text.slice(previous, comment.start))
|
|
376
|
+
expectations += text
|
|
377
|
+
.slice(comment.start + 4, comment.bodyEnd)
|
|
378
|
+
.split("\n")
|
|
379
|
+
.map((line) => `// ${line}`)
|
|
380
|
+
.join("\n")
|
|
381
|
+
previous = comment.end
|
|
382
|
+
}
|
|
383
|
+
expectations += code(text.slice(previous))
|
|
384
|
+
return { hrefs, expectationLines: expectations.split("\n") }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The 1-based line of an offset in `text`: a search over the line starts, so a file with thousands of links is not re-split for each. */
|
|
388
|
+
function lineLookup(text: string): (offset: number) => number {
|
|
389
|
+
const starts = [0]
|
|
390
|
+
for (let at = text.indexOf("\n"); at !== -1; at = text.indexOf("\n", at + 1)) starts.push(at + 1)
|
|
391
|
+
return (offset) => {
|
|
392
|
+
let low = 0
|
|
393
|
+
let high = starts.length - 1
|
|
394
|
+
while (low < high) {
|
|
395
|
+
const mid = (low + high + 1) >> 1
|
|
396
|
+
if ((starts[mid] ?? 0) <= offset) low = mid
|
|
397
|
+
else high = mid - 1
|
|
398
|
+
}
|
|
399
|
+
return low + 1
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async function templateFindings(
|
|
404
|
+
ctx: RuleContext,
|
|
405
|
+
routes: PageRoutes,
|
|
406
|
+
kind: Verdict["kind"],
|
|
407
|
+
code: string,
|
|
408
|
+
severity: "warn" | "info",
|
|
409
|
+
): Promise<Diagnostic[]> {
|
|
410
|
+
const findings: Diagnostic[] = []
|
|
411
|
+
const files: string[] = []
|
|
412
|
+
for await (const entry of new Glob("**/*.{vue,svelte}").scan({ cwd: ctx.root, dot: false })) {
|
|
413
|
+
// Bun yields `\` paths on Windows, which IGNORED and TEST_DIR (and the reported location) expect as `/`.
|
|
414
|
+
const file = toPosix(entry)
|
|
415
|
+
if (!IGNORED.test(file) && !TEST_DIR.test(file)) files.push(file)
|
|
416
|
+
}
|
|
417
|
+
for (const file of files.sort()) {
|
|
418
|
+
// The source index holds only a component's script blocks; the links are in its template.
|
|
419
|
+
const text = readFileSync(join(ctx.root, file), "utf8")
|
|
420
|
+
if (!text.includes("href")) continue
|
|
421
|
+
const { hrefs, expectationLines } = templateHrefs(text, file.endsWith(".vue"))
|
|
422
|
+
const lineOf = lineLookup(text)
|
|
423
|
+
for (const { at, tagAt, value } of hrefs) {
|
|
424
|
+
if (!value.startsWith("/") || value.startsWith("//")) continue
|
|
425
|
+
// A Svelte `{expression}` inside the value is a hole.
|
|
426
|
+
const verdict = judge(routes, value.replace(/\$?\{[^}]*\}/g, ANCHOR_HOLE))
|
|
427
|
+
if (verdict?.kind !== kind) continue
|
|
428
|
+
const line = lineOf(at)
|
|
429
|
+
const tagLine = lineOf(tagAt)
|
|
430
|
+
if (
|
|
431
|
+
commentBlockExpectation(expectationLines, tagLine, [code, "internal-link"]) !== undefined ||
|
|
432
|
+
commentBlockExpectation(expectationLines, line, [code, "internal-link"]) !== undefined
|
|
433
|
+
)
|
|
434
|
+
continue
|
|
435
|
+
findings.push(report(code, severity, file, line, verdict, undefined, true))
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return findings
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function internalLinkRule(
|
|
442
|
+
code: string,
|
|
443
|
+
title: string,
|
|
444
|
+
kind: Verdict["kind"],
|
|
445
|
+
severity: "warn" | "info",
|
|
446
|
+
): CheckRule {
|
|
447
|
+
return {
|
|
448
|
+
code,
|
|
449
|
+
title,
|
|
450
|
+
async scan(ctx) {
|
|
451
|
+
const routes = await pageRoutes(ctx)
|
|
452
|
+
if (routes === undefined) return []
|
|
453
|
+
const scripts = await scanSources(ctx, {
|
|
454
|
+
expectation: [code, "internal-link"],
|
|
455
|
+
mayMatch: (text) => text.includes("<a") && text.includes("href"),
|
|
456
|
+
find: (facts, tree) =>
|
|
457
|
+
TEST_DIR.test(tree.fileName)
|
|
458
|
+
? []
|
|
459
|
+
: facts.internalAnchors(tree).flatMap((anchor) => {
|
|
460
|
+
const verdict = judge(routes, anchor.path)
|
|
461
|
+
if (verdict?.kind !== kind) return []
|
|
462
|
+
return [
|
|
463
|
+
{
|
|
464
|
+
position: anchor.position,
|
|
465
|
+
report: (line: number, note?: string) =>
|
|
466
|
+
report(code, severity, tree.fileName, line, verdict, note),
|
|
467
|
+
},
|
|
468
|
+
]
|
|
469
|
+
}),
|
|
470
|
+
})
|
|
471
|
+
return [...scripts, ...(await templateFindings(ctx, routes, kind, code, severity))]
|
|
472
|
+
},
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export const deadInternalLinkRule = internalLinkRule(
|
|
477
|
+
"NF-C057",
|
|
478
|
+
"Internal link to a path no page serves",
|
|
479
|
+
"dead",
|
|
480
|
+
"warn",
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
export const uncheckedInternalLinkRule = internalLinkRule(
|
|
484
|
+
"NF-C058",
|
|
485
|
+
"Internal link written as a path string",
|
|
486
|
+
"unchecked",
|
|
487
|
+
"info",
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
export const internalLinkRules = Object.freeze([deadInternalLinkRule, uncheckedInternalLinkRule])
|
package/src/rules/legacy.ts
CHANGED
|
@@ -409,10 +409,31 @@ const typecheckRule: CheckRule = {
|
|
|
409
409
|
}
|
|
410
410
|
}
|
|
411
411
|
findings.push(...outsideProgramFindings(tc.outsideProgram ?? []))
|
|
412
|
+
if (tc.routeTableOutsideProgram !== undefined)
|
|
413
|
+
findings.push(routeTableFinding(tc.routeTableOutsideProgram))
|
|
412
414
|
return findings
|
|
413
415
|
},
|
|
414
416
|
}
|
|
415
417
|
|
|
418
|
+
/** The route table exists but no program loads it: every `to` and `search` stays loose, silently. */
|
|
419
|
+
function routeTableFinding(file: string): Diagnostic {
|
|
420
|
+
return legacyDiagnostic("typecheck", {
|
|
421
|
+
severity: "warning",
|
|
422
|
+
file,
|
|
423
|
+
message: `${file} is outside tsc's program, so navigate, redirect and Link accept any target and search: no route imports its ./+types module, and no tsconfig names the table`,
|
|
424
|
+
fix: `Add "${file}" to tsconfig.json "files" (a "files" entry is not filtered by "exclude"), or import a route's ./+types module from a file the program holds`,
|
|
425
|
+
suggestion: {
|
|
426
|
+
kind: "manual",
|
|
427
|
+
title: "Load the generated route table",
|
|
428
|
+
steps: [
|
|
429
|
+
`Add "${file}" to the "files" array in tsconfig.json, or in another tsconfig the typecheck runs.`,
|
|
430
|
+
'List it under "files", not "include": an "exclude" that names ".nifra" would drop it from "include".',
|
|
431
|
+
"Run `nifra check` again; links and redirects are now checked against the app's routes.",
|
|
432
|
+
],
|
|
433
|
+
},
|
|
434
|
+
})
|
|
435
|
+
}
|
|
436
|
+
|
|
416
437
|
/** One finding per zone folder: the fix (tsconfig `include`) is per folder, not per file. */
|
|
417
438
|
function outsideProgramFindings(files: readonly string[]): Diagnostic[] {
|
|
418
439
|
const byFolder = new Map<string, { readonly first: string; readonly group: string[] }>()
|
package/src/rules/pages.ts
CHANGED
|
@@ -48,6 +48,25 @@ export function readStaticApiPrefix(source: string): StaticApiPrefix {
|
|
|
48
48
|
|
|
49
49
|
const CONFIG_FILES = [FRAMEWORK_FILE, CONFIG_FILE] as const
|
|
50
50
|
|
|
51
|
+
type RouteManifest = ReturnType<typeof discoverRoutes>
|
|
52
|
+
|
|
53
|
+
/** The routes/ manifest; `undefined` without the tree or for one it refuses, which dev and build report themselves. */
|
|
54
|
+
function routeManifest(routesDir: string): RouteManifest | undefined {
|
|
55
|
+
if (!existsSync(routesDir)) return undefined
|
|
56
|
+
try {
|
|
57
|
+
return discoverRoutes(routesDir)
|
|
58
|
+
} catch {
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The manifest's script layouts, in code-unit order of their files. */
|
|
64
|
+
function scriptLayouts(manifest: RouteManifest): RouteManifest["layouts"][string][] {
|
|
65
|
+
return Object.values(manifest.layouts)
|
|
66
|
+
.filter((layout) => SCRIPT_LAYOUT.test(layout.file))
|
|
67
|
+
.sort((a, b) => codeUnitOrder(a.file, b.file))
|
|
68
|
+
}
|
|
69
|
+
|
|
51
70
|
export const shadowedPageRule: CheckRule = {
|
|
52
71
|
code: "NF-C027",
|
|
53
72
|
title: "Page route under the backend mount",
|
|
@@ -78,13 +97,8 @@ export const shadowedPageRule: CheckRule = {
|
|
|
78
97
|
}
|
|
79
98
|
if (apiPrefix === "") return []
|
|
80
99
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
manifest = discoverRoutes(routesDir)
|
|
84
|
-
} catch {
|
|
85
|
-
// A routes/ tree the manifest refuses fails dev and build with its own error.
|
|
86
|
-
return []
|
|
87
|
-
}
|
|
100
|
+
const manifest = routeManifest(routesDir)
|
|
101
|
+
if (manifest === undefined) return []
|
|
88
102
|
const findings: Diagnostic[] = []
|
|
89
103
|
for (const page of shadowedPages(manifest, [apiPrefix])) {
|
|
90
104
|
findings.push(
|
|
@@ -128,13 +142,8 @@ export const statusLayoutRule: CheckRule = {
|
|
|
128
142
|
title: "Root status page wraps the root layout itself",
|
|
129
143
|
async scan(ctx) {
|
|
130
144
|
const routesDir = join(ctx.root, "routes")
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
manifest = discoverRoutes(routesDir)
|
|
135
|
-
} catch {
|
|
136
|
-
return []
|
|
137
|
-
}
|
|
145
|
+
const manifest = routeManifest(routesDir)
|
|
146
|
+
if (manifest === undefined) return []
|
|
138
147
|
const layout = manifest.layouts._layout
|
|
139
148
|
if (layout === undefined) return []
|
|
140
149
|
const pages = [
|
|
@@ -240,6 +249,18 @@ function createWebAppCallee(
|
|
|
240
249
|
callee.name.text === "createWebApp"
|
|
241
250
|
}
|
|
242
251
|
|
|
252
|
+
/** The object literal a `createWebApp(...)` call takes as its options; `undefined` for any other node. */
|
|
253
|
+
function createWebAppOptions(
|
|
254
|
+
ts: typeof TSApi,
|
|
255
|
+
isCreateWebApp: (callee: TSApi.Expression) => boolean,
|
|
256
|
+
node: TSApi.Node,
|
|
257
|
+
): TSApi.ObjectLiteralExpression | undefined {
|
|
258
|
+
if (!ts.isCallExpression(node) || !isCreateWebApp(node.expression)) return undefined
|
|
259
|
+
const arg = node.arguments[0]
|
|
260
|
+
const options = arg === undefined ? undefined : unwrapExpression(ts, arg)
|
|
261
|
+
return options !== undefined && ts.isObjectLiteralExpression(options) ? options : undefined
|
|
262
|
+
}
|
|
263
|
+
|
|
243
264
|
const objectPropertyName = (
|
|
244
265
|
ts: typeof TSApi,
|
|
245
266
|
property: TSApi.ObjectLiteralElementLike,
|
|
@@ -318,15 +339,8 @@ export function entryApiMounts(
|
|
|
318
339
|
}
|
|
319
340
|
const mounts: EntryApiMount[] = []
|
|
320
341
|
const visit = (node: TSApi.Node): void => {
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
if (
|
|
324
|
-
ts.isCallExpression(node) &&
|
|
325
|
-
isCreateWebApp(node.expression) &&
|
|
326
|
-
options !== undefined &&
|
|
327
|
-
ts.isObjectLiteralExpression(options) &&
|
|
328
|
-
options.properties.some((property) => objectPropertyName(ts, property) === "api")
|
|
329
|
-
) {
|
|
342
|
+
const options = createWebAppOptions(ts, isCreateWebApp, node)
|
|
343
|
+
if (options?.properties.some((property) => objectPropertyName(ts, property) === "api")) {
|
|
330
344
|
// The last of an `apiPrefix` property and a spread decides, as it does at runtime.
|
|
331
345
|
let apiPrefix: EntryApiPrefix = { kind: "literal", value: "", unset: true }
|
|
332
346
|
let prefixNode: TSApi.Node | undefined
|
|
@@ -501,14 +515,8 @@ export function entryCopyChunksGaps(
|
|
|
501
515
|
manifest = statement.moduleSpecifier.text
|
|
502
516
|
const gaps: EntryCopyChunksGap[] = []
|
|
503
517
|
const visit = (node: TSApi.Node): void => {
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
if (
|
|
507
|
-
ts.isCallExpression(node) &&
|
|
508
|
-
isCreateWebApp(node.expression) &&
|
|
509
|
-
options !== undefined &&
|
|
510
|
-
ts.isObjectLiteralExpression(options)
|
|
511
|
-
) {
|
|
518
|
+
const options = createWebAppOptions(ts, isCreateWebApp, node)
|
|
519
|
+
if (options !== undefined) {
|
|
512
520
|
const names = options.properties.map((property) =>
|
|
513
521
|
ts.isSpreadAssignment(property) ? undefined : objectPropertyName(ts, property),
|
|
514
522
|
)
|
|
@@ -688,16 +696,9 @@ export const layoutMetaDataRule: CheckRule = {
|
|
|
688
696
|
title: "Layout meta reads data its layout never loads",
|
|
689
697
|
async scan(ctx) {
|
|
690
698
|
const routesDir = join(ctx.root, "routes")
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
manifest = discoverRoutes(routesDir)
|
|
695
|
-
} catch {
|
|
696
|
-
return []
|
|
697
|
-
}
|
|
698
|
-
const layouts = Object.values(manifest.layouts)
|
|
699
|
-
.filter((layout) => SCRIPT_LAYOUT.test(layout.file))
|
|
700
|
-
.sort((a, b) => codeUnitOrder(a.file, b.file))
|
|
699
|
+
const manifest = routeManifest(routesDir)
|
|
700
|
+
if (manifest === undefined) return []
|
|
701
|
+
const layouts = scriptLayouts(manifest)
|
|
701
702
|
if (layouts.length === 0) return []
|
|
702
703
|
const loaded = await loadRuleTypeScript(ctx)
|
|
703
704
|
const ts = loaded.compiler
|
|
@@ -784,16 +785,9 @@ export const ungatedLayoutRefusalRule: CheckRule = {
|
|
|
784
785
|
title: "Layout loader refuses access without a gate",
|
|
785
786
|
async scan(ctx) {
|
|
786
787
|
const routesDir = join(ctx.root, "routes")
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
manifest = discoverRoutes(routesDir)
|
|
791
|
-
} catch {
|
|
792
|
-
return []
|
|
793
|
-
}
|
|
794
|
-
const layouts = Object.values(manifest.layouts)
|
|
795
|
-
.filter((layout) => SCRIPT_LAYOUT.test(layout.file))
|
|
796
|
-
.sort((a, b) => codeUnitOrder(a.file, b.file))
|
|
788
|
+
const manifest = routeManifest(routesDir)
|
|
789
|
+
if (manifest === undefined) return []
|
|
790
|
+
const layouts = scriptLayouts(manifest)
|
|
797
791
|
if (layouts.length === 0) return []
|
|
798
792
|
const sources = layouts.map((layout) =>
|
|
799
793
|
[layout.file, ...(layout.backend === undefined ? [] : [layout.backend])].map((file) => ({
|
|
@@ -890,13 +884,8 @@ export const frontendServerExportRule: CheckRule = {
|
|
|
890
884
|
title: "Server-only export in a route frontend file",
|
|
891
885
|
async scan(ctx) {
|
|
892
886
|
const routesDir = join(ctx.root, "routes")
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
try {
|
|
896
|
-
manifest = discoverRoutes(routesDir)
|
|
897
|
-
} catch {
|
|
898
|
-
return []
|
|
899
|
-
}
|
|
887
|
+
const manifest = routeManifest(routesDir)
|
|
888
|
+
if (manifest === undefined) return []
|
|
900
889
|
const files = new Set<string>()
|
|
901
890
|
for (const entry of [
|
|
902
891
|
...manifest.routes,
|