@ontrails/warden 1.0.0-beta.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.
- package/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-lint.log +3 -0
- package/.turbo/turbo-typecheck.log +1 -0
- package/CHANGELOG.md +21 -0
- package/README.md +132 -0
- package/dist/cli.d.ts +46 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +221 -0
- package/dist/cli.js.map +1 -0
- package/dist/drift.d.ts +26 -0
- package/dist/drift.d.ts.map +1 -0
- package/dist/drift.js +27 -0
- package/dist/drift.js.map +1 -0
- package/dist/formatters.d.ts +29 -0
- package/dist/formatters.d.ts.map +1 -0
- package/dist/formatters.js +87 -0
- package/dist/formatters.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/ast.d.ts +41 -0
- package/dist/rules/ast.d.ts.map +1 -0
- package/dist/rules/ast.js +163 -0
- package/dist/rules/ast.js.map +1 -0
- package/dist/rules/context-no-surface-types.d.ts +12 -0
- package/dist/rules/context-no-surface-types.d.ts.map +1 -0
- package/dist/rules/context-no-surface-types.js +96 -0
- package/dist/rules/context-no-surface-types.js.map +1 -0
- package/dist/rules/implementation-returns-result.d.ts +13 -0
- package/dist/rules/implementation-returns-result.d.ts.map +1 -0
- package/dist/rules/implementation-returns-result.js +231 -0
- package/dist/rules/implementation-returns-result.js.map +1 -0
- package/dist/rules/index.d.ts +22 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +41 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/no-direct-impl-in-route.d.ts +12 -0
- package/dist/rules/no-direct-impl-in-route.d.ts.map +1 -0
- package/dist/rules/no-direct-impl-in-route.js +46 -0
- package/dist/rules/no-direct-impl-in-route.js.map +1 -0
- package/dist/rules/no-direct-implementation-call.d.ts +12 -0
- package/dist/rules/no-direct-implementation-call.d.ts.map +1 -0
- package/dist/rules/no-direct-implementation-call.js +39 -0
- package/dist/rules/no-direct-implementation-call.js.map +1 -0
- package/dist/rules/no-sync-result-assumption.d.ts +6 -0
- package/dist/rules/no-sync-result-assumption.d.ts.map +1 -0
- package/dist/rules/no-sync-result-assumption.js +98 -0
- package/dist/rules/no-sync-result-assumption.js.map +1 -0
- package/dist/rules/no-throw-in-detour-target.d.ts +12 -0
- package/dist/rules/no-throw-in-detour-target.d.ts.map +1 -0
- package/dist/rules/no-throw-in-detour-target.js +87 -0
- package/dist/rules/no-throw-in-detour-target.js.map +1 -0
- package/dist/rules/no-throw-in-implementation.d.ts +9 -0
- package/dist/rules/no-throw-in-implementation.d.ts.map +1 -0
- package/dist/rules/no-throw-in-implementation.js +34 -0
- package/dist/rules/no-throw-in-implementation.js.map +1 -0
- package/dist/rules/prefer-schema-inference.d.ts +7 -0
- package/dist/rules/prefer-schema-inference.d.ts.map +1 -0
- package/dist/rules/prefer-schema-inference.js +86 -0
- package/dist/rules/prefer-schema-inference.js.map +1 -0
- package/dist/rules/scan.d.ts +8 -0
- package/dist/rules/scan.d.ts.map +1 -0
- package/dist/rules/scan.js +32 -0
- package/dist/rules/scan.js.map +1 -0
- package/dist/rules/specs.d.ts +29 -0
- package/dist/rules/specs.d.ts.map +1 -0
- package/dist/rules/specs.js +192 -0
- package/dist/rules/specs.js.map +1 -0
- package/dist/rules/structure.d.ts +13 -0
- package/dist/rules/structure.d.ts.map +1 -0
- package/dist/rules/structure.js +142 -0
- package/dist/rules/structure.js.map +1 -0
- package/dist/rules/types.d.ts +52 -0
- package/dist/rules/types.d.ts.map +1 -0
- package/dist/rules/types.js +2 -0
- package/dist/rules/types.js.map +1 -0
- package/dist/rules/valid-describe-refs.d.ts +7 -0
- package/dist/rules/valid-describe-refs.d.ts.map +1 -0
- package/dist/rules/valid-describe-refs.js +51 -0
- package/dist/rules/valid-describe-refs.js.map +1 -0
- package/dist/rules/valid-detour-refs.d.ts +6 -0
- package/dist/rules/valid-detour-refs.d.ts.map +1 -0
- package/dist/rules/valid-detour-refs.js +116 -0
- package/dist/rules/valid-detour-refs.js.map +1 -0
- package/package.json +25 -0
- package/src/__tests__/cli.test.ts +198 -0
- package/src/__tests__/drift.test.ts +74 -0
- package/src/__tests__/formatters.test.ts +157 -0
- package/src/__tests__/implementation-returns-result.test.ts +75 -0
- package/src/__tests__/no-direct-implementation-call.test.ts +83 -0
- package/src/__tests__/no-sync-result-assumption.test.ts +85 -0
- package/src/__tests__/no-throw-in-detour-target.test.ts +78 -0
- package/src/__tests__/prefer-schema-inference.test.ts +84 -0
- package/src/__tests__/rules.test.ts +188 -0
- package/src/__tests__/valid-describe-refs.test.ts +60 -0
- package/src/cli.ts +343 -0
- package/src/drift.ts +50 -0
- package/src/formatters.ts +113 -0
- package/src/index.ts +47 -0
- package/src/rules/ast.ts +217 -0
- package/src/rules/context-no-surface-types.ts +150 -0
- package/src/rules/implementation-returns-result.ts +343 -0
- package/src/rules/index.ts +54 -0
- package/src/rules/no-direct-impl-in-route.ts +77 -0
- package/src/rules/no-direct-implementation-call.ts +47 -0
- package/src/rules/no-sync-result-assumption.ts +156 -0
- package/src/rules/no-throw-in-detour-target.ts +150 -0
- package/src/rules/no-throw-in-implementation.ts +41 -0
- package/src/rules/prefer-schema-inference.ts +141 -0
- package/src/rules/scan.ts +46 -0
- package/src/rules/specs.ts +384 -0
- package/src/rules/structure.ts +234 -0
- package/src/rules/types.ts +62 -0
- package/src/rules/valid-describe-refs.ts +94 -0
- package/src/rules/valid-detour-refs.ts +187 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
$ tsc -b
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
$ tsc --noEmit
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @ontrails/warden
|
|
2
|
+
|
|
3
|
+
## 1.0.0-beta.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Initial v1 beta release of the Trails framework.
|
|
8
|
+
|
|
9
|
+
- **@ontrails/core** — Result type, error taxonomy, trail/hike/event/topo, validateTopo, validateInput/Output, deriveFields, patterns, redaction, branded types, resilience
|
|
10
|
+
- **@ontrails/cli** — CLI surface adapter, Commander integration, flag derivation, layers
|
|
11
|
+
- **@ontrails/mcp** — MCP surface adapter, tool generation, annotations, progress bridge
|
|
12
|
+
- **@ontrails/logging** — Structured logging, sinks, formatters, LogTape adapter
|
|
13
|
+
- **@ontrails/testing** — testAll, testExamples, testTrail, testHike, testContracts, testDetours, surface harnesses
|
|
14
|
+
- **@ontrails/warden** — AST-based code convention rules via oxc-parser, drift detection, CI formatters
|
|
15
|
+
- **@ontrails/schema** — Surface map generation, hashing, semantic diffing
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies
|
|
20
|
+
- @ontrails/core@1.0.0-beta.0
|
|
21
|
+
- @ontrails/schema@1.0.0-beta.0
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @ontrails/warden
|
|
2
|
+
|
|
3
|
+
Governance and contract enforcement for Trails. Lint rules that keep agents (and humans) on trails, surface lock drift detection, and a CLI runner for CI gating.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add -d @ontrails/warden
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependencies: `@ontrails/core`, `@ontrails/schema`.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
From the Trails CLI:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
trails warden # Run all checks
|
|
19
|
+
trails warden --exit-code # Non-zero exit on errors or drift
|
|
20
|
+
trails warden --lint-only # Skip drift detection
|
|
21
|
+
trails warden --drift-only # Skip lint rules
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Or programmatically:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { runWarden, formatWardenReport } from '@ontrails/warden';
|
|
28
|
+
|
|
29
|
+
const report = await runWarden(app, { exitCode: true });
|
|
30
|
+
console.log(formatWardenReport(report));
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Lint Rules
|
|
34
|
+
|
|
35
|
+
10 built-in rules under the `trails/` namespace:
|
|
36
|
+
|
|
37
|
+
| Rule | Severity | What it catches |
|
|
38
|
+
| --- | --- | --- |
|
|
39
|
+
| `no-throw-in-implementation` | error | `throw` statements inside `implementation` bodies |
|
|
40
|
+
| `context-no-surface-types` | error | Surface-specific type imports in trail files |
|
|
41
|
+
| `prefer-schema-inference` | warn | `fields` overrides that only repeat schema-derived labels or enum options |
|
|
42
|
+
| `valid-describe-refs` | warn | `@see` references inside `.describe()` strings that do not resolve |
|
|
43
|
+
| `valid-detour-refs` | error | Detour targets that do not exist in the topo |
|
|
44
|
+
| `no-direct-implementation-call` | warn | Direct `.implementation()` calls in application code |
|
|
45
|
+
| `no-sync-result-assumption` | error | Missing `await` on `.implementation()` results |
|
|
46
|
+
| `implementation-returns-result` | error | Implementations that return raw values instead of `Result` |
|
|
47
|
+
| `no-throw-in-detour-target` | error | `throw` inside trails that serve as detour targets |
|
|
48
|
+
| `no-direct-impl-in-route` | warn | Direct `.implementation()` calls inside hike bodies |
|
|
49
|
+
|
|
50
|
+
Several structural checks (follows existence, recursive follows, event origins, example schema validation, output schema presence) are handled by `validateTopo()` in `@ontrails/core`. Follows coverage (matching `follows` declarations to actual `ctx.follow()` calls) is verified by `testExamples()` in `@ontrails/testing`.
|
|
51
|
+
|
|
52
|
+
### Configuration
|
|
53
|
+
|
|
54
|
+
Add to `.oxlintrc.json`:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"plugins": ["trails"],
|
|
59
|
+
"rules": {
|
|
60
|
+
"trails/no-throw-in-implementation": "error",
|
|
61
|
+
"trails/context-no-surface-types": "error",
|
|
62
|
+
"trails/valid-describe-refs": "warn",
|
|
63
|
+
"trails/valid-detour-refs": "error",
|
|
64
|
+
"trails/no-sync-result-assumption": "error",
|
|
65
|
+
"trails/implementation-returns-result": "error",
|
|
66
|
+
"trails/no-throw-in-detour-target": "error",
|
|
67
|
+
"trails/no-direct-implementation-call": "warn",
|
|
68
|
+
"trails/no-direct-impl-in-route": "warn",
|
|
69
|
+
"trails/prefer-schema-inference": "warn"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Key Rules
|
|
75
|
+
|
|
76
|
+
**`no-throw-in-implementation`** -- Implementations return `Result.err()`, never `throw`. Flags `ThrowStatement` nodes inside `implementation` function bodies.
|
|
77
|
+
|
|
78
|
+
**`context-no-surface-types`** -- Implementations are pure functions. Importing `Request`, `Response`, `McpSession`, or other surface types couples domain logic to a transport.
|
|
79
|
+
|
|
80
|
+
**`prefer-schema-inference`** -- `fields` is for enrichment, not repetition. If a label or enum options are already derivable from the Zod schema, remove the redundant override and let `derive()` supply it.
|
|
81
|
+
|
|
82
|
+
**`no-direct-implementation-call`** -- Direct `.implementation()` calls bypass validation, tracing, and layers. Application code should use `ctx.follow()` instead.
|
|
83
|
+
|
|
84
|
+
**`no-sync-result-assumption`** -- Trail implementations normalize to `Promise<Result>`, even when the author wrote a sync body. Callers must `await` before using `.isOk()`, `.value`, or other `Result` APIs.
|
|
85
|
+
|
|
86
|
+
**`valid-describe-refs`** -- `@see` tags inside schema `.describe()` strings are part of the contract surface. Warden warns when those references drift away from the actual topo.
|
|
87
|
+
|
|
88
|
+
## Drift Detection
|
|
89
|
+
|
|
90
|
+
Warden integrates with `@ontrails/schema` to detect when the topo has changed without updating `surface.lock`:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { checkDrift } from '@ontrails/warden';
|
|
94
|
+
|
|
95
|
+
const drift = await checkDrift(app);
|
|
96
|
+
if (drift.stale) {
|
|
97
|
+
console.log(
|
|
98
|
+
'surface.lock is stale -- regenerate with `trails survey generate`'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The check regenerates the surface map, hashes it, and compares against the committed `surface.lock`.
|
|
104
|
+
|
|
105
|
+
## CI Integration
|
|
106
|
+
|
|
107
|
+
Add to lefthook for pre-push enforcement:
|
|
108
|
+
|
|
109
|
+
```yaml
|
|
110
|
+
# lefthook.yml
|
|
111
|
+
pre-push:
|
|
112
|
+
commands:
|
|
113
|
+
warden:
|
|
114
|
+
run: trails warden --exit-code
|
|
115
|
+
tags: governance
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## API
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import {
|
|
122
|
+
runWarden,
|
|
123
|
+
formatWardenReport,
|
|
124
|
+
checkDrift,
|
|
125
|
+
wardenRules,
|
|
126
|
+
} from '@ontrails/warden';
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Further Reading
|
|
130
|
+
|
|
131
|
+
- [Architecture](../../docs/architecture.md)
|
|
132
|
+
- [Testing Guide](../../docs/testing.md)
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Warden CLI command runner.
|
|
3
|
+
*
|
|
4
|
+
* Scans TypeScript files, runs all warden rules, optionally checks drift,
|
|
5
|
+
* and returns a structured report.
|
|
6
|
+
*/
|
|
7
|
+
import type { Topo } from '@ontrails/core';
|
|
8
|
+
import type { DriftResult } from './drift.js';
|
|
9
|
+
import type { WardenDiagnostic } from './rules/types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Options for the warden CLI runner.
|
|
12
|
+
*/
|
|
13
|
+
export interface WardenOptions {
|
|
14
|
+
/** Root directory to scan for TypeScript files. Defaults to cwd. */
|
|
15
|
+
readonly rootDir?: string | undefined;
|
|
16
|
+
/** Only run lint rules, skip drift detection */
|
|
17
|
+
readonly lintOnly?: boolean | undefined;
|
|
18
|
+
/** Only run drift detection, skip lint rules */
|
|
19
|
+
readonly driftOnly?: boolean | undefined;
|
|
20
|
+
/** App topology for drift detection. When provided, enables real surface lock comparison. */
|
|
21
|
+
readonly topo?: Topo | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Result of a warden run.
|
|
25
|
+
*/
|
|
26
|
+
export interface WardenReport {
|
|
27
|
+
/** All diagnostics from lint rules */
|
|
28
|
+
readonly diagnostics: readonly WardenDiagnostic[];
|
|
29
|
+
/** Count of error-severity diagnostics */
|
|
30
|
+
readonly errorCount: number;
|
|
31
|
+
/** Count of warn-severity diagnostics */
|
|
32
|
+
readonly warnCount: number;
|
|
33
|
+
/** Drift detection result, or null if skipped */
|
|
34
|
+
readonly drift: DriftResult | null;
|
|
35
|
+
/** Whether the warden run passed (no errors, no drift) */
|
|
36
|
+
readonly passed: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Run all warden checks and return a structured report.
|
|
40
|
+
*/
|
|
41
|
+
export declare const runWarden: (options?: WardenOptions) => Promise<WardenReport>;
|
|
42
|
+
/**
|
|
43
|
+
* Format a warden report as a human-readable string.
|
|
44
|
+
*/
|
|
45
|
+
export declare const formatWardenReport: (report: WardenReport) => string;
|
|
46
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAS9C,OAAO,KAAK,EAGV,gBAAgB,EAEjB,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,oEAAoE;IACpE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,gDAAgD;IAChD,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACxC,gDAAgD;IAChD,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACzC,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,SAAS,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,sCAAsC;IACtC,QAAQ,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,CAAC;IAClD,0CAA0C;IAC1C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,yCAAyC;IACzC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,iDAAiD;IACjD,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IACnC,0DAA0D;IAC1D,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAyLD;;GAEG;AACH,eAAO,MAAM,SAAS,GACpB,UAAS,aAAkB,KAC1B,OAAO,CAAC,YAAY,CAqBtB,CAAC;AAsDF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,YAAY,KAAG,MAmBzD,CAAC"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Warden CLI command runner.
|
|
3
|
+
*
|
|
4
|
+
* Scans TypeScript files, runs all warden rules, optionally checks drift,
|
|
5
|
+
* and returns a structured report.
|
|
6
|
+
*/
|
|
7
|
+
import { resolve } from 'node:path';
|
|
8
|
+
import { checkDrift } from './drift.js';
|
|
9
|
+
import { findConfigProperty, findTrailDefinitions, parse, walk, } from './rules/ast.js';
|
|
10
|
+
import { wardenRules } from './rules/index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Collect all .ts files under a directory, excluding node_modules, dist, and .git.
|
|
13
|
+
*/
|
|
14
|
+
const isSourceFile = (match) => !match.endsWith('.d.ts') &&
|
|
15
|
+
!match.startsWith('node_modules/') &&
|
|
16
|
+
!match.startsWith('dist/') &&
|
|
17
|
+
!match.startsWith('.git/') &&
|
|
18
|
+
!match.includes('__tests__/') &&
|
|
19
|
+
!match.includes('__test__/') &&
|
|
20
|
+
!match.endsWith('.test.ts') &&
|
|
21
|
+
!match.endsWith('.spec.ts');
|
|
22
|
+
const collectTsFiles = (dir) => {
|
|
23
|
+
const glob = new Bun.Glob('**/*.ts');
|
|
24
|
+
let matches;
|
|
25
|
+
try {
|
|
26
|
+
matches = glob.scanSync({ cwd: dir, dot: false, onlyFiles: true });
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const files = [];
|
|
32
|
+
for (const match of matches) {
|
|
33
|
+
if (isSourceFile(match)) {
|
|
34
|
+
files.push(`${dir}/${match}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return files;
|
|
38
|
+
};
|
|
39
|
+
const collectKnownTrailIds = (sourceCode, filePath, knownTrailIds) => {
|
|
40
|
+
const ast = parse(filePath, sourceCode);
|
|
41
|
+
if (!ast) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
for (const def of findTrailDefinitions(ast)) {
|
|
45
|
+
knownTrailIds.add(def.id);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const collectDetourTargetTrailIds = (sourceCode, filePath, detourTargetTrailIds) => {
|
|
49
|
+
const ast = parse(filePath, sourceCode);
|
|
50
|
+
if (!ast) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const def of findTrailDefinitions(ast)) {
|
|
54
|
+
const detoursProp = findConfigProperty(def.config, 'detours');
|
|
55
|
+
if (!detoursProp) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
// Walk the detours value for string literals that look like trail IDs
|
|
59
|
+
walk(detoursProp, (node) => {
|
|
60
|
+
if (node.type !== 'Literal') {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const val = node.value;
|
|
64
|
+
if (val && val.includes('.')) {
|
|
65
|
+
detourTargetTrailIds.add(val);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const loadSourceFiles = async (rootDir) => {
|
|
71
|
+
const sourceFiles = [];
|
|
72
|
+
for (const filePath of collectTsFiles(rootDir)) {
|
|
73
|
+
try {
|
|
74
|
+
sourceFiles.push({
|
|
75
|
+
filePath,
|
|
76
|
+
sourceCode: await Bun.file(filePath).text(),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return sourceFiles;
|
|
84
|
+
};
|
|
85
|
+
const buildProjectContextFromTopo = (appTopo) => {
|
|
86
|
+
const knownTrailIds = new Set([
|
|
87
|
+
...appTopo.trails.keys(),
|
|
88
|
+
...appTopo.hikes.keys(),
|
|
89
|
+
]);
|
|
90
|
+
const detourTargetTrailIds = new Set();
|
|
91
|
+
for (const t of appTopo.trails.values()) {
|
|
92
|
+
const detours = t['detours'];
|
|
93
|
+
if (!detours) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
for (const targets of Object.values(detours)) {
|
|
97
|
+
for (const id of targets) {
|
|
98
|
+
detourTargetTrailIds.add(id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { detourTargetTrailIds, knownTrailIds };
|
|
103
|
+
};
|
|
104
|
+
const buildProjectContextFromFiles = (sourceFiles) => {
|
|
105
|
+
const knownTrailIds = new Set();
|
|
106
|
+
const detourTargetTrailIds = new Set();
|
|
107
|
+
for (const sourceFile of sourceFiles) {
|
|
108
|
+
collectKnownTrailIds(sourceFile.sourceCode, sourceFile.filePath, knownTrailIds);
|
|
109
|
+
collectDetourTargetTrailIds(sourceFile.sourceCode, sourceFile.filePath, detourTargetTrailIds);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
detourTargetTrailIds,
|
|
113
|
+
knownTrailIds,
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
const isProjectAwareRule = (rule) => 'checkWithContext' in rule;
|
|
117
|
+
/**
|
|
118
|
+
* Lint all files against all warden rules.
|
|
119
|
+
*/
|
|
120
|
+
const lintFiles = async (rootDir, appTopo) => {
|
|
121
|
+
const allDiagnostics = [];
|
|
122
|
+
const sourceFiles = await loadSourceFiles(rootDir);
|
|
123
|
+
const context = appTopo
|
|
124
|
+
? buildProjectContextFromTopo(appTopo)
|
|
125
|
+
: buildProjectContextFromFiles(sourceFiles);
|
|
126
|
+
for (const sourceFile of sourceFiles) {
|
|
127
|
+
for (const rule of wardenRules.values()) {
|
|
128
|
+
if (isProjectAwareRule(rule)) {
|
|
129
|
+
allDiagnostics.push(...rule.checkWithContext(sourceFile.sourceCode, sourceFile.filePath, context));
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
allDiagnostics.push(...rule.check(sourceFile.sourceCode, sourceFile.filePath));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return allDiagnostics;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Run all warden checks and return a structured report.
|
|
139
|
+
*/
|
|
140
|
+
export const runWarden = async (options = {}) => {
|
|
141
|
+
const rootDir = resolve(options.rootDir ?? process.cwd());
|
|
142
|
+
const allDiagnostics = options.driftOnly
|
|
143
|
+
? []
|
|
144
|
+
: await lintFiles(rootDir, options.topo);
|
|
145
|
+
const drift = options.lintOnly
|
|
146
|
+
? null
|
|
147
|
+
: await checkDrift(rootDir, options.topo);
|
|
148
|
+
const errorCount = allDiagnostics.filter((d) => d.severity === 'error').length;
|
|
149
|
+
const warnCount = allDiagnostics.filter((d) => d.severity === 'warn').length;
|
|
150
|
+
return {
|
|
151
|
+
diagnostics: allDiagnostics,
|
|
152
|
+
drift,
|
|
153
|
+
errorCount,
|
|
154
|
+
passed: errorCount === 0 && !(drift?.stale ?? false),
|
|
155
|
+
warnCount,
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Format the lint section of the report.
|
|
160
|
+
*/
|
|
161
|
+
const formatLintSection = (report) => {
|
|
162
|
+
if (report.diagnostics.length === 0) {
|
|
163
|
+
return ['Lint: clean'];
|
|
164
|
+
}
|
|
165
|
+
const lines = [
|
|
166
|
+
`Lint: ${report.errorCount} errors, ${report.warnCount} warnings`,
|
|
167
|
+
];
|
|
168
|
+
for (const d of report.diagnostics) {
|
|
169
|
+
const prefix = d.severity === 'error' ? 'ERROR' : 'WARN';
|
|
170
|
+
lines.push(` ${d.filePath}:${String(d.line)} [${prefix}] ${d.rule} ${d.message}`);
|
|
171
|
+
}
|
|
172
|
+
return lines;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Format the drift section of the report.
|
|
176
|
+
*/
|
|
177
|
+
const formatDriftSection = (drift) => {
|
|
178
|
+
if (drift === null) {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
const label = drift.stale
|
|
182
|
+
? 'Drift: surface.lock is stale (regenerate with `trails survey generate`)'
|
|
183
|
+
: 'Drift: clean';
|
|
184
|
+
return [label, ''];
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Format the result line.
|
|
188
|
+
*/
|
|
189
|
+
const formatResultLine = (report) => {
|
|
190
|
+
if (report.passed) {
|
|
191
|
+
return 'Result: PASS';
|
|
192
|
+
}
|
|
193
|
+
const parts = [];
|
|
194
|
+
if (report.errorCount > 0) {
|
|
195
|
+
parts.push(`${report.errorCount} errors`);
|
|
196
|
+
}
|
|
197
|
+
if (report.drift?.stale) {
|
|
198
|
+
parts.push('drift detected');
|
|
199
|
+
}
|
|
200
|
+
return `Result: FAIL (${parts.join(', ')})`;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Format a warden report as a human-readable string.
|
|
204
|
+
*/
|
|
205
|
+
export const formatWardenReport = (report) => {
|
|
206
|
+
const lintLines = formatLintSection(report);
|
|
207
|
+
const driftLines = formatDriftSection(report.drift);
|
|
208
|
+
if (lintLines.length === 0 && driftLines.length === 0) {
|
|
209
|
+
return ['Warden Report', '=============', '', 'No checks were run.'].join('\n');
|
|
210
|
+
}
|
|
211
|
+
return [
|
|
212
|
+
'Warden Report',
|
|
213
|
+
'=============',
|
|
214
|
+
'',
|
|
215
|
+
...lintLines,
|
|
216
|
+
'',
|
|
217
|
+
...driftLines,
|
|
218
|
+
formatResultLine(report),
|
|
219
|
+
].join('\n');
|
|
220
|
+
};
|
|
221
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,EACL,IAAI,GACL,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAsC/C;;GAEG;AACH,MAAM,YAAY,GAAG,CAAC,KAAa,EAAW,EAAE,CAC9C,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC;IACxB,CAAC,KAAK,CAAC,UAAU,CAAC,eAAe,CAAC;IAClC,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC;IAC1B,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC;IAC1B,CAAC,KAAK,CAAC,QAAQ,CAAC,YAAY,CAAC;IAC7B,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC;IAC5B,CAAC,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC;I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ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface lock drift detection.
|
|
3
|
+
*
|
|
4
|
+
* Compares the committed `surface.lock` hash against a freshly generated
|
|
5
|
+
* surface map hash to detect when the trail topology has changed without
|
|
6
|
+
* updating the lock file.
|
|
7
|
+
*/
|
|
8
|
+
import type { Topo } from '@ontrails/core';
|
|
9
|
+
/**
|
|
10
|
+
* Result of a drift check comparing committed surface.lock against the current state.
|
|
11
|
+
*/
|
|
12
|
+
export interface DriftResult {
|
|
13
|
+
/** Whether the committed lock is out of date */
|
|
14
|
+
readonly stale: boolean;
|
|
15
|
+
/** Hash from the committed surface.lock file, or null if not found */
|
|
16
|
+
readonly committedHash: string | null;
|
|
17
|
+
/** Hash computed from the current trail topology */
|
|
18
|
+
readonly currentHash: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Check whether the committed surface.lock is stale compared to the current topology.
|
|
22
|
+
*
|
|
23
|
+
* When no topo is provided, returns a clean result (no drift detectable without runtime info).
|
|
24
|
+
*/
|
|
25
|
+
export declare const checkDrift: (rootDir: string, topo?: Topo | undefined) => Promise<DriftResult>;
|
|
26
|
+
//# sourceMappingURL=drift.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drift.d.ts","sourceRoot":"","sources":["../src/drift.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAO3C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,sEAAsE;IACtE,QAAQ,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,oDAAoD;IACpD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAED;;;;GAIG;AACH,eAAO,MAAM,UAAU,GACrB,SAAS,MAAM,EACf,OAAO,IAAI,GAAG,SAAS,KACtB,OAAO,CAAC,WAAW,CAcrB,CAAC"}
|
package/dist/drift.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface lock drift detection.
|
|
3
|
+
*
|
|
4
|
+
* Compares the committed `surface.lock` hash against a freshly generated
|
|
5
|
+
* surface map hash to detect when the trail topology has changed without
|
|
6
|
+
* updating the lock file.
|
|
7
|
+
*/
|
|
8
|
+
import { generateSurfaceMap, hashSurfaceMap, readSurfaceLock, } from '@ontrails/schema';
|
|
9
|
+
/**
|
|
10
|
+
* Check whether the committed surface.lock is stale compared to the current topology.
|
|
11
|
+
*
|
|
12
|
+
* When no topo is provided, returns a clean result (no drift detectable without runtime info).
|
|
13
|
+
*/
|
|
14
|
+
export const checkDrift = async (rootDir, topo) => {
|
|
15
|
+
if (!topo) {
|
|
16
|
+
return { committedHash: null, currentHash: 'unknown', stale: false };
|
|
17
|
+
}
|
|
18
|
+
const surfaceMap = generateSurfaceMap(topo);
|
|
19
|
+
const currentHash = hashSurfaceMap(surfaceMap);
|
|
20
|
+
const committedHash = await readSurfaceLock({ dir: rootDir });
|
|
21
|
+
return {
|
|
22
|
+
committedHash,
|
|
23
|
+
currentHash,
|
|
24
|
+
stale: committedHash !== null && committedHash !== currentHash,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=drift.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drift.js","sourceRoot":"","sources":["../src/drift.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,eAAe,GAChB,MAAM,kBAAkB,CAAC;AAc1B;;;;GAIG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,OAAe,EACf,IAAuB,EACD,EAAE;IACxB,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IACvE,CAAC;IAED,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC;IAC5C,MAAM,WAAW,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;IAC/C,MAAM,aAAa,GAAG,MAAM,eAAe,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;IAE9D,OAAO;QACL,aAAa;QACb,WAAW;QACX,KAAK,EAAE,aAAa,KAAK,IAAI,IAAI,aAAa,KAAK,WAAW;KAC/D,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CI-oriented formatters for warden reports.
|
|
3
|
+
*
|
|
4
|
+
* Each formatter takes a `WardenReport` and produces output suited to a
|
|
5
|
+
* specific CI environment: GitHub Actions annotations, structured JSON,
|
|
6
|
+
* or a concise markdown summary.
|
|
7
|
+
*/
|
|
8
|
+
import type { WardenReport } from './cli.js';
|
|
9
|
+
/**
|
|
10
|
+
* Produce GitHub Actions workflow command annotations, one per diagnostic.
|
|
11
|
+
*
|
|
12
|
+
* Severity mapping: `error` to `::error`, `warn` to `::warning`.
|
|
13
|
+
* Drift staleness is emitted as a single `::error` annotation when detected.
|
|
14
|
+
*/
|
|
15
|
+
export declare const formatGitHubAnnotations: (report: WardenReport) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Produce a structured JSON string from the report.
|
|
18
|
+
*
|
|
19
|
+
* Includes a `summary` object with error, warning, and suggestion counts
|
|
20
|
+
* for easy consumption by downstream tooling.
|
|
21
|
+
*/
|
|
22
|
+
export declare const formatJson: (report: WardenReport) => string;
|
|
23
|
+
/**
|
|
24
|
+
* Produce a concise markdown summary suitable for a GitHub job summary or PR comment.
|
|
25
|
+
*
|
|
26
|
+
* Groups diagnostics by severity and includes drift status when relevant.
|
|
27
|
+
*/
|
|
28
|
+
export declare const formatSummary: (report: WardenReport) => string;
|
|
29
|
+
//# sourceMappingURL=formatters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAS7C;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,GAAI,QAAQ,YAAY,KAAG,MAiB9D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ,YAAY,KAAG,MAiBjD,CAAC;AA6BF;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,YAAY,KAAG,MAapD,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CI-oriented formatters for warden reports.
|
|
3
|
+
*
|
|
4
|
+
* Each formatter takes a `WardenReport` and produces output suited to a
|
|
5
|
+
* specific CI environment: GitHub Actions annotations, structured JSON,
|
|
6
|
+
* or a concise markdown summary.
|
|
7
|
+
*/
|
|
8
|
+
/** Map warden severity to GitHub Actions annotation level. */
|
|
9
|
+
const ghLevel = {
|
|
10
|
+
error: 'error',
|
|
11
|
+
warn: 'warning',
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Produce GitHub Actions workflow command annotations, one per diagnostic.
|
|
15
|
+
*
|
|
16
|
+
* Severity mapping: `error` to `::error`, `warn` to `::warning`.
|
|
17
|
+
* Drift staleness is emitted as a single `::error` annotation when detected.
|
|
18
|
+
*/
|
|
19
|
+
export const formatGitHubAnnotations = (report) => {
|
|
20
|
+
const lines = [];
|
|
21
|
+
for (const d of report.diagnostics) {
|
|
22
|
+
const level = ghLevel[d.severity];
|
|
23
|
+
lines.push(`::${level} file=${d.filePath},line=${String(d.line)}::${d.rule}: ${d.message}`);
|
|
24
|
+
}
|
|
25
|
+
if (report.drift?.stale) {
|
|
26
|
+
lines.push('::error::drift: surface.lock is stale (regenerate with `trails survey generate`)');
|
|
27
|
+
}
|
|
28
|
+
return lines.join('\n');
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Produce a structured JSON string from the report.
|
|
32
|
+
*
|
|
33
|
+
* Includes a `summary` object with error, warning, and suggestion counts
|
|
34
|
+
* for easy consumption by downstream tooling.
|
|
35
|
+
*/
|
|
36
|
+
export const formatJson = (report) => {
|
|
37
|
+
const summary = {
|
|
38
|
+
errors: report.errorCount,
|
|
39
|
+
suggestions: 0,
|
|
40
|
+
warnings: report.warnCount,
|
|
41
|
+
};
|
|
42
|
+
return JSON.stringify({
|
|
43
|
+
diagnostics: report.diagnostics,
|
|
44
|
+
drift: report.drift,
|
|
45
|
+
passed: report.passed,
|
|
46
|
+
summary,
|
|
47
|
+
}, null, 2);
|
|
48
|
+
};
|
|
49
|
+
/** Format a diagnostic as a markdown list item. */
|
|
50
|
+
const diagnosticLine = (d) => `- \`${d.filePath}:${String(d.line)}\` — ${d.rule}: ${d.message}`;
|
|
51
|
+
/** Render a severity group as a headed markdown section, or empty array. */
|
|
52
|
+
const severitySection = (heading, diagnostics) => {
|
|
53
|
+
if (diagnostics.length === 0) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
return ['', `### ${heading}`, ...diagnostics.map(diagnosticLine)];
|
|
57
|
+
};
|
|
58
|
+
/** Render a drift section if stale, otherwise empty array. */
|
|
59
|
+
const driftSection = (drift) => {
|
|
60
|
+
if (!drift?.stale) {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
return [
|
|
64
|
+
'',
|
|
65
|
+
'### Drift',
|
|
66
|
+
'- surface.lock is stale (regenerate with `trails survey generate`)',
|
|
67
|
+
];
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Produce a concise markdown summary suitable for a GitHub job summary or PR comment.
|
|
71
|
+
*
|
|
72
|
+
* Groups diagnostics by severity and includes drift status when relevant.
|
|
73
|
+
*/
|
|
74
|
+
export const formatSummary = (report) => {
|
|
75
|
+
const result = report.passed ? 'PASS' : 'FAIL';
|
|
76
|
+
const errors = report.diagnostics.filter((d) => d.severity === 'error');
|
|
77
|
+
const warnings = report.diagnostics.filter((d) => d.severity === 'warn');
|
|
78
|
+
return [
|
|
79
|
+
'## Warden Report',
|
|
80
|
+
'',
|
|
81
|
+
`**Result: ${result}** | ${String(report.errorCount)} errors, ${String(report.warnCount)} warnings`,
|
|
82
|
+
...severitySection('Errors', errors),
|
|
83
|
+
...severitySection('Warnings', warnings),
|
|
84
|
+
...driftSection(report.drift),
|
|
85
|
+
].join('\n');
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=formatters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatters.js","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,8DAA8D;AAC9D,MAAM,OAAO,GAAmC;IAC9C,KAAK,EAAE,OAAO;IACd,IAAI,EAAE,SAAS;CAChB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,MAAoB,EAAU,EAAE;IACtE,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;QACnC,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QAClC,KAAK,CAAC,IAAI,CACR,KAAK,KAAK,SAAS,CAAC,CAAC,QAAQ,SAAS,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,OAAO,EAAE,CAChF,CAAC;IACJ,CAAC;IAED,IAAI,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC;QACxB,KAAK,CAAC,IAAI,CACR,kFAAkF,CACnF,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAA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EAAE;IAC5D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;IAC/C,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IACxE,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,MAAM,CAAC,CAAC;IAEzE,OAAO;QACL,kBAAkB;QAClB,EAAE;QACF,aAAa,MAAM,QAAQ,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,YAAY,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;QACnG,GAAG,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC;QACpC,GAAG,eAAe,CAAC,UAAU,EAAE,QAAQ,CAAC;QACxC,GAAG,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC;KAC9B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Warden - Governance package for Trails.
|
|
3
|
+
*
|
|
4
|
+
* Provides lint rules, drift detection, and a CLI runner to enforce
|
|
5
|
+
* contract-first discipline at development time.
|
|
6
|
+
*
|
|
7
|
+
* Package: `@ontrails/warden`
|
|
8
|
+
*/
|
|
9
|
+
export type { ProjectAwareWardenRule, ProjectContext, WardenDiagnostic, WardenRule, WardenSeverity, } from './rules/index.js';
|
|
10
|
+
export { noThrowInImplementation } from './rules/no-throw-in-implementation.js';
|
|
11
|
+
export { contextNoSurfaceTypes } from './rules/context-no-surface-types.js';
|
|
12
|
+
export { validDetourRefs } from './rules/valid-detour-refs.js';
|
|
13
|
+
export { noDirectImplInRoute } from './rules/no-direct-impl-in-route.js';
|
|
14
|
+
export { noDirectImplementationCall } from './rules/no-direct-implementation-call.js';
|
|
15
|
+
export { noSyncResultAssumption } from './rules/no-sync-result-assumption.js';
|
|
16
|
+
export { implementationReturnsResult } from './rules/implementation-returns-result.js';
|
|
17
|
+
export { noThrowInDetourTarget } from './rules/no-throw-in-detour-target.js';
|
|
18
|
+
export { preferSchemaInference } from './rules/prefer-schema-inference.js';
|
|
19
|
+
export { validDescribeRefs } from './rules/valid-describe-refs.js';
|
|
20
|
+
export { wardenRules } from './rules/index.js';
|
|
21
|
+
export type { WardenOptions, WardenReport } from './cli.js';
|
|
22
|
+
export { formatWardenReport, runWarden } from './cli.js';
|
|
23
|
+
export { formatGitHubAnnotations, formatJson, formatSummary, } from './formatters.js';
|
|
24
|
+
export type { DriftResult } from './drift.js';
|
|
25
|
+
export { checkDrift } from './drift.js';
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|