Ts-morph: addConstructor throw error when class have an JSDOC at the end

Created on 14 Jan 2020  路  6Comments  路  Source: dsherret/ts-morph

Describe the bug

Version: latest

To Reproduce

const { Project, StructureKind } = require("ts-morph");

const project = new Project({});

const classFile = project.createSourceFile("src/MyClass.ts", `
class Foo {
/**
 * 
 */
}
`);
const classNode = classFile.getClasses()[0];
classNode.addConstructor({
  statements: "console.log('i am constructor');",
});

image

Expected behavior


no error

bug

All 6 comments

Interesting. So what's happening here is that the code changes to something like this:

class Foo {
  /**
   * 
   */
  constructor() {
  }
}

...which means that the constructor takes ownership of the jsdoc. So previously the class had 1 jsdoc child and now it also has 1 child (the constructor).

Thanks for reporting and providing an easy reproduction! I'll add some special case handling for this scenario.

For now, a workaround should be to just insert before the jsdoc using classNode.insertConstructor.

I've gotta hijack this to tell you that I love how you explain the bug when you understand it, @dsherret! Keep up the great work maintaining the lib :)

Thanks @lazarljubenovic! I have been taking a bit of a break from working on ts-morph, but I will get back to it very soon. I'll probably do a quick release to fix this bug over the weekend though.

@dsherret Thanks for your work 鉂わ笍. I want to know when there will be a new version released, since i noticed that you fixed this bug in master, but the default branch is latest.

@paranoidjk sorry, I was waiting for the TS 3.8 release. I'll do a release this weekend 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gregjacobs picture gregjacobs  路  4Comments

HamedFathi picture HamedFathi  路  3Comments

dsherret picture dsherret  路  3Comments

GrandSchtroumpf picture GrandSchtroumpf  路  3Comments

mi5ha picture mi5ha  路  3Comments