@mmmnt/feat-analyze
v0.3.0
Published
Pure analysis for .feat specs: lint, audit (B1–B3 buildability), and conservative formatting — no filesystem, no CLI
Readme
@mmmnt/feat-analyze
Pure analysis for .feat specs: lint, audit and format. Three functions,
no filesystem, no process, no CLI. Each takes a parsed spec (or the source text)
and returns findings you can print, gate on or show in an editor.
If you only write .feat specs, you do not need this package. The feat lint,
feat audit and feat fmt commands in
@mmmnt/feature are thin
callers over these functions and print the exact strings they return. Use this
package when you embed the same checks in a service, an editor or a bot.
Where it sits
.feat source ──► feat-core (parse) ──► BuiltSpec ──► lintSpec(spec) ──► Finding[]
│ └───────► auditSpec(spec) ──► { errors, warnings }
└─────────────────────────────────────────────► formatSource(src) ──► { formatted, changed, hasLeadingTab }The package depends only on
@mmmnt/feat-types, for the
BuiltSpec type. It does not parse. To get a BuiltSpec, use
@mmmnt/feat-core.
What the library leaves to a caller, because it needs the filesystem: whether a
spec has a paired <name>.contract.json, and whether its generated test file
exists. feat audit adds both as warnings.
Install
npm install @mmmnt/feat-analyze @mmmnt/feat-coreRequirements:
- Node 20 or later (
engines). - ESM only (
"type": "module"). @mmmnt/feat-typesinstalls with it.@mmmnt/feat-coreis only needed if you parse specs yourself, as below.
Usage
import { parseSource } from "@mmmnt/feat-core";
import { auditSpec, formatSource, lintSpec } from "@mmmnt/feat-analyze";
import type { BuiltSpec } from "@mmmnt/feat-types";
const source = `feat 1.0
spec SPEC-USR-001 "CreateUser"
context Users
aggregate User
type command
status draft
construct:
handler at handlers/create-user.ts
enforce:
rejects DUPLICATE_EMAIL when a user with this email already exists
rejects INVALID_EMAIL when the address is malformed
contract:
input CreateUserInput
response UserResponse
record UserRow
error ErrorResponse
scenario "rejects duplicate email":
when: CreateUser { email: "[email protected]" }
predict rejection DUPLICATE_EMAIL:
response 409 ErrorResponse { code: "DUPLICATE_EMAIL" }
database has []
`;
const parsed = parseSource(source, { serviceKeys: ["database"], commands: ["CreateUser"], actors: [] });
if (parsed.status !== "OK") throw new Error(JSON.stringify(parsed.body));
const spec = parsed.body as unknown as BuiltSpec;
console.log(lintSpec(spec));
console.log(auditSpec(spec));
console.log(formatSource(source).changed);That prints:
[
{ level: 'warn', message: "warn: schema 'UserResponse' is declared but never referenced by a scenario" },
{ level: 'warn', message: "warn: schema 'UserRow' is declared but never referenced by a scenario" },
{ level: 'info', message: 'info: status draft — not buildable until agreed' }
]
{
errors: [
'B3: no `touches` change boundary declared',
'B2: `rejects INVALID_EMAIL` is never predicted by any scenario'
],
warnings: [ 'status draft — not buildable until agreed' ]
}
falseAPI
lintSpec(spec: BuiltSpec): Finding[]
Language-quality rules beyond the buildability gate (ADR-0007, ADR-0017).
interface Finding {
level: "error" | "warn" | "info";
message: string; // already prefixed with its level, e.g. "warn: ..."
}| Rule | Level | Message |
| --- | --- | --- |
| A contract schema that no scenario references | warn | warn: schema 'X' is declared but never referenced by a scenario |
| A variable that nothing references | warn | warn: variable 'x' is declared but never referenced |
| The same kind schema declared twice in contract: | warn | warn: duplicate contract declaration 'kind X' |
| The spec is still a draft | info | info: status draft — not buildable until agreed |
What counts as a reference to a schema: the response schema of a prediction,
every record schema in a has or contains list, and every with <Schema> on
an inline seed record. input entries are used by definition (they are the
when: payload's contract), and stream entries are never checked. A variable
is referenced when a ${name} appears anywhere in a scenario, or in the template
of another variable.
lintOptionalFields(spec: BuiltSpec, registry: Record<string, object>): Finding[]
ADR-0038: a predicted response or record value block that leaves out an optional property of its schema is an
error, unless the block is marked partial. registry is the spec's contract, resolved by the schema adapter;
optional means declared and not required, following allOf and the bundle's local $refs.
| Rule | Level | Message |
| --- | --- | --- |
| A block leaves out an optional property | error | error [OPTIONAL_UNSTATED]: scenario "<name>" › <where>: <Schema> leaves out optional '<p>' — state each (absent, any, or a value), or mark the block '<Schema> partial { … }' (ADR-0038) |
lintNestedOpen(spec: BuiltSpec, registry: Record<string, object>): Finding[]
ADR-0039 (SPEC-ANL-001), the migration lint for exact nested blocks. From @mmmnt/feat-runtime 0.10.0 a nested value
block is exact. This rule names each nested block whose schema lets the object carry keys the block does not declare,
because only the block then states them. It checks every predicted response, has record and contains record,
partial or not.
A nested block's schema is the property it names, resolved at each depth through the bundle's local $refs and
allOf. It is closed when it says additionalProperties: false or unevaluatedProperties: false, directly, through
a ref, through an allOf part, or in every anyOf/oneOf branch. A property no schema describes is open. Literal objects
and any are not nested blocks.
| Rule | Level | Message |
| --- | --- | --- |
| A nested block under an open schema | warn | warn [NESTED_SCHEMA_OPEN]: scenario "<name>" › <response \| <service> <type>> › <path>: <Schema> leaves '<path>' open, so from feat-runtime 0.10.0 a key the block does not state fails the scenario (ADR-0039) — close it (additionalProperties: false), or check the block states every key |
feat lint runs all three and exits 1 on any error-level finding. NESTED_SCHEMA_OPEN is a warning and never fails the
run.
Findings come in rule order: schemas, variables, duplicates, status. No rule in
this release returns level: "error"; the type allows it so a future rule can.
A parse failure is the caller's to report, since there is no spec to lint.
auditSpec(spec: BuiltSpec): AuditResult
The buildability gate of an agreed spec (ADR-0008).
interface AuditResult {
errors: string[]; // B1 to B3: block the gate
warnings: string[]; // lifecycle surfacing: advisory
}| Rule | Fails when | Message |
| --- | --- | --- |
| B1 | the spec type is command, query, policy, projection or saga and there is no handler at | B1: no `handler at` for a code-producing spec type |
| B3 | there is no touches | B3: no `touches` change boundary declared |
| B2, predicted | a scenario predicts rejection X and no rejects X when ... line exists | B2: predicted rejection 'X' has no `rejects X when …` line |
| B2, declared | a rejects X line exists and no scenario predicts it | B2: `rejects X` is never predicted by any scenario |
Errors come in the order B1, B3, B2 (predicted first, then declared). Types
infrastructure, integration and scaffold are exempt from B1 but not from
B3. B2 is two-way: every rejection a spec promises is predicted, and every
predicted rejection is promised. Rejection ids are compared exactly and
case-sensitively (ADR-0022), so NotOperator and NOT_OPERATOR are different
ids.
In a scenario outline, a rejection id written as a placeholder (<code>) is
resolved through the examples: rows: every string value in that column counts
as a predicted id.
The one warning is status draft — not buildable until agreed, for a spec whose
status is draft. An agreed, built or verified spec has none.
formatSource(source: string): FormatResult
Conservative, content-preserving, idempotent formatting.
interface FormatResult {
formatted: string;
changed: boolean; // formatted !== source
hasLeadingTab: boolean; // a line that begins with a tab character
}It does exactly four things: it strips trailing spaces and tabs from every line, collapses any run of blank lines to one, removes blank lines at the end of the file, and ends the file with one newline. CRLF line endings become LF. It never re-indents, reorders or rewrites a token, and formatting its own output changes nothing.
hasLeadingTab reports a tab as the first character of a line. feat fmt
treats it as an error and leaves that file alone. A space followed by a tab is
not detected. The parser itself does not reject tab indentation (see
@mmmnt/feat-core).
In the CLI
| Command | Calls | Exit codes |
| --- | --- | --- |
| feat lint | lintSpec, lintOptionalFields and lintNestedOpen on every spec | 0, or 1 if any spec fails to parse. Warnings and notes never fail the run. |
| feat audit | auditSpec, plus the paired-file warnings | 0, or 1 if any spec has an error (a parse failure or a B1, B2 or B3 error). Warnings never fail the run. |
| feat fmt | formatSource; --check reports without writing | 0, or 1 with --check when any file would change, or when any file has a leading tab. |
A configuration error is exit 2, as everywhere in the toolchain. The library
itself returns values and never exits.
What changed in 0.3.0
lintNestedOpen(ADR-0039, SPEC-ANL-001): the warningNESTED_SCHEMA_OPENnames each nested block whose schema is open. It is the migration aid for@mmmnt/feat-runtime0.10.0, where nested blocks became exact. A warning, never a config switch;feat lintstill exits 1 only on errors.
What changed in 0.1.9
No source change in release v0.1.48. The package is republished as 0.1.9 against
@mmmnt/feat-types 0.2.0, with this README; until now it had none.
Specs and decisions
- Grammar reference: the language the rules apply to
- ADR-0022: UpperCamel rejection ids: why B2 compares ids exactly
- ADR-0007 (declared-but-unused lint), ADR-0008 (the buildability rules B1 to
B3 and the
draft,agreed,built,verifiedlifecycle) and ADR-0017 (the variable lint) record the rules. ADR-0001 to ADR-0021 are not in the repository yet.
Part of Feature, the .feat execution
specification language. Docs: https://github.com/mmmnt/feature/wiki
MIT
