@intentius/chant 0.124.85 → 0.124.87
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/dist/bundle-check.d.ts +102 -0
- package/dist/bundle-check.d.ts.map +1 -0
- package/dist/bundle-check.js +310 -0
- package/dist/chant-install.d.ts +100 -0
- package/dist/chant-install.d.ts.map +1 -0
- package/dist/chant-install.js +158 -0
- package/dist/ci-commit.d.ts +19 -6
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +23 -4
- package/dist/ci-evidence-jobs.d.ts +78 -0
- package/dist/ci-evidence-jobs.d.ts.map +1 -0
- package/dist/ci-evidence-jobs.js +85 -0
- package/dist/ci-image.d.ts +274 -0
- package/dist/ci-image.d.ts.map +1 -0
- package/dist/ci-image.js +635 -0
- package/dist/ci-login.d.ts +150 -0
- package/dist/ci-login.d.ts.map +1 -0
- package/dist/ci-login.js +240 -0
- package/dist/ci-product.d.ts +16 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +17 -0
- package/dist/cli/commands/import-index.js +2 -2
- package/dist/cli/handlers/build.d.ts +8 -0
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +22 -4
- package/dist/cli/handlers/ci-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-check.js +61 -2
- package/dist/cli/handlers/ci-evidence.d.ts +3 -0
- package/dist/cli/handlers/ci-evidence.d.ts.map +1 -0
- package/dist/cli/handlers/ci-evidence.js +174 -0
- package/dist/cli/handlers/components.js +3 -3
- package/dist/cli/handlers/dead-guards.d.ts +3 -0
- package/dist/cli/handlers/dead-guards.d.ts.map +1 -0
- package/dist/cli/handlers/dead-guards.js +35 -0
- package/dist/cli/handlers/graph.js +4 -4
- package/dist/cli/handlers/lifecycle.d.ts +2 -1
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +5 -4
- package/dist/cli/handlers/local-chant.d.ts +16 -0
- package/dist/cli/handlers/local-chant.d.ts.map +1 -0
- package/dist/cli/handlers/local-chant.js +109 -0
- package/dist/cli/handlers/operator.js +2 -2
- package/dist/cli/handlers/search.js +5 -5
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +38 -0
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +28 -0
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/check-hooks.d.ts +49 -0
- package/dist/components/check-hooks.d.ts.map +1 -1
- package/dist/components/check-hooks.js +64 -4
- package/dist/components/check-steps.d.ts +17 -0
- package/dist/components/check-steps.d.ts.map +1 -1
- package/dist/components/check-steps.js +37 -5
- package/dist/components/product-pipeline.d.ts +23 -0
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +6 -2
- package/dist/components/product-plan.d.ts +143 -0
- package/dist/components/product-plan.d.ts.map +1 -0
- package/dist/components/product-plan.js +145 -0
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +2 -2
- package/dist/config.d.ts +39 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -3
- package/dist/dead-guards.d.ts +82 -0
- package/dist/dead-guards.d.ts.map +1 -0
- package/dist/dead-guards.js +436 -0
- package/dist/discovery/files.d.ts +3 -1
- package/dist/discovery/files.d.ts.map +1 -1
- package/dist/discovery/files.js +5 -3
- package/dist/drift-issue.d.ts +152 -0
- package/dist/drift-issue.d.ts.map +1 -0
- package/dist/drift-issue.js +167 -0
- package/dist/floor-table.d.ts +30 -0
- package/dist/floor-table.d.ts.map +1 -0
- package/dist/floor-table.data.d.ts +3 -0
- package/dist/floor-table.data.d.ts.map +1 -0
- package/dist/floor-table.data.js +179 -0
- package/dist/floor-table.js +56 -0
- package/dist/forge-flags.d.ts +90 -0
- package/dist/forge-flags.d.ts.map +1 -0
- package/dist/forge-flags.js +184 -0
- package/dist/lexicon.d.ts +69 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.d.ts +7 -1
- package/dist/lifecycle/gated-op.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.js +7 -4
- package/dist/local-chant.d.ts +98 -0
- package/dist/local-chant.d.ts.map +1 -0
- package/dist/local-chant.js +307 -0
- package/dist/op/activities/predict-behaviour.js +2 -2
- package/dist/op/op-waves-evidence-spec.d.ts +13 -0
- package/dist/op/op-waves-evidence-spec.d.ts.map +1 -0
- package/dist/op/op-waves-evidence-spec.js +21 -0
- package/dist/op/op-waves-evidence.d.ts +40 -0
- package/dist/op/op-waves-evidence.d.ts.map +1 -0
- package/dist/op/op-waves-evidence.js +58 -0
- package/dist/op/op-waves-run.d.ts +4 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +24 -1
- package/dist/op/op-waves.d.ts +17 -1
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -0
- package/dist/op/schedule-overdue.d.ts +41 -9
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +154 -25
- package/dist/plan-summary.d.ts +25 -0
- package/dist/plan-summary.d.ts.map +1 -1
- package/dist/plan-summary.js +31 -0
- package/dist/pr-plan-note.d.ts +35 -0
- package/dist/pr-plan-note.d.ts.map +1 -1
- package/dist/pr-plan-note.js +10 -0
- package/dist/pr-post.d.ts +23 -0
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +125 -3
- package/dist/release-floors.d.ts +33 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +33 -1
- package/dist/state-migration.d.ts +24 -0
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +100 -5
- package/dist/wave-runtime.mjs +309 -302
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +4 -3
- package/dist/workspace/lexicon-outputs.d.ts.map +1 -1
- package/dist/workspace/lexicon-outputs.js +21 -7
- package/dist/workspace/reason-codes.d.ts +3 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/reason-codes.js +4 -0
- package/dist/workspace/ref-gate.schema.json +85 -0
- package/dist/workspace/search-contract.d.ts +1 -1
- package/dist/workspace/search-contract.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence-report.d.ts +58 -0
- package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence-report.js +109 -0
- package/dist/workspace/trust/check-evidence.d.ts +160 -0
- package/dist/workspace/trust/check-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence.js +309 -0
- package/dist/workspace/trust/ci-tag-statement.d.ts +2 -2
- package/dist/workspace/trust/ci-tag-statement.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.d.ts +40 -2
- package/dist/workspace/trust/dsse.d.ts.map +1 -1
- package/dist/workspace/trust/dsse.js +34 -2
- package/dist/workspace/trust/evidence.d.ts +2 -2
- package/dist/workspace/trust/evidence.d.ts.map +1 -1
- package/dist/workspace/trust/keyless-fixture.d.ts +41 -0
- package/dist/workspace/trust/keyless-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/keyless-fixture.js +161 -0
- package/dist/workspace/trust/keyless.d.ts +202 -0
- package/dist/workspace/trust/keyless.d.ts.map +1 -0
- package/dist/workspace/trust/keyless.js +488 -0
- package/dist/workspace/trust/local-evidence.d.ts +123 -0
- package/dist/workspace/trust/local-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/local-evidence.js +259 -0
- package/dist/workspace/trust/op-run-statement.d.ts +2 -2
- package/dist/workspace/trust/op-run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/policy.d.ts +140 -1
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +197 -3
- package/dist/workspace/trust/ref-gate-cli.d.ts +33 -0
- package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-cli.js +234 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts +38 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-fixture.js +97 -0
- package/dist/workspace/trust/ref-gate.d.ts +187 -0
- package/dist/workspace/trust/ref-gate.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate.js +432 -0
- package/dist/workspace/trust/run-statement.d.ts +3 -3
- package/dist/workspace/trust/run-statement.d.ts.map +1 -1
- package/dist/workspace/trust/runner-identity.d.ts +56 -0
- package/dist/workspace/trust/runner-identity.d.ts.map +1 -0
- package/dist/workspace/trust/runner-identity.js +65 -0
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +25 -1
- package/package.json +1 -1
- package/src/bundle-check.test.ts +140 -0
- package/src/bundle-check.ts +352 -0
- package/src/chant-install.test.ts +112 -0
- package/src/chant-install.ts +179 -0
- package/src/ci-commit.test.ts +45 -0
- package/src/ci-commit.ts +38 -4
- package/src/ci-evidence-jobs.ts +105 -0
- package/src/ci-image.test.ts +506 -0
- package/src/ci-image.ts +822 -0
- package/src/ci-login.test.ts +235 -0
- package/src/ci-login.ts +307 -0
- package/src/ci-product.ts +18 -0
- package/src/cli/commands/__fixtures__/source-dir-include/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-root/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.test.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/app.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/infra/net.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/import-index.ts +2 -2
- package/src/cli/handlers/build-source-dir.test.ts +64 -0
- package/src/cli/handlers/build.ts +21 -4
- package/src/cli/handlers/ci-check.ts +60 -3
- package/src/cli/handlers/ci-evidence.ts +167 -0
- package/src/cli/handlers/components.ts +3 -3
- package/src/cli/handlers/dead-guards.ts +37 -0
- package/src/cli/handlers/graph.ts +4 -4
- package/src/cli/handlers/lifecycle.ts +5 -4
- package/src/cli/handlers/local-chant.ts +106 -0
- package/src/cli/handlers/operator.ts +2 -2
- package/src/cli/handlers/search.ts +5 -5
- package/src/cli/main.test.ts +7 -0
- package/src/cli/main.ts +38 -0
- package/src/cli/parse-args.ts +21 -0
- package/src/cli/registry.ts +16 -0
- package/src/components/check-evidence-stage.test.ts +80 -0
- package/src/components/check-hooks.ts +84 -4
- package/src/components/check-local-evidence-stage.test.ts +141 -0
- package/src/components/check-steps.ts +56 -6
- package/src/components/product-pipeline.ts +28 -3
- package/src/components/product-plan.test.ts +87 -0
- package/src/components/product-plan.ts +230 -0
- package/src/components/release-compile.ts +2 -2
- package/src/config.ts +44 -3
- package/src/dead-guards.test.ts +206 -0
- package/src/dead-guards.ts +446 -0
- package/src/discovery/files.ts +5 -3
- package/src/drift-issue-bundle.test.ts +31 -0
- package/src/drift-issue.test.ts +206 -0
- package/src/drift-issue.ts +274 -0
- package/src/fit-note.test.ts +44 -0
- package/src/floor-table.data.ts +181 -0
- package/src/floor-table.ts +77 -0
- package/src/forge-flags.test.ts +110 -0
- package/src/forge-flags.ts +208 -0
- package/src/gate-decision-bundle.test.ts +1 -0
- package/src/lexicon.ts +68 -0
- package/src/lifecycle/gated-op.ts +10 -5
- package/src/local-chant.e2e.test.ts +94 -0
- package/src/local-chant.test.ts +162 -0
- package/src/local-chant.ts +341 -0
- package/src/op/activities/predict-behaviour.ts +2 -2
- package/src/op/op-waves-evidence-spec.ts +24 -0
- package/src/op/op-waves-evidence.test.ts +130 -0
- package/src/op/op-waves-evidence.ts +58 -0
- package/src/op/op-waves-run.ts +25 -1
- package/src/op/op-waves.ts +31 -1
- package/src/op/schedule-overdue.test.ts +85 -0
- package/src/op/schedule-overdue.ts +162 -26
- package/src/plan-summary.ts +40 -0
- package/src/pr-plan-note.ts +34 -0
- package/src/pr-post-plan-note.test.ts +146 -0
- package/src/pr-post.ts +127 -3
- package/src/release-floors.ts +47 -1
- package/src/stage-hooks-bundle.test.ts +1 -0
- package/src/state-migration.test.ts +52 -1
- package/src/state-migration.ts +107 -5
- package/src/workspace/ci-green.ts +4 -3
- package/src/workspace/lexicon-outputs.ts +23 -6
- package/src/workspace/reason-codes.test.ts +3 -0
- package/src/workspace/reason-codes.ts +4 -0
- package/src/workspace/ref-gate.schema.json +85 -0
- package/src/workspace/trust/check-evidence-report.ts +115 -0
- package/src/workspace/trust/check-evidence.test.ts +133 -0
- package/src/workspace/trust/check-evidence.ts +327 -0
- package/src/workspace/trust/ci-tag-statement.ts +2 -2
- package/src/workspace/trust/dsse.ts +48 -4
- package/src/workspace/trust/evidence.ts +2 -2
- package/src/workspace/trust/keyless-fixture.ts +214 -0
- package/src/workspace/trust/keyless.test.ts +220 -0
- package/src/workspace/trust/keyless.ts +557 -0
- package/src/workspace/trust/local-evidence.test.ts +127 -0
- package/src/workspace/trust/local-evidence.ts +268 -0
- package/src/workspace/trust/op-run-statement.ts +2 -2
- package/src/workspace/trust/policy.ts +263 -3
- package/src/workspace/trust/ref-gate-cli.ts +244 -0
- package/src/workspace/trust/ref-gate-fixture.ts +125 -0
- package/src/workspace/trust/ref-gate.e2e.test.ts +76 -0
- package/src/workspace/trust/ref-gate.test.ts +247 -0
- package/src/workspace/trust/ref-gate.ts +539 -0
- package/src/workspace/trust/run-statement.ts +3 -3
- package/src/workspace/trust/runner-identity.ts +92 -0
- package/src/workspace/trust/trust-files.test.ts +22 -0
- package/src/workspace/trust/trust-files.ts +54 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `chant dev dead-guards` (#4792) on fixture products: each feature-detect
|
|
3
|
+
* shape the products use, found when the pin meets its floor and kept when it
|
|
4
|
+
* does not, and the uses that are not checks left alone.
|
|
5
|
+
*/
|
|
6
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { describe, expect, it } from "vitest";
|
|
10
|
+
import { deadGuardLine, findDeadGuards, guardsIn, insideTry, pinOf, scanSource } from "./dead-guards";
|
|
11
|
+
import { chantMeets, compareChantVersions, FLOOR_TABLE, floorVersion, type FloorRow } from "./floor-table";
|
|
12
|
+
import { NEXT_RELEASE } from "./release-floors";
|
|
13
|
+
|
|
14
|
+
const TABLE: FloorRow[] = [
|
|
15
|
+
{ name: "CI_CHECK_HOOK_FLOOR", version: "0.124.55", gates: "", file: "op/op-waves.ts", modules: ["@intentius/chant/op", "@intentius/chant/components/check-hooks"] },
|
|
16
|
+
{ name: "TEMPLATE_REQUEST_FLOOR", version: "0.124.58", gates: "", file: "template-request/template-request.ts", modules: ["@intentius/chant/template-request"] },
|
|
17
|
+
{ name: "CI_COMMIT_FLOOR", version: "0.124.71", gates: "", file: "ci-commit.ts", modules: ["@intentius/chant/ci-commit"] },
|
|
18
|
+
{ name: "CI_AGENT_FLOOR", version: "0.124.55", gates: "", file: "ci-agent.ts", modules: ["@intentius/chant/ci-agent", "@intentius/chant/ci-product"] },
|
|
19
|
+
{ name: "RELEASE_VERSION_FLOOR", version: "0.124.55", gates: "", file: "lifecycle/release-ledger.ts", modules: ["@intentius/chant/lifecycle/release-ledger"] },
|
|
20
|
+
{ name: "RELEASE_VERSION_FLOOR", version: "0.124.79", gates: "", file: "lifecycle/release-version.ts", modules: ["@intentius/chant/lifecycle/release-version"] },
|
|
21
|
+
{ name: "NEW_THING_FLOOR", version: "0.124.85", gates: "", file: "release-floors.ts", modules: ["@intentius/chant/new-thing"] },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/** A chant-next.ts in the shapes terragucci, plywood and sql-yodeler write. */
|
|
25
|
+
const CHANT_NEXT = `import { CI_AGENT_FLOOR, productPipelineJobs } from "@intentius/chant/ci-product";
|
|
26
|
+
import * as ci from "@intentius/chant/ci-agent";
|
|
27
|
+
|
|
28
|
+
export interface ChantCheckHooks {
|
|
29
|
+
CI_CHECK_HOOK_FLOOR: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let hooks: Promise<ChantCheckHooks | undefined> | undefined;
|
|
33
|
+
export function chantCheckHooks() {
|
|
34
|
+
hooks ??= (async () => {
|
|
35
|
+
const specifier = "@intentius/chant/components/check-hooks";
|
|
36
|
+
try {
|
|
37
|
+
return (await import(specifier)) as ChantCheckHooks;
|
|
38
|
+
} catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
})();
|
|
42
|
+
return hooks;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function templates() {
|
|
46
|
+
const m = (await import("@intentius/chant/template-request").catch(() => ({}))) as Record<string, unknown>;
|
|
47
|
+
return typeof m.TEMPLATE_REQUEST_FLOOR === "string" ? m : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function commits() {
|
|
51
|
+
const m = (await import("@intentius/chant/ci-commit")) as { CI_COMMIT_FLOOR?: string };
|
|
52
|
+
return m.CI_COMMIT_FLOOR !== undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// A comment naming m.CI_CHECK_HOOK_FLOOR is not a check.
|
|
56
|
+
export const AGENT = { CI_AGENT_FLOOR, jobs: productPipelineJobs, floor: ci.CI_AGENT_FLOOR };
|
|
57
|
+
export const NAMES = ["CI_CHECK_HOOK_FLOOR"];
|
|
58
|
+
|
|
59
|
+
export async function run() {
|
|
60
|
+
try {
|
|
61
|
+
return await import("@intentius/chant/op");
|
|
62
|
+
} finally {
|
|
63
|
+
// nothing caught
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function newThing(m: Record<string, unknown>) {
|
|
68
|
+
return "NEW_THING_FLOOR" in m;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// chant-dead-guards: keep (a chant a user installs beside the product)
|
|
72
|
+
export const kept = (m: { CI_CHECK_HOOK_FLOOR?: string }) => m.CI_CHECK_HOOK_FLOOR;
|
|
73
|
+
export const keptToo = (m: { CI_CHECK_HOOK_FLOOR?: string }) => m.CI_CHECK_HOOK_FLOOR; // chant-dead-guards: keep
|
|
74
|
+
|
|
75
|
+
export const CHANT_CI_FLOOR = "0.124.50";
|
|
76
|
+
export const ledger = (m: { RELEASE_VERSION_FLOOR?: string }) => m.RELEASE_VERSION_FLOOR;
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
function product(files: Record<string, string>): string {
|
|
80
|
+
const dir = mkdtempSync(join(tmpdir(), "dead-guards-"));
|
|
81
|
+
for (const [file, text] of Object.entries(files)) {
|
|
82
|
+
mkdirSync(dirname(join(dir, file)), { recursive: true });
|
|
83
|
+
writeFileSync(join(dir, file), text);
|
|
84
|
+
}
|
|
85
|
+
return dir;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const pkg = (range: string) => JSON.stringify({ name: "p", dependencies: { "@intentius/chant": range } });
|
|
89
|
+
|
|
90
|
+
describe("guardsIn (#4792)", () => {
|
|
91
|
+
it("finds each feature-detect shape and leaves uses, comments, try/finally and kept lines alone", () => {
|
|
92
|
+
const found = guardsIn(CHANT_NEXT, TABLE).map((g) => `${g.line} ${g.kind} ${g.floor} ${g.version}`);
|
|
93
|
+
expect(found).toEqual([
|
|
94
|
+
"5 floor CI_CHECK_HOOK_FLOOR 0.124.55",
|
|
95
|
+
"11 module @intentius/chant/components/check-hooks 0.124.55",
|
|
96
|
+
"22 module @intentius/chant/template-request 0.124.58",
|
|
97
|
+
"23 floor TEMPLATE_REQUEST_FLOOR 0.124.58",
|
|
98
|
+
"27 floor CI_COMMIT_FLOOR 0.124.71",
|
|
99
|
+
"28 floor CI_COMMIT_FLOOR 0.124.71",
|
|
100
|
+
"44 floor NEW_THING_FLOOR 0.124.85",
|
|
101
|
+
"51 product-floor CHANT_CI_FLOOR 0.124.50",
|
|
102
|
+
"52 floor RELEASE_VERSION_FLOOR 0.124.79",
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("blanks comments but keeps strings, template text and regular expressions apart", () => {
|
|
107
|
+
const src = 'const a = "x // y"; // gone\nconst r = /"/; const t = `a ${b} c`; /* gone */';
|
|
108
|
+
const { code, inString } = scanSource(src);
|
|
109
|
+
expect(code).toContain('"x // y"');
|
|
110
|
+
expect(code).not.toContain("gone");
|
|
111
|
+
expect(inString[src.indexOf("x //")]).toBe(1);
|
|
112
|
+
expect(inString[src.indexOf("b}")]).toBe(0);
|
|
113
|
+
expect(inString[src.indexOf(" c`")]).toBe(1);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("counts a try only when it has a catch", () => {
|
|
117
|
+
const code = "try { await import(x) } catch { }\ntry { await import(y) } finally { }";
|
|
118
|
+
expect(insideTry(code, code.indexOf("import(x)"))).toBe(true);
|
|
119
|
+
expect(insideTry(code, code.indexOf("import(y)"))).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("findDeadGuards (#4792)", () => {
|
|
124
|
+
it("fails naming each guard the package.json pin meets, and skips tests, node_modules and builds", () => {
|
|
125
|
+
const dir = product({
|
|
126
|
+
"package.json": pkg("^0.124.60"),
|
|
127
|
+
"src/chant-next.ts": CHANT_NEXT,
|
|
128
|
+
"src/chant-next.test.ts": CHANT_NEXT,
|
|
129
|
+
"node_modules/x/index.js": CHANT_NEXT,
|
|
130
|
+
"dist/cli.js": CHANT_NEXT,
|
|
131
|
+
});
|
|
132
|
+
const r = findDeadGuards({ productDir: dir, table: TABLE });
|
|
133
|
+
expect(r.ok).toBe(false);
|
|
134
|
+
expect(r.files).toBe(1);
|
|
135
|
+
expect(r.pins).toEqual([{ file: "package.json", pin: "0.124.60" }]);
|
|
136
|
+
expect(r.dead.map((g) => `${g.file}:${g.line} ${g.floor}`)).toEqual([
|
|
137
|
+
"src/chant-next.ts:5 CI_CHECK_HOOK_FLOOR",
|
|
138
|
+
"src/chant-next.ts:11 @intentius/chant/components/check-hooks",
|
|
139
|
+
"src/chant-next.ts:22 @intentius/chant/template-request",
|
|
140
|
+
"src/chant-next.ts:23 TEMPLATE_REQUEST_FLOOR",
|
|
141
|
+
"src/chant-next.ts:51 CHANT_CI_FLOOR",
|
|
142
|
+
]);
|
|
143
|
+
expect(r.live.map((g) => `${g.line} ${g.floor}`)).toEqual(["27 CI_COMMIT_FLOOR", "28 CI_COMMIT_FLOOR", "44 NEW_THING_FLOOR", "52 RELEASE_VERSION_FLOOR"]);
|
|
144
|
+
expect(deadGuardLine(r.dead[1])).toBe("src/chant-next.ts:11: loads @intentius/chant/components/check-hooks behind a catch, which chant 0.124.55 has; the pin 0.124.60 meets it");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("passes when the pin meets none, and reads a nested package's own pin", () => {
|
|
148
|
+
const dir = product({
|
|
149
|
+
"package.json": pkg("0.124.54"),
|
|
150
|
+
"src/a.ts": "export const f = (m: { CI_CHECK_HOOK_FLOOR?: string }) => m.CI_CHECK_HOOK_FLOOR;\n",
|
|
151
|
+
"example/package.json": pkg("0.124.80"),
|
|
152
|
+
"example/b.ts": "export const g = (m: { CI_CHECK_HOOK_FLOOR?: string }) => m.CI_CHECK_HOOK_FLOOR;\n",
|
|
153
|
+
"docs/c.ts": "export const h = 1;\n",
|
|
154
|
+
});
|
|
155
|
+
const r = findDeadGuards({ productDir: dir, table: TABLE });
|
|
156
|
+
expect(r.dead.map((g) => `${g.file}:${g.line} ${g.pin}`)).toEqual(["example/b.ts:1 0.124.80"]);
|
|
157
|
+
expect(r.live.map((g) => `${g.file}:${g.line} ${g.pin}`)).toEqual(["src/a.ts:1 0.124.54"]);
|
|
158
|
+
const none = findDeadGuards({ productDir: product({ "package.json": pkg("0.124.54"), "src/a.ts": "export const f = (m: { CI_CHECK_HOOK_FLOOR?: string }) => m.CI_CHECK_HOOK_FLOOR;\n" }), table: TABLE });
|
|
159
|
+
expect(none.ok).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("checks every file against --chant-version in place of the pins", () => {
|
|
163
|
+
const dir = product({ "package.json": pkg("0.124.54"), "src/chant-next.ts": CHANT_NEXT });
|
|
164
|
+
const r = findDeadGuards({ productDir: dir, table: TABLE, chant: "0.124.84" });
|
|
165
|
+
expect(r.dead.map((g) => `${g.line} ${g.floor}`)).toEqual([
|
|
166
|
+
"5 CI_CHECK_HOOK_FLOOR",
|
|
167
|
+
"11 @intentius/chant/components/check-hooks",
|
|
168
|
+
"22 @intentius/chant/template-request",
|
|
169
|
+
"23 TEMPLATE_REQUEST_FLOOR",
|
|
170
|
+
"27 CI_COMMIT_FLOOR",
|
|
171
|
+
"28 CI_COMMIT_FLOOR",
|
|
172
|
+
"51 CHANT_CI_FLOOR",
|
|
173
|
+
"52 RELEASE_VERSION_FLOOR",
|
|
174
|
+
]);
|
|
175
|
+
expect(r.live.map((g) => g.floor)).toEqual(["NEW_THING_FLOOR"]);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("reads the lowest version a range admits, and none from a workspace or file range", () => {
|
|
179
|
+
expect(pinOf("^0.124.78")).toBe("0.124.78");
|
|
180
|
+
expect(pinOf(">=0.124.55 <0.125.0")).toBe("0.124.55");
|
|
181
|
+
expect(pinOf("workspace:*")).toBeUndefined();
|
|
182
|
+
expect(pinOf("file:../chant")).toBeUndefined();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("the floor table (#4792)", () => {
|
|
187
|
+
it("resolves a floor not yet released to the next release", () => {
|
|
188
|
+
expect(floorVersion("FLOOR_TABLE_FLOOR")).toBe(NEXT_RELEASE);
|
|
189
|
+
expect(chantMeets("0.124.84", "FLOOR_TABLE_FLOOR")).toBe(compareChantVersions("0.124.84", NEXT_RELEASE) >= 0);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("answers a released floor, and the higher version of a name two files declare", () => {
|
|
193
|
+
expect(floorVersion("CI_CHECK_HOOK_FLOOR")).toBe("0.124.55");
|
|
194
|
+
expect(chantMeets("0.124.84", "CI_CHECK_HOOK_FLOOR")).toBe(true);
|
|
195
|
+
expect(chantMeets("0.124.54", "CI_CHECK_HOOK_FLOOR")).toBe(false);
|
|
196
|
+
expect(FLOOR_TABLE.filter((r) => r.name === "RELEASE_VERSION_FLOOR").map((r) => r.version).sort()).toEqual(["0.124.55", "0.124.79"]);
|
|
197
|
+
expect(floorVersion("RELEASE_VERSION_FLOOR")).toBe("0.124.79");
|
|
198
|
+
expect(floorVersion("NO_SUCH_FLOOR")).toBeUndefined();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("names the subpaths a product imports a floor from", () => {
|
|
202
|
+
const stand = FLOOR_TABLE.find((r) => r.name === "OP_WAVE_STAND_DOWN_FLOOR")!;
|
|
203
|
+
expect(stand.modules).toContain("@intentius/chant/op");
|
|
204
|
+
expect(stand.issue).toBe("#4581");
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@intentius/chant/dead-guards` (#4792): find each feature-detect in a
|
|
3
|
+
* product's source whose chant floor the product's pin already meets.
|
|
4
|
+
*
|
|
5
|
+
* A product that runs on several chant releases checks for a part before it
|
|
6
|
+
* calls it, and keeps a fallback for a chant without it. Once the product
|
|
7
|
+
* pins a chant that has the part, the check always passes and the fallback is
|
|
8
|
+
* dead code that still gets tested and kept. `chant dev dead-guards` reads
|
|
9
|
+
* the pin from the product's package.json files, scans the product's source
|
|
10
|
+
* and fails naming every such check, so the product deletes it.
|
|
11
|
+
*
|
|
12
|
+
* What counts as a feature-detect on a floor, from the table in
|
|
13
|
+
* ./floor-table.ts:
|
|
14
|
+
*
|
|
15
|
+
* - `floor`: a floor's name read off a module the product did not import
|
|
16
|
+
* statically (`m.CI_CHECK_HOOK_FLOOR`, `typeof m.X_FLOOR === "string"`), in
|
|
17
|
+
* a string (`exportsAll(spec, ["OP_WAVE_STAND_DOWN_FLOOR"])`), as a member
|
|
18
|
+
* of a type the product writes out for a lazily loaded chant module
|
|
19
|
+
* (`CI_CONTROL_FLOOR: string;`), or destructured from a dynamic import. A
|
|
20
|
+
* floor the file imports statically is the product using the part, so it
|
|
21
|
+
* is not a check.
|
|
22
|
+
* - `module`: a chant subpath loaded with `import()` where a `catch` is near
|
|
23
|
+
* (`try { await import(specifier) } catch { return undefined }`, or
|
|
24
|
+
* `.catch(`), the shape of a chant-next.ts loader. Its floor is the lowest
|
|
25
|
+
* floor the subpath serves: a chant with that floor has the subpath.
|
|
26
|
+
* - `product-floor`: a product's own constant for the chant it needs,
|
|
27
|
+
* `const CHANT_CI_FLOOR = "0.124.67"`, whose checks the pin meets.
|
|
28
|
+
*
|
|
29
|
+
* A line holding `chant-dead-guards: keep`, or below a comment line that
|
|
30
|
+
* does, is left out: a check kept on purpose, such as one for a chant a user
|
|
31
|
+
* installs beside the product.
|
|
32
|
+
*
|
|
33
|
+
* This module imports only Node's own modules and the floor table.
|
|
34
|
+
*/
|
|
35
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
36
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
37
|
+
import { compareChantVersions, FLOOR_TABLE, type FloorRow } from "./floor-table";
|
|
38
|
+
|
|
39
|
+
/** One feature-detect the pin meets. */
|
|
40
|
+
export interface DeadGuard {
|
|
41
|
+
/** The file, relative to the product's directory, with `/` separators. */
|
|
42
|
+
file: string;
|
|
43
|
+
/** The line, from 1. */
|
|
44
|
+
line: number;
|
|
45
|
+
kind: "floor" | "module" | "product-floor";
|
|
46
|
+
/** The floor's name, or the subpath for `module`. */
|
|
47
|
+
floor: string;
|
|
48
|
+
/** The floor's version. */
|
|
49
|
+
version: string;
|
|
50
|
+
/** The chant pin that meets it. */
|
|
51
|
+
pin: string;
|
|
52
|
+
/** The line's text, trimmed. */
|
|
53
|
+
text: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface DeadGuardsOptions {
|
|
57
|
+
/** The product's root. */
|
|
58
|
+
productDir: string;
|
|
59
|
+
/** Check every file against this chant version, in place of the package.json pins. */
|
|
60
|
+
chant?: string;
|
|
61
|
+
/** The floor table; chant's own by default. */
|
|
62
|
+
table?: readonly FloorRow[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface DeadGuardsResult {
|
|
66
|
+
ok: boolean;
|
|
67
|
+
/** Each check the pin meets, by file and line. */
|
|
68
|
+
dead: DeadGuard[];
|
|
69
|
+
/** Each check the pin does not meet yet, the same shape, with `pin` the pin that falls short. */
|
|
70
|
+
live: DeadGuard[];
|
|
71
|
+
/** Each package.json with a chant pin and the version it pins at least. */
|
|
72
|
+
pins: { file: string; pin: string }[];
|
|
73
|
+
/** The number of source files read. */
|
|
74
|
+
files: number;
|
|
75
|
+
/** Source files with no chant pin above them, which no check was made in. */
|
|
76
|
+
unpinned: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const SKIP_DIRS = new Set(["node_modules", "dist", "build", "out", "coverage", "test", "tests", "__tests__", "__fixtures__", "fixtures"]);
|
|
80
|
+
const SOURCE = /\.(?:[cm]?[jt]s|tsx)$/;
|
|
81
|
+
const NOT_SOURCE = /\.d\.[cm]?ts$|\.(?:test|spec|e2e\.test)\.[cm]?[jt]sx?$/;
|
|
82
|
+
const CHANT_DEPS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"] as const;
|
|
83
|
+
const KEEP = "chant-dead-guards: keep";
|
|
84
|
+
|
|
85
|
+
/** The lowest version a chant range admits: `^0.124.78` is 0.124.78. Undefined for `workspace:`, `file:` and the like. */
|
|
86
|
+
export function pinOf(range: string): string | undefined {
|
|
87
|
+
const r = range.trim();
|
|
88
|
+
if (/^(?:workspace|file|link|git|github|https?):/.test(r)) return undefined;
|
|
89
|
+
return /(\d+\.\d+\.\d+)/.exec(r)?.[1];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The `@intentius/chant` range a package.json names, if any. */
|
|
93
|
+
function chantRange(file: string): string | undefined {
|
|
94
|
+
try {
|
|
95
|
+
const pkg = JSON.parse(readFileSync(file, "utf8")) as Record<string, Record<string, string> | undefined>;
|
|
96
|
+
for (const k of CHANT_DEPS) {
|
|
97
|
+
const v = pkg[k]?.["@intentius/chant"];
|
|
98
|
+
if (typeof v === "string") return v;
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
// not JSON; no pin
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The product's source files, relative, skipping dependencies, builds, tests and hidden directories. */
|
|
107
|
+
export function productSources(productDir: string): string[] {
|
|
108
|
+
const out: string[] = [];
|
|
109
|
+
const walk = (dir: string) => {
|
|
110
|
+
let names: string[];
|
|
111
|
+
try {
|
|
112
|
+
names = readdirSync(dir).sort();
|
|
113
|
+
} catch {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
for (const name of names) {
|
|
117
|
+
if (name.startsWith(".")) continue;
|
|
118
|
+
const at = join(dir, name);
|
|
119
|
+
let st;
|
|
120
|
+
try {
|
|
121
|
+
st = statSync(at);
|
|
122
|
+
} catch {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (st.isDirectory()) {
|
|
126
|
+
if (!SKIP_DIRS.has(name)) walk(at);
|
|
127
|
+
} else if (SOURCE.test(name) && !NOT_SOURCE.test(name)) {
|
|
128
|
+
out.push(relative(productDir, at).split(sep).join("/"));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
walk(productDir);
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* `src` with every comment blanked to spaces (line breaks kept), and a mask
|
|
138
|
+
* of which characters sit inside a string or template literal's text.
|
|
139
|
+
*/
|
|
140
|
+
export function scanSource(src: string): { code: string; inString: Uint8Array } {
|
|
141
|
+
const out = src.split("");
|
|
142
|
+
const inString = new Uint8Array(src.length);
|
|
143
|
+
// Each open template literal's brace depth at its `${`.
|
|
144
|
+
const templates: number[] = [];
|
|
145
|
+
let depth = 0;
|
|
146
|
+
let prev = ""; // the last significant character outside strings and comments
|
|
147
|
+
let i = 0;
|
|
148
|
+
const blank = (from: number, to: number) => {
|
|
149
|
+
for (let k = from; k < to; k++) if (out[k] !== "\n") out[k] = " ";
|
|
150
|
+
};
|
|
151
|
+
const quoted = (q: string) => {
|
|
152
|
+
const start = i++;
|
|
153
|
+
while (i < src.length && src[i] !== q && src[i] !== "\n") {
|
|
154
|
+
if (src[i] === "\\") i++;
|
|
155
|
+
i++;
|
|
156
|
+
}
|
|
157
|
+
for (let k = start + 1; k < i && k < src.length; k++) inString[k] = 1;
|
|
158
|
+
i++;
|
|
159
|
+
};
|
|
160
|
+
// Template text from i (just past "`" or "}") to the closing "`" or a "${".
|
|
161
|
+
const templateText = () => {
|
|
162
|
+
const start = i;
|
|
163
|
+
while (i < src.length) {
|
|
164
|
+
if (src[i] === "\\") {
|
|
165
|
+
i += 2;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (src[i] === "`") {
|
|
169
|
+
for (let k = start; k < i; k++) inString[k] = 1;
|
|
170
|
+
i++;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (src[i] === "$" && src[i + 1] === "{") {
|
|
174
|
+
for (let k = start; k < i; k++) inString[k] = 1;
|
|
175
|
+
i += 2;
|
|
176
|
+
templates.push(depth);
|
|
177
|
+
depth++;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
i++;
|
|
181
|
+
}
|
|
182
|
+
for (let k = start; k < src.length; k++) inString[k] = 1;
|
|
183
|
+
};
|
|
184
|
+
while (i < src.length) {
|
|
185
|
+
const c = src[i];
|
|
186
|
+
const n = src[i + 1];
|
|
187
|
+
if (c === "/" && n === "/") {
|
|
188
|
+
const end = src.indexOf("\n", i);
|
|
189
|
+
const to = end === -1 ? src.length : end;
|
|
190
|
+
blank(i, to);
|
|
191
|
+
i = to;
|
|
192
|
+
} else if (c === "/" && n === "*") {
|
|
193
|
+
const end = src.indexOf("*/", i + 2);
|
|
194
|
+
const to = end === -1 ? src.length : end + 2;
|
|
195
|
+
blank(i, to);
|
|
196
|
+
i = to;
|
|
197
|
+
} else if (c === '"' || c === "'") {
|
|
198
|
+
quoted(c);
|
|
199
|
+
prev = "a";
|
|
200
|
+
} else if (c === "`") {
|
|
201
|
+
i++;
|
|
202
|
+
templateText();
|
|
203
|
+
prev = "a";
|
|
204
|
+
} else if (c === "/" && (prev === "" || "(,=:[!&|?{};+-*%<>~^".includes(prev))) {
|
|
205
|
+
// A regular expression literal.
|
|
206
|
+
i++;
|
|
207
|
+
let cls = false;
|
|
208
|
+
while (i < src.length && src[i] !== "\n") {
|
|
209
|
+
if (src[i] === "\\") i++;
|
|
210
|
+
else if (src[i] === "[") cls = true;
|
|
211
|
+
else if (src[i] === "]") cls = false;
|
|
212
|
+
else if (src[i] === "/" && !cls) break;
|
|
213
|
+
i++;
|
|
214
|
+
}
|
|
215
|
+
i++;
|
|
216
|
+
prev = "a";
|
|
217
|
+
} else {
|
|
218
|
+
if (c === "{") depth++;
|
|
219
|
+
else if (c === "}") {
|
|
220
|
+
depth--;
|
|
221
|
+
if (templates.length > 0 && templates[templates.length - 1] === depth) {
|
|
222
|
+
templates.pop();
|
|
223
|
+
i++;
|
|
224
|
+
templateText();
|
|
225
|
+
prev = "a";
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!/\s/.test(c)) prev = /[A-Za-z0-9_$)\]]/.test(c) ? "a" : c;
|
|
230
|
+
i++;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { code: out.join(""), inString };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** The line of each offset, from 1. */
|
|
237
|
+
function lineStarts(src: string): number[] {
|
|
238
|
+
const starts = [0];
|
|
239
|
+
for (let i = 0; i < src.length; i++) if (src[i] === "\n") starts.push(i + 1);
|
|
240
|
+
return starts;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function lineAt(starts: number[], offset: number): number {
|
|
244
|
+
let lo = 0;
|
|
245
|
+
let hi = starts.length - 1;
|
|
246
|
+
while (lo < hi) {
|
|
247
|
+
const mid = (lo + hi + 1) >> 1;
|
|
248
|
+
if (starts[mid] <= offset) lo = mid;
|
|
249
|
+
else hi = mid - 1;
|
|
250
|
+
}
|
|
251
|
+
return lo + 1;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Whether `offset` in `code` (comments blanked) sits inside a `try { ... }`
|
|
256
|
+
* block that has a `catch`: an open brace before it, not yet closed, that
|
|
257
|
+
* follows `try`, and whose block is followed by `catch`. A `try` with only
|
|
258
|
+
* `finally` catches nothing. Braces in strings count too, which a template
|
|
259
|
+
* literal can throw off; the check is a heuristic, and
|
|
260
|
+
* `chant-dead-guards: keep` covers a miss.
|
|
261
|
+
*/
|
|
262
|
+
export function insideTry(code: string, offset: number): boolean {
|
|
263
|
+
let depth = 0;
|
|
264
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
265
|
+
const c = code[i];
|
|
266
|
+
if (c === "}") depth++;
|
|
267
|
+
else if (c === "{") {
|
|
268
|
+
if (depth > 0) depth--;
|
|
269
|
+
else if (/\btry\s*$/.test(code.slice(Math.max(0, i - 8), i))) {
|
|
270
|
+
let open = 0;
|
|
271
|
+
for (let j = i; j < code.length; j++) {
|
|
272
|
+
if (code[j] === "{") open++;
|
|
273
|
+
else if (code[j] === "}" && --open === 0) {
|
|
274
|
+
if (/^\s*catch\b/.test(code.slice(j + 1, j + 24))) return true;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** One finding in one file, before the pin is applied. */
|
|
285
|
+
export interface Guard {
|
|
286
|
+
line: number;
|
|
287
|
+
kind: DeadGuard["kind"];
|
|
288
|
+
floor: string;
|
|
289
|
+
version: string;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The feature-detects on a floor in one source file. `table` gives each
|
|
294
|
+
* floor's version; a name two files declare takes the higher one.
|
|
295
|
+
*/
|
|
296
|
+
export function guardsIn(src: string, table: readonly FloorRow[] = FLOOR_TABLE): Guard[] {
|
|
297
|
+
const floors = new Map<string, string>();
|
|
298
|
+
for (const r of table) {
|
|
299
|
+
const had = floors.get(r.name);
|
|
300
|
+
if (had === undefined || compareChantVersions(r.version, had) > 0) floors.set(r.name, r.version);
|
|
301
|
+
}
|
|
302
|
+
// The lowest floor each subpath serves.
|
|
303
|
+
const modules = new Map<string, string>();
|
|
304
|
+
for (const r of table) {
|
|
305
|
+
for (const m of r.modules) {
|
|
306
|
+
const had = modules.get(m);
|
|
307
|
+
if (had === undefined || compareChantVersions(r.version, had) < 0) modules.set(m, r.version);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const { code, inString } = scanSource(src);
|
|
311
|
+
const codeLines = code.split("\n");
|
|
312
|
+
const starts = lineStarts(src);
|
|
313
|
+
const lines = src.split("\n");
|
|
314
|
+
const out: Guard[] = [];
|
|
315
|
+
const seen = new Set<string>();
|
|
316
|
+
const push = (offset: number, kind: Guard["kind"], floor: string, version: string) => {
|
|
317
|
+
const line = lineAt(starts, offset);
|
|
318
|
+
const key = `${line}|${kind}|${floor}`;
|
|
319
|
+
if (seen.has(key)) return;
|
|
320
|
+
if (lines[line - 1]?.includes(KEEP) || /^\s*(?:\/\/|\/?\*).*chant-dead-guards: keep/.test(lines[line - 2] ?? "")) return;
|
|
321
|
+
seen.add(key);
|
|
322
|
+
out.push({ line, kind, floor, version });
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// Names bound by a static import or re-export, from anywhere: using one is
|
|
326
|
+
// using the part. And the spans of those statements, which hold no check.
|
|
327
|
+
const imported = new Set<string>();
|
|
328
|
+
const statements: [number, number][] = [];
|
|
329
|
+
for (const m of code.matchAll(/\b(?:import|export)\s+(?:type\s+)?\{([^}]*)\}\s*from\s*(["'])[^"'\n]*\2/g)) {
|
|
330
|
+
statements.push([m.index!, m.index! + m[0].length]);
|
|
331
|
+
if (!m[0].startsWith("import")) continue;
|
|
332
|
+
for (const part of m[1].split(",")) {
|
|
333
|
+
const local = part.trim().replace(/^type\s+/, "").split(/\s+as\s+/).pop()?.trim();
|
|
334
|
+
if (local) imported.add(local);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const inStatement = (at: number) => statements.some(([a, b]) => at >= a && at < b);
|
|
338
|
+
// `import * as chant from "@intentius/chant/x"`: `chant.X_FLOOR` is a use too.
|
|
339
|
+
const namespaces = new Set([...code.matchAll(/\bimport\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*["']/g)].map((m) => m[1]));
|
|
340
|
+
const declared = new Set([...code.matchAll(/\b(?:const|let|var|function|class)\s+([A-Z][A-Z0-9_]*_FLOOR)\b/g)].map((m) => m[1]));
|
|
341
|
+
|
|
342
|
+
// floor: a table name read off a module, in a string, as a type member, or destructured.
|
|
343
|
+
for (const m of code.matchAll(/\b[A-Z][A-Z0-9_]*_FLOOR\b/g)) {
|
|
344
|
+
const at = m.index!;
|
|
345
|
+
const name = m[0];
|
|
346
|
+
const version = floors.get(name);
|
|
347
|
+
if (version === undefined || inStatement(at)) continue;
|
|
348
|
+
if (inString[at]) {
|
|
349
|
+
// A name in a string is a check when the line asks a module for it:
|
|
350
|
+
// names a chant subpath or an import(), `"X_FLOOR" in m`, `m["X_FLOOR"]`.
|
|
351
|
+
const text = codeLines[lineAt(starts, at) - 1] ?? "";
|
|
352
|
+
if (/@intentius\/chant|\bimport\(|["'`]\s+in\s+[A-Za-z_$]/.test(text) || /[\w$\])]\s*\[\s*$/.test(code.slice(Math.max(0, at - 24), at - 1))) push(at, "floor", name, version);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const owner = /([A-Za-z_$][\w$]*)\s*\??\.\s*$/.exec(code.slice(Math.max(0, at - 64), at));
|
|
356
|
+
if (owner || /\.\s*$/.test(code.slice(Math.max(0, at - 2), at))) {
|
|
357
|
+
if (!owner || !namespaces.has(owner[1])) push(at, "floor", name, version);
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (imported.has(name) || declared.has(name)) continue;
|
|
361
|
+
push(at, "floor", name, version);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// module: a chant subpath loaded with import() inside a try, or with .catch( on the load.
|
|
365
|
+
for (const m of src.matchAll(/(["'`])(@intentius\/chant(?:\/[A-Za-z0-9_./-]+)?)\1/g)) {
|
|
366
|
+
const at = m.index!;
|
|
367
|
+
const spec = m[2];
|
|
368
|
+
const version = modules.get(spec);
|
|
369
|
+
if (version === undefined || inStatement(at) || !inString[at + 1]) continue;
|
|
370
|
+
const end = at + m[0].length;
|
|
371
|
+
let site: number | undefined;
|
|
372
|
+
if (/\bimport\(\s*$/.test(code.slice(Math.max(0, at - 16), at))) {
|
|
373
|
+
if (/typeof\s+import\(\s*$/.test(code.slice(Math.max(0, at - 24), at))) continue;
|
|
374
|
+
site = at;
|
|
375
|
+
} else {
|
|
376
|
+
// `const specifier = "<subpath>"`, then `import(specifier)` below.
|
|
377
|
+
const bound = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::\s*string\s*)?=\s*$/.exec(code.slice(Math.max(0, at - 64), at));
|
|
378
|
+
if (!bound) continue;
|
|
379
|
+
const call = new RegExp(`\\bimport\\(\\s*${bound[1].replace(/\$/g, "\\$")}\\s*\\)`).exec(code.slice(end, end + 2000));
|
|
380
|
+
if (!call) continue;
|
|
381
|
+
site = end + call.index;
|
|
382
|
+
}
|
|
383
|
+
const after = code.slice(site, site + 400);
|
|
384
|
+
const caught = /^[^;\n]*?\)\s*\.catch\(/.test(after) || insideTry(code, site);
|
|
385
|
+
if (caught) push(at, "module", spec, version);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// product-floor: the product's own constant for the chant it needs.
|
|
389
|
+
for (const m of code.matchAll(/\b(?:const|let|var)\s+([A-Z0-9_]*CHANT[A-Z0-9_]*_FLOOR)\s*(?::\s*string\s*)?=\s*/g)) {
|
|
390
|
+
const at = m.index! + m[0].length;
|
|
391
|
+
const lit = /^(["'])(\d+\.\d+\.\d+)\1/.exec(src.slice(at, at + 24));
|
|
392
|
+
if (lit) push(m.index!, "product-floor", m[1], lit[2]);
|
|
393
|
+
}
|
|
394
|
+
return out.sort((a, b) => a.line - b.line || a.floor.localeCompare(b.floor));
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** The chant pin over `file` (relative to `productDir`): the nearest package.json with one, up to the product's root. */
|
|
398
|
+
function pinFor(productDir: string, file: string, cache: Map<string, string | undefined | null>): { pin?: string; manifest?: string } {
|
|
399
|
+
for (let dir = dirname(join(productDir, file)); ; dir = dirname(dir)) {
|
|
400
|
+
const manifest = join(dir, "package.json");
|
|
401
|
+
let range = cache.get(manifest);
|
|
402
|
+
if (range === undefined) {
|
|
403
|
+
range = existsSync(manifest) ? (chantRange(manifest) ?? null) : null;
|
|
404
|
+
cache.set(manifest, range);
|
|
405
|
+
}
|
|
406
|
+
if (range !== null) return { pin: pinOf(range), manifest };
|
|
407
|
+
if (relative(productDir, dir) === "" || dir === dirname(dir)) return {};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Every feature-detect in the product at `productDir` and whether its pin
|
|
413
|
+
* meets it. `ok` is false when one does.
|
|
414
|
+
*/
|
|
415
|
+
export function findDeadGuards(options: DeadGuardsOptions): DeadGuardsResult {
|
|
416
|
+
const { productDir, chant } = options;
|
|
417
|
+
const table = options.table ?? FLOOR_TABLE;
|
|
418
|
+
const files = productSources(productDir);
|
|
419
|
+
const cache = new Map<string, string | undefined | null>();
|
|
420
|
+
const pins = new Map<string, string>();
|
|
421
|
+
const dead: DeadGuard[] = [];
|
|
422
|
+
const live: DeadGuard[] = [];
|
|
423
|
+
const unpinned: string[] = [];
|
|
424
|
+
for (const file of files) {
|
|
425
|
+
const at = pinFor(productDir, file, cache);
|
|
426
|
+
const pin = chant ?? at.pin;
|
|
427
|
+
if (at.manifest && at.pin) pins.set(relative(productDir, at.manifest).split(sep).join("/"), at.pin);
|
|
428
|
+
if (pin === undefined) {
|
|
429
|
+
unpinned.push(file);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const src = readFileSync(join(productDir, file), "utf8");
|
|
433
|
+
const lines = src.split("\n");
|
|
434
|
+
for (const g of guardsIn(src, table)) {
|
|
435
|
+
const row: DeadGuard = { file, ...g, pin, text: (lines[g.line - 1] ?? "").trim() };
|
|
436
|
+
(compareChantVersions(pin, g.version) >= 0 ? dead : live).push(row);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return { ok: dead.length === 0, dead, live, pins: [...pins].map(([file, pin]) => ({ file, pin })).sort((a, b) => a.file.localeCompare(b.file)), files: files.length, unpinned };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** One dead guard as a line: `file:line: ...`. */
|
|
443
|
+
export function deadGuardLine(g: DeadGuard): string {
|
|
444
|
+
const what = g.kind === "module" ? `loads ${g.floor} behind a catch` : g.kind === "product-floor" ? `declares ${g.floor}` : `checks ${g.floor}`;
|
|
445
|
+
return `${g.file}:${g.line}: ${what}, which chant ${g.version} has; the pin ${g.pin} meets it`;
|
|
446
|
+
}
|
package/src/discovery/files.ts
CHANGED
|
@@ -104,10 +104,12 @@ export function isSourceFileName(name: string): boolean {
|
|
|
104
104
|
* Find every TypeScript infrastructure file under `path`, through the one
|
|
105
105
|
* discovery walk (`./walk.ts`, #2527): test and spec files, chant-generated
|
|
106
106
|
* files, skip-marked files and whatever the walk's rules and the project's
|
|
107
|
-
* globs skip are left out.
|
|
107
|
+
* globs skip are left out. Walked from the root of a project with no
|
|
108
|
+
* `sourceDir`, its top-level `scripts/` is left out too (#4794,
|
|
109
|
+
* `ROOT_SOURCE_EXCLUDES`), unless `include` brings it back.
|
|
108
110
|
*/
|
|
109
111
|
export async function findInfraFiles(path: string, options?: FindInfraFilesOptions): Promise<string[]> {
|
|
110
|
-
const globs = options?.globs === null ? undefined : (options?.globs ?? (await resolveDiscoveryGlobs(path)));
|
|
112
|
+
const globs = options?.globs === null ? undefined : (options?.globs ?? (await resolveDiscoveryGlobs(path, { sourceWalk: true })));
|
|
111
113
|
return walkDiscovery({
|
|
112
114
|
walker: "source",
|
|
113
115
|
root: path,
|
|
@@ -125,7 +127,7 @@ export async function findInfraFiles(path: string, options?: FindInfraFilesOptio
|
|
|
125
127
|
* child projects and the project's `exclude` globs are left out for it too.
|
|
126
128
|
*/
|
|
127
129
|
export async function findSourceFiles(path: string, accept: (name: string, full: string) => boolean, options?: FindInfraFilesOptions): Promise<string[]> {
|
|
128
|
-
const globs = options?.globs === null ? undefined : (options?.globs ?? (await resolveDiscoveryGlobs(path)));
|
|
130
|
+
const globs = options?.globs === null ? undefined : (options?.globs ?? (await resolveDiscoveryGlobs(path, { sourceWalk: true })));
|
|
129
131
|
return walkDiscovery({
|
|
130
132
|
walker: "source",
|
|
131
133
|
root: path,
|