@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.
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
2
- import { U as UserConfig } from '../index-B0mS84Jj.cjs';
2
+ import { U as UserConfig } from '../index-_qRai4M3.cjs';
3
3
  import 'ts-morph';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
2
- import { U as UserConfig } from '../index-B0mS84Jj.js';
2
+ import { U as UserConfig } from '../index-_qRai4M3.js';
3
3
  import 'ts-morph';
4
4
 
5
5
  /**
@@ -530,6 +530,7 @@ function extractSchemaFromDto(classDecl, sourceFile, project) {
530
530
  warnings: [],
531
531
  warnedDecorators: /* @__PURE__ */ new Set(),
532
532
  emittedClasses: /* @__PURE__ */ new Map(),
533
+ usedSchemaNames: /* @__PURE__ */ new Set(),
533
534
  visiting: /* @__PURE__ */ new Set(),
534
535
  recursiveSchemas: /* @__PURE__ */ new Set(),
535
536
  depth: 0,
@@ -557,37 +558,28 @@ function buildProperty(prop, classFile, ctx) {
557
558
  const typeNode = prop.getTypeNode();
558
559
  const typeText = typeNode?.getText() ?? "unknown";
559
560
  const isArrayType = !!typeNode && Node2.isArrayTypeNode(typeNode);
561
+ const asField = (child) => applyPresence(
562
+ has("IsArray") || isArrayType ? { kind: "array", element: child } : child,
563
+ decorators
564
+ );
560
565
  const discriminator = resolveDiscriminator(dec("Type"));
561
566
  if (discriminator) {
562
567
  const options = discriminator.subTypes.map(
563
568
  (name) => buildNestedReference(name, classFile, ctx)
564
569
  );
565
- const unionNode = {
566
- kind: "union",
567
- options,
568
- discriminator: discriminator.property
569
- };
570
- const wrapArray = has("IsArray") || isArrayType;
571
- const node2 = wrapArray ? { kind: "array", element: unionNode } : unionNode;
572
- return applyPresence(node2, decorators);
570
+ return asField({ kind: "union", options, discriminator: discriminator.property });
573
571
  }
574
572
  const propTypeParam = singularClassName(typeText);
575
573
  if (propTypeParam && ctx.typeBindings.has(propTypeParam)) {
576
574
  const bound = ctx.typeBindings.get(propTypeParam);
577
- const childNode = buildNestedReference(bound, classFile, ctx);
578
- const wrapArray = has("IsArray") || isArrayType;
579
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
580
- return applyPresence(node2, decorators);
575
+ return asField(buildNestedReference(bound, classFile, ctx));
581
576
  }
582
577
  const typeRefName = resolveTypeFactoryName(dec("Type"));
583
578
  if (has("ValidateNested") || typeRefName) {
584
579
  const typeArgs = genericTypeArgNames(typeNode);
585
580
  const childName = typeRefName ?? singularClassName(typeText);
586
581
  if (childName) {
587
- const childNode = buildNestedReference(childName, classFile, ctx, typeArgs);
588
- const wrapArray = has("IsArray") || isArrayType;
589
- const node2 = wrapArray ? { kind: "array", element: childNode } : childNode;
590
- return applyPresence(node2, decorators);
582
+ return asField(buildNestedReference(childName, classFile, ctx, typeArgs));
591
583
  }
592
584
  }
593
585
  let base = baseFromType(typeText, isArrayType);
@@ -715,6 +707,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
715
707
  if (ctx.visiting.has(cacheKey)) {
716
708
  const reserved = ctx.emittedClasses.get(cacheKey) ?? aliasFor(schemaBase, ctx);
717
709
  ctx.emittedClasses.set(cacheKey, reserved);
710
+ ctx.usedSchemaNames.add(reserved);
718
711
  ctx.recursiveSchemas.add(reserved);
719
712
  if (!ctx.warnedDecorators.has(`recursive:${reserved}`)) {
720
713
  ctx.warnedDecorators.add(`recursive:${reserved}`);
@@ -748,6 +741,7 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
748
741
  });
749
742
  for (const [k, v] of newBindings) ctx.typeBindings.set(k, v);
750
743
  ctx.emittedClasses.set(cacheKey, schemaName);
744
+ ctx.usedSchemaNames.add(schemaName);
751
745
  ctx.visiting.add(cacheKey);
752
746
  ctx.depth += 1;
753
747
  const childNode = buildObject(resolved.decl, resolved.file, ctx);
@@ -755,15 +749,14 @@ function buildNestedReference(className, fromFile, ctx, typeArgs = []) {
755
749
  ctx.visiting.delete(cacheKey);
756
750
  for (const [k] of newBindings) ctx.typeBindings.delete(k);
757
751
  ctx.named.set(schemaName, childNode);
752
+ ctx.usedSchemaNames.add(schemaName);
758
753
  return { kind: "ref", name: schemaName };
759
754
  }
760
755
  function aliasFor(className, ctx) {
761
756
  const baseName = `${className}Schema`;
762
757
  let candidate = baseName;
763
758
  let i = 1;
764
- const used = new Set(ctx.named.keys());
765
- for (const v of ctx.emittedClasses.values()) used.add(v);
766
- while (used.has(candidate)) {
759
+ while (ctx.usedSchemaNames.has(candidate)) {
767
760
  candidate = `${baseName}_${i}`;
768
761
  i += 1;
769
762
  }
@@ -3234,18 +3227,27 @@ function refRootIdentifier(refName) {
3234
3227
  function hasSource(src) {
3235
3228
  return !!(src.schema || src.zodText || src.zodRef);
3236
3229
  }
3230
+ function escapeRegExp(s) {
3231
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3232
+ }
3233
+ var wordBoundaryRegexCache = /* @__PURE__ */ new Map();
3234
+ function wordBoundaryRegex(token) {
3235
+ let re = wordBoundaryRegexCache.get(token);
3236
+ if (re === void 0) {
3237
+ re = new RegExp(`\\b${escapeRegExp(token)}\\b`, "g");
3238
+ wordBoundaryRegexCache.set(token, re);
3239
+ }
3240
+ return re;
3241
+ }
3237
3242
  function applyRenames(text, renames) {
3238
3243
  if (!renames || renames.size === 0) return text;
3239
3244
  let out = text;
3240
3245
  for (const [from, to] of renames) {
3241
3246
  if (from === to) continue;
3242
- out = out.replace(new RegExp(`\\b${escapeRegExp(from)}\\b`, "g"), to);
3247
+ out = out.replace(wordBoundaryRegex(from), to);
3243
3248
  }
3244
3249
  return out;
3245
3250
  }
3246
- function escapeRegExp(s) {
3247
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3248
- }
3249
3251
  function isSelfReferential(name, text) {
3250
3252
  return new RegExp(`\\b${escapeRegExp(name)}\\b`).test(text);
3251
3253
  }
@@ -3257,7 +3259,23 @@ function planNestedSchemas(entries) {
3257
3259
  const local = Object.entries(entry.nestedSchemas);
3258
3260
  if (local.length === 0) continue;
3259
3261
  const rename = /* @__PURE__ */ new Map();
3260
- for (const [name] of local) rename.set(name, name);
3262
+ const renameValues = /* @__PURE__ */ new Set();
3263
+ const setRename = (key, value) => {
3264
+ const prev = rename.get(key);
3265
+ rename.set(key, value);
3266
+ if (prev !== void 0 && prev !== value) {
3267
+ let stillUsed = false;
3268
+ for (const v of rename.values()) {
3269
+ if (v === prev) {
3270
+ stillUsed = true;
3271
+ break;
3272
+ }
3273
+ }
3274
+ if (!stillUsed) renameValues.delete(prev);
3275
+ }
3276
+ renameValues.add(value);
3277
+ };
3278
+ for (const [name] of local) setRename(name, name);
3261
3279
  const textFor = (name) => {
3262
3280
  const raw = entry.nestedSchemas?.[name] ?? "";
3263
3281
  return applyRenames(raw, rename);
@@ -3275,11 +3293,11 @@ function planNestedSchemas(entries) {
3275
3293
  if (existing === text) continue;
3276
3294
  let i = 2;
3277
3295
  let candidate = `${name}_${i}`;
3278
- while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || [...rename.values()].includes(candidate)) {
3296
+ while (globalSchemas.has(candidate) && globalSchemas.get(candidate) !== textFor(name) || renameValues.has(candidate)) {
3279
3297
  i += 1;
3280
3298
  candidate = `${name}_${i}`;
3281
3299
  }
3282
- rename.set(name, candidate);
3300
+ setRename(name, candidate);
3283
3301
  changed = true;
3284
3302
  }
3285
3303
  }