-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Suggestion: Generate doc comments in emitted JS #10
Comments
Most importantly, for me: Propagate type information into existing JSDoc comments. This would allow me to use existing JSDoc documentation generators to produce nice looking HTML documentation for my TypeScript libraries without respecifying type signatures in JSDoc comments. (Of course, constructing a mapping between TypeScript types and JSDoc types might end up being nontrivial, especially where interface types are concerned.) |
Making sure I understand you correctly - you'd like to see the type information from TypeScript persisted into JSDoc comments in the generated .js files from your TypeScript, right? Can you give an example of a documentation generator you currently use? |
Correct.
I am not using a documentation generator at the moment (because it would be infeasible/frustrating to maintain type information in two locations as the code evolves), but I would like to use something like JSDoc: http://usejsdoc.org/ |
I've created a documentation generator that can directly parse |
Your tool looks really nice! I'll have to see how it holds up to my code. I figure it would be more elegant to avoid needing to maintain a separate project if we could find a decent mapping from TypeScript types and JSDoc annotations, but maybe that is a rathole in-and-of-itself. |
We have a similar tool, but with a bit broader scope. It uses a template engine to generate output, which makes it perfectly usable for generating docs. Granted, there is more work for you (have to write the templates), but then you can make the output nicely customized. Check it out: http://github.com/erecruit/TsT |
As the issues on Codeplex are now closed, I'd like to link to the JSDoc request there: http://typescript.codeplex.com/workitem/1596 |
+1 to herrernst |
+1 |
Yup, annotations for the google closure compiler are a must for me. Having to do it manually really limits the advantages of typing. If you get your manual declarations incorrect, it can lead to incorrectly optimized results. As far as the "needs proposal" tag goes.. do this please :) -- https://developers.google.com/closure/compiler/docs/js-for-compiler |
In addition to the JSDoc comments, it may be worth looking into also supporting the exports and externs for the Google Closure Compiler EDIT: Emitting export and externs for Google Closure is mentioned in #8 |
+1 for closure-style type comment annoataions |
I believe this feature would weigh in huge, as far as convincing established JS projects to "upgrade" their codebases to Typescript, since, effectively, this would eliminate the need to hand-write JSDoc annotations, while resulting in JS code (for plain JS consumers) of the same quality - or higher, for that matter, since Typescript annotations as well as code all gets checked at compile-time. In other words, this would save time and increase quality. As things stand, without this feature, some JS project maintainers may well hold back, because they don't want to lose their establish plain JS consumer base, a lot of whom depend on annotations for IDE support. (of course, IMO they should all "upgrade" to Typescript, but I'm obviously opinionated ;-) |
+1 for emitting jsdoc tags with type info as much as possible. |
I'd like to take a jab at this. Can somebody from the TS project weigh in on the expected amount of work this would require? From an outsider perspective this looks suspiciously easy. Do you foresee any major roadblocks? Or do you expect this to be as easy as it looks (slightly amend the last "code generating" compiler phase)? |
Hey @hraban, this should not be too much work. You'd basically just need to:
|
There's also the difference with nullability. For the full closure compiler On Friday, April 17, 2015, CyrusNajmabadi notifications@github.com wrote:
Tobiasz Cudnik |
@TobiaszCudnik I would consider this a different feature. I would also argue this has to be only supported for explicit types only not infered ones. |
Note IntelliJ's generation of JS Doc comments includes information about inferred types (in particular, inferred function return types). |
Doing only declared types allows to implement this as a syntactic transformation, which is a. simple, and b. works for single file emit. It also allows users to manage what there public API (assuming that this is what it is used for). |
Hey guys! I'm sorry but I am absolutely swamped at the moment. It's in the back of my mind, but on hold for a while. Definitely interested to see whatever happens once this gets picked up, though! |
This is very outdated, but probably could be helpful: https://github.com/evanw/typescript-closure-compiler |
Hello everyone, I've been looking around the relevant code recently and going through the comments here. I agree with the "we should extend existing JSDoc comments" point, eventually. It makes the most sense from an integration-with-current-tools perspective, and it would be a nice selling point, as the resulting JS would then be JSDoc consumable with automatic type annotations from TS. This leads to situations like: /**
* Determine the logarithm of a number to a specific base
*
* @param b The base of the logarithm
* @param {weirdType} g The number to determine the logarithm of
* @return The logarithm of g in base b
*/
function logB(b: number, g: number): number {
return Math.log(g) / Math.log(b);
} Where, ideally, you would want this as the output: /**
* Determine the logarithm of a number to a specific base
*
* @param {number} b The base of the logarithm
* @param {weirdType} g The number to determine the logarithm of
* @return {number} The logarithm of g in base b
*/
function logB(b, g) {
return Math.log(g) / Math.log(b);
} In that case, we'd need not only a proper JSDoc parser, but also a JSDoc serializer. Right now, JSDoc is not even parsed, at all, in .ts files, only in .js. We can enable it for .ts files, but that still leaves the problem of "changing" the comment before output; the comments are just copied verbatim from the source file, not rebuilt from a "jsdoc AST" as it were (even though they are properly parsed that way). So this would seem non-trivial. What are your thoughts on this, so far? There are a few ways around this, just to "get started", some quick and dirty solutions to get more than what we have right now, with less effort than the above. One option is to completely ignore nodes with existing JSDoc comments, hidden behind an appropriate flag, e.g. --generate-missing-JSDoc. It's not pretty (disincentivizes creating JSDoc by users), but it's a start. Ideally this will get us closer to a full solution. What do you think? Did I misunderstand something from the code, perhaps? I'm curious to hear what people who are more familiar with the code structure have to say about it. Regards Hraban |
@hraban you are right, we will need to 1. parse jsdoc in .ts files, 2. Emit the jsdoc correctly from the AST and 3. Augment the jsdoc with type information if the jsdoc already exist. 1, is fairly trivial and so is 2. 3 needs some synthetic nodes to be created, but should not be thst complicated either (I belive it should be done as a syntactic transformation and only for explicit type annotations and not infered types). If this is something you would like to contribute, I would be happy to help out as much as possible. |
@DanielRosenwasser Ouh, if that's the case then it's good to know now :) I sent Arthur an e-mail to ask him about it.
Thanks for offering help, I'm exploring what needs to be done for this and I might take you up on that offer. :) |
@aozgaa is working on jsdoc tooling in the language service. so this is up for grabs.
I do not know, what other tags we need? do you have a list in mind? i think we should be adding them as we go, or just add a catch all generic tag that would be available in the tree but ignored by tooling cause they do not understand them. @CyrusNajmabadi would have better ideas here.
|
Related: #7393 is the smaller feature request, specifically to expose the compiler's JSDoc parser API for tools like documentation extractors. Tools like TypeDoc currently must have their own separate parser for JSDoc on top of the TS API: |
+1 |
As TypeDoc has a lot of errors and cannot compile fully correct project, generated jsdoc annotated js file Only public TS API is used, any compilable project is supported. Currently, tested only for node modules. And function (callback) types are not supported (planned). |
@RyanCavanaugh - Can I ask what is the current state of this? Preserving type information through to jsdoc-consuming documentation generators is a sticking point for my team's adoption of Typescript in a environment that requires supporting non-typescript project groups. |
I am trying to solve this with custom transformers, as it looks like that the feature exists exactly for solving this kind of problem. However, I cannot find how to mutate Is it possible to mutate |
I would really love to see JSDoc comments in the emitted I believe solving this issue would really further the TypeScript team's story about being able to adopt the language incrementally and its compatibility with the existing JavaScript ecosystem (as opposed to forking the community and building separate tools). |
I would be using more TypeScript, but I need the full functionality of JSDoc. I am actually in the process of converting one of my experimental projects from TypeScript to ES6 simply because I need JSDoc documentation more than I need the features of TypeScript. |
I would like to add that the Google Closure Compiler might be able to leverage such JSDoc tags to produce a smaller file size. Since I know the topic of minification has been discussed before with respect to the superior type information available to TypeScript, this would seem like a two birds with one stone opportunity. |
Is there any progress on this? I would be very interested on add/augment JsDoc comments on Javascript files with types inferred by Typescript :-) |
+1 |
Agree that this should be done by typescript compiler as part of the compilation process, there's no sense in trying to implement this using some external tool or additional compilation step. Any improvements in this area are valid, even if not all TS types/constructs have a JSDoc equivalent, it's fine adding just the simple ones that do at first. The emitted JSDoc can slowly improve over time without worrying about backwards compatibility, since it's just a comment in a generated file. I was unsure if I should open a new issue but ... I'd like to propose a few things: I propose an option like We should make this (at first) specifically ONLY about emitting TS types -> JSDoc Only later we could think about optimizations, like:
💻 Use Cases
📃 Motivating Examplelet ctx: vscode.ExtensionContext;
export async function activate(context: vscode.ExtensionContext) {
} Could simply become something like: /**@type{vscode.ExtensionContext}*/ let ctx;
async function activate(/**@type{vscode.ExtensionContext}*/ context) {
}
exports.activate = activate; Then ship just the JS. No source maps, no .d.ts, etc. 🔍 Search Termsemit jsdoc type generated |
How is the parity of JSDoc with TS these days, feature wise? How accurate could the emitted types be? |
I would like to have JSDoc comments in generated js. |
I think inferred types would make sense, but agree can be seen somewhat superfluous, so I would left it as an option. |
Still an issue? |
How can we help to move this forward? |
Support some level of automatically generating doc comments for non-TypeScript JavaScript consumers.
Need more details on what exactly people would like to see generated.
The text was updated successfully, but these errors were encountered: