@amritk/helpers 0.20.0 → 0.22.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/dist/generate-type-definition.d.ts +6 -0
- package/dist/generate-type-definition.js +295 -87
- package/dist/identifier-mentions.d.ts +14 -0
- package/dist/identifier-mentions.js +88 -0
- package/dist/ref-to-filename.d.ts +23 -2
- package/dist/ref-to-filename.js +12 -6
- package/dist/referenced-conditional.d.ts +19 -0
- package/dist/referenced-conditional.js +17 -0
- package/dist/walk-ref-graph.js +22 -17
- package/package.json +1 -1
|
@@ -16,6 +16,12 @@ export type TypeOptions = {
|
|
|
16
16
|
* type and the import agree.
|
|
17
17
|
*/
|
|
18
18
|
readonly rootSchema?: Record<string, unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* Conditional definitions this render is already inlining, so a cycle through
|
|
21
|
+
* one terminates. Internal: callers never set it, `allOfMember` grows it as it
|
|
22
|
+
* descends.
|
|
23
|
+
*/
|
|
24
|
+
readonly inliningRefs?: ReadonlySet<string>;
|
|
19
25
|
};
|
|
20
26
|
/**
|
|
21
27
|
* Generates a TypeScript type definition from a JSON Schema.
|
|
@@ -3,6 +3,7 @@ import { assertSchemaDepth, MAX_SCHEMA_DEPTH } from "./max-schema-depth.js";
|
|
|
3
3
|
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive } from "./mjst-extension.js";
|
|
4
4
|
import { readKey } from "./read-key.js";
|
|
5
5
|
import { refToName } from "./ref-to-name.js";
|
|
6
|
+
import { referencedConditional } from "./referenced-conditional.js";
|
|
6
7
|
import { resolveRef } from "./resolve-ref.js";
|
|
7
8
|
import { safeKey } from "./safe-accessor.js";
|
|
8
9
|
import { isObjectSchema, isSchemaObject } from "./schema-guards.js";
|
|
@@ -15,6 +16,8 @@ const keywordMap = (schema, name) => {
|
|
|
15
16
|
};
|
|
16
17
|
const MAX_TYPE_DEPTH = Math.floor(MAX_SCHEMA_DEPTH / 5);
|
|
17
18
|
const declares = (schema, name) => Object.hasOwn(schema, name);
|
|
19
|
+
const COMPOSITION_KEYWORDS = ["$ref", "$dynamicRef", "allOf", "anyOf", "oneOf", "if", "not"];
|
|
20
|
+
const composes = (schema) => COMPOSITION_KEYWORDS.some((keyword) => declares(schema, keyword));
|
|
18
21
|
const refTypeName = (ref, options) => {
|
|
19
22
|
if (ref.startsWith("#"))
|
|
20
23
|
return refToName(ref, options.typeSuffix);
|
|
@@ -179,17 +182,14 @@ const withoutConditional = (schema) => {
|
|
|
179
182
|
}
|
|
180
183
|
return rest;
|
|
181
184
|
};
|
|
182
|
-
const
|
|
183
|
-
const ref = keywordOf(entry, "$ref");
|
|
184
|
-
if (typeof ref !== "string" || !ref.startsWith("#") || options.rootSchema === void 0)
|
|
185
|
-
return void 0;
|
|
186
|
-
const resolved = resolveRef(ref, options.rootSchema);
|
|
187
|
-
return resolved !== void 0 && isSchemaObject(resolved) && declares(resolved, "if") ? resolved : void 0;
|
|
188
|
-
};
|
|
185
|
+
const referencedConditionalOf = (entry, options) => referencedConditional(entry, options.rootSchema);
|
|
189
186
|
const allOfMember = (entry, domain, options, depth) => {
|
|
190
|
-
const
|
|
187
|
+
const ref = isSchemaObject(entry) ? keywordOf(entry, "$ref") : void 0;
|
|
188
|
+
const inlining = typeof ref === "string" && options.inliningRefs?.has(ref) === true;
|
|
189
|
+
const source = isSchemaObject(entry) && !inlining ? declares(entry, "if") ? entry : referencedConditionalOf(entry, options) : void 0;
|
|
191
190
|
if (source === void 0)
|
|
192
191
|
return wrapUnion(getTypeScriptType(entry, options, depth + 1));
|
|
192
|
+
const inner = typeof ref === "string" ? { ...options, inliningRefs: /* @__PURE__ */ new Set([...options.inliningRefs ?? [], ref]) } : options;
|
|
193
193
|
const own = keywordMap(source, "properties");
|
|
194
194
|
const merged = {};
|
|
195
195
|
for (const block of [domain, own]) {
|
|
@@ -198,9 +198,9 @@ const allOfMember = (entry, domain, options, depth) => {
|
|
|
198
198
|
for (const key of Object.keys(block))
|
|
199
199
|
assignKey(merged, key, readKey(block, key));
|
|
200
200
|
}
|
|
201
|
-
const conditional = conditionalMember(source, merged,
|
|
201
|
+
const conditional = conditionalMember(source, merged, inner, depth + 1);
|
|
202
202
|
const rest = source === entry ? withoutConditional(entry) : entry;
|
|
203
|
-
const remainder = wrapUnion(getTypeScriptType(rest,
|
|
203
|
+
const remainder = wrapUnion(getTypeScriptType(rest, inner, depth + 1));
|
|
204
204
|
return intersectionOf([remainder, ...conditional === void 0 ? [] : [wrapUnion(conditional)]]);
|
|
205
205
|
};
|
|
206
206
|
const isObjectLikeSchema = (schema) => {
|
|
@@ -241,10 +241,43 @@ const getTypeScriptType = (schema, options, depth) => {
|
|
|
241
241
|
return isNullableSchema(schema) ? withNull(branded) : branded;
|
|
242
242
|
};
|
|
243
243
|
const isNullableSchema = (schema) => isSchemaObject(schema) && keywordOf(schema, "nullable") === true;
|
|
244
|
+
const isTopLevelUnion = (type) => {
|
|
245
|
+
let depth = 0;
|
|
246
|
+
let quote;
|
|
247
|
+
for (let i = 0; i < type.length; i++) {
|
|
248
|
+
const char = type[i];
|
|
249
|
+
if (quote !== void 0) {
|
|
250
|
+
if (char === "\\")
|
|
251
|
+
i++;
|
|
252
|
+
else if (char === quote)
|
|
253
|
+
quote = void 0;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (char === "/" && type[i + 1] === "*") {
|
|
257
|
+
const end = type.indexOf("*/", i + 2);
|
|
258
|
+
if (end === -1)
|
|
259
|
+
return false;
|
|
260
|
+
i = end + 1;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (char === '"' || char === "'" || char === "`")
|
|
264
|
+
quote = char;
|
|
265
|
+
else if (char === "(" || char === "<" || char === "[" || char === "{")
|
|
266
|
+
depth++;
|
|
267
|
+
else if (char === ")" || char === ">" || char === "]" || char === "}")
|
|
268
|
+
depth--;
|
|
269
|
+
else if (char === "|" && depth === 0)
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
};
|
|
244
274
|
const wrapUnion = (type) => type.includes(" | ") ? `(${type})` : type;
|
|
245
275
|
const wrapIntersection = (type) => type.includes(" & ") ? `(${type})` : type;
|
|
246
276
|
const withNull = (type) => type.split(" | ").includes("null") ? type : `${type} | null`;
|
|
247
|
-
const unionOf = (members) =>
|
|
277
|
+
const unionOf = (members) => {
|
|
278
|
+
const unique = [...new Set(members)];
|
|
279
|
+
return unique.includes("unknown") ? "unknown" : unique.join(" | ");
|
|
280
|
+
};
|
|
248
281
|
const intersectionOf = (members) => {
|
|
249
282
|
const kept = [...new Set(members)].filter((member) => member !== "unknown");
|
|
250
283
|
if (kept.length === 0)
|
|
@@ -252,27 +285,130 @@ const intersectionOf = (members) => {
|
|
|
252
285
|
const meaningful = kept.length > 1 ? kept.filter((member) => member !== "object") : kept;
|
|
253
286
|
if (meaningful.length === 0)
|
|
254
287
|
return "object";
|
|
255
|
-
|
|
288
|
+
if (meaningful.length === 1)
|
|
289
|
+
return meaningful[0];
|
|
290
|
+
return meaningful.map((member) => isTopLevelUnion(member) ? `(${member})` : member).join(" & ");
|
|
256
291
|
};
|
|
257
292
|
const recordType = (keyType, valueType, options) => options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`;
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
293
|
+
const REGEX_SYNTAX = /* @__PURE__ */ new Set([
|
|
294
|
+
"\\",
|
|
295
|
+
"^",
|
|
296
|
+
"$",
|
|
297
|
+
".",
|
|
298
|
+
"|",
|
|
299
|
+
"?",
|
|
300
|
+
"*",
|
|
301
|
+
"+",
|
|
302
|
+
"(",
|
|
303
|
+
")",
|
|
304
|
+
"[",
|
|
305
|
+
"]",
|
|
306
|
+
"{",
|
|
307
|
+
"}"
|
|
308
|
+
]);
|
|
309
|
+
const startsOptional = (rest) => /^[*?{]/.test(rest);
|
|
310
|
+
const characterClassMembers = (body) => {
|
|
311
|
+
if (body === "" || body.startsWith("^") || body.includes("\\"))
|
|
261
312
|
return void 0;
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
313
|
+
const members = [];
|
|
314
|
+
for (let i = 0; i < body.length; i++) {
|
|
315
|
+
const char = body[i];
|
|
316
|
+
if (body[i + 1] === "-" && i + 2 < body.length) {
|
|
317
|
+
const from = char.codePointAt(0);
|
|
318
|
+
const to = body[i + 2].codePointAt(0);
|
|
319
|
+
if (to < from || to - from > 64)
|
|
320
|
+
return void 0;
|
|
321
|
+
for (let code = from; code <= to; code++)
|
|
322
|
+
members.push(String.fromCodePoint(code));
|
|
323
|
+
i += 2;
|
|
324
|
+
continue;
|
|
269
325
|
}
|
|
326
|
+
members.push(char);
|
|
270
327
|
}
|
|
271
|
-
|
|
328
|
+
return members.length > 0 && members.length <= 64 ? members : void 0;
|
|
329
|
+
};
|
|
330
|
+
const hasTopLevelAlternation = (pattern) => {
|
|
331
|
+
let depth = 0;
|
|
332
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
333
|
+
const char = pattern[i];
|
|
334
|
+
if (char === "\\")
|
|
335
|
+
i++;
|
|
336
|
+
else if (char === "(" || char === "[")
|
|
337
|
+
depth++;
|
|
338
|
+
else if (char === ")" || char === "]")
|
|
339
|
+
depth--;
|
|
340
|
+
else if (char === "|" && depth <= 0)
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
};
|
|
345
|
+
const leadingPrefixes = (pattern) => {
|
|
346
|
+
if (!pattern.startsWith("^"))
|
|
347
|
+
return void 0;
|
|
348
|
+
const rest = pattern.slice(1);
|
|
349
|
+
if (hasTopLevelAlternation(rest))
|
|
272
350
|
return void 0;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
351
|
+
let run = "";
|
|
352
|
+
let cursor = 0;
|
|
353
|
+
while (cursor < rest.length) {
|
|
354
|
+
const char = rest[cursor];
|
|
355
|
+
if (char === "\\") {
|
|
356
|
+
const escaped = rest[cursor + 1];
|
|
357
|
+
if (escaped === void 0 || /[A-Za-z0-9]/.test(escaped))
|
|
358
|
+
break;
|
|
359
|
+
run += escaped;
|
|
360
|
+
cursor += 2;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (REGEX_SYNTAX.has(char))
|
|
364
|
+
break;
|
|
365
|
+
run += char;
|
|
366
|
+
cursor += 1;
|
|
367
|
+
}
|
|
368
|
+
if (run !== "" && startsOptional(rest.slice(cursor)))
|
|
369
|
+
run = run.slice(0, -1);
|
|
370
|
+
if (run !== "")
|
|
371
|
+
return [run];
|
|
372
|
+
if (!rest.startsWith("["))
|
|
373
|
+
return void 0;
|
|
374
|
+
const close = rest.indexOf("]");
|
|
375
|
+
if (close === -1 || startsOptional(rest.slice(close + 1)))
|
|
376
|
+
return void 0;
|
|
377
|
+
return characterClassMembers(rest.slice(1, close));
|
|
378
|
+
};
|
|
379
|
+
const patternKeyType = (pattern) => {
|
|
380
|
+
const prefixes = leadingPrefixes(pattern);
|
|
381
|
+
if (prefixes === void 0 || prefixes.some((prefix) => /[`\\$]/.test(prefix)))
|
|
382
|
+
return "string";
|
|
383
|
+
return prefixes.map((prefix) => `\`${prefix}\${string}\``).join(" | ");
|
|
384
|
+
};
|
|
385
|
+
const patternIndexSignatures = (patternProperties, options, depth) => {
|
|
386
|
+
const entries = Object.entries(patternProperties);
|
|
387
|
+
if (entries.length === 0)
|
|
388
|
+
return [];
|
|
389
|
+
const signatures = entries.map(([pattern, value]) => ({
|
|
390
|
+
key: patternKeyType(pattern),
|
|
391
|
+
value: typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options, depth + 1)
|
|
392
|
+
}));
|
|
393
|
+
const widened = [{ key: "string", value: unionOf(signatures.map((signature) => signature.value)) }];
|
|
394
|
+
if (signatures.length === 1)
|
|
395
|
+
return signatures;
|
|
396
|
+
if (signatures.some((signature) => signature.key === "string"))
|
|
397
|
+
return widened;
|
|
398
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
399
|
+
for (const { key, value } of signatures)
|
|
400
|
+
byKey.set(key, [...byKey.get(key) ?? [], value]);
|
|
401
|
+
const grouped = [...byKey].map(([key, values]) => ({ key, value: unionOf(values) }));
|
|
402
|
+
return overlappingKeys(grouped.map((signature) => signature.key)) ? widened : grouped;
|
|
403
|
+
};
|
|
404
|
+
const indexSignaturesType = (indexes, options, asBlock) => {
|
|
405
|
+
const only = indexes[0];
|
|
406
|
+
if (!asBlock && indexes.length === 1)
|
|
407
|
+
return recordType(only.key, only.value, options);
|
|
408
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
409
|
+
return `{
|
|
410
|
+
${indexes.map(({ key, value }) => ` ${readonlyPrefix}[key: ${key}]: ${value};`).join("\n")}
|
|
411
|
+
}`;
|
|
276
412
|
};
|
|
277
413
|
const getTuplePositions = (schema) => {
|
|
278
414
|
const prefixItems = keywordOf(schema, "prefixItems");
|
|
@@ -312,32 +448,84 @@ const arrayTypeToTs = (schema, options, depth) => {
|
|
|
312
448
|
}
|
|
313
449
|
return options.readonly ? "readonly unknown[]" : "unknown[]";
|
|
314
450
|
};
|
|
315
|
-
const
|
|
451
|
+
const literalPatternKeys = (pattern) => {
|
|
452
|
+
const anchored = /^\^\((?:\?:)?([^()[\]{}.*+?^$\\|]+(?:\|[^()[\]{}.*+?^$\\|]+)*)\)\$$/.exec(pattern);
|
|
453
|
+
const bare = /^\^([^()[\]{}.*+?^$\\|]+)\$$/.exec(pattern);
|
|
454
|
+
const body = anchored?.[1] ?? bare?.[1];
|
|
455
|
+
return body === void 0 ? void 0 : body.split("|");
|
|
456
|
+
};
|
|
457
|
+
const openPatternProperties = (schema, patternProperties) => {
|
|
458
|
+
const declared = keywordMap(schema, "properties");
|
|
459
|
+
if (declared === void 0)
|
|
460
|
+
return patternProperties;
|
|
461
|
+
const kept = {};
|
|
462
|
+
for (const [pattern, value] of Object.entries(patternProperties)) {
|
|
463
|
+
const keys = literalPatternKeys(pattern);
|
|
464
|
+
if (keys?.every((key) => Object.hasOwn(declared, key)))
|
|
465
|
+
continue;
|
|
466
|
+
assignKey(kept, pattern, value);
|
|
467
|
+
}
|
|
468
|
+
return kept;
|
|
469
|
+
};
|
|
470
|
+
const declaresOpenKeys = (schema) => {
|
|
471
|
+
const additionalProperties = keywordOf(schema, "additionalProperties");
|
|
472
|
+
if (additionalProperties !== void 0 && typeof additionalProperties !== "boolean")
|
|
473
|
+
return true;
|
|
474
|
+
const declaredPatterns = keywordMap(schema, "patternProperties");
|
|
475
|
+
return declaredPatterns !== void 0 && Object.keys(openPatternProperties(schema, declaredPatterns)).length > 0;
|
|
476
|
+
};
|
|
477
|
+
const getIndexSignatures = (schema, options, depth) => {
|
|
316
478
|
const additionalProperties = keywordOf(schema, "additionalProperties");
|
|
317
479
|
if (additionalProperties !== void 0 && typeof additionalProperties !== "boolean") {
|
|
318
|
-
return { key: "string", value: getTypeScriptType(additionalProperties, options, depth + 1) };
|
|
480
|
+
return [{ key: "string", value: getTypeScriptType(additionalProperties, options, depth + 1) }];
|
|
319
481
|
}
|
|
320
|
-
const
|
|
321
|
-
if (
|
|
322
|
-
|
|
323
|
-
if (entries.length === 0)
|
|
324
|
-
return void 0;
|
|
325
|
-
const valueTypes = entries.map(([, value]) => typeof value === "boolean" ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options, depth + 1));
|
|
326
|
-
const key = entries.length === 1 && entries[0]?.[0] === "^x-" ? "`x-${string}`" : "string";
|
|
327
|
-
return { key, value: unionOf(valueTypes) };
|
|
482
|
+
const declaredPatterns = keywordMap(schema, "patternProperties");
|
|
483
|
+
if (declaredPatterns !== void 0) {
|
|
484
|
+
return patternIndexSignatures(openPatternProperties(schema, declaredPatterns), options, depth);
|
|
328
485
|
}
|
|
329
|
-
return
|
|
486
|
+
return [];
|
|
330
487
|
};
|
|
331
|
-
const buildIndexSignatureLine = (index,
|
|
488
|
+
const buildIndexSignatureLine = (index, declared, hasOptionalProperty, options) => {
|
|
332
489
|
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
333
|
-
|
|
490
|
+
const covered = index.key === "string" ? declared : declared.filter((property) => matchesKeyType(index.key, property.name));
|
|
491
|
+
if (covered.length === 0)
|
|
334
492
|
return `${readonlyPrefix}[key: ${index.key}]: ${index.value}`;
|
|
335
493
|
const widened = unionOf([
|
|
336
494
|
...index.value.split(" | "),
|
|
337
|
-
...
|
|
495
|
+
...covered.map((property) => property.type),
|
|
338
496
|
...hasOptionalProperty ? ["undefined"] : []
|
|
339
497
|
]);
|
|
340
|
-
return `${readonlyPrefix}[key:
|
|
498
|
+
return `${readonlyPrefix}[key: ${index.key}]: ${widened}`;
|
|
499
|
+
};
|
|
500
|
+
const matchesKeyType = (key, name) => {
|
|
501
|
+
const prefixes = keyTypePrefixes(key);
|
|
502
|
+
return prefixes === void 0 || prefixes.some((prefix) => name.startsWith(prefix));
|
|
503
|
+
};
|
|
504
|
+
const keyTypePrefixes = (key) => {
|
|
505
|
+
if (key === "string")
|
|
506
|
+
return void 0;
|
|
507
|
+
const prefixes = [];
|
|
508
|
+
for (const member of key.split(" | ")) {
|
|
509
|
+
const prefix = /^`(.*)\$\{string\}`$/.exec(member)?.[1];
|
|
510
|
+
if (prefix === void 0)
|
|
511
|
+
return void 0;
|
|
512
|
+
prefixes.push(prefix);
|
|
513
|
+
}
|
|
514
|
+
return prefixes;
|
|
515
|
+
};
|
|
516
|
+
const overlappingKeys = (keys) => {
|
|
517
|
+
const prefixLists = keys.map((key) => keyTypePrefixes(key));
|
|
518
|
+
if (prefixLists.some((prefixes) => prefixes === void 0))
|
|
519
|
+
return true;
|
|
520
|
+
for (let i = 0; i < prefixLists.length; i++) {
|
|
521
|
+
for (let j = i + 1; j < prefixLists.length; j++) {
|
|
522
|
+
const left = prefixLists[i];
|
|
523
|
+
const right = prefixLists[j];
|
|
524
|
+
if (left.some((a) => right.some((b) => a.startsWith(b) || b.startsWith(a))))
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return false;
|
|
341
529
|
};
|
|
342
530
|
const propertyDescription = (propSchema) => {
|
|
343
531
|
if (!isSchemaObject(propSchema))
|
|
@@ -351,14 +539,14 @@ const propertyDescription = (propSchema) => {
|
|
|
351
539
|
return void 0;
|
|
352
540
|
};
|
|
353
541
|
const objectTypeToTs = (schema, options, depth) => {
|
|
354
|
-
const
|
|
542
|
+
const indexes = getIndexSignatures(schema, options, depth);
|
|
355
543
|
const properties = keywordOf(schema, "properties");
|
|
356
544
|
if (properties && Object.keys(properties).length > 0) {
|
|
357
545
|
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
358
546
|
const declaredRequired = keywordOf(schema, "required");
|
|
359
547
|
const requiredSet = new Set(Array.isArray(declaredRequired) ? declaredRequired : []);
|
|
360
548
|
const hasDescriptions = Object.values(properties).some((p) => propertyDescription(p) !== void 0);
|
|
361
|
-
const
|
|
549
|
+
const declared = [];
|
|
362
550
|
let hasOptionalProperty = false;
|
|
363
551
|
const entries = [];
|
|
364
552
|
for (const key of Object.keys(properties)) {
|
|
@@ -368,7 +556,7 @@ const objectTypeToTs = (schema, options, depth) => {
|
|
|
368
556
|
if (!isRequired)
|
|
369
557
|
hasOptionalProperty = true;
|
|
370
558
|
const propType = getTypeScriptType(propSchema, options, depth + 1);
|
|
371
|
-
|
|
559
|
+
declared.push({ name: key, type: propType });
|
|
372
560
|
const declaration = readonlyPrefix + safeKey(key) + optional + ": " + propType;
|
|
373
561
|
if (!hasDescriptions) {
|
|
374
562
|
entries.push(declaration);
|
|
@@ -377,40 +565,46 @@ const objectTypeToTs = (schema, options, depth) => {
|
|
|
377
565
|
const inlineDescription = propertyDescription(propSchema);
|
|
378
566
|
entries.push((inlineDescription ? buildInlinePropertyComment(inlineDescription) : "") + " " + declaration + ";");
|
|
379
567
|
}
|
|
380
|
-
|
|
381
|
-
const line = buildIndexSignatureLine(index,
|
|
568
|
+
for (const index of indexes) {
|
|
569
|
+
const line = buildIndexSignatureLine(index, declared, hasOptionalProperty, options);
|
|
382
570
|
entries.push(hasDescriptions ? " " + line + ";" : line);
|
|
383
571
|
}
|
|
384
572
|
if (hasDescriptions)
|
|
385
573
|
return "{\n" + entries.join("\n") + "\n}";
|
|
386
574
|
return "{ " + entries.join("; ") + " }";
|
|
387
575
|
}
|
|
388
|
-
|
|
389
|
-
|
|
576
|
+
const only = indexes[0];
|
|
577
|
+
if (only !== void 0 && indexes.length === 1) {
|
|
578
|
+
return only.key === "string" && !options.readonly ? recordType("string", only.value, options) : recordType(only.key, only.value, options);
|
|
579
|
+
}
|
|
580
|
+
if (indexes.length > 1) {
|
|
581
|
+
const readonlyPrefix = options.readonly ? "readonly " : "";
|
|
582
|
+
return `{ ${indexes.map(({ key, value }) => `${readonlyPrefix}[key: ${key}]: ${value}`).join("; ")} }`;
|
|
390
583
|
}
|
|
391
584
|
return "object";
|
|
392
585
|
};
|
|
393
586
|
const getLocalShapeType = (schema, options, depth) => {
|
|
394
587
|
const type = keywordOf(schema, "type");
|
|
395
588
|
if (!type) {
|
|
396
|
-
|
|
397
|
-
|
|
589
|
+
if (declaresOpenKeys(schema))
|
|
590
|
+
return objectTypeToTs(schema, options, depth);
|
|
591
|
+
const properties = keywordOf(schema, "properties");
|
|
592
|
+
if (properties && Object.keys(properties).length > 0)
|
|
398
593
|
return objectTypeToTs(schema, options, depth);
|
|
399
594
|
const additionalProperties = keywordOf(schema, "additionalProperties");
|
|
400
595
|
if (typeof additionalProperties === "boolean") {
|
|
401
596
|
return recordType("string", getBooleanSubSchemaType(additionalProperties), options);
|
|
402
597
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
return "boolean";
|
|
598
|
+
if (!composes(schema)) {
|
|
599
|
+
const defaultValue = keywordOf(schema, "default");
|
|
600
|
+
if (defaultValue !== void 0) {
|
|
601
|
+
if (typeof defaultValue === "string")
|
|
602
|
+
return "string";
|
|
603
|
+
if (typeof defaultValue === "number")
|
|
604
|
+
return "number";
|
|
605
|
+
if (typeof defaultValue === "boolean")
|
|
606
|
+
return "boolean";
|
|
607
|
+
}
|
|
414
608
|
}
|
|
415
609
|
return void 0;
|
|
416
610
|
}
|
|
@@ -484,7 +678,7 @@ const getUnbrandedType = (schema, options, depth) => {
|
|
|
484
678
|
}
|
|
485
679
|
const unionBranches = unionBranchesOf(schema);
|
|
486
680
|
if (unionBranches) {
|
|
487
|
-
const union = unionOf(unionBranches.map((branch) =>
|
|
681
|
+
const union = unionOf(unionBranches.map((branch) => unionBranchType(branch, domain, options, depth)));
|
|
488
682
|
if (members.length === 0 && !Array.isArray(allOf))
|
|
489
683
|
return union;
|
|
490
684
|
members.push(wrapUnion(union));
|
|
@@ -493,6 +687,28 @@ const getUnbrandedType = (schema, options, depth) => {
|
|
|
493
687
|
return "unknown";
|
|
494
688
|
return intersectionOf(members);
|
|
495
689
|
};
|
|
690
|
+
const unionBranchType = (branch, domain, options, depth) => {
|
|
691
|
+
const standalone = () => getTypeScriptType(branch, options, depth + 1);
|
|
692
|
+
if (!isPlainFragment(branch))
|
|
693
|
+
return standalone();
|
|
694
|
+
const required = requiredOf(branch);
|
|
695
|
+
const own = keywordMap(branch, "properties");
|
|
696
|
+
if (required.length === 0)
|
|
697
|
+
return standalone();
|
|
698
|
+
const properties = {};
|
|
699
|
+
for (const key of required) {
|
|
700
|
+
const declared = domain === void 0 ? void 0 : readKey(domain, key);
|
|
701
|
+
assignKey(properties, key, declared === void 0 ? true : declared);
|
|
702
|
+
}
|
|
703
|
+
if (own !== void 0) {
|
|
704
|
+
for (const key of Object.keys(own)) {
|
|
705
|
+
const sub = readKey(own, key);
|
|
706
|
+
const existing = readKey(properties, key);
|
|
707
|
+
assignKey(properties, key, existing === void 0 ? sub : { allOf: [existing, sub] });
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return objectTypeToTs({ type: "object", properties, required: [...required] }, options, depth);
|
|
711
|
+
};
|
|
496
712
|
const unionBranchesOf = (schema) => {
|
|
497
713
|
const oneOf = keywordOf(schema, "oneOf");
|
|
498
714
|
if (Array.isArray(oneOf) && oneOf.length > 0)
|
|
@@ -506,9 +722,9 @@ const getCompositionMembers = (schema, options, depth) => {
|
|
|
506
722
|
if (!isSchemaObject(schema))
|
|
507
723
|
return [];
|
|
508
724
|
const members = [];
|
|
725
|
+
const domain = keywordMap(schema, "properties");
|
|
509
726
|
const allOf = keywordOf(schema, "allOf");
|
|
510
727
|
if (Array.isArray(allOf)) {
|
|
511
|
-
const domain = keywordMap(schema, "properties");
|
|
512
728
|
for (const entry of allOf)
|
|
513
729
|
members.push(allOfMember(entry, domain, options, depth));
|
|
514
730
|
}
|
|
@@ -518,7 +734,7 @@ const getCompositionMembers = (schema, options, depth) => {
|
|
|
518
734
|
}
|
|
519
735
|
const unionBranches = unionBranchesOf(schema);
|
|
520
736
|
if (unionBranches) {
|
|
521
|
-
members.push(wrapUnion(unionOf(unionBranches.map((branch) =>
|
|
737
|
+
members.push(wrapUnion(unionOf(unionBranches.map((branch) => unionBranchType(branch, domain, options, depth)))));
|
|
522
738
|
}
|
|
523
739
|
return [...new Set(members)].filter((member) => member !== "unknown");
|
|
524
740
|
};
|
|
@@ -548,30 +764,23 @@ const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
548
764
|
const hasProperties = declaredProperties !== void 0 && Object.keys(declaredProperties).length > 0;
|
|
549
765
|
const hasAdditionalProperties = typeof additionalProperties === "object" && additionalProperties !== null;
|
|
550
766
|
const hasPatternProperties = patternProperties !== void 0 && Object.keys(patternProperties).length > 0;
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
if (
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
let result2 = "";
|
|
563
|
-
if (jsDocTitle && jsDocDescription) {
|
|
564
|
-
result2 += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
767
|
+
const composed = (body) => [body, ...getCompositionMembers(schema, options, 0)].filter((member) => member !== "unknown").join(" & ");
|
|
768
|
+
if (!hasProperties && (hasPatternProperties || hasAdditionalProperties)) {
|
|
769
|
+
const indexes = getIndexSignatures(schema, options, 0);
|
|
770
|
+
if (indexes.length > 0) {
|
|
771
|
+
const body = indexSignaturesType(indexes, options, hasAdditionalProperties);
|
|
772
|
+
let result2 = "";
|
|
773
|
+
if (jsDocTitle && jsDocDescription) {
|
|
774
|
+
result2 += buildJsDocBlock(jsDocTitle, jsDocDescription);
|
|
775
|
+
}
|
|
776
|
+
result2 += `export type ${typeName} = ${composed(body)};`;
|
|
777
|
+
return result2;
|
|
565
778
|
}
|
|
566
|
-
result2 += `export type ${typeName} = {
|
|
567
|
-
${readonlyPrefix}[key: string]: ${additionalPropType};
|
|
568
|
-
};`;
|
|
569
|
-
return result2;
|
|
570
779
|
}
|
|
571
780
|
const schemaProps = declaredProperties ?? {};
|
|
572
781
|
const declaredRequired = keywordOf(schema, "required");
|
|
573
782
|
const requiredSet = new Set(Array.isArray(declaredRequired) ? declaredRequired : []);
|
|
574
|
-
const
|
|
783
|
+
const declared = [];
|
|
575
784
|
let hasOptionalProperty = false;
|
|
576
785
|
let properties = "";
|
|
577
786
|
let isFirstProp = true;
|
|
@@ -588,14 +797,13 @@ const generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
588
797
|
if (!isRequired)
|
|
589
798
|
hasOptionalProperty = true;
|
|
590
799
|
const propType = getTypeScriptType(propSchema, options, 0);
|
|
591
|
-
|
|
800
|
+
declared.push({ name: key, type: propType });
|
|
592
801
|
const quotedKey = readonlyPrefix + safeKey(key);
|
|
593
802
|
const inlineDescription = propertyDescription(propSchema);
|
|
594
803
|
appendLine(inlineDescription ? buildInlinePropertyComment(inlineDescription) + " " + quotedKey + optional + ": " + propType + ";" : " " + quotedKey + optional + ": " + propType + ";");
|
|
595
804
|
}
|
|
596
|
-
const index
|
|
597
|
-
|
|
598
|
-
appendLine(" " + buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options) + ";");
|
|
805
|
+
for (const index of getIndexSignatures(schema, options, 0)) {
|
|
806
|
+
appendLine(" " + buildIndexSignatureLine(index, declared, hasOptionalProperty, options) + ";");
|
|
599
807
|
}
|
|
600
808
|
let result = "";
|
|
601
809
|
if (jsDocTitle && jsDocDescription) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asks whether emitted source names an identifier — the question that decides
|
|
3
|
+
* which `$ref` imports a generated file actually needs.
|
|
4
|
+
*
|
|
5
|
+
* Deciding it from the text rather than from a second walk over the schema is
|
|
6
|
+
* what keeps the import list in step with whatever the type and function
|
|
7
|
+
* emitters chose to write: a `$ref` in a position the type emitter does not read
|
|
8
|
+
* is called and never named, one it inlines through is named in a file that
|
|
9
|
+
* never walked into it, and a ref inside a folded-away branch is neither.
|
|
10
|
+
*
|
|
11
|
+
* The remaining inexactness is the harmless direction — a name spelled in code
|
|
12
|
+
* that is not a reference to the import keeps a binding that could have gone.
|
|
13
|
+
*/
|
|
14
|
+
export declare const identifierMentions: (source: string) => ((name: string) => boolean);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const escapeForWordMatch = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2
|
+
const BEFORE_REGEX = /* @__PURE__ */ new Set(["(", ",", "=", ":", "[", "!", "&", "|", "?", "{", "}", ";", "\n"]);
|
|
3
|
+
const stripDataText = (source) => {
|
|
4
|
+
const out = source.split("");
|
|
5
|
+
let previous = "\n";
|
|
6
|
+
let i = 0;
|
|
7
|
+
const blank = (from, to) => {
|
|
8
|
+
for (let n = from; n < to; n++)
|
|
9
|
+
if (out[n] !== "\n")
|
|
10
|
+
out[n] = " ";
|
|
11
|
+
};
|
|
12
|
+
while (i < source.length) {
|
|
13
|
+
const char = source[i];
|
|
14
|
+
const next = source[i + 1];
|
|
15
|
+
if (char === "/" && next === "/") {
|
|
16
|
+
const end = source.indexOf("\n", i);
|
|
17
|
+
blank(i, end === -1 ? source.length : end);
|
|
18
|
+
i = end === -1 ? source.length : end;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (char === "/" && next === "*") {
|
|
22
|
+
const end = source.indexOf("*/", i + 2);
|
|
23
|
+
if (end === -1)
|
|
24
|
+
return source;
|
|
25
|
+
blank(i, end + 2);
|
|
26
|
+
i = end + 2;
|
|
27
|
+
previous = " ";
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
31
|
+
let n = i + 1;
|
|
32
|
+
while (n < source.length && source[n] !== char) {
|
|
33
|
+
if (source[n] === "\\")
|
|
34
|
+
n++;
|
|
35
|
+
if (char !== "`" && source[n] === "\n")
|
|
36
|
+
return source;
|
|
37
|
+
n++;
|
|
38
|
+
}
|
|
39
|
+
if (n >= source.length)
|
|
40
|
+
return source;
|
|
41
|
+
blank(i, n + 1);
|
|
42
|
+
i = n + 1;
|
|
43
|
+
previous = char;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (char === "/" && BEFORE_REGEX.has(previous)) {
|
|
47
|
+
let n = i + 1;
|
|
48
|
+
let inClass = false;
|
|
49
|
+
while (n < source.length) {
|
|
50
|
+
const c = source[n];
|
|
51
|
+
if (c === "\\")
|
|
52
|
+
n += 2;
|
|
53
|
+
else if (c === "\n")
|
|
54
|
+
return source;
|
|
55
|
+
else if (c === "[")
|
|
56
|
+
inClass = true, n++;
|
|
57
|
+
else if (c === "]")
|
|
58
|
+
inClass = false, n++;
|
|
59
|
+
else if (c === "/" && !inClass)
|
|
60
|
+
break;
|
|
61
|
+
else
|
|
62
|
+
n++;
|
|
63
|
+
}
|
|
64
|
+
if (n >= source.length)
|
|
65
|
+
return source;
|
|
66
|
+
let end = n + 1;
|
|
67
|
+
while (end < source.length && /[a-z]/.test(source[end]))
|
|
68
|
+
end++;
|
|
69
|
+
blank(i, end);
|
|
70
|
+
i = end;
|
|
71
|
+
previous = "/";
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (!/\s/.test(char))
|
|
75
|
+
previous = char;
|
|
76
|
+
else if (char === "\n")
|
|
77
|
+
previous = "\n";
|
|
78
|
+
i++;
|
|
79
|
+
}
|
|
80
|
+
return out.join("");
|
|
81
|
+
};
|
|
82
|
+
const identifierMentions = (source) => {
|
|
83
|
+
const code = stripDataText(source);
|
|
84
|
+
return (name) => new RegExp(`\\b${escapeForWordMatch(name)}\\b`).test(code);
|
|
85
|
+
};
|
|
86
|
+
export {
|
|
87
|
+
identifierMentions
|
|
88
|
+
};
|
|
@@ -15,9 +15,28 @@ export declare const toKebabCase: (value: string) => string;
|
|
|
15
15
|
/**
|
|
16
16
|
* Converts a JSON Schema $ref to a filename.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* The name is built from every part of the ref that names something: the base
|
|
19
|
+
* URI a relative ref points at, then the pointer's own definition names. For the
|
|
20
|
+
* refs almost every document writes — `#/$defs/contact`,
|
|
21
|
+
* `#/components/schemas/UserProfile`, `#named` — there is exactly one, and the
|
|
22
|
+
* name is what it has always been.
|
|
23
|
+
*
|
|
24
|
+
* Deriving it from the whole ref is what makes the name a *pure function of the
|
|
25
|
+
* ref*, and that is load-bearing rather than tidy. Two definitions in different
|
|
26
|
+
* parents (`#/$defs/user/$defs/meta` and `#/$defs/order/$defs/meta`) or in
|
|
27
|
+
* different embedded resources (`first#/$defs/stuff` and `second#/$defs/stuff`)
|
|
28
|
+
* are an ordinary shape in a real document, and naming both after their last
|
|
29
|
+
* segment made them one file — so generation refused rather than emit a silently
|
|
30
|
+
* wrong type. Resolving that by *renaming* whichever was reached second would
|
|
31
|
+
* need every emitter to agree on the walk order, since each turns a `$ref` into
|
|
32
|
+
* an import name on its own; deriving a distinct name from the ref itself means
|
|
33
|
+
* they agree without having to coordinate at all.
|
|
34
|
+
*
|
|
35
|
+
* Handles these ref forms:
|
|
19
36
|
* - Internal `#/$defs/contact` → `contact`
|
|
20
37
|
* - Internal `#/definitions/ServerVariable` → `server-variable`
|
|
38
|
+
* - Nested `#/$defs/user/$defs/meta` → `user-meta`
|
|
39
|
+
* - Relative-base `second#/$defs/stuff` → `second-stuff`
|
|
21
40
|
* - URI `http://example.com/definitions/3.1.0/channel.json` → `channel`
|
|
22
41
|
* - URI with fragment `http://example.com/channel.json#/definitions/queue` → `channel-queue`
|
|
23
42
|
*
|
|
@@ -28,8 +47,10 @@ export declare const toKebabCase: (value: string) => string;
|
|
|
28
47
|
* ```ts
|
|
29
48
|
* refToFilename('#/$defs/contact') // 'contact'
|
|
30
49
|
* refToFilename('#/$defs/server-variable') // 'server-variable'
|
|
31
|
-
* refToFilename('#/definitions/ServerVariable') // 'server-variable'
|
|
32
50
|
* refToFilename('#/definitions/APIKeySecurityScheme') // 'api-key-security-scheme'
|
|
51
|
+
* refToFilename('#/components/schemas/UserProfile') // 'user-profile'
|
|
52
|
+
* refToFilename('#/$defs/user/$defs/meta') // 'user-meta'
|
|
53
|
+
* refToFilename('second#/$defs/stuff') // 'second-stuff'
|
|
33
54
|
* refToFilename('http://asyncapi.com/definitions/3.1.0/channel.json') // 'channel'
|
|
34
55
|
* refToFilename('#named') // 'named' — a plain `$anchor` ref
|
|
35
56
|
* ```
|
package/dist/ref-to-filename.js
CHANGED
|
@@ -42,16 +42,22 @@ const uriRefToFilename = (uri) => {
|
|
|
42
42
|
return baseName;
|
|
43
43
|
return `${baseName}-${toKebabCase(fragLast)}`;
|
|
44
44
|
};
|
|
45
|
+
const CONTAINER_SEGMENTS = /* @__PURE__ */ new Set(["$defs", "definitions", "properties"]);
|
|
46
|
+
const pointerNameParts = (pointer) => {
|
|
47
|
+
const segments = pointer.split("/").filter((segment) => segment !== "");
|
|
48
|
+
const named = segments.map((segment, index) => ({ segment, nested: index > 0 && CONTAINER_SEGMENTS.has(segments[index - 1]) })).filter(({ segment }) => !CONTAINER_SEGMENTS.has(segment));
|
|
49
|
+
return named.filter((part, index) => part.nested || index === named.length - 1).map(({ segment }) => segment);
|
|
50
|
+
};
|
|
45
51
|
const refToFilename = (ref) => {
|
|
46
52
|
if (ref.startsWith("http://") || ref.startsWith("https://")) {
|
|
47
53
|
return normalizeFilename(uriRefToFilename(ref), ref);
|
|
48
54
|
}
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return normalizeFilename(
|
|
55
|
+
const hashIndex = ref.indexOf("#");
|
|
56
|
+
const base = hashIndex === -1 ? "" : ref.slice(0, hashIndex);
|
|
57
|
+
const pointer = hashIndex === -1 ? ref : ref.slice(hashIndex + 1);
|
|
58
|
+
const baseParts = base.replace(/\.json$/, "").split("/").filter((segment) => segment !== "");
|
|
59
|
+
const parts = [...baseParts, ...pointerNameParts(pointer)];
|
|
60
|
+
return normalizeFilename(parts.map((part) => /[A-Z]/.test(part) ? toKebabCase(part) : part).join("-"), ref);
|
|
55
61
|
};
|
|
56
62
|
export {
|
|
57
63
|
refToFilename,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
+
/**
|
|
3
|
+
* The conditional definition an `allOf` member's local `$ref` points at, when it
|
|
4
|
+
* points at one.
|
|
5
|
+
*
|
|
6
|
+
* OpenAPI's security scheme composes its per-type rules as `allOf: [{ $ref:
|
|
7
|
+
* '#/$defs/type-http' }, …]`, each definition an `if`/`then` on the `type` the
|
|
8
|
+
* composing schema enumerates. The definition's own file cannot see that
|
|
9
|
+
* enumeration, so its conditional drops there; the type emitter reads the
|
|
10
|
+
* definition *here* instead and renders its arms into the composing type.
|
|
11
|
+
*
|
|
12
|
+
* That makes this the one place where a type name reaches a file through a
|
|
13
|
+
* `$ref` the file never walks into, which is why the import collectors ask the
|
|
14
|
+
* same question rather than each deciding for themselves: `security-scheme.ts`
|
|
15
|
+
* named `OauthFlows` in an arm it had inlined while `security-scheme-type-oauth2.ts`
|
|
16
|
+
* imported it and rendered nothing — a `TS2304` in one file and a `TS6133` in
|
|
17
|
+
* the other, from a single disagreement about what gets inlined.
|
|
18
|
+
*/
|
|
19
|
+
export declare const referencedConditional: (entry: JSONSchema, rootSchema: Record<string, unknown> | undefined) => Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { readKey } from "./read-key.js";
|
|
2
|
+
import { resolveRef } from "./resolve-ref.js";
|
|
3
|
+
import { isSchemaObject } from "./schema-guards.js";
|
|
4
|
+
const referencedConditional = (entry, rootSchema) => {
|
|
5
|
+
if (!isSchemaObject(entry) || rootSchema === void 0)
|
|
6
|
+
return void 0;
|
|
7
|
+
const ref = readKey(entry, "$ref");
|
|
8
|
+
if (typeof ref !== "string" || !ref.startsWith("#"))
|
|
9
|
+
return void 0;
|
|
10
|
+
const resolved = resolveRef(ref, rootSchema);
|
|
11
|
+
if (resolved === void 0 || !isSchemaObject(resolved))
|
|
12
|
+
return void 0;
|
|
13
|
+
return Object.hasOwn(resolved, "if") ? resolved : void 0;
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
referencedConditional
|
|
17
|
+
};
|
package/dist/walk-ref-graph.js
CHANGED
|
@@ -231,6 +231,24 @@ const cachedExtractRefs = (cache, schema) => {
|
|
|
231
231
|
cache.extractRefsCache.set(schema, refs);
|
|
232
232
|
return refs;
|
|
233
233
|
};
|
|
234
|
+
const claimNames = (ref, resolved, typeSuffix, filenameOwners, typeNameOwners) => {
|
|
235
|
+
const filename = refToFilename(ref);
|
|
236
|
+
const typeName = refToName(ref, typeSuffix);
|
|
237
|
+
const fileOwner = filenameOwners.get(filename);
|
|
238
|
+
if (fileOwner !== void 0) {
|
|
239
|
+
if (sameDefinition(fileOwner.resolved, resolved))
|
|
240
|
+
return void 0;
|
|
241
|
+
throw new Error(`"${ref}" and ${fileOwner.label} both generate the file "${filename}.ts", so only one of them can be emitted and every reference to the other would resolve to the wrong type. Rename one definition.`);
|
|
242
|
+
}
|
|
243
|
+
const typeOwner = typeNameOwners.get(typeName);
|
|
244
|
+
if (typeOwner !== void 0) {
|
|
245
|
+
throw new Error(`"${ref}" and ${typeOwner.label} both generate the type name "${typeName}" from different files, so the generated output would import that name twice and fail to parse. Rename one definition.`);
|
|
246
|
+
}
|
|
247
|
+
const owner = { label: `"${ref}"`, resolved };
|
|
248
|
+
filenameOwners.set(filename, owner);
|
|
249
|
+
typeNameOwners.set(typeName, owner);
|
|
250
|
+
return { filename, typeName };
|
|
251
|
+
};
|
|
234
252
|
const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
235
253
|
const typeSuffix = options.typeSuffix ?? "";
|
|
236
254
|
const baseCache = getRootCache(rootSchema, options.schemas);
|
|
@@ -289,25 +307,12 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
289
307
|
if (!resolved) {
|
|
290
308
|
throw new Error(`Could not resolve $ref "${ref}". Nothing in the document defines it, so the generated output would reference a type and a function that are never emitted. Fix the ref, or drop it from the schema.`);
|
|
291
309
|
}
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
const fileOwner = filenameOwners.get(filename);
|
|
295
|
-
if (fileOwner) {
|
|
296
|
-
if (!sameDefinition(fileOwner.resolved, resolved)) {
|
|
297
|
-
throw new Error(`"${ref}" and ${fileOwner.label} both generate the file "${filename}.ts", so only one of them can be emitted and every reference to the other would resolve to the wrong type. Rename one definition.`);
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
const typeOwner = typeNameOwners.get(typeName);
|
|
301
|
-
if (typeOwner) {
|
|
302
|
-
throw new Error(`"${ref}" and ${typeOwner.label} both generate the type name "${typeName}" from different files, so the generated output would import that name twice and fail to parse. Rename one definition.`);
|
|
303
|
-
}
|
|
304
|
-
const owner = { label: `"${ref}"`, resolved };
|
|
305
|
-
filenameOwners.set(filename, owner);
|
|
306
|
-
typeNameOwners.set(typeName, owner);
|
|
310
|
+
const claimed = claimNames(ref, resolved, typeSuffix, filenameOwners, typeNameOwners);
|
|
311
|
+
if (claimed !== void 0) {
|
|
307
312
|
visit({
|
|
308
313
|
ref,
|
|
309
|
-
typeName,
|
|
310
|
-
filename,
|
|
314
|
+
typeName: claimed.typeName,
|
|
315
|
+
filename: claimed.filename,
|
|
311
316
|
schema: resolveDynamicRefs(resolved, dynamicRefMap),
|
|
312
317
|
rootSchema: upgraded,
|
|
313
318
|
isRoot: false
|