@nifrajs/cli 5.1.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3211 -0
- package/README.md +3 -0
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +1 -0
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +14 -1
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +131 -45
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +5 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +6 -3
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +52 -12
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +23 -12
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +9 -5
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +3 -2
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +26 -5
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +46 -6
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +193 -31
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +9 -1
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +6 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +106 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/load.d.ts +7 -2
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +11 -2
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +5 -2
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/migrate-layout.d.ts +2 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +434 -29
- package/dist/migrate-layout.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +7 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +2 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +2 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +2 -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 +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +316 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +24 -1
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +27 -0
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +177 -2
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +3 -2
- package/dist/rules/zones.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +37 -1
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +171 -20
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +2 -0
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +12 -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 +19 -1
- package/docs/llms-full.txt +625 -130
- package/docs/types.json +396 -50
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +1 -0
- package/src/assure.ts +11 -1
- package/src/capabilities-tool.ts +159 -50
- package/src/check-diagnostics.ts +8 -0
- package/src/check-scan.ts +9 -3
- package/src/check.ts +67 -12
- package/src/cli.ts +24 -11
- package/src/command-catalog.ts +16 -8
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +40 -6
- package/src/doctor.ts +102 -66
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +221 -41
- package/src/internal/project-specifiers.ts +9 -1
- package/src/internal/source-facts.ts +124 -0
- package/src/internal/workspace-packages.ts +218 -0
- package/src/load.ts +19 -3
- package/src/lock-links.ts +173 -0
- package/src/mcp-exec.ts +5 -2
- package/src/mcp-http.ts +1 -1
- package/src/mcp-root.ts +14 -8
- package/src/migrate-layout.ts +495 -31
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/index.ts +7 -0
- package/src/review.ts +2 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +2 -0
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/explanations.ts +329 -0
- package/src/rules/legacy.ts +24 -1
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +221 -2
- package/src/rules/routes.ts +4 -3
- package/src/rules/zones.ts +3 -2
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +227 -22
- package/src/verification-plan.ts +6 -0
- package/src/version-drift.ts +15 -0
- package/src/web-app-options.ts +9 -1
package/src/rules/pages.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* startup and build checks.
|
|
13
13
|
*/
|
|
14
14
|
import { existsSync, readFileSync } from "node:fs"
|
|
15
|
-
import { join } from "node:path"
|
|
15
|
+
import { join, posix } from "node:path"
|
|
16
16
|
import { discoverRoutes } from "@nifrajs/web/fs"
|
|
17
17
|
import { BACKEND_ROUTE_EXPORTS, shadowedPages } from "@nifrajs/web/route-manifest"
|
|
18
18
|
import type * as TSApi from "typescript"
|
|
@@ -173,10 +173,216 @@ function effectiveApiPrefix({ framework, config }: ApiPrefixSources): StaticApiP
|
|
|
173
173
|
return own.kind !== "default" || framework === undefined ? own : readStaticApiPrefix(framework)
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/** What a `createWebApp` call passes as `apiPrefix`: a value, the framework config's own, or neither. */
|
|
177
|
+
type EntryApiPrefix =
|
|
178
|
+
| { readonly kind: "literal"; readonly value: string; readonly unset: boolean }
|
|
179
|
+
| { readonly kind: "config" }
|
|
180
|
+
| { readonly kind: "unreadable" }
|
|
181
|
+
|
|
182
|
+
const SCRIPT_SOURCE = /\.[cm]?[jt]sx?$/
|
|
183
|
+
const WEB_PACKAGE = "@nifrajs/web"
|
|
184
|
+
const CONFIG_MODULES: ReadonlySet<string> = new Set(
|
|
185
|
+
[FRAMEWORK_FILE, CONFIG_FILE].map((file) => file.replace(SCRIPT_SOURCE, "")),
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
/** Whether a relative specifier in `file` names backend/framework.ts or nifra.config.ts. */
|
|
189
|
+
const namesFrameworkConfig = (file: string, specifier: string): boolean =>
|
|
190
|
+
specifier.startsWith(".") &&
|
|
191
|
+
CONFIG_MODULES.has(posix.join(posix.dirname(file), specifier).replace(SCRIPT_SOURCE, ""))
|
|
192
|
+
|
|
193
|
+
const unwrapExpression = (ts: typeof TSApi, node: TSApi.Expression): TSApi.Expression => {
|
|
194
|
+
let expr = node
|
|
195
|
+
while (
|
|
196
|
+
ts.isParenthesizedExpression(expr) ||
|
|
197
|
+
ts.isAsExpression(expr) ||
|
|
198
|
+
ts.isSatisfiesExpression(expr)
|
|
199
|
+
) {
|
|
200
|
+
expr = expr.expression
|
|
201
|
+
}
|
|
202
|
+
return expr
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A `createWebApp` call that mounts a backend (`api`), with the `apiPrefix` it passes. */
|
|
206
|
+
export interface EntryApiMount {
|
|
207
|
+
readonly line: number
|
|
208
|
+
readonly apiPrefix: EntryApiPrefix
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Every `createWebApp({ api, ... })` call in a module, `createWebApp` imported from `@nifrajs/web`.
|
|
213
|
+
* `apiPrefix` reads as a string literal, a same-module string `const`, or the framework config's own
|
|
214
|
+
* export (`import { apiPrefix } from "./backend/framework"`); a call whose options come from a spread
|
|
215
|
+
* or a variable is unreadable. A call with no `api` mounts nothing and is left out. Pure.
|
|
216
|
+
*/
|
|
217
|
+
export function entryApiMounts(
|
|
218
|
+
ts: typeof TSApi,
|
|
219
|
+
tree: TSApi.SourceFile,
|
|
220
|
+
file: string,
|
|
221
|
+
): EntryApiMount[] {
|
|
222
|
+
const webNames = new Set<string>()
|
|
223
|
+
const webNamespaces = new Set<string>()
|
|
224
|
+
const configNames = new Set<string>()
|
|
225
|
+
const configNamespaces = new Set<string>()
|
|
226
|
+
const strings = new Map<string, string>()
|
|
227
|
+
for (const statement of tree.statements) {
|
|
228
|
+
if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
229
|
+
const from = statement.moduleSpecifier.text
|
|
230
|
+
const bindings = statement.importClause?.namedBindings
|
|
231
|
+
const isWeb = from === WEB_PACKAGE
|
|
232
|
+
const isConfig = namesFrameworkConfig(file, from)
|
|
233
|
+
if (bindings === undefined || (!isWeb && !isConfig)) continue
|
|
234
|
+
if (ts.isNamespaceImport(bindings)) {
|
|
235
|
+
;(isWeb ? webNamespaces : configNamespaces).add(bindings.name.text)
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
for (const element of bindings.elements) {
|
|
239
|
+
const imported = (element.propertyName ?? element.name).text
|
|
240
|
+
if (isWeb && imported === "createWebApp") webNames.add(element.name.text)
|
|
241
|
+
if (isConfig && imported === "apiPrefix") configNames.add(element.name.text)
|
|
242
|
+
}
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
if (
|
|
246
|
+
!ts.isVariableStatement(statement) ||
|
|
247
|
+
(statement.declarationList.flags & ts.NodeFlags.Const) === 0
|
|
248
|
+
)
|
|
249
|
+
continue
|
|
250
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
251
|
+
const init =
|
|
252
|
+
declaration.initializer === undefined
|
|
253
|
+
? undefined
|
|
254
|
+
: unwrapExpression(ts, declaration.initializer)
|
|
255
|
+
if (
|
|
256
|
+
ts.isIdentifier(declaration.name) &&
|
|
257
|
+
init !== undefined &&
|
|
258
|
+
(ts.isStringLiteral(init) || ts.isNoSubstitutionTemplateLiteral(init))
|
|
259
|
+
)
|
|
260
|
+
strings.set(declaration.name.text, init.text)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (webNames.size === 0 && webNamespaces.size === 0) return []
|
|
264
|
+
|
|
265
|
+
const readPrefix = (node: TSApi.Expression): EntryApiPrefix => {
|
|
266
|
+
const expr = unwrapExpression(ts, node)
|
|
267
|
+
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
|
|
268
|
+
return { kind: "literal", value: expr.text, unset: false }
|
|
269
|
+
if (ts.isIdentifier(expr)) {
|
|
270
|
+
if (configNames.has(expr.text)) return { kind: "config" }
|
|
271
|
+
const value = strings.get(expr.text)
|
|
272
|
+
return value === undefined ? { kind: "unreadable" } : { kind: "literal", value, unset: false }
|
|
273
|
+
}
|
|
274
|
+
if (
|
|
275
|
+
ts.isPropertyAccessExpression(expr) &&
|
|
276
|
+
ts.isIdentifier(expr.expression) &&
|
|
277
|
+
configNamespaces.has(expr.expression.text) &&
|
|
278
|
+
expr.name.text === "apiPrefix"
|
|
279
|
+
)
|
|
280
|
+
return { kind: "config" }
|
|
281
|
+
return { kind: "unreadable" }
|
|
282
|
+
}
|
|
283
|
+
const isCreateWebApp = (callee: TSApi.Expression): boolean =>
|
|
284
|
+
ts.isIdentifier(callee)
|
|
285
|
+
? webNames.has(callee.text)
|
|
286
|
+
: ts.isPropertyAccessExpression(callee) &&
|
|
287
|
+
ts.isIdentifier(callee.expression) &&
|
|
288
|
+
webNamespaces.has(callee.expression.text) &&
|
|
289
|
+
callee.name.text === "createWebApp"
|
|
290
|
+
const propertyName = (property: TSApi.ObjectLiteralElementLike): string | undefined =>
|
|
291
|
+
property.name !== undefined &&
|
|
292
|
+
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))
|
|
293
|
+
? property.name.text
|
|
294
|
+
: undefined
|
|
295
|
+
|
|
296
|
+
const mounts: EntryApiMount[] = []
|
|
297
|
+
const visit = (node: TSApi.Node): void => {
|
|
298
|
+
const arg = ts.isCallExpression(node) ? node.arguments[0] : undefined
|
|
299
|
+
const options = arg === undefined ? undefined : unwrapExpression(ts, arg)
|
|
300
|
+
if (
|
|
301
|
+
ts.isCallExpression(node) &&
|
|
302
|
+
isCreateWebApp(node.expression) &&
|
|
303
|
+
options !== undefined &&
|
|
304
|
+
ts.isObjectLiteralExpression(options) &&
|
|
305
|
+
options.properties.some((property) => propertyName(property) === "api")
|
|
306
|
+
) {
|
|
307
|
+
// The last of an `apiPrefix` property and a spread decides, as it does at runtime.
|
|
308
|
+
let apiPrefix: EntryApiPrefix = { kind: "literal", value: "", unset: true }
|
|
309
|
+
for (const property of options.properties) {
|
|
310
|
+
if (ts.isSpreadAssignment(property)) apiPrefix = { kind: "unreadable" }
|
|
311
|
+
else if (propertyName(property) !== "apiPrefix") continue
|
|
312
|
+
else if (ts.isPropertyAssignment(property)) apiPrefix = readPrefix(property.initializer)
|
|
313
|
+
else if (ts.isShorthandPropertyAssignment(property)) apiPrefix = readPrefix(property.name)
|
|
314
|
+
else apiPrefix = { kind: "unreadable" }
|
|
315
|
+
}
|
|
316
|
+
const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
|
|
317
|
+
mounts.push({ line, apiPrefix })
|
|
318
|
+
}
|
|
319
|
+
ts.forEachChild(node, visit)
|
|
320
|
+
}
|
|
321
|
+
visit(tree)
|
|
322
|
+
return mounts
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const servedAt = (prefix: string): string => (prefix === "" ? "nowhere" : `at ${prefix}`)
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The NF-C035 findings for app modules that start their own server: a `createWebApp` call that
|
|
329
|
+
* mounts a backend at a different `apiPrefix` than the one `nifra dev` reads, or with none, so
|
|
330
|
+
* production serves the backend somewhere dev and e2e never exercised.
|
|
331
|
+
*/
|
|
332
|
+
async function entryApiPrefixFindings(
|
|
333
|
+
ctx: Parameters<CheckRule["scan"]>[0],
|
|
334
|
+
expected: string,
|
|
335
|
+
origin: string,
|
|
336
|
+
): Promise<Diagnostic[]> {
|
|
337
|
+
const candidates = ctx.sources.files.filter(
|
|
338
|
+
(file) => SCRIPT_SOURCE.test(file) && ctx.sources.read(file)?.includes("createWebApp") === true,
|
|
339
|
+
)
|
|
340
|
+
if (candidates.length === 0) return []
|
|
341
|
+
const ts = (await loadRuleTypeScript(ctx)).compiler
|
|
342
|
+
if (ts === undefined) return []
|
|
343
|
+
const findings: Diagnostic[] = []
|
|
344
|
+
for (const file of [...candidates].sort(codeUnitOrder)) {
|
|
345
|
+
const text = ctx.sources.read(file) ?? ""
|
|
346
|
+
const tree = ts.createSourceFile(
|
|
347
|
+
file,
|
|
348
|
+
text,
|
|
349
|
+
ts.ScriptTarget.Latest,
|
|
350
|
+
true,
|
|
351
|
+
scriptKindOf(ts, file),
|
|
352
|
+
)
|
|
353
|
+
for (const { line, apiPrefix } of entryApiMounts(ts, tree, file)) {
|
|
354
|
+
if (apiPrefix.kind !== "literal" || apiPrefix.value === expected) continue
|
|
355
|
+
const specifier = posix.relative(posix.dirname(file), origin.replace(SCRIPT_SOURCE, ""))
|
|
356
|
+
const passed = apiPrefix.unset
|
|
357
|
+
? "no apiPrefix"
|
|
358
|
+
: `apiPrefix ${JSON.stringify(apiPrefix.value)}`
|
|
359
|
+
findings.push(
|
|
360
|
+
diagnostic({
|
|
361
|
+
code: "NF-C035",
|
|
362
|
+
severity: "warn",
|
|
363
|
+
file,
|
|
364
|
+
line,
|
|
365
|
+
message: `${file} calls createWebApp with a backend and ${passed}, so the server it starts serves the backend ${servedAt(apiPrefix.value)}, while \`nifra dev\` serves it ${servedAt(expected)} (${origin}). Pass the framework config's value: \`import { apiPrefix } from "${specifier.startsWith(".") ? specifier : `./${specifier}`}"\` and \`createWebApp({ ..., apiPrefix })\`.`,
|
|
366
|
+
evidence: [
|
|
367
|
+
`createWebApp apiPrefix: ${apiPrefix.unset ? "unset" : JSON.stringify(apiPrefix.value)}`,
|
|
368
|
+
`${origin} apiPrefix: ${JSON.stringify(expected)}`,
|
|
369
|
+
],
|
|
370
|
+
verify: "nifra check",
|
|
371
|
+
}),
|
|
372
|
+
)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return findings
|
|
376
|
+
}
|
|
377
|
+
|
|
176
378
|
/**
|
|
177
379
|
* NF-C035: `backend/app.ts` exports a backend that is not served over HTTP, so a browser call to `/api`
|
|
178
380
|
* reaches page routing. The framework config sets no `apiPrefix`; loader-only is a valid choice, and
|
|
179
381
|
* the finding asks for it to be stated. An unreadable `apiPrefix` is NF-C027's.
|
|
382
|
+
*
|
|
383
|
+
* An app module that calls `createWebApp` itself is checked against the same value, since only the
|
|
384
|
+
* framework config reaches `nifra dev`: a backend it mounts at another `apiPrefix`, or with none, is
|
|
385
|
+
* served in production where dev never served it.
|
|
180
386
|
*/
|
|
181
387
|
export const implicitApiMountRule: CheckRule = {
|
|
182
388
|
code: "NF-C035",
|
|
@@ -190,7 +396,19 @@ export const implicitApiMountRule: CheckRule = {
|
|
|
190
396
|
existsSync(join(ctx.root, file)) ? readFileSync(join(ctx.root, file), "utf8") : undefined
|
|
191
397
|
const sources = { framework: read(FRAMEWORK_FILE), config: read(CONFIG_FILE) }
|
|
192
398
|
if (sources.framework === undefined && sources.config === undefined) return []
|
|
193
|
-
|
|
399
|
+
const effective = effectiveApiPrefix(sources)
|
|
400
|
+
if (effective.kind === "unreadable") return []
|
|
401
|
+
const configOwn = sources.config === undefined ? undefined : readStaticApiPrefix(sources.config)
|
|
402
|
+
const origin =
|
|
403
|
+
configOwn !== undefined && (configOwn.kind !== "default" || sources.framework === undefined)
|
|
404
|
+
? CONFIG_FILE
|
|
405
|
+
: FRAMEWORK_FILE
|
|
406
|
+
const entries = await entryApiPrefixFindings(
|
|
407
|
+
ctx,
|
|
408
|
+
effective.kind === "literal" ? effective.value : "",
|
|
409
|
+
origin,
|
|
410
|
+
)
|
|
411
|
+
if (effective.kind === "literal") return entries
|
|
194
412
|
const file = sources.framework === undefined ? CONFIG_FILE : FRAMEWORK_FILE
|
|
195
413
|
return [
|
|
196
414
|
diagnostic({
|
|
@@ -201,6 +419,7 @@ export const implicitApiMountRule: CheckRule = {
|
|
|
201
419
|
fix: { recipe: "framework.api-prefix", command: "nifra fix --code NF-C035" },
|
|
202
420
|
verify: "nifra check",
|
|
203
421
|
}),
|
|
422
|
+
...entries,
|
|
204
423
|
]
|
|
205
424
|
},
|
|
206
425
|
}
|
package/src/rules/routes.ts
CHANGED
|
@@ -388,8 +388,9 @@ export const paramModifierRule: CheckRule = {
|
|
|
388
388
|
|
|
389
389
|
/**
|
|
390
390
|
* NF-C036: a registration whose path the static scan cannot read. The route still serves; what it
|
|
391
|
-
* loses is every source-level analysis keyed by path -
|
|
392
|
-
*
|
|
391
|
+
* loses is every source-level analysis keyed by path - the route lints above, and the scan's half of
|
|
392
|
+
* capability provenance (the other half is the registering module recorded when the app loads) - so
|
|
393
|
+
* it is reported as information, naming the expression.
|
|
393
394
|
*/
|
|
394
395
|
export const unresolvedRoutePathRule: CheckRule = {
|
|
395
396
|
code: "NF-C036",
|
|
@@ -411,7 +412,7 @@ export const unresolvedRoutePathRule: CheckRule = {
|
|
|
411
412
|
severity: "info",
|
|
412
413
|
file: route.file,
|
|
413
414
|
line: route.line,
|
|
414
|
-
message: `${route.method} ${route.expression} - the static scan cannot read this route path, so
|
|
415
|
+
message: `${route.method} ${route.expression} - the static scan cannot read this route path, so the route lints do not see the route, and capability provenance finds its module only from the app \`nifra.assurance.ts\` loads. Write the path as a string literal, or as a module-level \`const\` (optionally \`as const\`) in this file or exported from a project file it imports; mark a deliberately computed path with a \`// nifra-expect NF-C036: <reason>\` comment above the registration`,
|
|
415
416
|
evidence: [`${route.method} ${route.expression}`],
|
|
416
417
|
verify: "nifra check --lints-only",
|
|
417
418
|
}),
|
package/src/rules/zones.ts
CHANGED
|
@@ -334,9 +334,10 @@ export const apiKindRule: CheckRule = {
|
|
|
334
334
|
const PREFIX_EXPORT =
|
|
335
335
|
/\bexport\s+const\s+publicEnvPrefix\s*(?::\s*string\s*)?=\s*(?:"([^"\\\r\n]*)"|'([^'\\\r\n]*)')/
|
|
336
336
|
|
|
337
|
-
/** The app's public-env prefix when its config declares it as a string literal, else the default.
|
|
337
|
+
/** The app's public-env prefix when its config declares it as a string literal, else the default.
|
|
338
|
+
* nifra.config.ts is read first, as `loadApp` does. */
|
|
338
339
|
function publicPrefix(root: string, app: string): string {
|
|
339
|
-
for (const config of [
|
|
340
|
+
for (const config of [CONFIG_FILE, FRAMEWORK_FILE]) {
|
|
340
341
|
const path = join(root, app, config)
|
|
341
342
|
if (!existsSync(path)) continue
|
|
342
343
|
const match = PREFIX_EXPORT.exec(stripComments(readFileSync(path, "utf8")))
|
|
@@ -13,6 +13,56 @@ const TS_SOURCE = /\.(?:[cm]?ts|tsx)$/
|
|
|
13
13
|
const NOT_PROGRAM_CODE = /\.d\.[cm]?ts$|\.(?:test|spec)\.[cm]?tsx?$/
|
|
14
14
|
const TSC_DIAGNOSTIC = /\(\d+,\d+\):\s*(?:error|warning)\s+TS\d+:/
|
|
15
15
|
|
|
16
|
+
const readTsconfig = (path: string): unknown => {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(stripComments(readFileSync(path, "utf8")).replace(/,(\s*[}\]])/g, "$1"))
|
|
19
|
+
} catch {
|
|
20
|
+
return undefined
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A `references[].path`, which names a tsconfig or the folder holding one, as a file. */
|
|
25
|
+
const referencedConfig = (from: string, path: string): string => {
|
|
26
|
+
const target = resolve(dirname(from), path)
|
|
27
|
+
return target.endsWith(".json") ? target : join(target, "tsconfig.json")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The tsconfigs `nifra check` typechecks, each as its own program: `configured` (nifra.check.json
|
|
32
|
+
* `tsconfigs`) when given, else `tsconfig.json` and every project it references, transitively. A
|
|
33
|
+
* solution-style tsconfig (`"files": []` plus `references`) holds no files itself, so without its
|
|
34
|
+
* references the typecheck would pass having read nothing. `missing` lists configured paths that do not
|
|
35
|
+
* exist; references that do not exist are tsc's to report.
|
|
36
|
+
*/
|
|
37
|
+
export function typecheckProjects(
|
|
38
|
+
cwd: string,
|
|
39
|
+
configured?: readonly string[],
|
|
40
|
+
): { readonly paths: readonly string[]; readonly missing: readonly string[] } {
|
|
41
|
+
if (configured !== undefined) {
|
|
42
|
+
const paths = configured.map((path) => resolve(cwd, path))
|
|
43
|
+
return {
|
|
44
|
+
paths: paths.filter((path) => existsSync(path)),
|
|
45
|
+
missing: configured.filter((_, index) => !existsSync(paths[index] ?? "")),
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const root = join(cwd, "tsconfig.json")
|
|
49
|
+
if (!existsSync(root)) return { paths: [], missing: [] }
|
|
50
|
+
const paths: string[] = []
|
|
51
|
+
const queue = [root]
|
|
52
|
+
while (queue.length > 0 && paths.length < 64) {
|
|
53
|
+
const path = queue.shift()
|
|
54
|
+
if (path === undefined || paths.includes(path) || !existsSync(path)) continue
|
|
55
|
+
paths.push(path)
|
|
56
|
+
const references: unknown = Reflect.get(Object(readTsconfig(path)), "references")
|
|
57
|
+
if (!Array.isArray(references)) continue
|
|
58
|
+
for (const reference of references) {
|
|
59
|
+
const target: unknown = Reflect.get(Object(reference), "path")
|
|
60
|
+
if (typeof target === "string") queue.push(referencedConfig(path, target))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { paths, missing: [] }
|
|
64
|
+
}
|
|
65
|
+
|
|
16
66
|
/** Split `tsc --listFiles` output into the program's files and the compiler's own output. */
|
|
17
67
|
export function splitListedFiles(output: string): {
|
|
18
68
|
readonly programFiles: readonly string[]
|
|
@@ -68,12 +118,7 @@ function hasOwnProgram(cwd: string, app: string): boolean {
|
|
|
68
118
|
export function tsconfigExcludes(cwd: string): string[] {
|
|
69
119
|
let path = join(cwd, "tsconfig.json")
|
|
70
120
|
for (let depth = 0; depth < 8 && existsSync(path); depth++) {
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
config = JSON.parse(stripComments(readFileSync(path, "utf8")).replace(/,(\s*[}\]])/g, "$1"))
|
|
74
|
-
} catch {
|
|
75
|
-
return []
|
|
76
|
-
}
|
|
121
|
+
const config = readTsconfig(path)
|
|
77
122
|
if (typeof config !== "object" || config === null) return []
|
|
78
123
|
const exclude: unknown = Reflect.get(config, "exclude")
|
|
79
124
|
if (Array.isArray(exclude)) {
|