@metaobjectsdev/cli 0.24.1 → 0.24.3

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.
@@ -0,0 +1,356 @@
1
+ // `meta verify` — the requirement AUTHORING lint.
2
+ //
3
+ // A different kind of claim from the gate in `requirement-check.ts`, which is why
4
+ // it is a separate function rather than more branches inside `checkRequirements`.
5
+ //
6
+ // THE GATE referential integrity — links sit at or below the floor, nesting
7
+ // agrees with levels, references resolve. A finding there means the
8
+ // ledger DISAGREES WITH THE MODEL.
9
+ // THE LINT authoring quality — the name is addressable, the prose slots hold
10
+ // distinct content, nothing was written into a slot no surface reads.
11
+ // A finding here means the ledger is INTERNALLY WASTEFUL: it still
12
+ // agrees with the model, it just records less than its author thinks.
13
+ //
14
+ // Keeping them apart is not tidiness. `meta verify` prints at most 20 warnings, and
15
+ // a ledger with 240 entries can produce hundreds of prose findings — enough to push
16
+ // every WARN_REQUIREMENT_OBJECT_UNCLAIMED off the end of the list. Two sections with
17
+ // two caps means the lint cannot drown the gate.
18
+ //
19
+ // IT READS THE LOADED MODEL, NEVER THE FILES. Extensions and overlays mean the text
20
+ // on disk is not the effective model: an attr set in one file and overridden in
21
+ // another, or inherited through `extends`, reads differently from every angle except
22
+ // the loaded tree. A raw-file linter would be wrong for any project using either.
23
+ //
24
+ // EVERY FINDING IS A WARNING, BY CONSTRUCTION — not by a switch. Stated as a rule so
25
+ // the next check added here follows it: a check newly added to a shipping gate cannot
26
+ // be shown not to fire on an estate that already exists, and prose findings that turn
27
+ // `meta verify` red on upgrade teach people to switch the gate off, which costs more
28
+ // than the padding they caught. The precedent is object coverage, which stayed a
29
+ // warning because on one real estate it reported every entity in the repository.
30
+ //
31
+ // There is deliberately NO severity constant to flip. `verify` prints this section
32
+ // with `log.warn` unconditionally and computes its exit code from the GATE alone, so
33
+ // a constant here would have promised a promotion it could not deliver. If a check in
34
+ // this file ever has to fail a build, the honest move is to move the check into the
35
+ // gate — which is a decision about what the check IS, not a severity edit.
36
+
37
+ import {
38
+ DOC_ATTR_DESCRIPTION,
39
+ DOC_ATTR_SUMMARY,
40
+ DOC_ATTR_TITLE,
41
+ REQUIREMENT_ATTR_COUNTEREXAMPLE,
42
+ REQUIREMENT_ATTR_STATEMENT,
43
+ REQUIREMENT_ATTR_TRACKED_BY,
44
+ // The port's ONE identifier splitter, used here for its word boundaries rather
45
+ // than its underscores: `normalise` lowercases and maps every separator to a
46
+ // space, so `toSnakeCase(name)` and a hand-rolled camel split are identical
47
+ // downstream (checked over 20k random identifiers). Sharing it means the lint
48
+ // splits a name the same way the column/table/kebab namers do — a private copy
49
+ // would silently stop agreeing the day that rule is corrected.
50
+ toSnakeCase,
51
+ type MetaData,
52
+ } from "@metaobjectsdev/metadata";
53
+ // The SAME collection the gate walks, so the two sections of one `meta verify` run
54
+ // address a node identically — and the dotted path each reports is the address the
55
+ // requirement-test generator turns into the stub's filename. (Pinned by a test that
56
+ // compares this path set against `walkRequirements()`, the generator's own walk,
57
+ // rather than leaving the two to agree by inspection.)
58
+ import {
59
+ collectAddressedRequirements, type AddressedRequirement, type Diagnostic,
60
+ } from "./requirement-check.js";
61
+
62
+ export const WARN_REQUIREMENT_NAME_NOT_ADDRESSABLE = "WARN_REQUIREMENT_NAME_NOT_ADDRESSABLE";
63
+ export const WARN_REQUIREMENT_NAME_READS_AS_PROSE = "WARN_REQUIREMENT_NAME_READS_AS_PROSE";
64
+ export const WARN_REQUIREMENT_NAME_RESTATES_STATEMENT = "WARN_REQUIREMENT_NAME_RESTATES_STATEMENT";
65
+ export const WARN_REQUIREMENT_PROSE_DUPLICATED = "WARN_REQUIREMENT_PROSE_DUPLICATED";
66
+ export const WARN_REQUIREMENT_PROSE_EMPTY = "WARN_REQUIREMENT_PROSE_EMPTY";
67
+ export const WARN_REQUIREMENT_INERT_DOC_SLOT = "WARN_REQUIREMENT_INERT_DOC_SLOT";
68
+ export const WARN_REQUIREMENT_TITLE_IS_AN_ID = "WARN_REQUIREMENT_TITLE_IS_AN_ID";
69
+
70
+ /**
71
+ * Characters that break the two things a requirement's `name` IS.
72
+ *
73
+ * `.` the dotted-path separator. A name containing one is INDISTINGUISHABLE
74
+ * from nesting: a single node named "Orders.Recorded" and a node "Orders"
75
+ * containing a node "Recorded" both produce the path `Orders.Recorded`, so
76
+ * the address stops identifying one node and the two collide on the same
77
+ * emitted stub file.
78
+ * `/` `\` path separators. The default stub path is
79
+ * `requirements/<path>.test.ts`, so a name of `../../thing` writes OUTSIDE
80
+ * the stub directory — and `owns()`, which is what lets the runner reap a
81
+ * stub whose requirement was deleted, will never claim it back.
82
+ * the rest illegal in a filename on Windows, so the stub cannot be written at
83
+ * all there. `:` doubles as half of the `::` package separator.
84
+ *
85
+ * Every one of these loads today: the loader constrains a requirement's name no
86
+ * more than any other node's, and nothing downstream re-checks it.
87
+ *
88
+ * A SEAM, recorded so the decision stays visible: the general form of this rule is
89
+ * not requirement-specific at all — a `.` in ANY node's name breaks the dotted
90
+ * child-name addressing the whole metamodel uses — so it arguably belongs in the
91
+ * loader as a cross-port warning, beside WARN_ENUM_NORMALIZE_AMBIGUOUS. It is
92
+ * scoped here because the requirement surface is where it currently bites (a name
93
+ * becomes a stub FILENAME only here), and because promoting it means five ports,
94
+ * expected-warnings.json, conformance fixtures and a metamodelVersion question.
95
+ * That is an FR, not a line in this file.
96
+ */
97
+ const UNADDRESSABLE = /[./\\:*?"<>|\p{Cc}]/gu;
98
+
99
+ /** How one offending character is named in the message, so the author is told which
100
+ * one to remove rather than left to diff their name against a regex.
101
+ *
102
+ * Only `.` needs a written entry — the rest are self-describing once quoted, and a
103
+ * hand-written entry per character was a table that had to be kept in step with
104
+ * UNADDRESSABLE: adding a character to the regex and forgetting the table printed
105
+ * a bare code point where the character itself was meant. Derived, so it cannot
106
+ * drift. */
107
+ function describeChar(c: string): string {
108
+ if (c === ".") return "'.' (the dotted-path separator)";
109
+ // A control character has no printable form to quote, so name its code point.
110
+ return /\p{Cc}/u.test(c) ? `U+${c.charCodeAt(0).toString(16).padStart(4, "0")}` : `'${c}'`;
111
+ }
112
+
113
+ /**
114
+ * Word count at which a name stops reading as a label and starts reading as prose.
115
+ *
116
+ * A threshold, and the only one in this file — chosen high on purpose. Five words
117
+ * is still plausibly a label ("Order recording for placed orders"); six is a
118
+ * sentence. Under-firing is the right failure here: renaming a requirement changes
119
+ * its address AND its emitted stub filename, so a false positive asks the author to
120
+ * pay a migration for nothing.
121
+ */
122
+ const PROSE_WORD_COUNT = 6;
123
+
124
+ /**
125
+ * Slots that say nothing on a requirement — and the two deliberate exclusions.
126
+ *
127
+ * `summary` qualifies: `@statement` is REQUIRED on both subtypes and is already the
128
+ * one-line sentence, so a summary beside it can only repeat it, and nothing reads
129
+ * it. `spec/capability-ledger.md`'s requirement attribute table does not list it.
130
+ *
131
+ * `title` is NOT on this list, and an earlier version of this file had it there —
132
+ * wrongly. That same attribute table charters it BY NAME on a requirement ("a short
133
+ * noun-phrase label — `name` is an identifier, this is what an index shows"), which
134
+ * is exactly this node type's situation: a requirement's address renders as a dotted
135
+ * camelCase path. Measured against three real ledgers the ban would have told two
136
+ * adopters to delete 355 authored labels, 123 of which carry words the name does not.
137
+ * That the requirements page does not render `title` yet is a gap in the RENDERER,
138
+ * and a tool reporting its own backlog in an adopter's terminal is noise.
139
+ *
140
+ * `notes` is excluded for the opposite reason: chartered internal-only, so being
141
+ * unrendered is the point of it.
142
+ */
143
+ const INERT_SLOTS = [DOC_ATTR_SUMMARY] as const;
144
+
145
+ /**
146
+ * A catalogue or ticket id at the head of a label — `FR-448 …`, `PLAT-77 …`.
147
+ *
148
+ * The real failure the id case represents is FIELD OVERLOADING, not an unread slot:
149
+ * a citation lives in the display label because nothing else was offered, and
150
+ * `@trackedBy` (free-form, deliberately never resolved) is what was. The measured
151
+ * values carry an id AND a noun phrase — "FR-448 — prompt construction as typed
152
+ * payloads through a render engine" — so the fix is to SPLIT them. Moving the whole
153
+ * string to `@trackedBy` would throw the label away, which is why the message says
154
+ * split rather than move.
155
+ */
156
+ const TITLE_IS_AN_ID = /^[A-Z]{2,}[- ]?\d+/;
157
+
158
+ /** Lowercase, drop everything that is not alphanumeric, collapse the gaps.
159
+ * Two slots "say the same thing" only if they survive this identically — no
160
+ * similarity score, no threshold. A fuzzy match on prose produces findings the
161
+ * author can argue with, and a gate people argue with is a gate people mute. */
162
+ function normalise(text: string): string {
163
+ return text.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
164
+ }
165
+
166
+ /** Whether `text`'s LEADING sentence says the same thing as `key`. False when the
167
+ * text is a single sentence — the caller's whole-string comparison covers that.
168
+ *
169
+ * Newlines are collapsed FIRST. A `description` is routinely authored as a YAML
170
+ * literal block, so the repeated opening sentence usually wraps — and `.` in a JS
171
+ * regex does not cross a newline, which made this arm silently miss exactly the
172
+ * authoring style most likely to trip it. The whole-string arm never had the bug
173
+ * because `normalise` already flattens whitespace. */
174
+ function leadingSentenceMatches(text: string, key: string): boolean {
175
+ const flat = text.replace(/\s+/g, " ").trim();
176
+ const m = /^(.+?[.!?])\s+\S/.exec(flat);
177
+ return m?.[1] !== undefined && normalise(m[1]) === key;
178
+ }
179
+
180
+ /** A resolving string read, normalised so "declared but blank" and "absent" are
181
+ * distinguishable — the first is a finding, the second usually is not.
182
+ * `attr()` RESOLVES in TypeScript (ADR-0039), so a requirement inheriting its
183
+ * prose through `extends` is linted on what it effectively carries. */
184
+ function readSlot(node: MetaData, name: string): string | undefined {
185
+ const raw = node.attr(name);
186
+ return typeof raw === "string" ? raw : undefined;
187
+ }
188
+
189
+ /**
190
+ * An OWN-ONLY string read — one of the two sanctioned `own*()` uses in this file
191
+ * (ADR-0039 requires every such call to name its case).
192
+ *
193
+ * The checks split by what they are ABOUT, and the split decides the accessor:
194
+ *
195
+ * RESOLVING a check about what a node effectively SAYS. Two slots holding one
196
+ * sentence is a property of the effective node — a child may override
197
+ * one slot and inherit the other, and only the resolved pair shows it.
198
+ * OWN-ONLY a check about a DECLARATION, whose fix is a single edit at a single
199
+ * node. `title` set once on an abstract is inherited by every child, so
200
+ * a resolving read reports it once per child at addresses where the
201
+ * author will find no `title` to delete — on a ledger using the shared
202
+ * abstract idiom, one mistake can fill the whole lint cap with lines
203
+ * nobody can act on where they are pointed.
204
+ */
205
+ function readOwnSlot(node: MetaData, name: string): string | undefined {
206
+ const raw = node.ownAttr(name);
207
+ return typeof raw === "string" ? raw : undefined;
208
+ }
209
+
210
+ /** Echo an authored value back inside a message without letting a long one take over
211
+ * the terminal. Findings are printed one per line and a `description`-length slot
212
+ * would wrap for a dozen of them. */
213
+ function excerpt(value: string): string {
214
+ const LIMIT = 80;
215
+ const flat = value.replace(/\s+/g, " ").trim();
216
+ return JSON.stringify(flat.length > LIMIT ? `${flat.slice(0, LIMIT - 1)}…` : flat);
217
+ }
218
+
219
+ function warn(path: string, code: string, message: string): Diagnostic {
220
+ return { severity: "warn", code, path, message };
221
+ }
222
+
223
+ /**
224
+ * Lint every `requirement.*` node in the loaded model.
225
+ *
226
+ * Returns `[]` for a model declaring none — opt-in by declaration, the same way
227
+ * the gate and the docs surface are, so turning this on by default is a no-op for
228
+ * every project without a ledger.
229
+ *
230
+ * What it CANNOT tell you: whether a statement is true, whether a description is
231
+ * useful, or whether the counterexample would actually falsify the claim. Those
232
+ * are the judgements the ledger exists to record and no check reaches them. Every
233
+ * finding below is about a slot's MECHANICS — is this content reachable, is it
234
+ * distinct from its neighbour, is the name still an address.
235
+ */
236
+ export function lintRequirements(
237
+ root: MetaData,
238
+ /** The run's already-collected requirements, when one exists. The lint takes the
239
+ * ADDRESSES alone rather than the gate's full `RequirementScan`: it has nothing
240
+ * to do with who claims what, and asking for the claim set would make a
241
+ * standalone call pay for a resolution it never reads. */
242
+ addressed: readonly AddressedRequirement[] = collectAddressedRequirements(root),
243
+ ): Diagnostic[] {
244
+ const out: Diagnostic[] = [];
245
+
246
+ for (const { node, path } of addressed) {
247
+ const name = node.name;
248
+
249
+ // -- the name is an address, not prose ------------------------------------
250
+ // EVERY problem with one name is reported in ONE finding. These were two
251
+ // branches of an if/else, so a name that was both padded and dotted reported
252
+ // only the dot: the author fixed it, re-ran, and was told about the padding on
253
+ // a second pass. One name is one edit, so it is one finding.
254
+ const problems: string[] = [];
255
+ const offenders = [...new Set(name.match(UNADDRESSABLE) ?? [])];
256
+ if (offenders.length > 0) {
257
+ problems.push(`contains ${offenders.map(describeChar).join(", ")}`);
258
+ }
259
+ if (name.trim() === "") problems.push("is blank");
260
+ else if (name.trim() !== name) problems.push("has leading or trailing whitespace");
261
+ if (problems.length > 0) {
262
+ out.push(warn(path, WARN_REQUIREMENT_NAME_NOT_ADDRESSABLE,
263
+ `name ${JSON.stringify(name)} ${problems.join(", and ")}. A requirement's name is its ` +
264
+ `address — it is the segment of the dotted path '${path}' and the filename of its generated ` +
265
+ `test stub — so this either collides with nesting or produces a path the stub cannot be ` +
266
+ `written to.`));
267
+ }
268
+
269
+ const statementKey = normalise(readSlot(node, REQUIREMENT_ATTR_STATEMENT) ?? "");
270
+
271
+ // -- the name is not the claim --------------------------------------------
272
+ // Ordered before the prose-shape check and exclusive with it: when the name IS
273
+ // the statement, "rename it" is the wrong instruction. The instruction is "you
274
+ // have written the claim twice; @statement is the one that is read".
275
+ if (statementKey !== "" && normalise(toSnakeCase(name)) === statementKey) {
276
+ out.push(warn(path, WARN_REQUIREMENT_NAME_RESTATES_STATEMENT,
277
+ `name '${name}' says the same thing as @statement. The claim belongs in @statement, which every ` +
278
+ `surface reads; the name is an address and is better as a short identifier.`));
279
+ } else if (name.trim().split(/\s+/).filter((w) => w !== "").length >= PROSE_WORD_COUNT) {
280
+ out.push(warn(path, WARN_REQUIREMENT_NAME_READS_AS_PROSE,
281
+ `name '${name}' reads as a sentence. The name is an address — the dotted path other entries and ` +
282
+ `the generated stub filename are built from — so a short identifier keeps both legible. Put the ` +
283
+ `prose in @statement.`));
284
+ }
285
+
286
+ // -- required prose that is present but says nothing -----------------------
287
+ // The loader enforces PRESENCE (min: 1), never content, so `statement: ""`
288
+ // loads clean today and satisfies a required attribute with nothing in it.
289
+ for (const slot of [REQUIREMENT_ATTR_STATEMENT, REQUIREMENT_ATTR_COUNTEREXAMPLE]) {
290
+ // OWN-ONLY: the fix is deleting or filling one declaration. See readOwnSlot.
291
+ const value = readOwnSlot(node, slot);
292
+ if (value !== undefined && value.trim() === "") {
293
+ out.push(warn(path, WARN_REQUIREMENT_PROSE_EMPTY,
294
+ `@${slot} is declared but empty. The loader requires the attribute to be present, not to say ` +
295
+ `anything — an empty one passes every check while recording nothing.`));
296
+ }
297
+ }
298
+
299
+ // -- two slots holding one sentence ---------------------------------------
300
+ // The padding failure the authoring guidance names: restating the claim under a
301
+ // second heading reads as diligence and makes every later reader trust the
302
+ // ledger less. Only EXACT repeats are reported, whole or as the description's
303
+ // leading sentence; paraphrase is the broader failure and no mechanical rule
304
+ // reaches it without inventing findings.
305
+ if (statementKey !== "") {
306
+ // No blank-check needed: this arm only runs when `statementKey` is non-empty,
307
+ // and a blank description normalises to "" — which cannot equal it. That makes
308
+ // this guard identical to the `counterexample` arm below.
309
+ const description = readSlot(node, DOC_ATTR_DESCRIPTION);
310
+ if (description !== undefined) {
311
+ if (normalise(description) === statementKey) {
312
+ out.push(warn(path, WARN_REQUIREMENT_PROSE_DUPLICATED,
313
+ `description repeats @statement verbatim. @statement already IS the description of what the ` +
314
+ `requirement is; description holds the SCOPE — what the claim covers, what it deliberately ` +
315
+ `does not, which sibling entry owns the rest. If the scope is obvious, leave it off.`));
316
+ } else if (leadingSentenceMatches(description, statementKey)) {
317
+ out.push(warn(path, WARN_REQUIREMENT_PROSE_DUPLICATED,
318
+ `description opens by repeating @statement verbatim, then continues. Drop the first sentence — ` +
319
+ `it is already read from @statement, and description is only the scope that follows it.`));
320
+ }
321
+ }
322
+
323
+ const counterexample = readSlot(node, REQUIREMENT_ATTR_COUNTEREXAMPLE);
324
+ if (counterexample !== undefined && normalise(counterexample) === statementKey) {
325
+ out.push(warn(path, WARN_REQUIREMENT_PROSE_DUPLICATED,
326
+ `@counterexample repeats @statement verbatim. It must describe what BREAKING the claim looks ` +
327
+ `like — the thing you could point at to falsify it — which is what makes the claim checkable.`));
328
+ }
329
+ }
330
+
331
+ // -- content written where nothing reads it --------------------------------
332
+ for (const slot of INERT_SLOTS) {
333
+ // OWN-ONLY: the fix is deleting one attribute. See readOwnSlot.
334
+ const value = readOwnSlot(node, slot);
335
+ if (value === undefined || value.trim() === "") continue;
336
+ out.push(warn(path, WARN_REQUIREMENT_INERT_DOC_SLOT,
337
+ `@${slot} says nothing here that @${REQUIREMENT_ATTR_STATEMENT} does not. A requirement's ` +
338
+ `statement is REQUIRED and is already the one-line sentence, so a summary beside it can only ` +
339
+ `repeat it — and no requirement surface reads it. Its content is invisible: ${excerpt(value)}. ` +
340
+ `Delete it. (@title is different and is NOT flagged: it is chartered as the entry's LABEL, ` +
341
+ `because a requirement's name is an identifier.)`));
342
+ }
343
+
344
+ // -- an id is not a label -------------------------------------------------
345
+ const title = readOwnSlot(node, DOC_ATTR_TITLE);
346
+ if (title !== undefined && TITLE_IS_AN_ID.test(title.trim())) {
347
+ out.push(warn(path, WARN_REQUIREMENT_TITLE_IS_AN_ID,
348
+ `@title opens with a catalogue or ticket id: ${excerpt(title)}. A title is a NOUN PHRASE and ` +
349
+ `an id is not a name, so this is two things in one slot. SPLIT them — put the id in ` +
350
+ `@${REQUIREMENT_ATTR_TRACKED_BY}, which is the free-form reference slot and IS read, and leave ` +
351
+ `the phrase as the title. Moving the whole string would throw the label away.`));
352
+ }
353
+ }
354
+
355
+ return out;
356
+ }