jsdoc-tsd icon indicating copy to clipboard operation
jsdoc-tsd copied to clipboard

default exports not working

Open samparsky opened this issue 6 years ago • 3 comments

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

samparsky avatar Aug 30 '19 06:08 samparsky

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?

wehrstedt avatar Aug 30 '19 07:08 wehrstedt

I could work on it if you can explain further

samparsky avatar Aug 30 '19 07:08 samparsky

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.

wehrstedt avatar Aug 30 '19 11:08 wehrstedt