Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inferExtension doesn't work with typescript def #3

Closed
kierans opened this issue Feb 11, 2021 · 7 comments
Closed

inferExtension doesn't work with typescript def #3

kierans opened this issue Feb 11, 2021 · 7 comments

Comments

@kierans
Copy link

kierans commented Feb 11, 2021

I'm wanting to use this plugin with my project where the source is in Javascript, but we have types defined in Typescript that's imported from a company module (we also have local TS defs to the project). Because the types are in TS declaration files, they have a .d.ts extension.

If I try to type my JS var and then generate the documentation, it fails.

For example

// mycode.js

// Assume MyType is in 'MyType.d.ts'
/** @type {import('../../types/MyType').SomeInterface} */
const obj = {};
internal/validators.js:120
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at validateString (internal/validators.js:120:11)
    at Object.join (path.js:1039:7)
    at inferExtension (/Project/node_modules/jsdoc-tsimport-plugin/index.js:188:15)

Looking at the source I think it's because path.extname is not stripping off the .d which means that MyType !== MyType.d so find returns undefined which causes join to blow up.

> path.extname("Foo.ts")
'.ts'
> path.extname("Foo.d.ts")
'.ts'

A possible solution would be to substring from the first . found eg: filename.substring(0, filename.indexOf(".")) This would see MyType.d.ts and MyType.ts become MyType

@nbilyk
Copy link
Contributor

nbilyk commented Feb 11, 2021

I hadn't considered/tried the TS definition case. I suspect JSDoc won't be able to understand TS files, you'll likely need TSDoc for that. However, there does seem to be a bug you're describing where this plugin shouldn't fail if the jsdoc points to a .d.ts file. I'll check it out and possibly make a separate ticket.

PR requests are also welcome if you come up with a simple solution.

@kierans
Copy link
Author

kierans commented Feb 11, 2021

Thanks @nbilyk.

I'm using a plugin called better-docs to incorporate the TS defs into JSDoc. This is a much better solution where I can use TS defs to describe my types than have to create JSDoc comments

@kierans
Copy link
Author

kierans commented Feb 11, 2021

PR requests are also welcome if you come up with a simple solution.

I did propose a solution in the ticket, however I don't know if that's sufficient.

@nbilyk
Copy link
Contributor

nbilyk commented Feb 11, 2021

It might be, I'm looking into it. The question is how does TypeScript expect to resolve the file. For example if foo.bar.js should be referenced in the import as "foo.bar" that would break, so I'm doing a few tests first.

@nbilyk
Copy link
Contributor

nbilyk commented Feb 11, 2021

I've identified 3 cases to handle -

  • if the JS file has a dot in the name such as foo.bar.js
  • If the TS file is named foo.d.ts and referenced as import('foo')
  • If the TS file is named foo.d.ts and referenced as import('foo.d')

@nbilyk nbilyk closed this as completed in e5fcd89 Feb 11, 2021
@nbilyk
Copy link
Contributor

nbilyk commented Feb 11, 2021

Give 1.0.4 a try

Note that the fix doesn't interop between jsdoc and tsdoc, the fix just prevents a crash when the referenced file can't be resolved.
infer extension still won't infer d.ts, but that's ok because jsdoc won't be able to read that file anyway :)

@kierans
Copy link
Author

kierans commented Feb 11, 2021

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants