@dudousxd/nestjs-codegen 0.17.2 → 0.19.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/cli/main.js CHANGED
@@ -3472,10 +3472,46 @@ function resolveReturnedClass(factoryDecl) {
3472
3472
  }
3473
3473
  return void 0;
3474
3474
  }
3475
+ function factoryCalleeName(call) {
3476
+ const callee = unwrapExpression(call.getExpression());
3477
+ if (Node5.isIdentifier(callee)) return callee;
3478
+ if (Node5.isPropertyAccessExpression(callee)) {
3479
+ const name = callee.getNameNode();
3480
+ return Node5.isIdentifier(name) ? name : void 0;
3481
+ }
3482
+ return void 0;
3483
+ }
3475
3484
  function resolveFactoryDeclaration(call) {
3476
- const callee = call.getExpression();
3477
- if (!Node5.isIdentifier(callee)) return void 0;
3478
- return callee.getDefinitions().map((d) => d.getDeclarationNode()).find((n) => Node5.isFunctionDeclaration(n));
3485
+ const name = factoryCalleeName(call);
3486
+ return name ? resolveFactoryFromName(name, /* @__PURE__ */ new Set()) : void 0;
3487
+ }
3488
+ function resolveFactoryFromName(name, seen) {
3489
+ if (seen.has(name)) return void 0;
3490
+ seen.add(name);
3491
+ const decls = name.getDefinitions().map((d) => d.getDeclarationNode()).filter((n) => n !== void 0);
3492
+ for (const decl of decls) {
3493
+ if (Node5.isFunctionDeclaration(decl) || Node5.isMethodDeclaration(decl)) return decl;
3494
+ if (Node5.isPropertyAssignment(decl)) {
3495
+ const init = decl.getInitializer();
3496
+ const inner = init ? unwrapExpression(init) : void 0;
3497
+ if (inner && Node5.isIdentifier(inner)) {
3498
+ const resolved = resolveFactoryFromName(inner, seen);
3499
+ if (resolved) return resolved;
3500
+ }
3501
+ continue;
3502
+ }
3503
+ if (Node5.isShorthandPropertyAssignment(decl)) {
3504
+ const nameNode = decl.getNameNode();
3505
+ const resolved = Node5.isIdentifier(nameNode) ? resolveFactoryFromName(nameNode, seen) : void 0;
3506
+ if (resolved) return resolved;
3507
+ }
3508
+ }
3509
+ return void 0;
3510
+ }
3511
+ function warnUnresolvedFactory(cls, calleeText, reason) {
3512
+ console.warn(
3513
+ `[nestjs-codegen/fast] ${cls.getName() ?? "<anonymous class>"} in ${cls.getSourceFile().getFilePath()} extends ${calleeText}(...) but ${reason} \u2014 it contributes NO routes to the generated client. Resolvable factory callees: a name (\`factory(...)\`), a namespace or object property (\`ns.factory(...)\`), or a static method (\`Factory.create(...)\`), each resolving to a function or method declaration that returns a class.`
3514
+ );
3479
3515
  }
3480
3516
  function resolveFactoryStaticClass(node) {
3481
3517
  if (!Node5.isPropertyAccessExpression(node)) return void 0;
@@ -3497,30 +3533,78 @@ function resolveFactoryStaticClass(node) {
3497
3533
  function resolveInheritedMethods(cls) {
3498
3534
  const expr = resolveHeritageCall(cls.getExtends()?.getExpression());
3499
3535
  if (!expr) return void 0;
3500
- const callee = expr.getExpression();
3501
- if (!Node5.isIdentifier(callee)) return void 0;
3536
+ const isController = cls.getDecorator("Controller") !== void 0;
3537
+ const calleeText = expr.getExpression().getText();
3502
3538
  const factoryDecl = resolveFactoryDeclaration(expr);
3503
- if (!factoryDecl) return void 0;
3539
+ if (!factoryDecl) {
3540
+ if (isController) {
3541
+ warnUnresolvedFactory(
3542
+ cls,
3543
+ calleeText,
3544
+ factoryCalleeName(expr) ? "its callee does not resolve to a function or method declaration" : "its callee is not a name that can be resolved statically"
3545
+ );
3546
+ }
3547
+ return void 0;
3548
+ }
3504
3549
  const returnedClass = resolveReturnedClass(factoryDecl);
3505
- if (!returnedClass) return void 0;
3550
+ if (!returnedClass) {
3551
+ if (isController) {
3552
+ warnUnresolvedFactory(cls, calleeText, "that factory does not return a class declaration");
3553
+ }
3554
+ return void 0;
3555
+ }
3506
3556
  const classArgs = [];
3557
+ const namedClassArgs = {};
3507
3558
  for (const arg of expr.getArguments()) {
3508
- if (!Node5.isIdentifier(arg)) continue;
3509
- const decl = arg.getDefinitions().map((d) => d.getDeclarationNode()).find((n) => Node5.isClassDeclaration(n));
3510
- if (decl) {
3511
- classArgs.push({
3512
- name: decl.getName() ?? arg.getText(),
3513
- filePath: decl.getSourceFile().getFilePath()
3514
- });
3559
+ const positional = resolveClassReference(arg);
3560
+ if (positional) {
3561
+ classArgs.push(positional);
3562
+ continue;
3563
+ }
3564
+ if (!Node5.isObjectLiteralExpression(arg)) continue;
3565
+ for (const prop of arg.getProperties()) {
3566
+ const named = resolvePropertyClassReference(prop);
3567
+ if (!named) continue;
3568
+ namedClassArgs[named.key] ??= named.ref;
3515
3569
  }
3516
3570
  }
3517
3571
  return {
3518
3572
  methods: returnedClass.getMethods(),
3519
- factoryName: callee.getText(),
3573
+ // The DECLARATION's name, not the call-site text: consumers look the factory
3574
+ // back up by name inside `factoryFilePath`, which a qualified
3575
+ // `tables.createTableController` would never match. For a bare identifier
3576
+ // the two coincide (bar an import alias, where the declaration name is the
3577
+ // one that resolves anyway).
3578
+ factoryName: factoryDecl.getName() ?? calleeText,
3520
3579
  factoryFilePath: factoryDecl.getSourceFile().getFilePath(),
3521
- classArgs
3580
+ classArgs,
3581
+ namedClassArgs
3522
3582
  };
3523
3583
  }
3584
+ function resolveClassReference(node) {
3585
+ const expr = unwrapExpression(node);
3586
+ if (!Node5.isIdentifier(expr)) return void 0;
3587
+ const decl = expr.getDefinitions().map((d) => d.getDeclarationNode()).find((n) => n !== void 0 && Node5.isClassDeclaration(n));
3588
+ if (!decl) return void 0;
3589
+ return {
3590
+ name: decl.getName() ?? expr.getText(),
3591
+ filePath: decl.getSourceFile().getFilePath()
3592
+ };
3593
+ }
3594
+ function resolvePropertyClassReference(prop) {
3595
+ if (Node5.isShorthandPropertyAssignment(prop)) {
3596
+ const ref2 = resolveClassReference(prop.getNameNode());
3597
+ return ref2 ? { key: prop.getName(), ref: ref2 } : void 0;
3598
+ }
3599
+ if (!Node5.isPropertyAssignment(prop)) return void 0;
3600
+ const init = prop.getInitializer();
3601
+ if (!init) return void 0;
3602
+ const ref = resolveClassReference(init);
3603
+ if (!ref) return void 0;
3604
+ const nameNode = prop.getNameNode();
3605
+ const key = Node5.isStringLiteral(nameNode) ? nameNode.getLiteralValue() : prop.getName();
3606
+ return { key, ref };
3607
+ }
3524
3608
  var mixinTypeProject;
3525
3609
  function getMixinTypeProject() {
3526
3610
  mixinTypeProject ??= new Project3({
@@ -3550,7 +3634,7 @@ function resolveInstantiatedReturnType(cls, methodName) {
3550
3634
 
3551
3635
  // src/discovery/filter-for.ts
3552
3636
  function resolveMixinEntityClass(mixin, project) {
3553
- const entityArg = mixin?.classArgs[0];
3637
+ const entityArg = mixin?.namedClassArgs?.entity ?? mixin?.classArgs[0];
3554
3638
  if (!entityArg) return void 0;
3555
3639
  const file = project.getSourceFile(entityArg.filePath) ?? project.addSourceFileAtPathIfExists(entityArg.filePath);
3556
3640
  return file?.getClass(entityArg.name);
@@ -4802,7 +4886,8 @@ function extractFromSourceFile(sourceFile, project) {
4802
4886
  const mixinBinding = inherited ? {
4803
4887
  factoryName: inherited.factoryName,
4804
4888
  factoryFilePath: inherited.factoryFilePath,
4805
- classArgs: inherited.classArgs
4889
+ classArgs: inherited.classArgs,
4890
+ namedClassArgs: inherited.namedClassArgs
4806
4891
  } : void 0;
4807
4892
  const ownMethods = cls.getMethods();
4808
4893
  const ownNames = new Set(ownMethods.map((m) => m.getName()));
@@ -5034,7 +5119,7 @@ async function watch(config, onChange, options = {}) {
5034
5119
  }
5035
5120
 
5036
5121
  // src/index.ts
5037
- var VERSION = "0.17.2";
5122
+ var VERSION = "0.19.0";
5038
5123
 
5039
5124
  // src/cli/codegen.ts
5040
5125
  async function runCodegen(opts = {}) {