otris / jsdoc-tsd

JSDoc template for generating TypeScript definition files based on JSDoc comments.
MIT License
29 stars 7 forks source link

default exports not working #73

Open samparsky opened 5 years ago

samparsky commented 5 years ago

For example, the below code does not generate the correct typing definition

/**
 * @module test1
 */

class One {}
export default One
exports.tag = "one"

It generates the below type description which does not include the default export

declare module 'test1' {
    class One { };
    export var tag: string;
}

It's missing the default export

wehrstedt commented 5 years ago

In resolveNamespaceMember is some logic which handles the export-flag. My first assumption is that this could be moved to the handleFlags function. Otherwise we have to add this logic to resolveModuleMember, too.

Would you like to contribute to this?

samparsky commented 5 years ago

I could work on it if you can explain further

wehrstedt commented 5 years ago

I think the easiest way is to checkout the project. run yarn install and add a test like this

it.only("should export 'default' classes, async () => {
     const data = await parseData(`<your jsdoc / javascript goes in here`);
     const parser = new JSDocTsdParser();

     // Step in here to check why the flag is not set
     parser.parse(data);

     // Get the transformed class
     const result = parser.resolveMembership();
     result.should.include.keys("One");
     const classDeclaration: dom.ClassDeclaration= result.get("One") as dom.ClassDeclaration;

      // Ensure that the flag is set correctly
      expect(classDeclaration.flags).to.equal(dom.DeclarationFlags.ExportDefault);

      // You don't have to do this in the test, but you can get the result dts with
      const dts = parser.generateTypeDefinition();
      console.log(dts);
});

You can debug the test with vs code config Mocha Current File. Take a look at the handleFlags-function which is called here. As you can see here there is no handling for "default export", I assume that is the responsible point.