vigiles 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/.claude/settings.json +46 -0
  2. package/.claude/settings.local.json +8 -0
  3. package/.claude-plugin/hooks/post-edit.sh +34 -0
  4. package/.claude-plugin/hooks/pre-edit.sh +40 -0
  5. package/.claude-plugin/hooks/session-start.sh +38 -0
  6. package/.claude-plugin/marketplace.json +14 -0
  7. package/.claude-plugin/plugin.json +47 -0
  8. package/.github/workflows/ci.yml +81 -0
  9. package/.prettierignore +1 -0
  10. package/.vigiles/generated.d.ts +205 -0
  11. package/CLAUDE.md +95 -0
  12. package/CLAUDE.md.spec.ts +142 -0
  13. package/CONTRIBUTING.md +121 -0
  14. package/LICENSE +21 -0
  15. package/README.md +377 -0
  16. package/action.yml +25 -0
  17. package/dist/action.d.ts +7 -0
  18. package/dist/action.d.ts.map +1 -0
  19. package/dist/action.js +180 -0
  20. package/dist/action.js.map +1 -0
  21. package/dist/cli.d.ts +12 -0
  22. package/dist/cli.d.ts.map +1 -0
  23. package/dist/cli.js +1267 -0
  24. package/dist/cli.js.map +1 -0
  25. package/dist/cli.test.d.ts +2 -0
  26. package/dist/cli.test.d.ts.map +1 -0
  27. package/dist/cli.test.js +650 -0
  28. package/dist/cli.test.js.map +1 -0
  29. package/dist/compile.d.ts +101 -0
  30. package/dist/compile.d.ts.map +1 -0
  31. package/dist/compile.js +503 -0
  32. package/dist/compile.js.map +1 -0
  33. package/dist/evolve.d.ts +132 -0
  34. package/dist/evolve.d.ts.map +1 -0
  35. package/dist/evolve.js +599 -0
  36. package/dist/evolve.js.map +1 -0
  37. package/dist/freshness.d.ts +67 -0
  38. package/dist/freshness.d.ts.map +1 -0
  39. package/dist/freshness.js +244 -0
  40. package/dist/freshness.js.map +1 -0
  41. package/dist/freshness.test.d.ts +2 -0
  42. package/dist/freshness.test.d.ts.map +1 -0
  43. package/dist/freshness.test.js +356 -0
  44. package/dist/freshness.test.js.map +1 -0
  45. package/dist/generate-types.d.ts +34 -0
  46. package/dist/generate-types.d.ts.map +1 -0
  47. package/dist/generate-types.js +381 -0
  48. package/dist/generate-types.js.map +1 -0
  49. package/dist/inline.d.ts +58 -0
  50. package/dist/inline.d.ts.map +1 -0
  51. package/dist/inline.js +142 -0
  52. package/dist/inline.js.map +1 -0
  53. package/dist/inline.test.d.ts +5 -0
  54. package/dist/inline.test.d.ts.map +1 -0
  55. package/dist/inline.test.js +152 -0
  56. package/dist/inline.test.js.map +1 -0
  57. package/dist/linters.d.ts +38 -0
  58. package/dist/linters.d.ts.map +1 -0
  59. package/dist/linters.js +588 -0
  60. package/dist/linters.js.map +1 -0
  61. package/dist/proofs.d.ts +272 -0
  62. package/dist/proofs.d.ts.map +1 -0
  63. package/dist/proofs.js +622 -0
  64. package/dist/proofs.js.map +1 -0
  65. package/dist/proofs.test.d.ts +9 -0
  66. package/dist/proofs.test.d.ts.map +1 -0
  67. package/dist/proofs.test.js +952 -0
  68. package/dist/proofs.test.js.map +1 -0
  69. package/dist/spec.d.ts +258 -0
  70. package/dist/spec.d.ts.map +1 -0
  71. package/dist/spec.js +113 -0
  72. package/dist/spec.js.map +1 -0
  73. package/dist/spec.test.d.ts +2 -0
  74. package/dist/spec.test.d.ts.map +1 -0
  75. package/dist/spec.test.js +1222 -0
  76. package/dist/spec.test.js.map +1 -0
  77. package/dist/types.d.ts +101 -0
  78. package/dist/types.d.ts.map +1 -0
  79. package/dist/types.js +3 -0
  80. package/dist/types.js.map +1 -0
  81. package/dist/validate.d.ts +10 -0
  82. package/dist/validate.d.ts.map +1 -0
  83. package/dist/validate.js +286 -0
  84. package/dist/validate.js.map +1 -0
  85. package/dist/validate.test.d.ts +2 -0
  86. package/dist/validate.test.d.ts.map +1 -0
  87. package/dist/validate.test.js +531 -0
  88. package/dist/validate.test.js.map +1 -0
  89. package/docs/agent-setup.md +85 -0
  90. package/docs/agent-workflows.md +103 -0
  91. package/docs/comparison.md +71 -0
  92. package/docs/freshness.md +124 -0
  93. package/docs/inline-mode.md +119 -0
  94. package/docs/linter-support.md +166 -0
  95. package/docs/spec-format.md +194 -0
  96. package/eslint.config.mjs +79 -0
  97. package/examples/CLAUDE.md +54 -0
  98. package/examples/CLAUDE.md.spec.ts +65 -0
  99. package/examples/SKILL.md +50 -0
  100. package/examples/SKILL.md.spec.ts +57 -0
  101. package/fixtures/example-project/CLAUDE.md +11 -0
  102. package/fixtures/example-project/package.json +9 -0
  103. package/fixtures/example-project/src/index.ts +3 -0
  104. package/fixtures/example-project/src/utils.test.ts +2 -0
  105. package/fixtures/example-project/src/utils.ts +3 -0
  106. package/logo.png +0 -0
  107. package/package.json +42 -0
  108. package/research/adoption-strategy.md +111 -0
  109. package/research/agent-integration.md +145 -0
  110. package/research/ai-code-quality.md +197 -0
  111. package/research/code-search-for-agents.md +313 -0
  112. package/research/competitive-landscape.md +163 -0
  113. package/research/doc-freshness.md +516 -0
  114. package/research/executable-specs.md +368 -0
  115. package/research/feature-ideas.md +464 -0
  116. package/research/formal-proofs-for-agents.md +338 -0
  117. package/research/fp-for-agent-harness.md +150 -0
  118. package/research/fp-for-deterministic-ai.md +131 -0
  119. package/research/self-evolving-specs.md +298 -0
  120. package/schemas/claude-md-strict.yml +18 -0
  121. package/schemas/claude-md.yml +6 -0
  122. package/schemas/skill-strict.yml +12 -0
  123. package/schemas/skill.yml +5 -0
  124. package/skills/audit-feedback-loop/SKILL.md +76 -0
  125. package/skills/edit-spec/SKILL.md +131 -0
  126. package/skills/enforce-rules-format/SKILL.md +71 -0
  127. package/skills/generate-logo/SKILL.md +102 -0
  128. package/skills/generate-rule/SKILL.md +90 -0
  129. package/skills/linter-docs/clippy.md +241 -0
  130. package/skills/linter-docs/eslint.md +384 -0
  131. package/skills/linter-docs/pylint.md +288 -0
  132. package/skills/linter-docs/rubocop.md +277 -0
  133. package/skills/linter-docs/ruff.md +187 -0
  134. package/skills/linter-docs/stylelint.md +247 -0
  135. package/skills/migrate-to-spec/SKILL.md +124 -0
  136. package/skills/pr-to-lint-rule/SKILL.md +97 -0
  137. package/skills/strengthen/SKILL.md +168 -0
  138. package/src/action.ts +214 -0
  139. package/src/cli.test.ts +914 -0
  140. package/src/cli.ts +1631 -0
  141. package/src/compile.ts +691 -0
  142. package/src/evolve.ts +781 -0
  143. package/src/freshness.test.ts +449 -0
  144. package/src/freshness.ts +299 -0
  145. package/src/generate-types.ts +448 -0
  146. package/src/inline.test.ts +206 -0
  147. package/src/inline.ts +164 -0
  148. package/src/linters.ts +739 -0
  149. package/src/proofs.test.ts +1314 -0
  150. package/src/proofs.ts +849 -0
  151. package/src/spec.test.ts +1471 -0
  152. package/src/spec.ts +427 -0
  153. package/src/types.ts +117 -0
  154. package/src/validate.test.ts +701 -0
  155. package/src/validate.ts +381 -0
  156. package/tsconfig.json +23 -0
@@ -0,0 +1,448 @@
1
+ /**
2
+ * vigiles generate-types — emit .d.ts with type unions from actual project state.
3
+ *
4
+ * Scans linter configs, package.json, and filesystem to generate TypeScript
5
+ * types that the compiler uses to PROVE references are valid at authoring time.
6
+ *
7
+ * Generated types:
8
+ * - EslintRule / StylelintRule / RuffRule / ... — enabled rules per linter
9
+ * - NpmScript — scripts from package.json
10
+ * - ProjectFile — files in the project (scoped to src/ by default)
11
+ */
12
+
13
+ import { existsSync, readFileSync } from "node:fs";
14
+ import { resolve } from "node:path";
15
+ import { execSync } from "node:child_process";
16
+ import { globSync } from "glob";
17
+
18
+ // ---------------------------------------------------------------------------
19
+ // Linter config detection
20
+ // ---------------------------------------------------------------------------
21
+
22
+ function fileContainsSection(filePath: string, section: string): boolean {
23
+ if (!existsSync(filePath)) return false;
24
+ try {
25
+ return readFileSync(filePath, "utf-8").includes(section);
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ function hasRuffConfig(basePath: string): boolean {
32
+ return (
33
+ existsSync(resolve(basePath, "ruff.toml")) ||
34
+ existsSync(resolve(basePath, ".ruff.toml")) ||
35
+ fileContainsSection(resolve(basePath, "pyproject.toml"), "[tool.ruff")
36
+ );
37
+ }
38
+
39
+ function hasPylintConfig(basePath: string): boolean {
40
+ return (
41
+ existsSync(resolve(basePath, ".pylintrc")) ||
42
+ existsSync(resolve(basePath, "pylintrc")) ||
43
+ fileContainsSection(resolve(basePath, "pyproject.toml"), "[tool.pylint") ||
44
+ fileContainsSection(resolve(basePath, "setup.cfg"), "[pylint")
45
+ );
46
+ }
47
+
48
+ function hasRubocopConfig(basePath: string): boolean {
49
+ return existsSync(resolve(basePath, ".rubocop.yml"));
50
+ }
51
+
52
+ // ---------------------------------------------------------------------------
53
+ // Linter rule discovery
54
+ // ---------------------------------------------------------------------------
55
+
56
+ interface DiscoveredRules {
57
+ linter: string;
58
+ rules: string[];
59
+ via: string;
60
+ }
61
+
62
+ function discoverEslintRules(basePath: string): DiscoveredRules | null {
63
+ try {
64
+ const script = `
65
+ const { loadESLint } = require("eslint");
66
+ (async () => {
67
+ try {
68
+ const ESLint = await loadESLint();
69
+ const eslint = new ESLint({ cwd: ${JSON.stringify(basePath)} });
70
+ const config = await eslint.calculateConfigForFile("dummy.js");
71
+ const enabled = Object.entries(config.rules || {})
72
+ .filter(([, v]) => {
73
+ const sev = Array.isArray(v) ? v[0] : v;
74
+ return sev !== 0 && sev !== "off";
75
+ })
76
+ .map(([k]) => k);
77
+ console.log(JSON.stringify(enabled));
78
+ } catch(e) {
79
+ console.log("[]");
80
+ }
81
+ })();
82
+ `;
83
+ const output = execSync(`node -e '${script.replace(/'/g, "'\\''")}'`, {
84
+ encoding: "utf-8",
85
+ cwd: basePath,
86
+ stdio: ["pipe", "pipe", "pipe"],
87
+ timeout: 15000,
88
+ });
89
+ const rules = JSON.parse(output.trim() || "[]") as string[];
90
+ if (rules.length === 0) return null;
91
+ return { linter: "eslint", rules, via: "flat config" };
92
+ } catch {
93
+ return null;
94
+ }
95
+ }
96
+
97
+ function discoverStylelintRules(basePath: string): DiscoveredRules | null {
98
+ try {
99
+ const script = `
100
+ const stylelint = require("stylelint");
101
+ (async () => {
102
+ try {
103
+ const linter = stylelint.createLinter({});
104
+ const result = await linter.getConfigForFile(${JSON.stringify(resolve(basePath, "dummy.css"))});
105
+ const enabled = Object.entries(result.config.rules || {})
106
+ .filter(([, v]) => v !== null && !(Array.isArray(v) && v[0] === null))
107
+ .map(([k]) => k);
108
+ console.log(JSON.stringify(enabled));
109
+ } catch(e) {
110
+ console.log("[]");
111
+ }
112
+ })();
113
+ `;
114
+ const output = execSync(`node -e '${script.replace(/'/g, "'\\''")}'`, {
115
+ encoding: "utf-8",
116
+ cwd: basePath,
117
+ stdio: ["pipe", "pipe", "pipe"],
118
+ timeout: 15000,
119
+ });
120
+ const rules = JSON.parse(output.trim() || "[]") as string[];
121
+ if (rules.length === 0) return null;
122
+ return { linter: "stylelint", rules, via: "config" };
123
+ } catch {
124
+ return null;
125
+ }
126
+ }
127
+
128
+ function discoverRuffRules(basePath: string): DiscoveredRules | null {
129
+ try {
130
+ if (!hasRuffConfig(basePath)) return null;
131
+ execSync("which ruff", { stdio: "ignore" });
132
+ const dummyPath = resolve(basePath, "dummy.py");
133
+ const output = execSync(`ruff check --show-settings ${dummyPath}`, {
134
+ encoding: "utf-8",
135
+ cwd: basePath,
136
+ stdio: ["pipe", "pipe", "pipe"],
137
+ timeout: 10000,
138
+ });
139
+ const enabledMatch = output.match(
140
+ /linter\.rules\.enabled\s*=\s*\[([\s\S]*?)\]/,
141
+ );
142
+ const rules: string[] = [];
143
+ if (enabledMatch?.[1]) {
144
+ const codeRe = /\(([A-Z]+\d*)\)/g;
145
+ let m: RegExpExecArray | null;
146
+ while ((m = codeRe.exec(enabledMatch[1])) !== null) {
147
+ rules.push(m[1]);
148
+ }
149
+ }
150
+ if (rules.length === 0) return null;
151
+ return { linter: "ruff", rules, via: "CLI" };
152
+ } catch {
153
+ return null;
154
+ }
155
+ }
156
+
157
+ function discoverPylintRules(basePath: string): DiscoveredRules | null {
158
+ try {
159
+ if (!hasPylintConfig(basePath)) return null;
160
+ execSync("which pylint", { stdio: "ignore" });
161
+ const output = execSync("pylint --list-msgs-enabled", {
162
+ encoding: "utf-8",
163
+ cwd: basePath,
164
+ stdio: ["pipe", "pipe", "pipe"],
165
+ timeout: 15000,
166
+ });
167
+ const disabledIdx = output.indexOf("Disabled messages:");
168
+ const enabledSection =
169
+ disabledIdx >= 0 ? output.substring(0, disabledIdx) : output;
170
+ // Extract rule IDs like (C0114), (W0611) etc.
171
+ const rules: string[] = [];
172
+ const idRe = /\(([A-Z]\d{4})\)/g;
173
+ let m: RegExpExecArray | null;
174
+ while ((m = idRe.exec(enabledSection)) !== null) {
175
+ rules.push(m[1]);
176
+ }
177
+ // Also extract symbolic names like "missing-module-docstring"
178
+ const nameRe = /^(\w[\w-]+)\s*\(/gm;
179
+ while ((m = nameRe.exec(enabledSection)) !== null) {
180
+ rules.push(m[1]);
181
+ }
182
+ if (rules.length === 0) return null;
183
+ return { linter: "pylint", rules, via: "CLI" };
184
+ } catch {
185
+ return null;
186
+ }
187
+ }
188
+
189
+ function discoverRubocopRules(basePath: string): DiscoveredRules | null {
190
+ try {
191
+ if (!hasRubocopConfig(basePath)) return null;
192
+ execSync("which rubocop", { stdio: "ignore" });
193
+ const output = execSync(
194
+ "rubocop --list-target-files --show-cops 2>/dev/null | head -500",
195
+ {
196
+ encoding: "utf-8",
197
+ cwd: basePath,
198
+ stdio: ["pipe", "pipe", "pipe"],
199
+ timeout: 15000,
200
+ shell: "/bin/sh",
201
+ },
202
+ );
203
+ const rules: string[] = [];
204
+ const copRe = /^(\w+\/\w+):/gm;
205
+ let m: RegExpExecArray | null;
206
+ while ((m = copRe.exec(output)) !== null) {
207
+ rules.push(m[1]);
208
+ }
209
+ if (rules.length === 0) return null;
210
+ return { linter: "rubocop", rules, via: "CLI" };
211
+ } catch {
212
+ return null;
213
+ }
214
+ }
215
+
216
+ function discoverClippyRules(basePath: string): DiscoveredRules | null {
217
+ try {
218
+ const cargoPath = resolve(basePath, "Cargo.toml");
219
+ if (!existsSync(cargoPath)) return null;
220
+ execSync("which cargo", { stdio: "ignore" });
221
+ // Clippy doesn't have a good "list enabled lints" command.
222
+ // We read Cargo.toml [lints.clippy] section for explicit config,
223
+ // and include default warn/deny lints from clippy -W clippy::all
224
+ const content = readFileSync(cargoPath, "utf-8");
225
+ const sectionMatch = content.match(/\[lints\.clippy\]([\s\S]*?)(?=\n\[|$)/);
226
+ const rules: string[] = [];
227
+ if (sectionMatch?.[1]) {
228
+ const ruleRe = /^(\w[\w-]+)\s*=\s*"(\w+)"/gm;
229
+ let m: RegExpExecArray | null;
230
+ while ((m = ruleRe.exec(sectionMatch[1])) !== null) {
231
+ if (m[2] !== "allow") {
232
+ rules.push(m[1]);
233
+ }
234
+ }
235
+ }
236
+ if (rules.length === 0) return null;
237
+ return { linter: "clippy", rules, via: "Cargo.toml" };
238
+ } catch {
239
+ return null;
240
+ }
241
+ }
242
+
243
+ // ---------------------------------------------------------------------------
244
+ // NPM script discovery
245
+ // ---------------------------------------------------------------------------
246
+
247
+ function discoverNpmScripts(basePath: string): string[] {
248
+ const pkgPath = resolve(basePath, "package.json");
249
+ if (!existsSync(pkgPath)) return [];
250
+ try {
251
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")) as {
252
+ scripts?: Record<string, string>;
253
+ };
254
+ return Object.keys(pkg.scripts ?? {});
255
+ } catch {
256
+ return [];
257
+ }
258
+ }
259
+
260
+ // ---------------------------------------------------------------------------
261
+ // Project file discovery
262
+ // ---------------------------------------------------------------------------
263
+
264
+ function discoverProjectFiles(
265
+ basePath: string,
266
+ globs: string[] = ["src/**/*"],
267
+ ): string[] {
268
+ const files: string[] = [];
269
+ for (const pattern of globs) {
270
+ const matches = globSync(pattern, {
271
+ cwd: basePath,
272
+ ignore: ["node_modules/**", "dist/**", ".git/**"],
273
+ nodir: true,
274
+ });
275
+ files.push(...matches);
276
+ }
277
+ return [...new Set(files)].sort();
278
+ }
279
+
280
+ // ---------------------------------------------------------------------------
281
+ // Type generation
282
+ // ---------------------------------------------------------------------------
283
+
284
+ function escapeForUnion(s: string): string {
285
+ return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
286
+ }
287
+
288
+ function formatUnion(items: string[], indent: string = " "): string {
289
+ if (items.length === 0) return "never";
290
+ if (items.length <= 3) {
291
+ return items.map((i) => `"${escapeForUnion(i)}"`).join(" | ");
292
+ }
293
+ return (
294
+ "\n" + items.map((i) => `${indent}| "${escapeForUnion(i)}"`).join("\n")
295
+ );
296
+ }
297
+
298
+ export interface GenerateTypesOptions {
299
+ basePath?: string;
300
+ fileGlobs?: string[];
301
+ }
302
+
303
+ export interface GenerateTypesResult {
304
+ dts: string;
305
+ linters: DiscoveredRules[];
306
+ scripts: string[];
307
+ files: string[];
308
+ }
309
+
310
+ /**
311
+ * Generate .d.ts content with type unions from actual project state.
312
+ *
313
+ * Scans all available linters, package.json scripts, and project files.
314
+ */
315
+ export function generateTypes(
316
+ options: GenerateTypesOptions = {},
317
+ ): GenerateTypesResult {
318
+ const basePath = options.basePath ?? process.cwd();
319
+ const fileGlobs = options.fileGlobs ?? ["src/**/*"];
320
+
321
+ // Discover everything
322
+ const linters: DiscoveredRules[] = [];
323
+
324
+ const eslint = discoverEslintRules(basePath);
325
+ if (eslint) linters.push(eslint);
326
+
327
+ const stylelint = discoverStylelintRules(basePath);
328
+ if (stylelint) linters.push(stylelint);
329
+
330
+ const ruff = discoverRuffRules(basePath);
331
+ if (ruff) linters.push(ruff);
332
+
333
+ const pylint = discoverPylintRules(basePath);
334
+ if (pylint) linters.push(pylint);
335
+
336
+ const rubocop = discoverRubocopRules(basePath);
337
+ if (rubocop) linters.push(rubocop);
338
+
339
+ const clippy = discoverClippyRules(basePath);
340
+ if (clippy) linters.push(clippy);
341
+
342
+ const scripts = discoverNpmScripts(basePath);
343
+ const files = discoverProjectFiles(basePath, fileGlobs);
344
+
345
+ // Generate .d.ts
346
+ const sections: string[] = [];
347
+
348
+ sections.push(`/**`);
349
+ sections.push(` * Auto-generated by \`vigiles generate-types\`.`);
350
+ sections.push(
351
+ ` * DO NOT EDIT — re-run \`vigiles generate-types\` to update.`,
352
+ );
353
+ sections.push(` */`);
354
+ sections.push(``);
355
+ // ---------------------------------------------------------------------------
356
+ // vigiles/generated — standalone types for direct import
357
+ // ---------------------------------------------------------------------------
358
+
359
+ sections.push(`declare module "vigiles/generated" {`);
360
+
361
+ // Linter rules
362
+ for (const { linter, rules, via } of linters) {
363
+ const typeName =
364
+ linter.charAt(0).toUpperCase() +
365
+ linter.slice(1).replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()) +
366
+ "Rule";
367
+ sections.push(``);
368
+ sections.push(
369
+ ` /** ${String(rules.length)} enabled ${linter} rules (via ${via}). */`,
370
+ );
371
+ sections.push(` export type ${typeName} = ${formatUnion(rules, " ")};`);
372
+ }
373
+
374
+ // Combined linter rule type
375
+ if (linters.length > 0) {
376
+ const allNames = linters.map(
377
+ (l) =>
378
+ l.linter.charAt(0).toUpperCase() +
379
+ l.linter
380
+ .slice(1)
381
+ .replace(/-([a-z])/g, (_, c: string) => c.toUpperCase()) +
382
+ "Rule",
383
+ );
384
+ sections.push(``);
385
+ sections.push(
386
+ ` /** All enabled linter rules across all detected linters. */`,
387
+ );
388
+ sections.push(` export type LinterRule = ${allNames.join(" | ")};`);
389
+ }
390
+
391
+ // NPM scripts
392
+ if (scripts.length > 0) {
393
+ sections.push(``);
394
+ sections.push(
395
+ ` /** ${String(scripts.length)} npm scripts from package.json. */`,
396
+ );
397
+ sections.push(` export type NpmScript = ${formatUnion(scripts, " ")};`);
398
+ }
399
+
400
+ // Project files
401
+ if (files.length > 0) {
402
+ sections.push(``);
403
+ sections.push(` /** ${String(files.length)} project files. */`);
404
+ sections.push(` export type ProjectFile = ${formatUnion(files, " ")};`);
405
+ }
406
+
407
+ sections.push(`}`);
408
+
409
+ // ---------------------------------------------------------------------------
410
+ // vigiles/spec augmentation — populates KnownLinterRules, KnownProjectFiles,
411
+ // KnownNpmScripts interfaces so enforce(), file(), cmd() narrow automatically.
412
+ // ---------------------------------------------------------------------------
413
+
414
+ sections.push(``);
415
+ sections.push(`declare module "vigiles/spec" {`);
416
+
417
+ if (linters.length > 0) {
418
+ sections.push(` interface KnownLinterRules {`);
419
+ for (const { linter, rules } of linters) {
420
+ sections.push(
421
+ ` "${escapeForUnion(linter)}": ${formatUnion(rules, " ")};`,
422
+ );
423
+ }
424
+ sections.push(` }`);
425
+ }
426
+
427
+ if (files.length > 0) {
428
+ sections.push(` interface KnownProjectFiles {`);
429
+ sections.push(` files: ${formatUnion(files, " ")};`);
430
+ sections.push(` }`);
431
+ }
432
+
433
+ if (scripts.length > 0) {
434
+ sections.push(` interface KnownNpmScripts {`);
435
+ sections.push(` scripts: ${formatUnion(scripts, " ")};`);
436
+ sections.push(` }`);
437
+ }
438
+
439
+ sections.push(`}`);
440
+ sections.push(``);
441
+
442
+ return {
443
+ dts: sections.join("\n"),
444
+ linters,
445
+ scripts,
446
+ files,
447
+ };
448
+ }
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Tests for the inline-rule parser.
3
+ */
4
+
5
+ import { describe, it } from "node:test";
6
+ import assert from "node:assert/strict";
7
+
8
+ import { parseInlineRules, hasInlineRules } from "./inline.js";
9
+
10
+ describe("parseInlineRules", () => {
11
+ it("extracts a single enforce comment", () => {
12
+ const { rules, errors } = parseInlineRules(
13
+ `# Project
14
+
15
+ <!-- vigiles:enforce eslint/no-console "Use structured logger" -->
16
+
17
+ ## Logging
18
+
19
+ All output through logger.ts.
20
+ `,
21
+ );
22
+ assert.equal(errors.length, 0);
23
+ assert.equal(rules.length, 1);
24
+ assert.equal(rules[0].linterRule, "eslint/no-console");
25
+ assert.equal(rules[0].why, "Use structured logger");
26
+ assert.equal(rules[0].line, 3);
27
+ });
28
+
29
+ it("extracts multiple enforce comments and tracks line numbers", () => {
30
+ const content = `# Doc
31
+ <!-- vigiles:enforce eslint/no-console "a" -->
32
+ text
33
+ <!-- vigiles:enforce ruff/F401 "b" -->
34
+ `;
35
+ const { rules, errors } = parseInlineRules(content);
36
+ assert.equal(errors.length, 0);
37
+ assert.equal(rules.length, 2);
38
+ assert.equal(rules[0].linterRule, "eslint/no-console");
39
+ assert.equal(rules[0].line, 2);
40
+ assert.equal(rules[1].linterRule, "ruff/F401");
41
+ assert.equal(rules[1].line, 4);
42
+ });
43
+
44
+ it("supports plugin-scoped rule names with slashes", () => {
45
+ const { rules, errors } = parseInlineRules(
46
+ `<!-- vigiles:enforce eslint/@typescript-eslint/no-floating-promises "Await or void" -->`,
47
+ );
48
+ assert.equal(errors.length, 0);
49
+ assert.equal(rules.length, 1);
50
+ assert.equal(
51
+ rules[0].linterRule,
52
+ "eslint/@typescript-eslint/no-floating-promises",
53
+ );
54
+ });
55
+
56
+ it("reports malformed vigiles:enforce as a parse error", () => {
57
+ const { rules, errors } = parseInlineRules(
58
+ `<!-- vigiles:enforce eslint/no-console no quotes here -->`,
59
+ );
60
+ assert.equal(rules.length, 0);
61
+ assert.equal(errors.length, 1);
62
+ assert.match(errors[0].message, /Malformed vigiles:enforce/);
63
+ });
64
+
65
+ it("reports unknown vigiles markers", () => {
66
+ const { rules, errors } = parseInlineRules(
67
+ `<!-- vigiles:strengthen eslint/no-console -->`,
68
+ );
69
+ assert.equal(rules.length, 0);
70
+ assert.equal(errors.length, 1);
71
+ assert.match(errors[0].message, /Unknown vigiles marker "strengthen"/);
72
+ });
73
+
74
+ it("returns empty results for a file with no vigiles comments", () => {
75
+ const { rules, errors } = parseInlineRules(
76
+ `# Plain\n\nJust prose, no rules.\n`,
77
+ );
78
+ assert.equal(rules.length, 0);
79
+ assert.equal(errors.length, 0);
80
+ });
81
+
82
+ it("skips the compiled-file hash marker without reporting an error", () => {
83
+ const { rules, errors } = parseInlineRules(
84
+ `<!-- vigiles:sha256:abc123 compiled from CLAUDE.md.spec.ts -->
85
+ # Project
86
+ <!-- vigiles:enforce eslint/no-console "why" -->
87
+ `,
88
+ );
89
+ assert.equal(errors.length, 0, JSON.stringify(errors));
90
+ assert.equal(rules.length, 1);
91
+ });
92
+
93
+ it("ignores vigiles:enforce markers inside fenced code blocks", () => {
94
+ // Illustrative example in docs/inline-mode.md would otherwise get
95
+ // picked up as a live rule.
96
+ const { rules, errors } = parseInlineRules(
97
+ `# Docs
98
+
99
+ Example usage:
100
+
101
+ \`\`\`md
102
+ <!-- vigiles:enforce eslint/no-console "Use structured logger" -->
103
+ <!-- vigiles:enforce ruff/F401 "No unused imports" -->
104
+ \`\`\`
105
+
106
+ And a real one outside the fence:
107
+
108
+ <!-- vigiles:enforce eslint/no-eval "Never eval user input" -->
109
+ `,
110
+ );
111
+ assert.equal(errors.length, 0, JSON.stringify(errors));
112
+ assert.equal(rules.length, 1);
113
+ assert.equal(rules[0].linterRule, "eslint/no-eval");
114
+ });
115
+
116
+ it("ignores markers inside tilde-fenced code blocks", () => {
117
+ const { rules } = parseInlineRules(
118
+ `~~~md
119
+ <!-- vigiles:enforce eslint/no-console "inside fence" -->
120
+ ~~~
121
+ <!-- vigiles:enforce eslint/no-eval "outside fence" -->
122
+ `,
123
+ );
124
+ assert.equal(rules.length, 1);
125
+ assert.equal(rules[0].linterRule, "eslint/no-eval");
126
+ });
127
+
128
+ it("ignores markers inside inline code spans in prose", () => {
129
+ // Prose that describes the syntax with a backtick-wrapped example
130
+ // must not trigger "malformed vigiles:enforce".
131
+ const { rules, errors } = parseInlineRules(
132
+ `You add \`<!-- vigiles:enforce eslint/no-console "why" -->\` comments to adopt gradually.
133
+
134
+ Real rule below:
135
+
136
+ <!-- vigiles:enforce eslint/no-eval "Never eval" -->
137
+ `,
138
+ );
139
+ assert.equal(errors.length, 0, JSON.stringify(errors));
140
+ assert.equal(rules.length, 1);
141
+ assert.equal(rules[0].linterRule, "eslint/no-eval");
142
+ });
143
+
144
+ it("ignores ellipsis placeholders inside inline code spans", () => {
145
+ const { rules, errors } = parseInlineRules(
146
+ `You add \`<!-- vigiles:enforce ... -->\` comments to adopt gradually.
147
+ `,
148
+ );
149
+ assert.equal(errors.length, 0, JSON.stringify(errors));
150
+ assert.equal(rules.length, 0);
151
+ });
152
+
153
+ it("handles nested backtick fences of different lengths", () => {
154
+ // Opening with 4 backticks, closing with 3 inside should NOT close
155
+ // the outer block — only a 4+ backtick close matches.
156
+ const { rules } = parseInlineRules(
157
+ `\`\`\`\`md
158
+ \`\`\`
159
+ <!-- vigiles:enforce eslint/no-console "inside outer" -->
160
+ \`\`\`
161
+ \`\`\`\`
162
+ <!-- vigiles:enforce eslint/no-eval "outside" -->
163
+ `,
164
+ );
165
+ assert.equal(rules.length, 1);
166
+ assert.equal(rules[0].linterRule, "eslint/no-eval");
167
+ });
168
+ });
169
+
170
+ describe("hasInlineRules", () => {
171
+ it("returns true when a valid enforce comment exists", () => {
172
+ assert.equal(
173
+ hasInlineRules(`<!-- vigiles:enforce eslint/no-console "why" -->`),
174
+ true,
175
+ );
176
+ });
177
+
178
+ it("returns false for plain markdown", () => {
179
+ assert.equal(hasInlineRules(`# Title\n\nparagraph\n`), false);
180
+ });
181
+
182
+ it("returns false for a malformed marker", () => {
183
+ assert.equal(
184
+ hasInlineRules(`<!-- vigiles:enforce eslint/no-console no-quotes -->`),
185
+ false,
186
+ );
187
+ });
188
+
189
+ it("returns false for markers trapped inside fenced code blocks", () => {
190
+ assert.equal(
191
+ hasInlineRules(
192
+ `# Docs\n\n\`\`\`md\n<!-- vigiles:enforce eslint/no-console "x" -->\n\`\`\`\n`,
193
+ ),
194
+ false,
195
+ );
196
+ });
197
+
198
+ it("returns true only for parseable rules outside fences", () => {
199
+ assert.equal(
200
+ hasInlineRules(
201
+ `\`\`\`md\n<!-- vigiles:enforce eslint/no-console "inside" -->\n\`\`\`\n<!-- vigiles:enforce eslint/no-eval "outside" -->\n`,
202
+ ),
203
+ true,
204
+ );
205
+ });
206
+ });