@contractkit/prettier-plugin 0.14.2 → 0.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,718 +3,8 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
3
3
 
4
4
  // src/index.ts
5
5
  import { builders } from "prettier/doc";
6
- import { parseCk, DiagnosticCollector } from "@contractkit/core";
7
-
8
- // src/indent.ts
9
- var INDENT = " ";
10
-
11
- // src/print-type.ts
12
- function printType(type) {
13
- switch (type.kind) {
14
- case "scalar": {
15
- const constraints = [];
16
- if (type.format !== void 0) {
17
- const fmt = type.format;
18
- constraints.push(/^[a-zA-Z0-9\-.:/]+$/.test(fmt) ? fmt : `"${fmt}"`);
19
- }
20
- if (type.min !== void 0) constraints.push(`min=${type.min}`);
21
- if (type.max !== void 0) constraints.push(`max=${type.max}`);
22
- if (type.len !== void 0) constraints.push(`len=${type.len}`);
23
- if (type.regex !== void 0) constraints.push(`regex=/${type.regex}/`);
24
- return constraints.length > 0 ? `${type.name}(${constraints.join(", ")})` : type.name;
25
- }
26
- case "array": {
27
- const args = [
28
- printType(type.item)
29
- ];
30
- if (type.min !== void 0) args.push(`min=${type.min}`);
31
- if (type.max !== void 0) args.push(`max=${type.max}`);
32
- return `array(${args.join(", ")})`;
33
- }
34
- case "tuple":
35
- return `tuple(${type.items.map(printType).join(", ")})`;
36
- case "record":
37
- return `record(${printType(type.key)}, ${printType(type.value)})`;
38
- case "enum":
39
- return `enum(${type.values.map(formatEnumValue).join(", ")})`;
40
- case "literal":
41
- return typeof type.value === "string" ? `literal("${type.value}")` : `literal(${type.value})`;
42
- case "union":
43
- return type.members.map(printType).join(" | ");
44
- case "discriminatedUnion":
45
- return `discriminated(by=${type.discriminator}, ${type.members.map(printType).join(" | ")})`;
46
- case "intersection":
47
- return type.members.map(printType).join(" & ");
48
- case "ref":
49
- return type.name;
50
- case "inlineObject":
51
- return printInlineObjectCompact(type);
52
- case "lazy":
53
- return `lazy(${printType(type.inner)})`;
54
- }
55
- }
56
- __name(printType, "printType");
57
- function printInlineObjectCompact(obj) {
58
- const prefix = obj.mode ? `mode(${obj.mode}) ` : "";
59
- if (obj.fields.length === 0) return `${prefix}{}`;
60
- const parts = obj.fields.map((f) => {
61
- const opt = f.optional ? "?" : "";
62
- let t = printType(f.type);
63
- if (f.nullable) t += " | null";
64
- return `${f.name}${opt}: ${t}`;
65
- });
66
- return `${prefix}{ ${parts.join(", ")} }`;
67
- }
68
- __name(printInlineObjectCompact, "printInlineObjectCompact");
69
- function printEnumExpanded(values, indent) {
70
- const innerIndent = indent + INDENT;
71
- return `enum(
72
- ${values.map((v) => `${innerIndent}${formatEnumValue(v)}`).join(",\n")}
73
- ${indent})`;
74
- }
75
- __name(printEnumExpanded, "printEnumExpanded");
76
- function printField(field, indent, printWidth = 80) {
77
- const opt = field.optional ? "?" : "";
78
- const ovr = field.override ? "override " : "";
79
- const dep = field.deprecated ? "deprecated " : "";
80
- const vis = field.visibility !== "normal" ? `${field.visibility} ` : "";
81
- const mods = `${ovr}${dep}${vis}`;
82
- const def = field.default !== void 0 ? ` = ${formatDefault(field.default)}` : "";
83
- const comment = field.description ? ` # ${field.description}` : "";
84
- const innerIndent = indent + INDENT;
85
- if (!field.nullable && field.default === void 0) {
86
- const trailing = extractTrailingInlineObject(field.type);
87
- if (trailing) {
88
- const { prefix, inlineObj } = trailing;
89
- const modePart = inlineObj.mode ? `mode(${inlineObj.mode}) ` : "";
90
- const header = prefix ? `${indent}${field.name}${opt}: ${mods}${prefix} & ${modePart}{${comment}` : `${indent}${field.name}${opt}: ${mods}${modePart}{${comment}`;
91
- return [
92
- header,
93
- ...printInlineObjectExpanded(inlineObj, innerIndent, printWidth),
94
- `${indent}}`
95
- ].join("\n");
96
- }
97
- }
98
- let typeStr = printType(field.type);
99
- if (field.nullable) typeStr += " | null";
100
- const fullLine = `${indent}${field.name}${opt}: ${mods}${typeStr}${def}${comment}`;
101
- if (field.type.kind === "enum" && !field.nullable && field.default === void 0 && fullLine.length > printWidth) {
102
- const enumStr = printEnumExpanded(field.type.values, indent);
103
- return `${indent}${field.name}${opt}: ${mods}${enumStr}${comment}`;
104
- }
105
- return fullLine;
106
- }
107
- __name(printField, "printField");
108
- function printInlineObjectExpanded(obj, indent, printWidth = 80) {
109
- const lines = obj.fields.map((f) => printField(f, indent, printWidth));
110
- for (const comment of obj.trailingComments ?? []) {
111
- lines.push(`${indent}# ${comment}`);
112
- }
113
- return lines;
114
- }
115
- __name(printInlineObjectExpanded, "printInlineObjectExpanded");
116
- function formatEnumValue(v) {
117
- if (/^[a-zA-Z_$][a-zA-Z0-9_$\-.]*$/.test(v)) return v;
118
- return `"${v}"`;
119
- }
120
- __name(formatEnumValue, "formatEnumValue");
121
- function formatDefault(val) {
122
- if (typeof val === "number" || typeof val === "boolean") return String(val);
123
- if (/^[a-zA-Z_$][a-zA-Z0-9_$\-.]*$/.test(val)) return val;
124
- return `"${val}"`;
125
- }
126
- __name(formatDefault, "formatDefault");
127
- function extractTrailingInlineObject(type) {
128
- if (type.kind === "inlineObject") {
129
- return {
130
- prefix: null,
131
- inlineObj: type
132
- };
133
- }
134
- if (type.kind === "intersection") {
135
- const last = type.members[type.members.length - 1];
136
- if (last?.kind === "inlineObject") {
137
- const prefixStr = type.members.slice(0, -1).map(printType).join(" & ");
138
- return {
139
- prefix: prefixStr,
140
- inlineObj: last
141
- };
142
- }
143
- }
144
- return null;
145
- }
146
- __name(extractTrailingInlineObject, "extractTrailingInlineObject");
147
-
148
- // src/print-contract.ts
149
- function printModelDecl(model, printWidth = 80) {
150
- if (model.type !== void 0) {
151
- return printTypeAlias(model, printWidth);
152
- }
153
- const commentSuffix = model.description && model.descriptionInline ? ` # ${model.description}` : "";
154
- const modifiers = [
155
- model.deprecated ? "deprecated" : "",
156
- model.inputCase || model.outputCase ? `format(${[
157
- model.inputCase ? `input=${model.inputCase}` : "",
158
- model.outputCase ? `output=${model.outputCase}` : ""
159
- ].filter(Boolean).join(", ")})` : "",
160
- model.mode ? `mode(${model.mode})` : ""
161
- ].filter(Boolean).join(" ");
162
- const modePrefix = modifiers ? `${modifiers} ` : "";
163
- const baseChain = model.bases && model.bases.length > 0 ? `${model.bases.join(" & ")} & ` : "";
164
- const header = `${modePrefix}${model.name}: ${baseChain}{${commentSuffix}`;
165
- const lines = [
166
- header
167
- ];
168
- for (const field of model.fields) {
169
- lines.push(printField(field, INDENT, printWidth));
170
- }
171
- for (const comment of model.trailingComments ?? []) {
172
- lines.push(`${INDENT}# ${comment}`);
173
- }
174
- lines.push("}");
175
- return lines.join("\n");
176
- }
177
- __name(printModelDecl, "printModelDecl");
178
- function printTypeAlias(model, printWidth) {
179
- const type = model.type;
180
- const commentSuffix = model.description && model.descriptionInline ? ` # ${model.description}` : "";
181
- const modifiers = [
182
- model.deprecated ? "deprecated" : "",
183
- model.inputCase || model.outputCase ? `format(${[
184
- model.inputCase ? `input=${model.inputCase}` : "",
185
- model.outputCase ? `output=${model.outputCase}` : ""
186
- ].filter(Boolean).join(", ")})` : "",
187
- model.mode ? `mode(${model.mode})` : ""
188
- ].filter(Boolean).join(" ");
189
- const modePrefix = modifiers ? `${modifiers} ` : "";
190
- const trailing = extractTrailingInlineObject(type);
191
- if (trailing) {
192
- const { prefix, inlineObj } = trailing;
193
- const modePart = inlineObj.mode ? `mode(${inlineObj.mode}) ` : "";
194
- const header = prefix ? `${modePrefix}${model.name}: ${prefix} & ${modePart}{${commentSuffix}` : `${modePrefix}${model.name}: ${modePart}{${commentSuffix}`;
195
- const lines = [
196
- header,
197
- ...printInlineObjectExpanded(inlineObj, INDENT, printWidth),
198
- "}"
199
- ];
200
- return lines.join("\n");
201
- }
202
- const singleLine = `${modePrefix}${model.name}: ${printType(type)}${commentSuffix}`;
203
- if (type.kind === "enum" && "contract ".length + singleLine.length > printWidth) {
204
- return `${modePrefix}${model.name}: ${printEnumExpanded(type.values, "")}${commentSuffix}`;
205
- }
206
- return singleLine;
207
- }
208
- __name(printTypeAlias, "printTypeAlias");
209
-
210
- // src/print-operation.ts
211
- import { SECURITY_NONE } from "@contractkit/core";
212
- var I1 = INDENT;
213
- var I2 = INDENT.repeat(2);
214
- var I3 = INDENT.repeat(3);
215
- var I4 = INDENT.repeat(4);
216
- function flushBlocks(out, blocks, idx, beforeLine, _indent = "") {
217
- while (idx.value < blocks.length && blocks[idx.value].startLine < beforeLine) {
218
- for (const l of blocks[idx.value].lines) out.push(l);
219
- idx.value++;
220
- }
221
- }
222
- __name(flushBlocks, "flushBlocks");
223
- function printRoute(route, blocks, idx, nextRouteStart) {
224
- const lines = [];
225
- lines.push(`${route.path}: {`);
226
- if (route.params !== void 0) {
227
- lines.push(...printParamsBlock(route.params, I1, route.paramsMode));
228
- }
229
- if (route.security !== void 0) {
230
- lines.push(...printSecurity(route.security, I1, I2));
231
- }
232
- for (const op of route.operations) {
233
- if (op.blankLineBefore && lines.length > 1) lines.push("");
234
- flushBlocks(lines, blocks, idx, op.loc.line, I1);
235
- lines.push(...printOperation(op));
236
- }
237
- flushBlocks(lines, blocks, idx, nextRouteStart, I1);
238
- for (const comment of route.trailingComments ?? []) {
239
- lines.push(`${I1}# ${comment}`);
240
- }
241
- lines.push("}");
242
- return lines.join("\n");
243
- }
244
- __name(printRoute, "printRoute");
245
- function printParamsBlock(source, indent, mode) {
246
- const prefix = mode ? `mode(${mode}) ` : "";
247
- if (source.kind === "ref") {
248
- return [
249
- `${indent}${prefix}params: ${source.name}`
250
- ];
251
- }
252
- if (source.kind === "params") {
253
- const lines = [
254
- `${indent}${prefix}params: {`
255
- ];
256
- const inner = indent + INDENT;
257
- for (const p of source.nodes) {
258
- const opt = p.optional ? "?" : "";
259
- let t = printType(p.type);
260
- if (p.nullable) t += " | null";
261
- const def = p.default !== void 0 ? ` = ${formatDefault(p.default)}` : "";
262
- const comment = p.description ? ` # ${p.description}` : "";
263
- lines.push(`${inner}${p.name}${opt}: ${t}${def}${comment}`);
264
- }
265
- lines.push(`${indent}}`);
266
- return lines;
267
- }
268
- return [
269
- `${indent}${prefix}params: ${printType(source.node)}`
270
- ];
271
- }
272
- __name(printParamsBlock, "printParamsBlock");
273
- var CANONICAL_KEY_ORDER = [
274
- "name",
275
- "service",
276
- "sdk",
277
- "mcp",
278
- "signature",
279
- "security",
280
- "plugins",
281
- "query",
282
- "headers",
283
- "request",
284
- "responses"
285
- ];
286
- function printOperationKey(op, key) {
287
- switch (key) {
288
- case "name":
289
- return op.name ? [
290
- `${I2}name: ${op.name}`
291
- ] : [];
292
- case "service":
293
- return op.service ? [
294
- `${I2}service: ${op.service}`
295
- ] : [];
296
- case "sdk":
297
- return op.sdk ? [
298
- `${I2}sdk: ${op.sdk}`
299
- ] : [];
300
- case "mcp":
301
- if (op.mcp === true) return [
302
- `${I2}mcp: true`
303
- ];
304
- if (op.mcp === false) return [
305
- `${I2}mcp: false`
306
- ];
307
- return op.mcp ? printMcpBlock(op.mcp) : [];
308
- case "signature": {
309
- if (!op.signature) return [];
310
- const comment = op.signatureDescription ? ` # ${op.signatureDescription}` : "";
311
- if (op.signaturePolicy) {
312
- return [
313
- `${I2}signature: {`,
314
- `${I3}options: ${formatSignatureValue(op.signature)}${comment}`,
315
- `${I3}policy: ${op.signaturePolicy}`,
316
- `${I2}}`
317
- ];
318
- }
319
- return [
320
- `${I2}signature: ${formatSignatureValue(op.signature)}${comment}`
321
- ];
322
- }
323
- case "security":
324
- return op.security !== void 0 ? printSecurity(op.security) : [];
325
- case "plugins": {
326
- if (!op.plugins || Object.keys(op.plugins).length === 0) return [];
327
- const lines = [
328
- `${I2}plugins: {`
329
- ];
330
- for (const [k, val] of Object.entries(op.plugins)) lines.push(...printPluginEntry(k, val, I3));
331
- lines.push(`${I2}}`);
332
- return lines;
333
- }
334
- case "query":
335
- return op.query !== void 0 ? printQueryOrHeaders("query", op.query, op.queryMode) : [];
336
- case "headers":
337
- if (op.requestHeadersOptOut) return [
338
- `${I2}headers: none`
339
- ];
340
- return op.headers !== void 0 ? printQueryOrHeaders("headers", op.headers, op.headersMode) : [];
341
- case "request": {
342
- if (!op.request) return [];
343
- const lines = [
344
- `${I2}request: {`
345
- ];
346
- for (const body of op.request.bodies) lines.push(...printContentTypeLine(body.contentType, body.bodyType, I3));
347
- lines.push(`${I2}}`);
348
- return lines;
349
- }
350
- case "responses":
351
- return op.responses.length > 0 ? printResponseBlock(op.responses, op.responsesTrailingComments) : [];
352
- }
353
- }
354
- __name(printOperationKey, "printOperationKey");
355
- function printOperation(op) {
356
- const lines = [];
357
- const modPart = op.modifiers?.length ? `(${op.modifiers[0]})` : "";
358
- const inlineDescription = op.descriptionInline ?? true;
359
- for (const c of op.leadingComments ?? []) lines.push(`${I1}# ${c}`);
360
- if (op.description && !inlineDescription) {
361
- for (const line of op.description.split("\n")) lines.push(`${I1}# ${line}`);
362
- }
363
- const commentSuffix = op.description && inlineDescription ? ` # ${op.description}` : "";
364
- lines.push(`${I1}${op.method}${modPart}: {${commentSuffix}`);
365
- const order = op.keyOrder ?? [];
366
- const rest = CANONICAL_KEY_ORDER.filter((k) => !order.includes(k));
367
- const orphaned = [];
368
- for (const key of [
369
- ...order,
370
- ...rest
371
- ]) {
372
- const keyLines = printOperationKey(op, key);
373
- const comments = op.bodyLeadingComments?.[key] ?? [];
374
- if (keyLines.length === 0) {
375
- orphaned.push(...comments);
376
- continue;
377
- }
378
- for (const c of comments) lines.push(`${I2}# ${c}`);
379
- lines.push(...keyLines);
380
- }
381
- for (const c of [
382
- ...orphaned,
383
- ...op.bodyTrailingComments ?? []
384
- ]) lines.push(`${I2}# ${c}`);
385
- lines.push(`${I1}}`);
386
- return lines;
387
- }
388
- __name(printOperation, "printOperation");
389
- function mcpHintTokens(mcp) {
390
- const tokens = [];
391
- if (mcp.readOnlyHint !== void 0) tokens.push(mcp.readOnlyHint ? "readOnly" : "nonReadOnly");
392
- if (mcp.idempotentHint !== void 0) tokens.push(mcp.idempotentHint ? "idempotent" : "nonIdempotent");
393
- if (mcp.destructiveHint !== void 0) tokens.push(mcp.destructiveHint ? "destructive" : "nonDestructive");
394
- if (mcp.openWorldHint !== void 0) tokens.push(mcp.openWorldHint ? "openWorld" : "closedWorld");
395
- return tokens;
396
- }
397
- __name(mcpHintTokens, "mcpHintTokens");
398
- function printMcpBlock(mcp) {
399
- const lines = [
400
- `${I2}mcp: {`
401
- ];
402
- if (mcp.name !== void 0) lines.push(`${I3}name: "${escapeString(mcp.name)}"`);
403
- if (mcp.title !== void 0) lines.push(`${I3}title: "${escapeString(mcp.title)}"`);
404
- if (mcp.description !== void 0) lines.push(`${I3}description: "${escapeString(mcp.description)}"`);
405
- const tokens = mcpHintTokens(mcp);
406
- if (tokens.length > 0) lines.push(`${I3}hint: ${tokens.join(", ")}`);
407
- lines.push(`${I2}}`);
408
- return lines;
409
- }
410
- __name(printMcpBlock, "printMcpBlock");
411
- var IDENT_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
412
- function escapeString(s) {
413
- return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
414
- }
415
- __name(escapeString, "escapeString");
416
- function printPluginEntry(key, value, indent) {
417
- const lines = [];
418
- const inline = printPluginInline(value);
419
- if (inline !== null) {
420
- lines.push(`${indent}${key}: ${inline}`);
421
- } else {
422
- const head = `${indent}${key}: `;
423
- const block = printPluginBlock(value, indent);
424
- lines.push(`${head}${block[0].trimStart()}`);
425
- for (let i = 1; i < block.length; i++) lines.push(block[i]);
426
- }
427
- return lines;
428
- }
429
- __name(printPluginEntry, "printPluginEntry");
430
- function printPluginInline(value) {
431
- if (typeof value === "string") return `"${escapeString(value)}"`;
432
- if (typeof value === "number" || typeof value === "boolean") return String(value);
433
- if (value === null) return "null";
434
- if (Array.isArray(value) && value.length === 0) return "[]";
435
- if (!Array.isArray(value) && typeof value === "object" && Object.keys(value).length === 0) return "{}";
436
- return null;
437
- }
438
- __name(printPluginInline, "printPluginInline");
439
- function printPluginBlock(value, indent) {
440
- const inner = indent + INDENT;
441
- const lines = [];
442
- if (Array.isArray(value)) {
443
- lines.push(`${indent}[`);
444
- for (const item of value) {
445
- const inline = printPluginInline(item);
446
- if (inline !== null) {
447
- lines.push(`${inner}${inline}`);
448
- } else {
449
- const block = printPluginBlock(item, inner);
450
- for (const l of block) lines.push(l);
451
- }
452
- }
453
- lines.push(`${indent}]`);
454
- return lines;
455
- }
456
- if (typeof value === "object" && value !== null) {
457
- lines.push(`${indent}{`);
458
- for (const [k, v] of Object.entries(value)) {
459
- const fieldKey = IDENT_RE.test(k) ? k : `"${escapeString(k)}"`;
460
- const inline = printPluginInline(v);
461
- if (inline !== null) {
462
- lines.push(`${inner}${fieldKey}: ${inline}`);
463
- } else {
464
- const block = printPluginBlock(v, inner);
465
- lines.push(`${inner}${fieldKey}: ${block[0].trimStart()}`);
466
- for (let i = 1; i < block.length; i++) lines.push(block[i]);
467
- }
468
- }
469
- lines.push(`${indent}}`);
470
- return lines;
471
- }
472
- return [
473
- `${indent}${printPluginInline(value)}`
474
- ];
475
- }
476
- __name(printPluginBlock, "printPluginBlock");
477
- function formatSignatureValue(value) {
478
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(value) ? value : `"${value}"`;
479
- }
480
- __name(formatSignatureValue, "formatSignatureValue");
481
- function printSecurity(security, indent = I2, innerIndent = I3) {
482
- if (security === SECURITY_NONE) return [
483
- `${indent}security: none`
484
- ];
485
- const fields = security;
486
- const leading = fields.leadingComments ?? [];
487
- const trailing = fields.trailingComments ?? [];
488
- if (fields.policy === void 0 && leading.length === 0 && trailing.length === 0) return [];
489
- const lines = [
490
- `${indent}security: {`
491
- ];
492
- for (const c of leading) lines.push(`${innerIndent}# ${c}`);
493
- if (fields.policy !== void 0) {
494
- const comment = fields.policyDescription ? ` # ${fields.policyDescription}` : "";
495
- const value = fields.policy === false ? "none" : fields.policy;
496
- lines.push(`${innerIndent}policy: ${value}${comment}`);
497
- }
498
- for (const c of trailing) lines.push(`${innerIndent}# ${c}`);
499
- lines.push(`${indent}}`);
500
- return lines;
501
- }
502
- __name(printSecurity, "printSecurity");
503
- function printQueryOrHeaders(keyword, source, mode) {
504
- const prefix = mode ? `mode(${mode}) ` : "";
505
- if (source.kind === "ref") {
506
- return [
507
- `${I2}${prefix}${keyword}: ${source.name}`
508
- ];
509
- }
510
- if (source.kind === "params") {
511
- if (source.nodes.length === 0) return [];
512
- const lines = [
513
- `${I2}${prefix}${keyword}: {`
514
- ];
515
- for (const p of source.nodes) {
516
- const opt = p.optional ? "?" : "";
517
- let t = printType(p.type);
518
- if (p.nullable) t += " | null";
519
- const def = p.default !== void 0 ? ` = ${formatDefault(p.default)}` : "";
520
- const comment = p.description ? ` # ${p.description}` : "";
521
- lines.push(`${I3}${p.name}${opt}: ${t}${def}${comment}`);
522
- }
523
- lines.push(`${I2}}`);
524
- return lines;
525
- }
526
- return [
527
- `${I2}${prefix}${keyword}: ${printType(source.node)}`
528
- ];
529
- }
530
- __name(printQueryOrHeaders, "printQueryOrHeaders");
531
- function printContentTypeLine(contentType, bodyType, lineIndent) {
532
- if (bodyType.kind === "inlineObject") {
533
- const fieldIndent = lineIndent + INDENT;
534
- const lines = [
535
- `${lineIndent}${contentType}: {`
536
- ];
537
- for (const f of bodyType.fields) {
538
- const opt = f.optional ? "?" : "";
539
- let t = printType(f.type);
540
- if (f.nullable) t += " | null";
541
- const def = f.default !== void 0 ? ` = ${formatDefault(f.default)}` : "";
542
- const comment = f.description ? ` # ${f.description}` : "";
543
- lines.push(`${fieldIndent}${f.name}${opt}: ${t}${def}${comment}`);
544
- }
545
- lines.push(`${lineIndent}}`);
546
- return lines;
547
- }
548
- return [
549
- `${lineIndent}${contentType}: ${printType(bodyType)}`
550
- ];
551
- }
552
- __name(printContentTypeLine, "printContentTypeLine");
553
- function printResponseBlock(responses, trailingComments) {
554
- const lines = [
555
- `${I2}response: {`
556
- ];
557
- for (const resp of responses) {
558
- for (const comment of resp.leadingComments ?? []) lines.push(`${I3}# ${comment}`);
559
- const bodies = resp.bodies;
560
- const hasHeaders = resp.headers && resp.headers.length > 0;
561
- const optOut = resp.headersOptOut;
562
- const code = resp.emit ? `${resp.statusCode}(${resp.emit})` : `${resp.statusCode}`;
563
- const inlinable = resp.inline && bodies.length > 0 && !hasHeaders && !optOut && bodies.every((b) => b.bodyType.kind !== "inlineObject");
564
- if (inlinable) {
565
- const inner = bodies.map((b) => `${b.contentType}: ${printType(b.bodyType)}`).join(" ");
566
- lines.push(`${I3}${code}: { ${inner} }`);
567
- } else if (bodies.length === 0 && !hasHeaders && !optOut && resp.hasBlock) {
568
- lines.push(`${I3}${code}: {}`);
569
- } else if (bodies.length > 0 || hasHeaders || optOut || (resp.trailingComments?.length ?? 0) > 0) {
570
- lines.push(`${I3}${code}: {`);
571
- for (const body of bodies) {
572
- for (const comment of body.leadingComments ?? []) lines.push(`${I4}# ${comment}`);
573
- lines.push(...printContentTypeLine(body.contentType, body.bodyType, I4));
574
- }
575
- for (const comment of resp.headersLeadingComments ?? []) lines.push(`${I4}# ${comment}`);
576
- if (optOut) {
577
- lines.push(`${I4}headers: none`);
578
- } else if (hasHeaders) {
579
- lines.push(`${I4}headers: {`);
580
- for (const h of resp.headers) {
581
- const opt = h.optional ? "?" : "";
582
- const trail = h.description ? ` # ${h.description}` : "";
583
- lines.push(`${I4}${INDENT}${h.name}${opt}: ${printType(h.type)}${trail}`);
584
- }
585
- lines.push(`${I4}}`);
586
- }
587
- for (const comment of resp.trailingComments ?? []) lines.push(`${I4}# ${comment}`);
588
- lines.push(`${I3}}`);
589
- } else {
590
- lines.push(`${I3}${code}:`);
591
- }
592
- }
593
- for (const comment of trailingComments ?? []) lines.push(`${I3}# ${comment}`);
594
- lines.push(`${I2}}`);
595
- return lines;
596
- }
597
- __name(printResponseBlock, "printResponseBlock");
598
-
599
- // src/print-ck.ts
600
- var DEFAULT_PRINT_WIDTH = 80;
601
- function quoteOptionsValue(value, wasUnquoted = false) {
602
- if (/^[a-zA-Z_$][a-zA-Z0-9_$\-.]*$/.test(value)) return value;
603
- const roundTripsBare = value.length > 0 && value === value.trim() && !/[\n\r}]/.test(value) && !/[ \t]#/.test(value) && !/^["']/.test(value);
604
- return wasUnquoted && roundTripsBare ? value : `"${value}"`;
605
- }
606
- __name(quoteOptionsValue, "quoteOptionsValue");
607
- function emitOptionsEntries(lines, entries, comments, unquoted) {
608
- const I22 = INDENT + INDENT;
609
- const wasUnquoted = new Set(unquoted ?? []);
610
- for (const [key, value] of Object.entries(entries)) {
611
- for (const c of comments?.leading?.[key] ?? []) lines.push(`${I22}# ${c}`);
612
- const inline = comments?.inline?.[key];
613
- lines.push(`${I22}${key}: ${quoteOptionsValue(value, wasUnquoted.has(key))}${inline !== void 0 ? ` # ${inline}` : ""}`);
614
- }
615
- for (const c of comments?.trailing ?? []) lines.push(`${I22}# ${c}`);
616
- }
617
- __name(emitOptionsEntries, "emitOptionsEntries");
618
- function printOptionsBlock(ast) {
619
- const hasMeta = Object.keys(ast.meta).length > 0;
620
- const hasServices = Object.keys(ast.services).length > 0;
621
- const hasSecurity = ast.security !== void 0;
622
- const hasRequestHeaders = (ast.requestHeaders?.length ?? 0) > 0;
623
- const hasResponseHeaders = (ast.responseHeaders?.length ?? 0) > 0;
624
- const hasBodyComments = ast.optionsComments?.body !== void 0;
625
- if (!hasMeta && !hasServices && !hasSecurity && !hasRequestHeaders && !hasResponseHeaders && !hasBodyComments) return null;
626
- const lines = [
627
- ...(ast.optionsComments?.leading ?? []).map((c) => `# ${c}`),
628
- "options {"
629
- ];
630
- const body = ast.optionsComments?.body;
631
- const emitLeading = /* @__PURE__ */ __name((scope) => {
632
- for (const c of body?.leading?.[scope] ?? []) lines.push(`${INDENT}# ${c}`);
633
- }, "emitLeading");
634
- if (hasMeta) {
635
- emitLeading("keys");
636
- lines.push(`${INDENT}keys: {`);
637
- emitOptionsEntries(lines, ast.meta, ast.optionsComments?.keys, ast.optionsUnquoted?.keys);
638
- lines.push(`${INDENT}}`);
639
- }
640
- if (hasServices) {
641
- emitLeading("services");
642
- lines.push(`${INDENT}services: {`);
643
- emitOptionsEntries(lines, ast.services, ast.optionsComments?.services, ast.optionsUnquoted?.services);
644
- lines.push(`${INDENT}}`);
645
- }
646
- if (hasRequestHeaders) {
647
- emitLeading("request");
648
- lines.push(...printOptionsHeaderScope("request", ast.requestHeaders));
649
- }
650
- if (hasResponseHeaders) {
651
- emitLeading("response");
652
- lines.push(...printOptionsHeaderScope("response", ast.responseHeaders));
653
- }
654
- if (hasSecurity) {
655
- emitLeading("security");
656
- lines.push(...printSecurity(ast.security, INDENT, INDENT + INDENT));
657
- }
658
- for (const c of body?.trailing ?? []) lines.push(`${INDENT}# ${c}`);
659
- lines.push("}");
660
- return lines.join("\n");
661
- }
662
- __name(printOptionsBlock, "printOptionsBlock");
663
- function printOptionsHeaderScope(keyword, headers) {
664
- const I22 = INDENT + INDENT;
665
- const I32 = INDENT + INDENT + INDENT;
666
- const lines = [
667
- `${INDENT}${keyword}: {`,
668
- `${I22}headers: {`
669
- ];
670
- for (const h of headers) {
671
- const opt = h.optional ? "?" : "";
672
- const trail = h.description ? ` # ${h.description}` : "";
673
- lines.push(`${I32}${h.name}${opt}: ${printType(h.type)}${trail}`);
674
- }
675
- lines.push(`${I22}}`);
676
- lines.push(`${INDENT}}`);
677
- return lines;
678
- }
679
- __name(printOptionsHeaderScope, "printOptionsHeaderScope");
680
- function printCk(ast, printWidth = DEFAULT_PRINT_WIDTH) {
681
- const parts = [];
682
- const options = printOptionsBlock(ast);
683
- if (options) parts.push(options);
684
- for (const model of ast.models) {
685
- if (parts.length > 0) parts.push("");
686
- parts.push(printDeclLeadIn(model.leadingComments, model.descriptionInline ? void 0 : model.description) + `contract ${printModelDecl(model, printWidth)}`);
687
- }
688
- const emptyBlocks = [];
689
- const emptyIdx = {
690
- value: 0
691
- };
692
- for (const route of ast.routes) {
693
- if (parts.length > 0) parts.push("");
694
- const modPart = route.modifiers?.length ? `(${route.modifiers[0]})` : "";
695
- parts.push(printDeclLeadIn(route.leadingComments, route.description) + `operation${modPart} ${printRoute(route, emptyBlocks, emptyIdx, Infinity)}`);
696
- }
697
- if (ast.trailingComments?.length) {
698
- if (parts.length > 0) parts.push("");
699
- for (const c of ast.trailingComments) parts.push(`# ${c}`);
700
- }
701
- return parts.join("\n") + "\n";
702
- }
703
- __name(printCk, "printCk");
704
- function printDeclLeadIn(leadingComments, description) {
705
- const lines = [];
706
- if (leadingComments?.length) {
707
- for (const c of leadingComments) lines.push(`# ${c}`);
708
- lines.push("");
709
- }
710
- if (description) {
711
- for (const line of description.split("\n")) lines.push(`# ${line}`);
712
- }
713
- return lines.length > 0 ? lines.join("\n") + "\n" : "";
714
- }
715
- __name(printDeclLeadIn, "printDeclLeadIn");
716
-
717
- // src/index.ts
6
+ import { parseCk, DiagnosticCollector, printCk } from "@contractkit/core";
7
+ import { printCk as printCk2, DEFAULT_PRINT_WIDTH } from "@contractkit/core";
718
8
  var { hardline, join } = builders;
719
9
  function toDoc(text) {
720
10
  const lines = text.trimEnd().split("\n");
@@ -763,6 +53,6 @@ var index_default = plugin;
763
53
  export {
764
54
  DEFAULT_PRINT_WIDTH,
765
55
  index_default as default,
766
- printCk
56
+ printCk2 as printCk
767
57
  };
768
58
  //# sourceMappingURL=index.js.map