@mmmnt/feat-analyze 0.1.7

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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Moment
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,35 @@
1
+ import type { BuiltSpec } from "@mmmnt/feat-types";
2
+ export interface Finding {
3
+ level: "error" | "warn" | "info";
4
+ message: string;
5
+ }
6
+ /**
7
+ * Language-quality lint rules beyond audit's buildability gate (ADR-0007):
8
+ * declared-but-unused schemas, duplicate contract declarations, draft surfacing.
9
+ */
10
+ export declare function lintSpec(spec: BuiltSpec): Finding[];
11
+ export interface AuditResult {
12
+ /** ADR-0008 buildability failures (B1–B3) — block the agreed gate. */
13
+ errors: string[];
14
+ /** Lifecycle surfacing — advisory. */
15
+ warnings: string[];
16
+ }
17
+ /**
18
+ * Spec-level audit: B1 handler-for-code-types, B2 two-way rejects↔rejection
19
+ * traceability (outline placeholder IDs resolved through example rows), B3
20
+ * touches present, lifecycle status. File-pairing checks (contract/test
21
+ * existence) are filesystem concerns and live with the caller.
22
+ */
23
+ export declare function auditSpec(spec: BuiltSpec): AuditResult;
24
+ export interface FormatResult {
25
+ formatted: string;
26
+ changed: boolean;
27
+ /** Tabs in leading whitespace are a parse error; fmt reports, never rewrites. */
28
+ hasLeadingTab: boolean;
29
+ }
30
+ /**
31
+ * Conservative canonical formatting: trailing-whitespace strip, collapse >1
32
+ * blank line, ensure single trailing newline. Content-preserving, idempotent.
33
+ */
34
+ export declare function formatSource(source: string): FormatResult;
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;IACjC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,EAAE,CAmCnD;AAID,MAAM,WAAW,WAAW;IAC1B,sEAAsE;IACtE,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,sCAAsC;IACtC,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,SAAS,GAAG,WAAW,CAmCtD;AAED,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;IACjB,iFAAiF;IACjF,aAAa,EAAE,OAAO,CAAC;CACxB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAezD"}
package/dist/index.js ADDED
@@ -0,0 +1,121 @@
1
+ // @mmmnt/feat-analyze — pure analysis over parsed specs and spec source.
2
+ // No filesystem, no process, no CLI: everything here is (spec | source) → findings.
3
+ // The `feat lint/audit/fmt` commands are thin callers; embedders (services,
4
+ // editors) call these directly. Finding messages are the exact strings the CLI
5
+ // prints — extraction changed nothing observable.
6
+ /**
7
+ * Language-quality lint rules beyond audit's buildability gate (ADR-0007):
8
+ * declared-but-unused schemas, duplicate contract declarations, draft surfacing.
9
+ */
10
+ export function lintSpec(spec) {
11
+ const findings = [];
12
+ // ADR-0007: declared-but-unused schema = lint warning
13
+ const used = new Set();
14
+ for (const sc of spec.scenarios) {
15
+ if (sc.prediction.response)
16
+ used.add(sc.prediction.response.schemaName);
17
+ for (const svc of Object.values(sc.prediction.services ?? {})) {
18
+ for (const r of svc.records ?? [])
19
+ used.add(r.schemaName);
20
+ for (const r of svc.contains ?? [])
21
+ used.add(r.schemaName);
22
+ }
23
+ for (const seed of sc.given?.seeds ?? [])
24
+ if ("records" in seed)
25
+ for (const r of seed.records)
26
+ if (r.schemaName)
27
+ used.add(r.schemaName);
28
+ }
29
+ for (const entry of spec.contract) {
30
+ if (entry.kind === "stream")
31
+ continue;
32
+ // `input` is used by definition: it is the when-payload's contract.
33
+ if (entry.kind === "input")
34
+ continue;
35
+ if (!used.has(entry.schemaName))
36
+ findings.push({ level: "warn", message: `warn: schema '${entry.schemaName}' is declared but never referenced by a scenario` });
37
+ }
38
+ // Style: duplicate contract declarations
39
+ const seen = new Set();
40
+ for (const entry of spec.contract) {
41
+ if (entry.kind === "stream")
42
+ continue;
43
+ const key = `${entry.kind}:${entry.schemaName}`;
44
+ if (seen.has(key))
45
+ findings.push({ level: "warn", message: `warn: duplicate contract declaration '${entry.kind} ${entry.schemaName}'` });
46
+ seen.add(key);
47
+ }
48
+ if (spec.identity.status === "draft")
49
+ findings.push({ level: "info", message: "info: status draft — not buildable until agreed" });
50
+ return findings;
51
+ }
52
+ const CODE_TYPES = new Set(["command", "query", "policy", "projection", "saga"]);
53
+ /**
54
+ * Spec-level audit: B1 handler-for-code-types, B2 two-way rejects↔rejection
55
+ * traceability (outline placeholder IDs resolved through example rows), B3
56
+ * touches present, lifecycle status. File-pairing checks (contract/test
57
+ * existence) are filesystem concerns and live with the caller.
58
+ */
59
+ export function auditSpec(spec) {
60
+ const errors = [];
61
+ const warnings = [];
62
+ // B1: handler present for code-producing types
63
+ if (CODE_TYPES.has(spec.identity.type) && !spec.construct.some((c) => c.kind === "handler"))
64
+ errors.push("B1: no `handler at` for a code-producing spec type");
65
+ // B3: touches always required
66
+ if (!spec.construct.some((c) => c.kind === "touches"))
67
+ errors.push("B3: no `touches` change boundary declared");
68
+ // B2: two-way rejects ↔ predicted rejection IDs
69
+ const rejectsIds = new Set(spec.enforce.filter((e) => e.kind === "rejects").map((e) => e.rejectionId));
70
+ const predictedIds = new Set();
71
+ for (const sc of spec.scenarios) {
72
+ const rid = sc.prediction.rejectionId;
73
+ if (!rid)
74
+ continue;
75
+ const ph = /^<(.+)>$/.exec(rid);
76
+ if (ph) {
77
+ for (const row of sc.examples ?? []) {
78
+ const v = row[ph[1]];
79
+ if (typeof v === "string")
80
+ predictedIds.add(v);
81
+ }
82
+ }
83
+ else
84
+ predictedIds.add(rid);
85
+ }
86
+ for (const id of predictedIds)
87
+ if (!rejectsIds.has(id))
88
+ errors.push(`B2: predicted rejection '${id}' has no \`rejects ${id} when …\` line`);
89
+ for (const id of rejectsIds)
90
+ if (!predictedIds.has(id))
91
+ errors.push(`B2: \`rejects ${id}\` is never predicted by any scenario`);
92
+ // Lifecycle
93
+ if (spec.identity.status === "draft")
94
+ warnings.push("status draft — not buildable until agreed");
95
+ return { errors, warnings };
96
+ }
97
+ /**
98
+ * Conservative canonical formatting: trailing-whitespace strip, collapse >1
99
+ * blank line, ensure single trailing newline. Content-preserving, idempotent.
100
+ */
101
+ export function formatSource(source) {
102
+ const hasLeadingTab = /^\t/m.test(source);
103
+ const lines = source.split(/\r?\n/).map((l) => l.replace(/[ \t]+$/, ""));
104
+ const out = [];
105
+ let blanks = 0;
106
+ for (const line of lines) {
107
+ if (line === "") {
108
+ blanks++;
109
+ if (blanks > 1)
110
+ continue;
111
+ }
112
+ else
113
+ blanks = 0;
114
+ out.push(line);
115
+ }
116
+ while (out.length > 0 && out[out.length - 1] === "")
117
+ out.pop();
118
+ const formatted = out.join("\n") + "\n";
119
+ return { formatted, changed: formatted !== source, hasLeadingTab };
120
+ }
121
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,oFAAoF;AACpF,4EAA4E;AAC5E,+EAA+E;AAC/E,kDAAkD;AASlD;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAe;IACtC,MAAM,QAAQ,GAAc,EAAE,CAAC;IAE/B,sDAAsD;IACtD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QAChC,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ;YAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACxE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE,CAA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;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC;IACzE,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,IAAI,KAAK,EAAE,EAAE,CAAC;YAChB,MAAM,EAAE,CAAC;YACT,IAAI,MAAM,GAAG,CAAC;gBAAE,SAAS;QAC3B,CAAC;;YAAM,MAAM,GAAG,CAAC,CAAC;QAClB,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;IACD,OAAO,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE;QAAE,GAAG,CAAC,GAAG,EAAE,CAAC;IAC/D,MAAM,SAAS,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACxC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,KAAK,MAAM,EAAE,aAAa,EAAE,CAAC;AACrE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@mmmnt/feat-analyze",
3
+ "version": "0.1.7",
4
+ "description": "Pure analysis for .feat specs: lint, audit (B1–B3 buildability), and conservative formatting — no filesystem, no CLI",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "dependencies": {
12
+ "@mmmnt/feat-types": "^0.1.7"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.8.0",
16
+ "vitest": "^3.2.0"
17
+ },
18
+ "engines": {
19
+ "node": ">=20"
20
+ },
21
+ "license": "MIT",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/mmmnt/feature.git",
25
+ "directory": "packages/feat-analyze"
26
+ },
27
+ "homepage": "https://github.com/mmmnt/feature#readme",
28
+ "bugs": {
29
+ "url": "https://github.com/mmmnt/feature/issues"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc -p tsconfig.json",
36
+ "typecheck": "tsc -p tsconfig.json --noEmit",
37
+ "test": "vitest run --passWithNoTests"
38
+ }
39
+ }