All files ast.ts

90% Statements 9/10
57.14% Branches 4/7
100% Functions 3/3
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348              5x                                                                                                                                                                                                                                                                                                                                                                       5x                                                                                                                                                                                                                                 1x 1x               2x 1x                                                                          
// ─── Shared ────────────────────────────────────────────────────────────────
 
export interface SourceLocation {
    file: string;
    line: number;
}
 
export const SCALAR_NAMES: ReadonlySet<string> = new Set<ScalarTypeNode['name']>([
    'string',
    'number',
    'int',
    'bigint',
    'boolean',
    'date',
    'time',
    'datetime',
    'duration',
    'interval',
    'email',
    'url',
    'uuid',
    'unknown',
    'null',
    'object',
    'binary',
    'json',
]);
 
// ─── Contracts AST (.ck) ──────────────────────────────────────────────────
 
export type ContractTypeNode =
    | ScalarTypeNode
    | ArrayTypeNode
    | TupleTypeNode
    | RecordTypeNode
    | EnumTypeNode
    | LiteralTypeNode
    | UnionTypeNode
    | DiscriminatedUnionTypeNode
    | IntersectionTypeNode
    | ModelRefTypeNode
    | InlineObjectTypeNode
    | LazyTypeNode;
 
export interface ScalarTypeNode {
    kind: 'scalar';
    name:
        | 'string'
        | 'number'
        | 'int'
        | 'bigint'
        | 'boolean'
        | 'date'
        | 'time'
        | 'datetime'
        | 'duration'
        | 'interval'
        | 'email'
        | 'url'
        | 'uuid'
        | 'unknown'
        | 'null'
        | 'object'
        | 'binary'
        | 'json';
    min?: number | bigint | string;
    max?: number | bigint | string;
    len?: number;
    regex?: string;
    format?: string;
}
 
export interface ArrayTypeNode {
    kind: 'array';
    item: ContractTypeNode;
    min?: number;
    max?: number;
}
 
export interface TupleTypeNode {
    kind: 'tuple';
    items: ContractTypeNode[];
}
 
export interface RecordTypeNode {
    kind: 'record';
    key: ContractTypeNode;
    value: ContractTypeNode;
}
 
export interface EnumTypeNode {
    kind: 'enum';
    values: string[];
}
 
export interface LiteralTypeNode {
    kind: 'literal';
    value: string | number | boolean;
}
 
export interface UnionTypeNode {
    kind: 'union';
    members: ContractTypeNode[];
}
 
export interface DiscriminatedUnionTypeNode {
    kind: 'discriminatedUnion';
    discriminator: string;
    members: ContractTypeNode[];
}
 
export interface ModelRefTypeNode {
    kind: 'ref';
    name: string;
    lazy?: boolean;
}
 
export interface InlineObjectTypeNode {
    kind: 'inlineObject';
    fields: FieldNode[];
    mode?: ObjectMode;
}
 
export interface IntersectionTypeNode {
    kind: 'intersection';
    members: ContractTypeNode[];
}
 
export interface LazyTypeNode {
    kind: 'lazy';
    inner: ContractTypeNode;
}
 
export interface FieldNode {
    name: string;
    optional: boolean;
    nullable: boolean;
    visibility: 'readonly' | 'writeonly' | 'normal';
    type: ContractTypeNode;
    default?: string | number | boolean;
    deprecated?: boolean;
    /** Set when the field is declared with the `override` modifier — used by inheritance validation
     * to confirm the field is intentionally redeclaring a conflicting base field. */
    override?: boolean;
    description?: string;
    loc: SourceLocation;
}
 
export interface ModelNode {
    kind: 'model';
    name: string;
    /** Names of base contracts this model extends, in left-to-right declaration order.
     * `contract C: A & B & { ... }` produces `bases: ['A', 'B']`. Empty/undefined for non-inherited models. */
    bases?: string[];
    fields: FieldNode[];
    type?: ContractTypeNode; // type alias: Name: typeExpression (fields will be empty)
    mode?: ObjectMode; // object validation mode — defaults to 'strict'
    inputCase?: 'camel' | 'snake' | 'pascal'; // format(input=) — key casing of incoming data
    outputCase?: 'camel' | 'snake' | 'pascal'; // format(output=) — key casing of emitted data
    deprecated?: boolean;
    description?: string;
    loc: SourceLocation;
}
 
export interface ContractRootNode {
    kind: 'contractRoot';
    meta: Record<string, string>;
    /** Service name → module path mappings from `options { services { ... } }`. */
    services?: Record<string, string>;
    models: ModelNode[];
    file: string;
    /** Comment lines not attached to any node, sorted by line number. */
    orphanComments?: Array<{ line: number; text: string }>;
}
 
// ─── Operations AST (.op) ──────────────────────────────────────────────────
 
/** Constrained security declaration. */
export interface SecurityFields {
    /** Whether this endpoint requires MFA. */
    requireMfa?: boolean;
    /** Inline comment attached to the `requireMfa:` line. */
    requireMfaDescription?: string;
    loc: SourceLocation;
}
 
/** Sentinel value for explicitly public endpoints (`security: none`). */
export const SECURITY_NONE = 'none' as const;
export type SecurityNone = typeof SECURITY_NONE;
 
/** Security declaration: explicit public (`none`), or constrained auth fields. */
export type SecurityNode = SecurityNone | SecurityFields;
 
export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
 
/** Controls how Zod handles unknown keys on an object schema. */
export type ObjectMode = 'strict' | 'strip' | 'loose';
 
/** Visibility/lifecycle modifiers on routes and operations.
 * `public` is operation-only: overrides inherited route-level modifiers. */
export type RouteModifier = 'internal' | 'deprecated' | 'public';
 
/** JSON-like value tree used for `plugins` entries — strings, numbers, booleans, null, nested objects, and arrays. */
export type PluginValue = string | number | boolean | null | PluginValue[] | { [key: string]: PluginValue };
 
export interface OpParamNode {
    name: string;
    optional: boolean;
    nullable: boolean;
    type: ContractTypeNode;
    default?: string | number | boolean;
    description?: string;
    loc: SourceLocation;
}
 
/** Either inline param declarations, a single type reference name, or a ContractTypeNode. */
export type ParamSource = { kind: 'params'; nodes: OpParamNode[] } | { kind: 'ref'; name: string } | { kind: 'type'; node: ContractTypeNode };
 
/**
 * Recognized request mime types that codegen has dedicated handling for. Other strings are
 * still permitted (any RFC 6838-shaped `type/subtype`) and pass through unchanged; codegen
 * falls back to a JSON-ish default for `+json` suffixes and a generic body for everything else.
 */
export type KnownRequestContentType = 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
 
export interface OpRequestBodyNode {
    contentType: string;
    bodyType: ContractTypeNode;
}
 
export interface OpRequestNode {
    bodies: OpRequestBodyNode[];
}
 
export interface OpResponseHeaderNode {
    /** Header name as written in the .ck source (preserves casing/hyphens, e.g. `preference-applied`, `ETag`). */
    name: string;
    optional: boolean;
    type: ContractTypeNode;
    description?: string;
}
 
export interface OpResponseNode {
    statusCode: number;
    contentType?: string;
    bodyType?: ContractTypeNode;
    /** Declared response headers for this status code. Undefined = none declared. */
    headers?: OpResponseHeaderNode[];
    /** Set when the status code body declares `headers: none` — suppresses options-level response header merge for this code. */
    headersOptOut?: boolean;
}
 
export interface OpOperationNode {
    method: HttpMethod;
    name?: string; // e.g. "Create an Offer" — human-readable name for docs/collections
    service?: string; // e.g. "LedgerService.updateCategoryNesting"
    sdk?: string; // e.g. "getUser" — explicit SDK method name
    /** HMAC signature key name for this endpoint (e.g. `WEBHOOK_SECRET`). */
    signature?: string;
    /** Inline comment attached to the `signature:` line. */
    signatureDescription?: string;
    request?: OpRequestNode;
    responses: OpResponseNode[];
    query?: ParamSource;
    queryMode?: ObjectMode;
    headers?: ParamSource;
    headersMode?: ObjectMode;
    /** Set when the operation declares `headers: none` — suppresses options-level request header merge for this op. */
    requestHeadersOptOut?: boolean;
    security?: SecurityNode; // overrides config default; "none" = explicitly public
    /** Explicit modifiers. undefined = inherit from route; [] or array = override. */
    modifiers?: RouteModifier[];
    /** Raw plugin values from the grammar, e.g. `{ bruno: { template: "file://request-token.yml" } }`. */
    plugins?: Record<string, PluginValue>;
    /** Resolved plugin extension values keyed by plugin name. Populated by the CLI resolver — same shape as `plugins`, but every `file://` URL string is replaced with the file's contents. Never set by the parser. */
    pluginExtensions?: Record<string, PluginValue>;
    description?: string;
    loc: SourceLocation;
}
 
export interface OpRouteNode {
    path: string;
    params?: ParamSource;
    paramsMode?: ObjectMode;
    operations: OpOperationNode[];
    /** Route-level modifiers — cascade to all operations unless overridden. */
    modifiers?: RouteModifier[];
    /** Route-level security default — cascades to operations that have no explicit security declaration. */
    security?: SecurityNode;
    description?: string;
    loc: SourceLocation;
}
 
/**
 * Resolves the effective modifiers for an operation, applying route-level cascade.
 * If the operation specifies any explicit modifiers, those replace (not merge) the route's.
 * `public` on an operation acts as an explicit override that clears inherited modifiers;
 * it is stripped from the returned array (it is not a codegen modifier itself).
 */
export function resolveModifiers(route: OpRouteNode, op: OpOperationNode): RouteModifier[] {
    const raw = op.modifiers ?? route.modifiers ?? [];
    return raw.filter(m => m !== 'public');
}
 
/**
 * Resolves the effective security for an operation, applying cascade from operation → route → file.
 * Operation-level security always wins; if absent, the route's security is used; if absent, the file's.
 */
export function resolveSecurity(route: OpRouteNode, op: OpOperationNode, root?: OpRootNode): SecurityNode | undefined {
    if (op.security !== undefined) return op.security;
    Eif (route.security !== undefined) return route.security;
    return root?.security;
}
 
export interface OpRootNode {
    kind: 'opRoot';
    meta: Record<string, string>;
    /** Service name → module path mappings from `options { services { ... } }`. */
    services?: Record<string, string>;
    /** File-level security default — cascades to all routes/operations unless overridden. */
    security?: SecurityNode;
    /** File-level request headers from `options { request: { headers { ... } } }` — merged into every operation's request headers. */
    requestHeaders?: OpResponseHeaderNode[];
    /** File-level response headers from `options { response: { headers { ... } } }` — merged into every status code on every operation. */
    responseHeaders?: OpResponseHeaderNode[];
    routes: OpRouteNode[];
    file: string;
    /** Comment lines not attached to any node, sorted by line number. */
    orphanComments?: Array<{ line: number; text: string }>;
}
 
// ─── Unified AST (.ck) ───────────────────────────────────────────────────
 
export interface CkRootNode {
    kind: 'ckRoot';
    meta: Record<string, string>;
    services: Record<string, string>;
    /** File-level security default — cascades to all routes/operations unless overridden. */
    security?: SecurityNode;
    /** File-level request headers from `options { request: { headers { ... } } }` — merged into every operation's request headers. */
    requestHeaders?: OpResponseHeaderNode[];
    /** File-level response headers from `options { response: { headers { ... } } }` — merged into every status code on every operation. */
    responseHeaders?: OpResponseHeaderNode[];
    models: ModelNode[];
    routes: OpRouteNode[];
    file: string;
}