@nifrajs/cli 5.0.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3211 -0
- package/README.md +10 -2
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +5 -2
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts +12 -2
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +73 -9
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +152 -49
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +8 -2
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +49 -13
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +91 -45
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +128 -59
- package/dist/cli.js.map +1 -1
- package/dist/client-module.d.ts +5 -3
- package/dist/client-module.d.ts.map +1 -1
- package/dist/client-module.js +34 -10
- package/dist/client-module.js.map +1 -1
- package/dist/command-catalog.d.ts +9 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +75 -26
- package/dist/command-catalog.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +19 -1
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +114 -0
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/fix-recipes.d.ts +2 -0
- package/dist/fix-recipes.d.ts.map +1 -1
- package/dist/fix-recipes.js +93 -12
- package/dist/fix-recipes.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +60 -4
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +196 -33
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/config-paths.d.ts +16 -0
- package/dist/internal/config-paths.d.ts.map +1 -0
- package/dist/internal/config-paths.js +124 -0
- package/dist/internal/config-paths.js.map +1 -0
- package/dist/internal/installed-package.d.ts +4 -0
- package/dist/internal/installed-package.d.ts.map +1 -0
- package/dist/internal/installed-package.js +17 -0
- package/dist/internal/installed-package.js.map +1 -0
- package/dist/internal/project-specifiers.d.ts +14 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +63 -16
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +12 -1
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +117 -5
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +5 -3
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/introspect.d.ts +9 -0
- package/dist/introspect.d.ts.map +1 -1
- package/dist/introspect.js +32 -1
- package/dist/introspect.js.map +1 -1
- package/dist/levels-tool.d.ts.map +1 -1
- package/dist/levels-tool.js +5 -8
- package/dist/levels-tool.js.map +1 -1
- package/dist/load.d.ts +9 -4
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +32 -3
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-context.d.ts.map +1 -1
- package/dist/mcp-context.js +6 -21
- package/dist/mcp-context.js.map +1 -1
- package/dist/mcp-delegate.d.ts +2 -2
- package/dist/mcp-delegate.d.ts.map +1 -1
- package/dist/mcp-delegate.js +19 -7
- package/dist/mcp-delegate.js.map +1 -1
- package/dist/mcp-exec.d.ts +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +13 -5
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts +13 -0
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +23 -0
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-path.d.ts +2 -1
- package/dist/mcp-path.d.ts.map +1 -1
- package/dist/mcp-path.js +9 -24
- package/dist/mcp-path.js.map +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +40 -17
- package/dist/mcp.js.map +1 -1
- package/dist/migrate-layout.d.ts +41 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +1729 -236
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts +34 -0
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +132 -21
- package/dist/output-schemas.js.map +1 -1
- package/dist/project-path.d.ts +6 -0
- package/dist/project-path.d.ts.map +1 -1
- package/dist/project-path.js +48 -14
- package/dist/project-path.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +7 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/release-verification.d.ts +5 -0
- package/dist/release-verification.d.ts.map +1 -1
- package/dist/release-verification.js +14 -0
- package/dist/release-verification.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +7 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +5 -1
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +5 -1
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/data-guard.d.ts +6 -0
- package/dist/rules/data-guard.d.ts.map +1 -1
- package/dist/rules/data-guard.js +37 -0
- package/dist/rules/data-guard.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +316 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +44 -2
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +56 -3
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +412 -23
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/security.d.ts +3 -2
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +42 -11
- package/dist/rules/security.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +34 -11
- package/dist/rules/zones.js.map +1 -1
- package/dist/stylex-migrate.d.ts.map +1 -1
- package/dist/stylex-migrate.js +127 -17
- package/dist/stylex-migrate.js.map +1 -1
- package/dist/test-tool.d.ts.map +1 -1
- package/dist/test-tool.js +3 -9
- package/dist/test-tool.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +57 -6
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +247 -19
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +7 -2
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +66 -3
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +21 -3
- package/docs/llms-full.txt +1083 -168
- package/docs/types.json +535 -112
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +5 -1
- package/src/assure.ts +99 -11
- package/src/capabilities-tool.ts +188 -53
- package/src/check-diagnostics.ts +10 -0
- package/src/check-scan.ts +65 -12
- package/src/check.ts +122 -51
- package/src/cli.ts +127 -45
- package/src/client-module.ts +45 -9
- package/src/command-catalog.ts +92 -28
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +146 -1
- package/src/doctor.ts +102 -66
- package/src/fix-recipes.ts +101 -10
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +225 -44
- package/src/internal/config-paths.ts +154 -0
- package/src/internal/installed-package.ts +15 -0
- package/src/internal/project-specifiers.ts +78 -17
- package/src/internal/source-facts.ts +147 -5
- package/src/internal/typescript-import.ts +5 -3
- package/src/internal/workspace-packages.ts +218 -0
- package/src/introspect.ts +39 -1
- package/src/levels-tool.ts +7 -9
- package/src/load.ts +46 -4
- package/src/lock-links.ts +173 -0
- package/src/mcp-context.ts +5 -18
- package/src/mcp-delegate.ts +22 -10
- package/src/mcp-exec.ts +14 -4
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +33 -0
- package/src/mcp-path.ts +8 -22
- package/src/mcp-root.ts +14 -8
- package/src/mcp.ts +48 -19
- package/src/migrate-layout.ts +1968 -245
- package/src/output-schemas.ts +179 -23
- package/src/project-path.ts +44 -13
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/index.ts +7 -0
- package/src/release-verification.ts +20 -0
- package/src/review.ts +7 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +5 -1
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/data-guard.ts +40 -0
- package/src/rules/explanations.ts +329 -0
- package/src/rules/legacy.ts +46 -2
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +486 -25
- package/src/rules/routes.ts +4 -3
- package/src/rules/security.ts +49 -11
- package/src/rules/zones.ts +33 -9
- package/src/stylex-migrate.ts +117 -17
- package/src/test-tool.ts +3 -9
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +312 -19
- package/src/verification-plan.ts +15 -2
- package/src/version-drift.ts +68 -2
- package/src/web-app-options.ts +9 -1
package/src/rules/security.ts
CHANGED
|
@@ -74,8 +74,9 @@ function unexpectedMessage(
|
|
|
74
74
|
const parentOf = (dir: string): string => dir.slice(0, Math.max(dir.lastIndexOf("/"), 0))
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
|
-
* Whether NF-S002 may treat `file` as browser code,
|
|
78
|
-
*
|
|
77
|
+
* Whether NF-S001 and NF-S002 may treat `file` as browser code, where a finding is advisory: a
|
|
78
|
+
* browser compares values the client already holds, and a gate there that fails open grants nothing
|
|
79
|
+
* the server does not check again. It must be browser code by path (a route's frontend half,
|
|
79
80
|
* `frontend/`) and sit inside a nifra web app: a `routes/` folder in an API-only project or a package
|
|
80
81
|
* without a web dependency runs on the server. `shared/` also runs on the server, and a file that
|
|
81
82
|
* cannot be classified is treated as server - fail closed.
|
|
@@ -557,29 +558,46 @@ const GATE_NAMES = [
|
|
|
557
558
|
/^(?:require|assert|authori[sz]e|authenticate)/i,
|
|
558
559
|
/^can[A-Z]/,
|
|
559
560
|
/.Guard$/,
|
|
560
|
-
/^(?:ensure|verify|check|validate)(?:Auth
|
|
561
|
+
/^(?:ensure|verify|check|validate)(?:Auth\w*|LoggedIn|Login|Session|Token|Jwt|JWT|Signatures?|Webhook|Hmac|HMAC|Password|Credentials?|Permissions?|Access|Roles?|Admin(?:istrator)?|Owner(?:ship)?|Scopes?|Csrf|CSRF|ApiKey|Origin|Tenant|User|Member(?:ship)?)(?![a-z])/,
|
|
561
562
|
]
|
|
562
563
|
|
|
564
|
+
/** An authenticator returns who is calling, so its "nobody" result (`null`, `undefined`) is a denial. */
|
|
565
|
+
const AUTHENTICATOR = /^authenticate/i
|
|
566
|
+
|
|
567
|
+
/** Result-object fields whose `false` is the denial (`{ ok: false, error }`). */
|
|
568
|
+
const DECISION_FIELDS = new Set([
|
|
569
|
+
"ok",
|
|
570
|
+
"allowed",
|
|
571
|
+
"authorized",
|
|
572
|
+
"authenticated",
|
|
573
|
+
"valid",
|
|
574
|
+
"success",
|
|
575
|
+
"granted",
|
|
576
|
+
])
|
|
577
|
+
|
|
563
578
|
/** Helpers whose whole job is to fail (`fail(…)`, `deny(…)`, `reject(…)`, …). */
|
|
564
579
|
const FAILING_HELPERS = new Set(["fail", "deny", "reject", "forbid", "unauthorized", "abort"])
|
|
565
580
|
|
|
566
581
|
/**
|
|
567
|
-
* Whether a gate's catch block fails closed: it throws, returns `false`, a
|
|
568
|
-
* `
|
|
582
|
+
* Whether a gate's catch block fails closed: it throws, returns `false`, a result object whose decision
|
|
583
|
+
* field is `false`, a `new Response(…)` or a `deny…(…)` call (an authenticator also `null` or
|
|
584
|
+
* nothing), or hands the failure to a {@link FAILING_HELPERS} helper. The last form is why
|
|
569
585
|
* conformance/invariant asserts that funnel every error through a `never`-returning `fail()` are not
|
|
570
586
|
* fail-open - the scanner reads no types, so it trusts the helper by name, the same trade the SQL and
|
|
571
587
|
* gate-name heuristics already make. Read from the syntax, so a "throw" in a comment or a string counts
|
|
572
588
|
* for nothing, and a nested function's statements are not the catch's own.
|
|
573
589
|
*/
|
|
574
|
-
function failsClosed(ts: typeof TSApi, block: TSApi.Block): boolean {
|
|
590
|
+
function failsClosed(ts: typeof TSApi, block: TSApi.Block, gate: string): boolean {
|
|
591
|
+
const authenticator = AUTHENTICATOR.test(gate)
|
|
575
592
|
let closed = false
|
|
576
593
|
const visit = (node: TSApi.Node): void => {
|
|
577
594
|
if (closed || ts.isFunctionLike(node)) return
|
|
578
595
|
if (
|
|
579
596
|
ts.isThrowStatement(node) ||
|
|
580
597
|
(ts.isReturnStatement(node) &&
|
|
581
|
-
node.expression
|
|
582
|
-
|
|
598
|
+
(node.expression === undefined
|
|
599
|
+
? authenticator
|
|
600
|
+
: isDenial(ts, node.expression, authenticator))) ||
|
|
583
601
|
(ts.isCallExpression(node) &&
|
|
584
602
|
ts.isIdentifier(node.expression) &&
|
|
585
603
|
FAILING_HELPERS.has(node.expression.text))
|
|
@@ -593,7 +611,7 @@ function failsClosed(ts: typeof TSApi, block: TSApi.Block): boolean {
|
|
|
593
611
|
return closed
|
|
594
612
|
}
|
|
595
613
|
|
|
596
|
-
function isDenial(ts: typeof TSApi, expression: TSApi.Expression): boolean {
|
|
614
|
+
function isDenial(ts: typeof TSApi, expression: TSApi.Expression, authenticator: boolean): boolean {
|
|
597
615
|
let value = expression
|
|
598
616
|
while (
|
|
599
617
|
ts.isParenthesizedExpression(value) ||
|
|
@@ -603,6 +621,22 @@ function isDenial(ts: typeof TSApi, expression: TSApi.Expression): boolean {
|
|
|
603
621
|
)
|
|
604
622
|
value = value.expression
|
|
605
623
|
if (value.kind === ts.SyntaxKind.FalseKeyword) return true
|
|
624
|
+
if (
|
|
625
|
+
authenticator &&
|
|
626
|
+
(value.kind === ts.SyntaxKind.NullKeyword ||
|
|
627
|
+
ts.isVoidExpression(value) ||
|
|
628
|
+
(ts.isIdentifier(value) && value.text === "undefined"))
|
|
629
|
+
)
|
|
630
|
+
return true
|
|
631
|
+
if (ts.isObjectLiteralExpression(value)) {
|
|
632
|
+
return value.properties.some(
|
|
633
|
+
(property) =>
|
|
634
|
+
ts.isPropertyAssignment(property) &&
|
|
635
|
+
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) &&
|
|
636
|
+
DECISION_FIELDS.has(property.name.text) &&
|
|
637
|
+
property.initializer.kind === ts.SyntaxKind.FalseKeyword,
|
|
638
|
+
)
|
|
639
|
+
}
|
|
606
640
|
if (ts.isNewExpression(value))
|
|
607
641
|
return ts.isIdentifier(value.expression) && value.expression.text === "Response"
|
|
608
642
|
return (
|
|
@@ -621,23 +655,26 @@ export const failOpenGateRule: CheckRule = {
|
|
|
621
655
|
const ts = loaded.compiler
|
|
622
656
|
if (ts === undefined) return didNotRun("NF-S001", "Fail-open gate scan")
|
|
623
657
|
const findings: Diagnostic[] = []
|
|
658
|
+
const webDirs = createWebAppProbe(ctx.root)
|
|
624
659
|
for (const file of ctx.project.source.files) {
|
|
625
660
|
const parsed = parsedFile(ts, ctx.project.source, file)
|
|
626
661
|
if (parsed === undefined) continue
|
|
627
662
|
const { tree, lines } = parsed
|
|
663
|
+
let browser: boolean | undefined
|
|
628
664
|
const visit = (node: TSApi.Node): void => {
|
|
629
665
|
if (ts.isCatchClause(node)) {
|
|
630
666
|
let parent: TSApi.Node | undefined = node.parent
|
|
631
667
|
while (parent !== undefined && !ts.isFunctionLike(parent)) parent = parent.parent
|
|
632
668
|
const name = parent === undefined ? undefined : functionName(ts, parent)
|
|
633
669
|
if (name !== undefined && GATE_NAMES.some((gate) => gate.test(name))) {
|
|
634
|
-
if (!failsClosed(ts, node.block)) {
|
|
670
|
+
if (!failsClosed(ts, node.block, name)) {
|
|
635
671
|
const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
|
|
636
672
|
const reviewed = expectedReason(lines, line, "NF-S001") !== undefined
|
|
673
|
+
browser ??= provenBrowserFile(ctx.root, file, webDirs)
|
|
637
674
|
findings.push(
|
|
638
675
|
diagnostic({
|
|
639
676
|
code: "NF-S001",
|
|
640
|
-
severity: reviewed ? "info" : "error",
|
|
677
|
+
severity: reviewed ? "info" : browser ? "warn" : "error",
|
|
641
678
|
file,
|
|
642
679
|
line,
|
|
643
680
|
message: reviewed
|
|
@@ -651,6 +688,7 @@ export const failOpenGateRule: CheckRule = {
|
|
|
651
688
|
evidence: [
|
|
652
689
|
name,
|
|
653
690
|
"catch block has no denial or rethrow",
|
|
691
|
+
...(browser ? ["browser code: the server's own gate decides"] : []),
|
|
654
692
|
...expectationEvidence(lines, line, "NF-S001"),
|
|
655
693
|
],
|
|
656
694
|
verify: "nifra check --lints-only",
|
package/src/rules/zones.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
importRuleMessage,
|
|
18
18
|
privateEnvReads,
|
|
19
19
|
privateEnvReason,
|
|
20
|
+
specifierPackage,
|
|
20
21
|
type ZoneClassifier,
|
|
21
22
|
ZoneConfigError,
|
|
22
23
|
} from "@nifrajs/web/zones"
|
|
@@ -26,6 +27,7 @@ import { appDirs, clientModuleRoot } from "../client-module.ts"
|
|
|
26
27
|
import { componentCheckReceipts } from "../component-check.ts"
|
|
27
28
|
import { type Diagnostic, diagnostic } from "../diagnostics.ts"
|
|
28
29
|
import { codeUnitOrder } from "../internal/code-unit-order.ts"
|
|
30
|
+
import { installedManifest } from "../internal/installed-package.ts"
|
|
29
31
|
import {
|
|
30
32
|
createApiOnlyProbe,
|
|
31
33
|
createKindProbe,
|
|
@@ -35,7 +37,9 @@ import {
|
|
|
35
37
|
withApiKindHint,
|
|
36
38
|
} from "../internal/nifra-project.ts"
|
|
37
39
|
import { createSpecifierResolver } from "../internal/project-specifiers.ts"
|
|
40
|
+
import { createSourceFacts } from "../internal/source-facts.ts"
|
|
38
41
|
import type { CheckRule, RuleContext } from "./index.ts"
|
|
42
|
+
import { loadRuleTypeScript } from "./typescript.ts"
|
|
39
43
|
|
|
40
44
|
const ZONE_FOLDER = new Set(["routes", "frontend", "backend", "shared"])
|
|
41
45
|
const FRONTEND = new Set(["route-frontend", "frontend"])
|
|
@@ -186,6 +190,15 @@ export const zoneImportRule: CheckRule = {
|
|
|
186
190
|
// Outside any web app, a finding may only mean the project serves no pages: say how to declare it.
|
|
187
191
|
const inWebApp = createWebAppProbe(ctx.root)
|
|
188
192
|
const resolveSpecifier = createSpecifierResolver()
|
|
193
|
+
// Without a compiler nothing proves an import erased, so every import stays an edge.
|
|
194
|
+
const { compiler } = await loadRuleTypeScript(ctx)
|
|
195
|
+
const facts =
|
|
196
|
+
compiler === undefined
|
|
197
|
+
? undefined
|
|
198
|
+
: createSourceFacts(compiler, {
|
|
199
|
+
keepsTypeOnlyImports: (file) =>
|
|
200
|
+
resolveSpecifier.keepsTypeOnlyImports(isAbsolute(file) ? file : join(ctx.root, file)),
|
|
201
|
+
})
|
|
189
202
|
const queue: Importer[] = []
|
|
190
203
|
const queued = new Set<string>()
|
|
191
204
|
const follow = (importer: Importer): void => {
|
|
@@ -201,7 +214,7 @@ export const zoneImportRule: CheckRule = {
|
|
|
201
214
|
// The browser entry imports the app's `clientModule`, so the build classifies it like any file it
|
|
202
215
|
// loads, and loads what it imports.
|
|
203
216
|
for (const app of appDirs(ctx.sources.files)) {
|
|
204
|
-
const entry = clientModuleRoot(ctx.root, app, resolveSpecifier)
|
|
217
|
+
const entry = clientModuleRoot(ctx.root, app, resolveSpecifier, compiler)
|
|
205
218
|
if (entry === undefined) continue
|
|
206
219
|
let classification: Classification
|
|
207
220
|
try {
|
|
@@ -235,11 +248,21 @@ export const zoneImportRule: CheckRule = {
|
|
|
235
248
|
for (const { file, app, zone: from } of queue) {
|
|
236
249
|
const content = ctx.sources.read(file)
|
|
237
250
|
if (content === undefined) continue
|
|
238
|
-
for (const { specifier, line } of importSites(content, file)) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
251
|
+
for (const { specifier, line } of importSites(content, file, facts)) {
|
|
252
|
+
let target = resolveSpecifier(join(ctx.root, file), specifier)
|
|
253
|
+
let to: Classification
|
|
254
|
+
if (target === undefined) {
|
|
255
|
+
// An installed package is judged here only by the entry it names, when it gives each
|
|
256
|
+
// entry its own side; a package-wide side is NF-C004's in browser code.
|
|
257
|
+
const name = specifierPackage(specifier)
|
|
258
|
+
const manifest =
|
|
259
|
+
name === undefined ? undefined : installedManifest(join(ctx.root, file), name)
|
|
260
|
+
if (manifest === undefined) continue
|
|
261
|
+
to = classifierFor(app).classify(manifest, specifier)
|
|
262
|
+
if (to.entry === undefined) continue
|
|
263
|
+
target = manifest
|
|
264
|
+
} else to = classifierFor(app).classify(target, specifier)
|
|
265
|
+
const targetName = to.entry === undefined ? display(ctx.root, target) : specifier
|
|
243
266
|
if (
|
|
244
267
|
["frontend", "route-frontend", "shared"].includes(from) &&
|
|
245
268
|
(receipts.has(target) || receipts.has(targetName))
|
|
@@ -250,7 +273,7 @@ export const zoneImportRule: CheckRule = {
|
|
|
250
273
|
if (to.package !== undefined) {
|
|
251
274
|
if (reportedPackages.has(to.package)) continue
|
|
252
275
|
reportedPackages.add(to.package)
|
|
253
|
-
} else {
|
|
276
|
+
} else if (to.entry === undefined) {
|
|
254
277
|
// A file in no zone loads what it imports too: the build refuses each one it reaches.
|
|
255
278
|
follow({ file: targetName, app, zone: "error" })
|
|
256
279
|
}
|
|
@@ -311,9 +334,10 @@ export const apiKindRule: CheckRule = {
|
|
|
311
334
|
const PREFIX_EXPORT =
|
|
312
335
|
/\bexport\s+const\s+publicEnvPrefix\s*(?::\s*string\s*)?=\s*(?:"([^"\\\r\n]*)"|'([^'\\\r\n]*)')/
|
|
313
336
|
|
|
314
|
-
/** 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. */
|
|
315
339
|
function publicPrefix(root: string, app: string): string {
|
|
316
|
-
for (const config of [
|
|
340
|
+
for (const config of [CONFIG_FILE, FRAMEWORK_FILE]) {
|
|
317
341
|
const path = join(root, app, config)
|
|
318
342
|
if (!existsSync(path)) continue
|
|
319
343
|
const match = PREFIX_EXPORT.exec(stripComments(readFileSync(path, "utf8")))
|
package/src/stylex-migrate.ts
CHANGED
|
@@ -1099,27 +1099,126 @@ const existingStylexImport = (source: string): string | undefined => {
|
|
|
1099
1099
|
return match?.[1]
|
|
1100
1100
|
}
|
|
1101
1101
|
|
|
1102
|
-
const
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
const
|
|
1110
|
-
if (
|
|
1111
|
-
|
|
1112
|
-
|
|
1102
|
+
const isIdentifierChar = (char: string | undefined): boolean =>
|
|
1103
|
+
char !== undefined && /[\w$]/.test(char)
|
|
1104
|
+
|
|
1105
|
+
/** Past the whitespace and comments at `index`. `inLine` stops at the end of the line. */
|
|
1106
|
+
function skipTrivia(source: string, index: number, inLine = false): number {
|
|
1107
|
+
let at = index
|
|
1108
|
+
while (at < source.length) {
|
|
1109
|
+
const char = source[at]
|
|
1110
|
+
if (char === " " || char === "\t" || char === "\r" || (char === "\n" && !inLine)) at++
|
|
1111
|
+
else if (source.startsWith("//", at)) {
|
|
1112
|
+
const end = source.indexOf("\n", at)
|
|
1113
|
+
if (end === -1) return source.length
|
|
1114
|
+
at = inLine ? end : end + 1
|
|
1115
|
+
} else if (source.startsWith("/*", at)) {
|
|
1116
|
+
const end = source.indexOf("*/", at + 2)
|
|
1117
|
+
if (end === -1) return source.length
|
|
1118
|
+
at = end + 2
|
|
1119
|
+
} else break
|
|
1120
|
+
}
|
|
1121
|
+
return at
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function skipString(source: string, index: number): number {
|
|
1125
|
+
const quote = source[index]
|
|
1126
|
+
let at = index + 1
|
|
1127
|
+
while (at < source.length && source[at] !== quote && source[at] !== "\n")
|
|
1128
|
+
at += source[at] === "\\" ? 2 : 1
|
|
1129
|
+
return Math.min(at + 1, source.length)
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/** Past an import's specifier: its `with { ... }` (or `assert`) attributes and a `;`. */
|
|
1133
|
+
function afterSpecifier(source: string, index: number): number {
|
|
1134
|
+
let at = index
|
|
1135
|
+
const next = skipTrivia(source, at, true)
|
|
1136
|
+
for (const keyword of ["with", "assert"]) {
|
|
1137
|
+
if (!source.startsWith(keyword, next) || isIdentifierChar(source[next + keyword.length]))
|
|
1138
|
+
continue
|
|
1139
|
+
const open = skipTrivia(source, next + keyword.length)
|
|
1140
|
+
const close = source[open] === "{" ? source.indexOf("}", open) : -1
|
|
1141
|
+
if (close !== -1) at = close + 1
|
|
1142
|
+
}
|
|
1143
|
+
const semicolon = skipTrivia(source, at, true)
|
|
1144
|
+
return source[semicolon] === ";" ? semicolon + 1 : at
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/** Where the import declaration whose `import` keyword is at `index` ends, or `undefined` for an
|
|
1148
|
+
* `import(...)` / `import.meta` expression or a declaration this scan cannot read. */
|
|
1149
|
+
function importEnd(source: string, index: number): number | undefined {
|
|
1150
|
+
let at = skipTrivia(source, index + "import".length)
|
|
1151
|
+
if (source[at] === "(" || source[at] === ".") return undefined
|
|
1152
|
+
if (source[at] === '"' || source[at] === "'")
|
|
1153
|
+
return afterSpecifier(source, skipString(source, at))
|
|
1154
|
+
let depth = 0
|
|
1155
|
+
while (at < source.length) {
|
|
1156
|
+
const char = source[at]
|
|
1157
|
+
if (char === '"' || char === "'") {
|
|
1158
|
+
at = skipString(source, at)
|
|
1113
1159
|
continue
|
|
1114
1160
|
}
|
|
1115
|
-
if (
|
|
1116
|
-
|
|
1161
|
+
if (source.startsWith("//", at) || source.startsWith("/*", at)) {
|
|
1162
|
+
at = skipTrivia(source, at)
|
|
1117
1163
|
continue
|
|
1118
1164
|
}
|
|
1119
|
-
|
|
1165
|
+
if (char === "{") depth++
|
|
1166
|
+
else if (char === "}") depth--
|
|
1167
|
+
else if (depth === 0 && (char === ";" || char === "=")) return undefined
|
|
1168
|
+
else if (
|
|
1169
|
+
depth === 0 &&
|
|
1170
|
+
source.startsWith("from", at) &&
|
|
1171
|
+
!isIdentifierChar(source[at - 1]) &&
|
|
1172
|
+
!isIdentifierChar(source[at + 4])
|
|
1173
|
+
) {
|
|
1174
|
+
const specifier = skipTrivia(source, at + 4)
|
|
1175
|
+
if (source[specifier] === '"' || source[specifier] === "'")
|
|
1176
|
+
return afterSpecifier(source, skipString(source, specifier))
|
|
1177
|
+
}
|
|
1178
|
+
at++
|
|
1179
|
+
}
|
|
1180
|
+
return undefined
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/** Past the rest of the line `index` is on when only trivia is left on it, then any blank lines. */
|
|
1184
|
+
function nextLine(source: string, index: number): number {
|
|
1185
|
+
let at = skipTrivia(source, index, true)
|
|
1186
|
+
if (at < source.length && source[at] !== "\n") return index
|
|
1187
|
+
for (;;) {
|
|
1188
|
+
if (source[at] !== "\n") return at
|
|
1189
|
+
let next = at + 1
|
|
1190
|
+
while (source[next] === " " || source[next] === "\t" || source[next] === "\r") next++
|
|
1191
|
+
if (next < source.length && source[next] !== "\n") return at + 1
|
|
1192
|
+
at = next
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/**
|
|
1197
|
+
* Where the generated block goes: after the directive prologue and the import declarations. Comments
|
|
1198
|
+
* are read as comments, so one before `"use client"` keeps the directive first, and a trailing
|
|
1199
|
+
* `// note` on the last import does not hide where it ends.
|
|
1200
|
+
*/
|
|
1201
|
+
const insertionOffset = (source: string): number => {
|
|
1202
|
+
let end = source.startsWith("#!") ? source.indexOf("\n") + 1 || source.length : 0
|
|
1203
|
+
let prologue = true
|
|
1204
|
+
for (;;) {
|
|
1205
|
+
const at = skipTrivia(source, end)
|
|
1206
|
+
const char = source[at]
|
|
1207
|
+
let statementEnd: number | undefined
|
|
1208
|
+
if (prologue && (char === '"' || char === "'")) {
|
|
1209
|
+
const literal = skipString(source, at)
|
|
1210
|
+
const after = skipTrivia(source, literal, true)
|
|
1211
|
+
// A string with more of an expression after it on its line is not a directive.
|
|
1212
|
+
if (source[after] === ";") statementEnd = after + 1
|
|
1213
|
+
else if (after === source.length || source[after] === "\n") statementEnd = literal
|
|
1214
|
+
} else if (source.startsWith("import", at) && !isIdentifierChar(source[at + 6])) {
|
|
1215
|
+
prologue = false
|
|
1216
|
+
statementEnd = importEnd(source, at)
|
|
1217
|
+
}
|
|
1218
|
+
if (statementEnd === undefined) break
|
|
1219
|
+
end = nextLine(source, statementEnd)
|
|
1120
1220
|
}
|
|
1121
|
-
|
|
1122
|
-
return offset
|
|
1221
|
+
return end
|
|
1123
1222
|
}
|
|
1124
1223
|
|
|
1125
1224
|
/** Transform one source file. The returned source is unchanged when every match is unsupported. */
|
|
@@ -1196,7 +1295,8 @@ export function migrateTailwindSource(source: string, file: string): SourceMigra
|
|
|
1196
1295
|
const needsImport = existingStylexImport(source) === undefined
|
|
1197
1296
|
const generated = `${needsImport ? `import * as ${stylexName} from "${STYLEX_SPECIFIER}"\n\n` : ""}const ${stylesName} = ${stylexName}.create(${JSON.stringify(Object.fromEntries(styles), null, 2)})\n\n`
|
|
1198
1297
|
const offset = insertionOffset(rewritten)
|
|
1199
|
-
const
|
|
1298
|
+
const separator = offset > 0 && rewritten[offset - 1] !== "\n" ? "\n" : ""
|
|
1299
|
+
const withGenerated = `${rewritten.slice(0, offset)}${separator}${generated}${rewritten.slice(offset)}`
|
|
1200
1300
|
return { source: withGenerated, changed: true, replacements: replacements.length, issues }
|
|
1201
1301
|
}
|
|
1202
1302
|
|
package/src/test-tool.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
escalatingStop,
|
|
2
3
|
type HeadTailOutput,
|
|
3
4
|
OWN_PROCESS_GROUP,
|
|
4
5
|
readHeadTail,
|
|
@@ -63,8 +64,6 @@ function normalizeTimeout(value: unknown): number {
|
|
|
63
64
|
return Math.min(Math.max(Math.trunc(value), 1_000), MAX_TIMEOUT_MS)
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
const KILL_GRACE_MS = 2_000
|
|
67
|
-
|
|
68
67
|
const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-?]*[ -/]*[@-~]`, "g")
|
|
69
68
|
|
|
70
69
|
// A UTF-8 character is at most four bytes, so these hold every character the result can show.
|
|
@@ -157,12 +156,7 @@ export async function collectTestResult(
|
|
|
157
156
|
})
|
|
158
157
|
let timedOut = false
|
|
159
158
|
let cancelled = false
|
|
160
|
-
|
|
161
|
-
// A test can ignore SIGTERM, so the group gets SIGKILL once the grace period runs out.
|
|
162
|
-
const stop = (): void => {
|
|
163
|
-
signalProcessGroup(proc, "SIGTERM")
|
|
164
|
-
escalation ??= setTimeout(() => signalProcessGroup(proc, "SIGKILL"), KILL_GRACE_MS)
|
|
165
|
-
}
|
|
159
|
+
const { stop, dispose } = escalatingStop((name) => signalProcessGroup(proc, name))
|
|
166
160
|
const abort = (): void => {
|
|
167
161
|
cancelled = true
|
|
168
162
|
stop()
|
|
@@ -189,7 +183,7 @@ export async function collectTestResult(
|
|
|
189
183
|
exitCode = result[2]
|
|
190
184
|
} finally {
|
|
191
185
|
clearTimeout(timer)
|
|
192
|
-
|
|
186
|
+
dispose()
|
|
193
187
|
opts.signal?.removeEventListener("abort", abort)
|
|
194
188
|
}
|
|
195
189
|
const stdout = clean(stdoutRaw)
|
|
@@ -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)) {
|