-
Notifications
You must be signed in to change notification settings - Fork 1
jsdoc supporting typescript import style #1
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
Conversation
`catharsis.parse` does not accept the typescript import expression `@param p { import("./a").Pet }` - it raises `Invalid type expression` . This patch does a simple regexp to remove this pattern before the string is parsed. So in this example, `@param p { import("./a").Pet }` in jsdoc parse time is interpreted like it was `@param p { Pet }`
* Supporting typescript import style `catharsis.parse` does not accept the typescript import expression `@param p { import("./a").Pet }` - it raises `Invalid type expression` . This patch does a simple regexp to remove this pattern before the string is parsed. So in this example, `@param p { import("./a").Pet }` in jsdoc parse time is interpreted like it was `@param p { Pet }` * Adding README.md
Publish as More info at https://github.com/ricardohbin/jsdoc readme |
@ricardohbin may you allow the repo people to create issues? This logic doesn't work with indexed types. You can try by yourself:
/**
* @typedef {import("aws-lambda").APIGatewayProxyResult["headers"]} Headers
*/
|
return tagInfo; | ||
} | ||
|
||
// if expression has typescript import types. Ex: @typedef { import("./").Foo } | ||
if (typeExpression.match(/import\(.*\)\./)) { | ||
typeExpression = typeExpression.replace(/import\(.*\)\./, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be resolving the type not just replacing the string.
For example this doesn't work even though the index file is exporting a default.
/* @typedef { import('./index') } */
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've managed to bodge the problem by adding a regex check
// if expression has typescript inport types which exported by default. Ex @typedef {import("./foo")}
if (typeExpression.match(/import\(.*\)/)) {
regex = /import\(['"]?(?:.+)\/([^/\.]+)?(?:\..+)?['"]\)/.exec(typeExpression)
typeExpression = regex[1]
}
That regex will capture filename of the import. This is very hacky and wont fix the problem if the filename is not the same as the export.
Should I do a pull request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would really need to parse the file for it to be usable as the default export may not be named exactly the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@debugmaster sorry for my delay, I enabled issues only now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider improving how typeExpression
is patched as is done here: https://github.com/polyforest/jsdoc-tsimport-plugin/blob/main/index.js#L134
I'm really looking forward to this patch. Any movement? |
You can use the patched version, I already published to npm |
@ricardohbin if I run
I still get
// config.js
/**
* @typedef { import("@organization/package/path") } SSRConfig
* / I'm not sure what I could be doing wrong. |
I don't know if it has something to do but you're not closing the comment block correctly in your code snippet. Should be /**
*
*/ Without a space at the end between |
jsdoc uses catharsis to parse the expressions and
catharsis.parse
does not accept the typescript import expression@param p { import("./a").Pet }
- it raisesInvalid type expression
(because it doesn't accept parentheses) , so this patch does a simple regexp to remove this pattern before the string is parsed.In this example,
@param p { import("./a").Pet }
in jsdoc parse time is interpreted like it was@param p { Pet }
Works great if you want are using Typescript to type check your jsdoc - (https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) - compatible with all jsdoc templates
To use it asap, just add this fork in your
package.json
->npm install git+ssh://[email protected]:ricardohbin/jsdoc.git#support-ts-jsdocs