@dudousxd/nestjs-codegen 0.5.0 → 0.5.2

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.
@@ -556,6 +556,7 @@ function extractSchemaFromDto(classDecl, sourceFile, project) {
556
556
  warnings: [],
557
557
  warnedDecorators: /* @__PURE__ */ new Set(),
558
558
  emittedClasses: /* @__PURE__ */ new Map(),
559
+ usedSchemaNames: /* @__PURE__ */ new Set(),
559
560
  visiting: /* @__PURE__ */ new Set(),
560
561
  recursiveSchemas: /* @__PURE__ */ new Set(),
561
562
  depth: 0,
@@ -583,37 +584,28 @@ function buildProperty(prop, classFile, ctx) {
583
584
  const typeNode = prop.getTypeNode();
584
585
  const typeText = typeNode?.getText() ?? "unknown";
585
586
  const isArrayType = !!typeNode && import_ts_morph2.Node.isArrayTypeNode(typeNode);
587
+ const asField = (child) => applyPresence(
588
+ has("IsArray") || isArrayType ? { kind: "array", element: child } : child,
589
+ decorators
590
+ );
586
591
  const discriminator = resolveDiscriminator(dec("Type"));
587
592
  if (discriminator) {
588
593
  const options = discriminator.subTypes.map(
589
594
  (name) => buildNestedReference(name, classFile, ctx)
590
595
  );
591
- const unionNode = {
592
- kind: "union",
593
- options,
594
- discriminator: discriminator.property
595
- };
596
- const wrapArray = has("IsArray") || isArrayType;
597
- const node2 = wrapArray ? { kind: "array", element: unionNode } : unionNode;
598
- return applyPresence(node2, decorators);
596
+ return asField({ kind: "union", options, discriminator: discriminator.property });
599
597
  }
600
598
  const propTypeParam = singularClassName(typeText);
601
599
  if (propTypeParam && ctx.typeBindings.has(propTypeParam)) {
602
600
  const bound = ctx.typeBindings.get(propTypeParam);
603
- const childNode = buildNestedReference(bound, classFile, ctx);
604
- const wrapArray = has("IsArray") || isArrayType;
605
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
606
- return applyPresence(node2, decorators);
601
+ return asField(buildNestedReference(bound, classFile, ctx));
607
602
  }
608
603
  const typeRefName = resolveTypeFactoryName(dec("Type"));
609
604
  if (has("ValidateNested") || typeRefName) {
610
605
  const typeArgs = genericTypeArgNames(typeNode);
611
606
  const childName = typeRefName ?? singularClassName(typeText);
612
607
  if (childName) {
613
- const childNode = buildNestedReference(childName, classFile, ctx, typeArgs);
614
- const wrapArray = has("IsArray") || isArrayType;
615
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
616
- return applyPresence(node2, decorators);
608
+ return asField(buildNestedReference(childName, classFile, ctx, typeArgs));
617
609
  }
618
610
  }
619
611
  let base = baseFromType(typeText, isArrayType);
@@ -741,6 +733,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
741
733
  if (ctx.visiting.has(cacheKey)) {
742
734
  const reserved = ctx.emittedClasses.get(cacheKey) ?? aliasFor(schemaBase, ctx);
743
735
  ctx.emittedClasses.set(cacheKey, reserved);
736
+ ctx.usedSchemaNames.add(reserved);
744
737
  ctx.recursiveSchemas.add(reserved);
745
738
  if (!ctx.warnedDecorators.has(`recursive:${reserved}`)) {
746
739
  ctx.warnedDecorators.add(`recursive:${reserved}`);
@@ -774,6 +767,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
774
767
  });
775
768
  for (const [k, v] of newBindings) ctx.typeBindings.set(k, v);
776
769
  ctx.emittedClasses.set(cacheKey, schemaName);
770
+ ctx.usedSchemaNames.add(schemaName);
777
771
  ctx.visiting.add(cacheKey);
778
772
  ctx.depth += 1;
779
773
  const childNode = buildObject(resolved.decl, resolved.file, ctx);
@@ -781,15 +775,14 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
781
775
  ctx.visiting.delete(cacheKey);
782
776
  for (const [k] of newBindings) ctx.typeBindings.delete(k);
783
777
  ctx.named.set(schemaName, childNode);
778
+ ctx.usedSchemaNames.add(schemaName);
784
779
  return { kind: "ref", name: schemaName };
785
780
  }
786
781
  function aliasFor(className, ctx) {
787
782
  const baseName = `${className}Schema`;
788
783
  let candidate = baseName;
789
784
  let i = 1;
790
- const used = new Set(ctx.named.keys());
791
- for (const v of ctx.emittedClasses.values()) used.add(v);
792
- while (used.has(candidate)) {
785
+ while (ctx.usedSchemaNames.has(candidate)) {
793
786
  candidate = `${baseName}_${i}`;
794
787
  i += 1;
795
788
  }
@@ -3255,18 +3248,27 @@ function refRootIdentifier(refName) {
3255
3248
  function hasSource(src) {
3256
3249
  return !!(src.schema || src.zodText || src.zodRef);
3257
3250
  }
3251
+ function escapeRegExp(s) {
3252
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3253
+ }
3254
+ var wordBoundaryRegexCache = /* @__PURE__ */ new Map();
3255
+ function wordBoundaryRegex(token) {
3256
+ let re = wordBoundaryRegexCache.get(token);
3257
+ if (re === void 0) {
3258
+ re = new RegExp(`\\b${escapeRegExp(token)}\\b`, "g");
3259
+ wordBoundaryRegexCache.set(token, re);
3260
+ }
3261
+ return re;
3262
+ }
3258
3263
  function applyRenames(text, renames) {
3259
3264
  if (!renames || renames.size === 0) return text;
3260
3265
  let out = text;
3261
3266
  for (const [from, to] of renames) {
3262
3267
  if (from === to) continue;
3263
- out = out.replace(new RegExp(`\\b${escapeRegExp(from)}\\b`, "g"), to);
3268
+ out = out.replace(wordBoundaryRegex(from), to);
3264
3269
  }
3265
3270
  return out;
3266
3271
  }
3267
- function escapeRegExp(s) {
3268
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3269
- }
3270
3272
  function isSelfReferential(name, text) {
3271
3273
  return new RegExp(`\\b${escapeRegExp(name)}\\b`).test(text);
3272
3274
  }
@@ -3278,7 +3280,23 @@ function planNestedSchemas(entries) {
3278
3280
  const local = Object.entries(entry.nestedSchemas);
3279
3281
  if (local.length === 0) continue;
3280
3282
  const rename = /* @__PURE__ */ new Map();
3281
- for (const [name] of local) rename.set(name, name);
3283
+ const renameValues = /* @__PURE__ */ new Set();
3284
+ const setRename = (key, value) => {
3285
+ const prev = rename.get(key);
3286
+ rename.set(key, value);
3287
+ if (prev !== void 0 && prev !== value) {
3288
+ let stillUsed = false;
3289
+ for (const v of rename.values()) {
3290
+ if (v === prev) {
3291
+ stillUsed = true;
3292
+ break;
3293
+ }
3294
+ }
3295
+ if (!stillUsed) renameValues.delete(prev);
3296
+ }
3297
+ renameValues.add(value);
3298
+ };
3299
+ for (const [name] of local) setRename(name, name);
3282
3300
  const textFor = (name) => {
3283
3301
  const raw = entry.nestedSchemas?.[name] ?? "";
3284
3302
  return applyRenames(raw, rename);
@@ -3296,11 +3314,11 @@ function planNestedSchemas(entries) {
3296
3314
  if (existing === text) continue;
3297
3315
  let i = 2;
3298
3316
  let candidate = `${name}_${i}`;
3299
- while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || [...rename.values()].includes(candidate)) {
3317
+ while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || renameValues.has(candidate)) {
3300
3318
  i += 1;
3301
3319
  candidate = `${name}_${i}`;
3302
3320
  }
3303
- rename.set(name, candidate);
3321
+ setRename(name, candidate);
3304
3322
  changed = true;
3305
3323
  }
3306
3324
  }