@nifrajs/cli 5.9.0 → 5.11.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 +85 -0
- 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 +1 -0
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +8 -1
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +7 -2
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +21 -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 +2 -21
- package/dist/command-catalog.js.map +1 -1
- package/dist/doctor.js +2 -2
- package/dist/doctor.js.map +1 -1
- 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 +127 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/load.d.ts +4 -2
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +6 -1
- package/dist/load.js.map +1 -1
- package/dist/mcp-delegate.d.ts +17 -3
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +24 -9
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-http.js.map +1 -1
- package/dist/mcp-relay.d.ts +37 -0
- package/dist/mcp-relay.d.ts.map +1 -0
- package/dist/mcp-relay.js +129 -0
- package/dist/mcp-relay.js.map +1 -0
- 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.d.ts.map +1 -1
- package/dist/mcp.js +55 -1
- package/dist/mcp.js.map +1 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +45 -33
- package/dist/migrate-layout.js.map +1 -1
- package/dist/recipes/5.10.0.d.ts +13 -0
- package/dist/recipes/5.10.0.d.ts.map +1 -0
- package/dist/recipes/5.10.0.js +45 -0
- package/dist/recipes/5.10.0.js.map +1 -0
- package/dist/recipes/5.11.0.d.ts +10 -0
- package/dist/recipes/5.11.0.d.ts.map +1 -0
- package/dist/recipes/5.11.0.js +50 -0
- package/dist/recipes/5.11.0.js.map +1 -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 +3 -0
- 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/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/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/source-rewrites.d.ts +0 -5
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +350 -10
- package/dist/source-rewrites.js.map +1 -1
- 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 +19 -1
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +55 -2
- package/dist/upgrade.js.map +1 -1
- package/docs/examples.json +21 -9
- package/docs/llms-full.txt +800 -262
- package/docs/types.json +868 -128
- package/package.json +11 -11
- package/src/check-diagnostics.ts +5 -0
- package/src/check-scan.ts +9 -1
- package/src/check.ts +15 -3
- package/src/cli.ts +25 -20
- package/src/command-catalog.ts +2 -26
- package/src/doctor.ts +2 -2
- package/src/internal/source-facts.ts +155 -0
- package/src/load.ts +15 -3
- package/src/mcp-delegate.ts +37 -11
- package/src/mcp-http.ts +1 -1
- package/src/mcp-relay.ts +155 -0
- package/src/mcp-tool-meta.ts +0 -1
- package/src/mcp.ts +51 -1
- package/src/migrate-layout.ts +61 -29
- package/src/recipes/5.10.0.ts +46 -0
- package/src/recipes/5.11.0.ts +51 -0
- package/src/recipes/index.ts +4 -0
- package/src/review.ts +3 -0
- package/src/rules/codes.ts +3 -0
- 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/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/source-rewrites.ts +383 -12
- package/src/typecheck-coverage.ts +23 -0
- package/src/upgrade.ts +71 -3
- 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
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createSourceFacts } from "../internal/source-facts.ts"
|
|
3
|
-
import { commentBlockExpectation } from "./comment-markers.ts"
|
|
1
|
+
import { diagnostic } from "../diagnostics.ts"
|
|
4
2
|
import type { CheckRule } from "./index.ts"
|
|
5
|
-
import {
|
|
3
|
+
import { scanSources } from "./source-scan.ts"
|
|
6
4
|
|
|
7
|
-
const TEXT_SLOT_BOOLEAN_EXPECTATION = ["NF-C055", "text-slot-boolean"]
|
|
5
|
+
const TEXT_SLOT_BOOLEAN_EXPECTATION = ["NF-C055", "text-slot-boolean"] as const
|
|
8
6
|
const ZOD_IMPORT = /from\s+["']zod(?:\/v4)?(?:\/mini)?["']/
|
|
9
7
|
const VALUE_IN = { query: "query", headers: "header", cookies: "cookie" } as const
|
|
10
8
|
|
|
@@ -16,41 +14,27 @@ const VALUE_IN = { query: "query", headers: "header", cookies: "cookie" } as con
|
|
|
16
14
|
export const textSlotBooleanRule: CheckRule = {
|
|
17
15
|
code: "NF-C055",
|
|
18
16
|
title: "zod coerce.boolean() reads request text",
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
},
|
|
17
|
+
scan: (ctx) =>
|
|
18
|
+
scanSources(ctx, {
|
|
19
|
+
expectation: TEXT_SLOT_BOOLEAN_EXPECTATION,
|
|
20
|
+
mayMatch: (text) => text.includes("coerce") && ZOD_IMPORT.test(text),
|
|
21
|
+
find: (facts, tree) =>
|
|
22
|
+
facts.textSlotBooleanCoercions(tree).map((found) => ({
|
|
23
|
+
position: found.position,
|
|
24
|
+
report: (line) =>
|
|
25
|
+
diagnostic({
|
|
26
|
+
code: "NF-C055",
|
|
27
|
+
severity: "warn",
|
|
28
|
+
file: tree.fileName,
|
|
29
|
+
line,
|
|
30
|
+
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.`,
|
|
31
|
+
evidence: [
|
|
32
|
+
"zod's coerce.boolean() is Boolean(value): any non-empty string is true",
|
|
33
|
+
`${found.slot} values arrive as text, so "false" never reaches the schema as false`,
|
|
34
|
+
"a use kept on purpose: `// nifra-expect NF-C055: <reason>` above the call",
|
|
35
|
+
],
|
|
36
|
+
verify: "nifra check",
|
|
37
|
+
}),
|
|
38
|
+
})),
|
|
39
|
+
}),
|
|
56
40
|
}
|
package/src/source-rewrites.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* syntax tree with the project's own TypeScript. A rewrite changes only a shape it recognizes whole
|
|
4
4
|
* and lists every other site for the user to edit by hand; it never guesses.
|
|
5
5
|
*/
|
|
6
|
+
import { isSameOriginPath } from "@nifrajs/core/pattern"
|
|
6
7
|
import type * as TS from "typescript"
|
|
7
8
|
import { codeUnitOrder } from "./internal/code-unit-order.ts"
|
|
8
9
|
import { scriptKindOf } from "./internal/script-kind.ts"
|
|
@@ -48,7 +49,7 @@ const WEB = "@nifrajs/web"
|
|
|
48
49
|
const NODE = "@nifrajs/node"
|
|
49
50
|
const DENO = "@nifrajs/deno"
|
|
50
51
|
const CORE_STATIC = "@nifrajs/core/serve-static"
|
|
51
|
-
const DAY =
|
|
52
|
+
const DAY = "public, max-age=86400"
|
|
52
53
|
const YEAR = "public, max-age=31536000, immutable"
|
|
53
54
|
const PUBLIC_DIR_KEYS = new Set(["dir", "hashedPrefix", "hashedFiles", "cache", "files"])
|
|
54
55
|
const PUBLIC_DIR_FORM =
|
|
@@ -73,13 +74,46 @@ function propertyName(ts: typeof TS, name: TS.PropertyName): string | undefined
|
|
|
73
74
|
* `serveStatic()`; and `signals` on `@nifrajs/node`'s and `@nifrajs/deno`'s `serve()` becomes
|
|
74
75
|
* `gracefulSignals`.
|
|
75
76
|
*/
|
|
76
|
-
|
|
77
|
+
/** Parse `text` as `file`, with the line lookup a rewrite lists its manual sites by. */
|
|
78
|
+
function parseSource(ts: typeof TS, file: string, text: string) {
|
|
77
79
|
const tree = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file))
|
|
80
|
+
const lineOf = (node: TS.Node): number =>
|
|
81
|
+
tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
|
|
82
|
+
return { tree, lineOf }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Named-import order: by the imported name, a `type` modifier aside. */
|
|
86
|
+
const specifierOrder = (a: string, b: string): number =>
|
|
87
|
+
codeUnitOrder(a.replace(/^type /, ""), b.replace(/^type /, ""))
|
|
88
|
+
|
|
89
|
+
/** The quote most of the file's string literals use, apart from module specifiers and JSX attributes
|
|
90
|
+
* (double-quoted in files whose code is not); `fallback` on a tie. */
|
|
91
|
+
function quoteOf(ts: typeof TS, tree: TS.SourceFile, fallback: "'" | '"'): "'" | '"' {
|
|
92
|
+
let single = 0
|
|
93
|
+
let double = 0
|
|
94
|
+
const visit = (node: TS.Node): void => {
|
|
95
|
+
if (
|
|
96
|
+
ts.isStringLiteral(node) &&
|
|
97
|
+
!ts.isJsxAttribute(node.parent) &&
|
|
98
|
+
!ts.isImportDeclaration(node.parent) &&
|
|
99
|
+
!ts.isExportDeclaration(node.parent)
|
|
100
|
+
) {
|
|
101
|
+
if (node.getText(tree).startsWith("'")) single++
|
|
102
|
+
else double++
|
|
103
|
+
}
|
|
104
|
+
ts.forEachChild(node, visit)
|
|
105
|
+
}
|
|
106
|
+
visit(tree)
|
|
107
|
+
return single === double ? fallback : single > double ? "'" : '"'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteResult {
|
|
111
|
+
const { tree, lineOf } = parseSource(ts, file, text)
|
|
112
|
+
const importQuote = /from\s*'/.test(text) ? "'" : '"'
|
|
113
|
+
const quote = quoteOf(ts, tree, importQuote)
|
|
78
114
|
const edits: Edit[] = []
|
|
79
115
|
const manual: ManualSite[] = []
|
|
80
116
|
let count = 0
|
|
81
|
-
const lineOf = (node: TS.Node): number =>
|
|
82
|
-
tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
|
|
83
117
|
const source = (node: TS.Node): string => node.getText(tree)
|
|
84
118
|
const imports = new Map<string, ModuleImport>()
|
|
85
119
|
// Every import of @nifrajs/web, with the specifiers of names 5.9 moved out of it.
|
|
@@ -238,12 +272,12 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
|
|
|
238
272
|
}
|
|
239
273
|
if (cacheControl === undefined) {
|
|
240
274
|
if (coreDay) dayUsed = true
|
|
241
|
-
cacheControl = coreDay ? "ONE_DAY" : DAY
|
|
275
|
+
cacheControl = coreDay ? "ONE_DAY" : `${quote}${DAY}${quote}`
|
|
242
276
|
}
|
|
243
277
|
const prefixValue = values.get("hashedPrefix")
|
|
244
278
|
// A shorthand `hashedPrefix` is a binding of that name.
|
|
245
279
|
const hashedPrefix = !values.has("hashedPrefix")
|
|
246
|
-
?
|
|
280
|
+
? `${quote}/assets/${quote}`
|
|
247
281
|
: prefixValue === undefined
|
|
248
282
|
? "hashedPrefix"
|
|
249
283
|
: source(prefixValue)
|
|
@@ -252,7 +286,7 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
|
|
|
252
286
|
: `hashedFiles: (path) => path.startsWith(${hashedPrefix})`
|
|
253
287
|
const fields = [
|
|
254
288
|
field("dir"),
|
|
255
|
-
|
|
289
|
+
`prefix: ${quote}/${quote}`,
|
|
256
290
|
// A miss is the app's: servePublicDir answered `undefined` for every path it could not serve.
|
|
257
291
|
"fallthrough: true",
|
|
258
292
|
hashed,
|
|
@@ -396,9 +430,6 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
|
|
|
396
430
|
: `type HashedFiles as ${hashedFilesLocal}`,
|
|
397
431
|
)
|
|
398
432
|
}
|
|
399
|
-
const quote = /from\s*'/.test(text) ? "'" : '"'
|
|
400
|
-
const specifierOrder = (a: string, b: string): number =>
|
|
401
|
-
codeUnitOrder(a.replace(/^type /, ""), b.replace(/^type /, ""))
|
|
402
433
|
const target = imports.get(runtimeModule)
|
|
403
434
|
let added: string | undefined
|
|
404
435
|
if (names.length > 0) {
|
|
@@ -410,7 +441,7 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
|
|
|
410
441
|
text: `{ ${all.join(", ")} }`,
|
|
411
442
|
})
|
|
412
443
|
} else {
|
|
413
|
-
added = `import { ${names.toSorted(specifierOrder).join(", ")} } from ${
|
|
444
|
+
added = `import { ${names.toSorted(specifierOrder).join(", ")} } from ${importQuote}${runtimeModule}${importQuote}`
|
|
414
445
|
}
|
|
415
446
|
}
|
|
416
447
|
// The new import goes where the first web import that loses a name was, or after the imports.
|
|
@@ -446,6 +477,342 @@ function rewriteServeStatic(ts: typeof TS, file: string, text: string): RewriteR
|
|
|
446
477
|
return { text: applyEdits(text, edits), count, manual }
|
|
447
478
|
}
|
|
448
479
|
|
|
480
|
+
const WEB_APP = "createWebApp"
|
|
481
|
+
|
|
482
|
+
/** `createWebApp({ experimental: { ppr } })` becomes `createWebApp({ ppr })`: 5.10 moved the option. */
|
|
483
|
+
function rewritePprOption(ts: typeof TS, file: string, text: string): RewriteResult {
|
|
484
|
+
const { tree, lineOf } = parseSource(ts, file, text)
|
|
485
|
+
const edits: Edit[] = []
|
|
486
|
+
const manual: ManualSite[] = []
|
|
487
|
+
let count = 0
|
|
488
|
+
// The names createWebApp is imported under, and the names @nifrajs/web is imported as a namespace.
|
|
489
|
+
const locals = new Set<string>()
|
|
490
|
+
const namespaces = new Set<string>()
|
|
491
|
+
for (const statement of tree.statements) {
|
|
492
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
493
|
+
continue
|
|
494
|
+
if (statement.moduleSpecifier.text !== WEB) continue
|
|
495
|
+
const bindings = statement.importClause?.namedBindings
|
|
496
|
+
if (bindings === undefined) continue
|
|
497
|
+
if (ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
|
|
498
|
+
else {
|
|
499
|
+
for (const element of bindings.elements) {
|
|
500
|
+
if ((element.propertyName ?? element.name).text === WEB_APP) locals.add(element.name.text)
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const isCreateWebApp = (callee: TS.Expression): boolean =>
|
|
505
|
+
(ts.isIdentifier(callee) && locals.has(callee.text)) ||
|
|
506
|
+
(ts.isPropertyAccessExpression(callee) &&
|
|
507
|
+
ts.isIdentifier(callee.expression) &&
|
|
508
|
+
namespaces.has(callee.expression.text) &&
|
|
509
|
+
callee.name.text === WEB_APP)
|
|
510
|
+
const nameOf = (property: TS.ObjectLiteralElementLike): string | undefined =>
|
|
511
|
+
ts.isShorthandPropertyAssignment(property)
|
|
512
|
+
? property.name.text
|
|
513
|
+
: ts.isPropertyAssignment(property)
|
|
514
|
+
? propertyName(ts, property.name)
|
|
515
|
+
: undefined
|
|
516
|
+
const property = (
|
|
517
|
+
object: TS.ObjectLiteralExpression,
|
|
518
|
+
name: string,
|
|
519
|
+
): TS.ObjectLiteralElementLike | undefined => object.properties.find((p) => nameOf(p) === name)
|
|
520
|
+
const handled = new Set<TS.ObjectLiteralExpression>()
|
|
521
|
+
|
|
522
|
+
const move = (options: TS.ObjectLiteralExpression): void => {
|
|
523
|
+
const experimental = property(options, "experimental")
|
|
524
|
+
if (experimental === undefined) return
|
|
525
|
+
const spread = options.properties.some((p) => ts.isSpreadAssignment(p))
|
|
526
|
+
const inner = ts.isPropertyAssignment(experimental) ? experimental.initializer : undefined
|
|
527
|
+
const moved = inner !== undefined && ts.isObjectLiteralExpression(inner) ? inner : undefined
|
|
528
|
+
const only = moved?.properties.length === 1 ? moved.properties[0] : undefined
|
|
529
|
+
if (
|
|
530
|
+
moved === undefined ||
|
|
531
|
+
only === undefined ||
|
|
532
|
+
nameOf(only) !== "ppr" ||
|
|
533
|
+
property(options, "ppr") !== undefined ||
|
|
534
|
+
spread
|
|
535
|
+
) {
|
|
536
|
+
manual.push({
|
|
537
|
+
line: lineOf(experimental),
|
|
538
|
+
reason:
|
|
539
|
+
"experimental.ppr is now ppr: pass it beside the other createWebApp options and remove `experimental`",
|
|
540
|
+
})
|
|
541
|
+
return
|
|
542
|
+
}
|
|
543
|
+
edits.push({
|
|
544
|
+
start: experimental.getStart(tree),
|
|
545
|
+
end: experimental.end,
|
|
546
|
+
text: ts.isPropertyAssignment(only) ? `ppr: ${only.initializer.getText(tree)}` : "ppr",
|
|
547
|
+
})
|
|
548
|
+
count++
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const visit = (node: TS.Node): void => {
|
|
552
|
+
if (ts.isCallExpression(node) && isCreateWebApp(node.expression)) {
|
|
553
|
+
const options = node.arguments[0]
|
|
554
|
+
if (options !== undefined && ts.isObjectLiteralExpression(options)) {
|
|
555
|
+
handled.add(options)
|
|
556
|
+
move(options)
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
ts.forEachChild(node, visit)
|
|
560
|
+
}
|
|
561
|
+
visit(tree)
|
|
562
|
+
|
|
563
|
+
// Options built apart from the call, or passed through a name, are the user's to move.
|
|
564
|
+
const sweep = (node: TS.Node): void => {
|
|
565
|
+
if (ts.isObjectLiteralExpression(node) && !handled.has(node)) {
|
|
566
|
+
const experimental = property(node, "experimental")
|
|
567
|
+
const inner =
|
|
568
|
+
experimental !== undefined && ts.isPropertyAssignment(experimental)
|
|
569
|
+
? experimental.initializer
|
|
570
|
+
: undefined
|
|
571
|
+
if (
|
|
572
|
+
inner !== undefined &&
|
|
573
|
+
ts.isObjectLiteralExpression(inner) &&
|
|
574
|
+
property(inner, "ppr") !== undefined
|
|
575
|
+
) {
|
|
576
|
+
manual.push({
|
|
577
|
+
line: lineOf(node),
|
|
578
|
+
reason:
|
|
579
|
+
"experimental.ppr is now ppr: pass it beside the other createWebApp options and remove `experimental`",
|
|
580
|
+
})
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
ts.forEachChild(node, sweep)
|
|
584
|
+
}
|
|
585
|
+
sweep(tree)
|
|
586
|
+
return {
|
|
587
|
+
text: applyEdits(text, edits),
|
|
588
|
+
count,
|
|
589
|
+
manual: manual.toSorted((a, b) => a.line - b.line),
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const HREF_RAW = "rawHref"
|
|
594
|
+
// Where `redirect`, `useNavigate` and React's `Link` come from; each also exports `rawHref`.
|
|
595
|
+
const HREF_PROVIDER = /^@nifrajs\/web(?:-(?:react|preact|vue|svelte|solid)\/router)?$/
|
|
596
|
+
const LINK_PROVIDER = "@nifrajs/web-react/router"
|
|
597
|
+
const LINK_TAGS = ["Link", "NavLink", "Navigate"]
|
|
598
|
+
const STRING_BUILT =
|
|
599
|
+
"the destination is built from a string: build it with href({ to, params, search }), or wrap a path built elsewhere in rawHref(path)"
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* 5.10: `navigate()`, `redirect()` and React's `Link`, `NavLink` and `Navigate` take an `Href`. A
|
|
603
|
+
* string path written whole at one of those sites becomes `rawHref(path)`, which sends the same
|
|
604
|
+
* request the string did. A destination built from a template or a concatenation, an object passed to
|
|
605
|
+
* `navigate`, and a path `rawHref` refuses are listed, since each needs a route and its params.
|
|
606
|
+
*/
|
|
607
|
+
function rewriteHrefNavigation(ts: typeof TS, file: string, text: string): RewriteResult {
|
|
608
|
+
const { tree } = parseSource(ts, file, text)
|
|
609
|
+
const source = (node: TS.Node): string => node.getText(tree)
|
|
610
|
+
const bound = new Map<
|
|
611
|
+
string,
|
|
612
|
+
{ readonly exported: string; readonly from: string; readonly named: TS.NamedImports }
|
|
613
|
+
>()
|
|
614
|
+
for (const statement of tree.statements) {
|
|
615
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
616
|
+
continue
|
|
617
|
+
const from = statement.moduleSpecifier.text
|
|
618
|
+
const clause = statement.importClause
|
|
619
|
+
const bindings = clause?.namedBindings
|
|
620
|
+
if (!HREF_PROVIDER.test(from) || clause?.isTypeOnly === true) continue
|
|
621
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue
|
|
622
|
+
for (const element of bindings.elements) {
|
|
623
|
+
if (element.isTypeOnly) continue
|
|
624
|
+
bound.set(element.name.text, {
|
|
625
|
+
exported: (element.propertyName ?? element.name).text,
|
|
626
|
+
from,
|
|
627
|
+
named: bindings,
|
|
628
|
+
})
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
const localsOf = (exported: string | readonly string[], from?: string): Set<string> =>
|
|
632
|
+
new Set(
|
|
633
|
+
[...bound]
|
|
634
|
+
.filter(
|
|
635
|
+
([, entry]) =>
|
|
636
|
+
(typeof exported === "string"
|
|
637
|
+
? entry.exported === exported
|
|
638
|
+
: exported.includes(entry.exported)) &&
|
|
639
|
+
(from === undefined || entry.from === from),
|
|
640
|
+
)
|
|
641
|
+
.map(([local]) => local),
|
|
642
|
+
)
|
|
643
|
+
const redirects = localsOf("redirect", "@nifrajs/web")
|
|
644
|
+
const useNavigates = localsOf("useNavigate")
|
|
645
|
+
const tags = localsOf(LINK_TAGS, LINK_PROVIDER)
|
|
646
|
+
if (redirects.size + useNavigates.size + tags.size === 0) return { text, count: 0, manual: [] }
|
|
647
|
+
const rawLocal = [...bound].find(([, entry]) => entry.exported === HREF_RAW)?.[0]
|
|
648
|
+
const importQuote = /from\s*'/.test(text) ? "'" : '"'
|
|
649
|
+
const quote = quoteOf(ts, tree, importQuote)
|
|
650
|
+
|
|
651
|
+
const navigates = new Set<string>()
|
|
652
|
+
const collect = (node: TS.Node): void => {
|
|
653
|
+
if (
|
|
654
|
+
ts.isVariableDeclaration(node) &&
|
|
655
|
+
ts.isIdentifier(node.name) &&
|
|
656
|
+
node.initializer !== undefined &&
|
|
657
|
+
ts.isCallExpression(node.initializer) &&
|
|
658
|
+
ts.isIdentifier(node.initializer.expression) &&
|
|
659
|
+
useNavigates.has(node.initializer.expression.text)
|
|
660
|
+
) {
|
|
661
|
+
navigates.add(node.name.text)
|
|
662
|
+
}
|
|
663
|
+
ts.forEachChild(node, collect)
|
|
664
|
+
}
|
|
665
|
+
collect(tree)
|
|
666
|
+
|
|
667
|
+
const edits: Edit[] = []
|
|
668
|
+
const manualAt: { readonly pos: number; readonly reason: string }[] = []
|
|
669
|
+
// The import of the first name a rewritten site uses; `rawHref` is added to it.
|
|
670
|
+
let host: TS.NamedImports | undefined
|
|
671
|
+
let count = 0
|
|
672
|
+
const raw = rawLocal ?? HREF_RAW
|
|
673
|
+
// A `rawHref` of the file's own would be called in place of the export.
|
|
674
|
+
const taken = rawLocal === undefined && /\brawHref\b/.test(text)
|
|
675
|
+
|
|
676
|
+
const isStringy = (node: TS.Expression): boolean =>
|
|
677
|
+
ts.isStringLiteral(node) ||
|
|
678
|
+
ts.isNoSubstitutionTemplateLiteral(node) ||
|
|
679
|
+
ts.isTemplateExpression(node) ||
|
|
680
|
+
(ts.isBinaryExpression(node) &&
|
|
681
|
+
node.operatorToken.kind === ts.SyntaxKind.PlusToken &&
|
|
682
|
+
(isStringy(node.left) || isStringy(node.right)))
|
|
683
|
+
const list = (node: TS.Node, reason: string): void => {
|
|
684
|
+
manualAt.push({ pos: node.getStart(tree), reason })
|
|
685
|
+
}
|
|
686
|
+
// Rewrites one destination; `owner` names the import that brought the API in, and `attribute` is a
|
|
687
|
+
// JSX attribute's own string, which gains the braces of an expression.
|
|
688
|
+
const destination = (node: TS.Expression, owner: string, attribute = false): void => {
|
|
689
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
690
|
+
if (!isSameOriginPath(node.text)) {
|
|
691
|
+
list(
|
|
692
|
+
node,
|
|
693
|
+
`${JSON.stringify(node.text)} is not a same-origin path, which an Href must be: another site is a plain <a href>, or redirect(url, { external: true })`,
|
|
694
|
+
)
|
|
695
|
+
} else if (taken) {
|
|
696
|
+
list(node, `${HREF_RAW} is already a name in this file: import it under another name`)
|
|
697
|
+
} else {
|
|
698
|
+
const written =
|
|
699
|
+
attribute && ts.isStringLiteral(node) ? jsxAttributeValue(node) : source(node)
|
|
700
|
+
if (written === undefined) {
|
|
701
|
+
list(node, "wrap the path in rawHref(path), written as a JavaScript string")
|
|
702
|
+
return
|
|
703
|
+
}
|
|
704
|
+
edits.push({
|
|
705
|
+
start: node.getStart(tree),
|
|
706
|
+
end: node.end,
|
|
707
|
+
text: attribute ? `{${raw}(${written})}` : `${raw}(${written})`,
|
|
708
|
+
})
|
|
709
|
+
host ??= bound.get(owner)?.named
|
|
710
|
+
count++
|
|
711
|
+
}
|
|
712
|
+
} else if (isStringy(node)) list(node, STRING_BUILT)
|
|
713
|
+
}
|
|
714
|
+
// A JSX attribute string is not JavaScript: entities and escapes read differently.
|
|
715
|
+
const jsxAttributeValue = (node: TS.StringLiteral): string | undefined => {
|
|
716
|
+
const value = node.text
|
|
717
|
+
if (/[\\&\n]/.test(value) || value.includes(quote)) return undefined
|
|
718
|
+
return `${quote}${value}${quote}`
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const visit = (node: TS.Node): void => {
|
|
722
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
723
|
+
const callee = node.expression.text
|
|
724
|
+
const first = node.arguments[0]
|
|
725
|
+
const options = node.arguments[1]
|
|
726
|
+
const isRedirect = redirects.has(callee)
|
|
727
|
+
if (first !== undefined && (isRedirect || navigates.has(callee))) {
|
|
728
|
+
const external =
|
|
729
|
+
isRedirect &&
|
|
730
|
+
options !== undefined &&
|
|
731
|
+
ts.isObjectLiteralExpression(options) &&
|
|
732
|
+
options.properties.some(
|
|
733
|
+
(p) => p.name !== undefined && propertyName(ts, p.name) === "external",
|
|
734
|
+
)
|
|
735
|
+
if (!external) {
|
|
736
|
+
if (ts.isObjectLiteralExpression(first)) {
|
|
737
|
+
list(
|
|
738
|
+
first,
|
|
739
|
+
"navigate({ to, search, replace }) is navigate(href({ to, search }), { replace }): the route and its params are the app's to name",
|
|
740
|
+
)
|
|
741
|
+
} else {
|
|
742
|
+
const owner = isRedirect
|
|
743
|
+
? callee
|
|
744
|
+
: ([...bound].find(([, entry]) => entry.exported === "useNavigate")?.[0] ?? callee)
|
|
745
|
+
destination(first, owner)
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
} else if (
|
|
750
|
+
(ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) &&
|
|
751
|
+
ts.isIdentifier(node.tagName) &&
|
|
752
|
+
tags.has(node.tagName.text)
|
|
753
|
+
) {
|
|
754
|
+
for (const attribute of node.attributes.properties) {
|
|
755
|
+
if (!ts.isJsxAttribute(attribute) || attribute.name.getText(tree) !== "to") continue
|
|
756
|
+
const value = attribute.initializer
|
|
757
|
+
if (value === undefined) continue
|
|
758
|
+
if (ts.isStringLiteral(value)) destination(value, node.tagName.text, true)
|
|
759
|
+
else if (ts.isJsxExpression(value) && value.expression !== undefined)
|
|
760
|
+
destination(value.expression, node.tagName.text)
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
ts.forEachChild(node, visit)
|
|
764
|
+
}
|
|
765
|
+
visit(tree)
|
|
766
|
+
|
|
767
|
+
if (host !== undefined && rawLocal === undefined) {
|
|
768
|
+
const all = [...host.elements.map(source), HREF_RAW].toSorted(specifierOrder)
|
|
769
|
+
const multiline = source(host).includes("\n")
|
|
770
|
+
const first = host.elements[0]
|
|
771
|
+
const indent =
|
|
772
|
+
first === undefined
|
|
773
|
+
? " "
|
|
774
|
+
: text.slice(text.lastIndexOf("\n", first.getStart(tree)) + 1, first.getStart(tree))
|
|
775
|
+
const close = text.slice(text.lastIndexOf("\n", host.end - 1) + 1, host.end - 1)
|
|
776
|
+
edits.push({
|
|
777
|
+
start: host.getStart(tree),
|
|
778
|
+
end: host.end,
|
|
779
|
+
text: multiline
|
|
780
|
+
? `{\n${all.map((name) => `${indent}${name},`).join("\n")}\n${close}}`
|
|
781
|
+
: `{ ${all.join(", ")} }`,
|
|
782
|
+
})
|
|
783
|
+
}
|
|
784
|
+
const next = applyEdits(text, edits)
|
|
785
|
+
// The line a site has in the rewritten file, which is the one a reader opens.
|
|
786
|
+
const lineAfter = (pos: number): number => {
|
|
787
|
+
let at = pos
|
|
788
|
+
for (const edit of edits) if (edit.end <= pos) at += edit.text.length - (edit.end - edit.start)
|
|
789
|
+
return next.slice(0, at).split("\n").length
|
|
790
|
+
}
|
|
791
|
+
return {
|
|
792
|
+
text: next,
|
|
793
|
+
count,
|
|
794
|
+
manual: manualAt
|
|
795
|
+
.map(({ pos, reason }) => ({ line: lineAfter(pos), reason }))
|
|
796
|
+
.toSorted((a, b) => a.line - b.line),
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
const HREF_NAVIGATION: SourceRewrite = {
|
|
801
|
+
id: "href-navigation",
|
|
802
|
+
description:
|
|
803
|
+
"string paths given to navigate(), redirect() and React's Link, NavLink and Navigate → rawHref(path)",
|
|
804
|
+
mentions: (text) =>
|
|
805
|
+
/@nifrajs\/web/.test(text) && /\b(?:redirect|useNavigate|Link|NavLink|Navigate)\b/.test(text),
|
|
806
|
+
rewrite: rewriteHrefNavigation,
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const PPR_OPTION: SourceRewrite = {
|
|
810
|
+
id: "ppr-option",
|
|
811
|
+
description: "createWebApp({ experimental: { ppr } }) → createWebApp({ ppr })",
|
|
812
|
+
mentions: (text) => /\bexperimental\b/.test(text) && /\bcreateWebApp\b/.test(text),
|
|
813
|
+
rewrite: rewritePprOption,
|
|
814
|
+
}
|
|
815
|
+
|
|
449
816
|
const SERVE_STATIC: SourceRewrite = {
|
|
450
817
|
id: "serve-static",
|
|
451
818
|
description:
|
|
@@ -456,7 +823,11 @@ const SERVE_STATIC: SourceRewrite = {
|
|
|
456
823
|
rewrite: rewriteServeStatic,
|
|
457
824
|
}
|
|
458
825
|
|
|
459
|
-
const REWRITES: Readonly<Record<string, SourceRewrite>> = {
|
|
826
|
+
const REWRITES: Readonly<Record<string, SourceRewrite>> = {
|
|
827
|
+
[SERVE_STATIC.id]: SERVE_STATIC,
|
|
828
|
+
[PPR_OPTION.id]: PPR_OPTION,
|
|
829
|
+
[HREF_NAVIGATION.id]: HREF_NAVIGATION,
|
|
830
|
+
}
|
|
460
831
|
|
|
461
832
|
export function getSourceRewrite(id: string): SourceRewrite | undefined {
|
|
462
833
|
return REWRITES[id]
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { existsSync, readFileSync, realpathSync } from "node:fs"
|
|
8
8
|
import { dirname, isAbsolute, join, relative, resolve } from "node:path"
|
|
9
|
+
import { ROUTE_TABLE_FILE } from "@nifrajs/web/route-types"
|
|
9
10
|
import { stripComments } from "./check-scan.ts"
|
|
10
11
|
|
|
11
12
|
const ZONE_FOLDER = new Set(["routes", "frontend", "backend", "shared"])
|
|
@@ -140,6 +141,28 @@ const excludedBy = (patterns: readonly string[], file: string): boolean =>
|
|
|
140
141
|
(pattern) => new Bun.Glob(pattern).match(file) || new Bun.Glob(`${pattern}/**`).match(file),
|
|
141
142
|
)
|
|
142
143
|
|
|
144
|
+
// A named import of a navigation API from `@nifrajs/web` or an adapter: what the route table types.
|
|
145
|
+
const NAVIGATION_IMPORT =
|
|
146
|
+
/\bimport\s+(?:type\s+)?\{[^}]*\b(?:useNavigate|Link|NavLink|Navigate|redirect|href|rawHref)\b[^}]*\}\s*from\s*["']@nifrajs\/web(?:-[a-z]+)?(?:\/(?:router|client))?["']/
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* `.nifra/types/routes.d.ts` (project-relative) when a source file uses a navigation API, the table
|
|
150
|
+
* exists, and no program holds it. A program that imports any route's `./+types` module loads the table
|
|
151
|
+
* through that module's reference; one that does not, and does not name it, never sees it. Its `href`
|
|
152
|
+
* calls have no route keys to check against. An app that uses none of these APIs is left alone.
|
|
153
|
+
*/
|
|
154
|
+
export function routeTableOutsideProgram(
|
|
155
|
+
cwd: string,
|
|
156
|
+
programFiles: readonly string[],
|
|
157
|
+
sources: ReadonlyArray<{ readonly content: string }>,
|
|
158
|
+
): string | undefined {
|
|
159
|
+
const table = join(cwd, ROUTE_TABLE_FILE)
|
|
160
|
+
if (!existsSync(table) || !sources.some(({ content }) => NAVIGATION_IMPORT.test(content)))
|
|
161
|
+
return undefined
|
|
162
|
+
const wanted = new Set([canonical(table), canonical(real(table))])
|
|
163
|
+
return programFiles.some((file) => wanted.has(canonical(file))) ? undefined : ROUTE_TABLE_FILE
|
|
164
|
+
}
|
|
165
|
+
|
|
143
166
|
/**
|
|
144
167
|
* The TypeScript files under a zone folder that `programFiles` does not contain, project-relative and
|
|
145
168
|
* in input order. Declaration files and tests are left out: a project may typecheck tests with a
|