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 | 1x 1x 1x 1x 103x 103x 50x 50x 50x 50x 3x 50x 2x 50x 53x 53x 50x 50x 50x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 53x 53x 53x 53x 53x 53x 53x 53x 2x 2x 53x 53x 6x 10x 10x 6x 53x 53x 1x 52x 2x 53x 3x 3x 4x 3x 53x 7x 53x 53x 1x 8x 10x 10x 10x 6x 4x 4x 4x 11x 10x 16x 8x 5x 5x 5x 5x 5x 5x 5x 1x 1x 2x 2x 2x 1x 1x 4x 4x 4x 4x 4x 4x 3x 1x 1x 2x 4x 4x 2x 5x 5x 3x 3x 5x 3x 3x 5x 5x 5x 9x 9x 9x 9x 9x 9x 11x 11x 11x 11x 11x 11x 9x 9x 10x 10x 7x 7x 8x 8x 8x 8x 8x 8x 6x 8x 1x 7x 2x 2x 3x 3x 3x 2x 8x 7x 7x | import type {
OpRouteNode,
OpOperationNode,
OpResponseNode,
ParamSource,
SecurityNode,
SecurityFields,
ContractTypeNode,
ObjectMode,
PluginValue,
} from '@contractkit/core';
import { SECURITY_NONE } from '@contractkit/core';
import { printType, formatDefault } from './print-type.js';
import { INDENT } from './indent.js';
const I1 = INDENT;
const I2 = INDENT.repeat(2);
const I3 = INDENT.repeat(3);
const I4 = INDENT.repeat(4);
// ─── Orphan comment helpers ──────────────────────────────────────────────────
type CommentEntry = { line: number; text: string };
export type CommentBlock = { startLine: number; lines: string[] };
/** Group sorted orphan comment entries into consecutive-line blocks. */
export function groupComments(entries: CommentEntry[]): CommentBlock[] {
const blocks: CommentBlock[] = [];
let current: CommentBlock | null = null;
for (const { line, text } of entries) {
if (current && line === current.startLine + current.lines.length) {
current.lines.push(text);
} else {
if (current) blocks.push(current);
current = { startLine: line, lines: [text] };
}
}
if (current) blocks.push(current);
return blocks;
}
/**
* Emit any comment blocks whose startLine is < beforeLine.
* Lines are emitted verbatim — they already carry their original indentation.
*/
export function flushBlocks(out: string[], blocks: CommentBlock[], idx: { value: number }, beforeLine: number, _indent = '') {
while (idx.value < blocks.length && blocks[idx.value]!.startLine < beforeLine) {
for (const l of blocks[idx.value]!.lines) out.push(l);
idx.value++;
}
}
// ─── Route ───────────────────────────────────────────────────────────────────
export function printRoute(route: OpRouteNode, blocks: CommentBlock[], idx: { value: number }, nextRouteStart: number): string {
const lines: string[] = [];
const commentSuffix = route.description ? ` # ${route.description}` : '';
lines.push(`${route.path}: {${commentSuffix}`);
if (route.params !== undefined) {
lines.push(...printParamsBlock(route.params, I1, route.paramsMode));
}
if (route.security !== undefined) {
lines.push(...printSecurity(route.security, I1, I2));
}
for (Iconst op of route.operations) {
// Flush comment blocks that appear before this operation (inside the route)
flushBlocks(lines, blocks, idx, op.loc.line, I1);
lines.push(...printOperation(op));
}
// Flush comment blocks between last operation and the next route
flushBlocks(lines, blocks, idx, nextRouteStart, I1);
lines.push('}');
return lines.join('\n');
}
// ─── Params block ────────────────────────────────────────────────────────────
function printParamsBlock(source: ParamSource, indent: string, mode?: ObjectMode): string[] {
const prefix = mode ? `mode(${mode}) ` : '';
Iif (source.kind === 'ref') {
return [`${indent}${prefix}params: ${source.name}`];
}
Eif (source.kind === 'params') {
const lines: string[] = [`${indent}${prefix}params: {`];
const inner = indent + INDENT;
for (Iconst p of source.nodes) {
const opt = p.optional ? '?' : '';
let t = printType(p.type);
Iif (p.nullable) t += ' | null';
const def = p.default !== undefined ? ` = ${formatDefault(p.default)}` : '';
const comment = p.description ? ` # ${p.description}` : '';
lines.push(`${inner}${p.name}${opt}: ${t}${def}${comment}`);
}
lines.push(`${indent}}`);
return lines;
}
// ContractTypeNode
return [`${indent}${prefix}params: ${printType(source.node)}`];
}
// ─── HTTP operation ──────────────────────────────────────────────────────────
function printOperation(op: OpOperationNode): string[] {
const lines: string[] = [];
const commentSuffix = op.description ? ` # ${op.description}` : '';
const modPart = op.modifiers?.length ? `(${op.modifiers[0]})` : '';
lines.push(`${I1}${op.method}${modPart}: {${commentSuffix}`);
Iif (op.name) lines.push(`${I2}name: ${op.name}`);
Iif (op.service) lines.push(`${I2}service: ${op.service}`);
Iif (op.sdk) lines.push(`${I2}sdk: ${op.sdk}`);
if (op.signature) {
const comment = op.signatureDescription ? ` # ${op.signatureDescription}` : '';
lines.push(`${I2}signature: ${formatSignatureValue(op.signature)}${comment}`);
}
if (op.security !== undefined) lines.push(...printSecurity(op.security));
if (op.plugins && Object.keys(op.plugins).length > 0) {
lines.push(`${I2}plugins: {`);
for (Iconst [key, val] of Object.entries(op.plugins)) {
lines.push(...printPluginEntry(key, val, I3));
}
lines.push(`${I2}}`);
}
if (op.query !== undefined) lines.push(...printQueryOrHeaders('query', op.query, op.queryMode));
if (op.requestHeadersOptOut) {
lines.push(`${I2}headers: none`);
} else if (op.headers !== undefined) {
lines.push(...printQueryOrHeaders('headers', op.headers, op.headersMode));
}
if (op.request) {
lines.push(`${I2}request: {`);
for (Iconst body of op.request.bodies) {
lines.push(...printContentTypeLine(body.contentType, body.bodyType, I3));
}
lines.push(`${I2}}`);
}
if (op.responses.length > 0) {
lines.push(...printResponseBlock(op.responses));
}
lines.push(`${I1}}`);
return lines;
}
// ─── Plugins block ───────────────────────────────────────────────────────────
const IDENT_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
function escapeString(s: string): string {
return s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
function printPluginEntry(key: string, value: PluginValue, indent: string): string[] {
const lines: string[] = [];
const inline = printPluginInline(value);
if (inline !== null) {
lines.push(`${indent}${key}: ${inline}`);
} else {
const head = `${indent}${key}: `;
const block = printPluginBlock(value, indent);
lines.push(`${head}${block[0]!.trimStart()}`);
for (let i = 1; i < block.length; i++) lines.push(block[i]!);
}
return lines;
}
function printPluginInline(value: PluginValue): string | null {
if (typeof value === 'string') return `"${escapeString(value)}"`;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
Iif (value === null) return 'null';
Iif (Array.isArray(value) && value.length === 0) return '[]';
Iif (!Array.isArray(value) && typeof value === 'object' && Object.keys(value).length === 0) return '{}';
return null;
}
function printPluginBlock(value: PluginValue, indent: string): string[] {
const inner = indent + INDENT;
const lines: string[] = [];
if (Array.isArray(value)) {
lines.push(`${indent}[`);
for (Iconst item of value) {
const inline = printPluginInline(item);
if (inline !== null) {
lines.push(`${inner}${inline}`);
} else E{
const block = printPluginBlock(item, inner);
for (const l of block) lines.push(l);
}
}
lines.push(`${indent}]`);
return lines;
}
Eif (typeof value === 'object' && value !== null) {
lines.push(`${indent}{`);
for (Iconst [k, v] of Object.entries(value)) {
const fieldKey = IDENT_RE.test(k) ? k : `"${escapeString(k)}"`;
const inline = printPluginInline(v);
if (inline !== null) {
lines.push(`${inner}${fieldKey}: ${inline}`);
} else {
const block = printPluginBlock(v, inner);
lines.push(`${inner}${fieldKey}: ${block[0]!.trimStart()}`);
for (let i = 1; i < block.length; i++) lines.push(block[i]!);
}
}
lines.push(`${indent}}`);
return lines;
}
// Scalars are always inline; printPluginInline already handles them.
return [`${indent}${printPluginInline(value)}`];
}
// ─── Security ────────────────────────────────────────────────────────────────
/** Print a signature key: unquoted when it's a plain identifier, quoted otherwise. */
function formatSignatureValue(value: string): string {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(value) ? value : `"${value}"`;
}
// indent: indentation for the `security` keyword line
// innerIndent: indentation for field lines inside the block
export function printSecurity(security: SecurityNode, indent = I2, innerIndent = I3): string[] {
if (security === SECURITY_NONE) return [`${indent}security: none`];
const fields = security as SecurityFields;
const hasRoles = fields.roles && fields.roles.length > 0;
Iif (!hasRoles) return [];
const lines = [`${indent}security: {`];
const comment = fields.rolesDescription ? ` # ${fields.rolesDescription}` : '';
lines.push(`${innerIndent}roles: ${fields.roles!.join(' ')}${comment}`);
lines.push(`${indent}}`);
return lines;
}
// ─── Query / headers ─────────────────────────────────────────────────────────
function printQueryOrHeaders(keyword: 'query' | 'headers', source: ParamSource, mode?: ObjectMode): string[] {
const prefix = mode ? `mode(${mode}) ` : '';
Iif (source.kind === 'ref') {
return [`${I2}${prefix}${keyword}: ${source.name}`];
}
Eif (source.kind === 'params') {
Iif (source.nodes.length === 0) return [];
const lines: string[] = [`${I2}${prefix}${keyword}: {`];
for (Iconst p of source.nodes) {
const opt = p.optional ? '?' : '';
let t = printType(p.type);
if (p.nullable) t += ' | null';
const def = p.default !== undefined ? ` = ${formatDefault(p.default)}` : '';
const comment = p.description ? ` # ${p.description}` : '';
lines.push(`${I3}${p.name}${opt}: ${t}${def}${comment}`);
}
lines.push(`${I2}}`);
return lines;
}
// ContractTypeNode (e.g. intersection)
return [`${I2}${prefix}${keyword}: ${printType(source.node)}`];
}
// ─── Content-type line ───────────────────────────────────────────────────────
/** Print a `contentType: bodyType` line, expanding inline brace objects onto separate lines. */
function printContentTypeLine(contentType: string, bodyType: ContractTypeNode, lineIndent: string): string[] {
Iif (bodyType.kind === 'inlineObject') {
const fieldIndent = lineIndent + INDENT;
const lines: string[] = [`${lineIndent}${contentType}: {`];
for (const f of bodyType.fields) {
const opt = f.optional ? '?' : '';
let t = printType(f.type);
if (f.nullable) t += ' | null';
const def = f.default !== undefined ? ` = ${formatDefault(f.default)}` : '';
const comment = f.description ? ` # ${f.description}` : '';
lines.push(`${fieldIndent}${f.name}${opt}: ${t}${def}${comment}`);
}
lines.push(`${lineIndent}}`);
return lines;
}
return [`${lineIndent}${contentType}: ${printType(bodyType)}`];
}
// ─── Response block ──────────────────────────────────────────────────────────
function printResponseBlock(responses: OpResponseNode[]): string[] {
const lines: string[] = [`${I2}response: {`];
for (Iconst resp of responses) {
const hasBody = resp.contentType && resp.bodyType;
const hasHeaders = resp.headers && resp.headers.length > 0;
const optOut = resp.headersOptOut;
if (hasBody || hasHeaders || optOut) {
lines.push(`${I3}${resp.statusCode}: {`);
if (hasBody) {
lines.push(...printContentTypeLine(resp.contentType!, resp.bodyType!, I4));
}
if (optOut) {
lines.push(`${I4}headers: none`);
} else if (hasHeaders) {
lines.push(`${I4}headers: {`);
for (Iconst h of resp.headers!) {
const opt = h.optional ? '?' : '';
const trail = h.description ? ` # ${h.description}` : '';
lines.push(`${I4}${INDENT}${h.name}${opt}: ${printType(h.type)}${trail}`);
}
lines.push(`${I4}}`);
}
lines.push(`${I3}}`);
} else E{
lines.push(`${I3}${resp.statusCode}:`);
}
}
lines.push(`${I2}}`);
return lines;
}
|