-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Parse Google Closure type annotations correctly #152
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
Comments
I'm hoping to tackle this sometime in the very near future. Developers at my company are extremely keen to start using Closure Compiler (CC) types, but we can't make the switch until JSDoc is able to parse them. My initial idea is to do something along these lines, which would also take care of #118:
That idea raises the following issues, though (and probably many others):
@micmath, any thoughts? |
I'm not going to suggest anyone should kill themselves to make JSDoc type declarations 100% compatible with Closure Compiler's type declarations, because it's just not that important to me personally. But I'm more than happy if someone wants to contribute that work, and I think it would be a great benefit to those that want to use both. I definitely see the problem with the "|" confusion. For what it's worth, Google have published some docs on their type parsing algorithm that might be useful. As to the question of converting CC types to JSDoc documentation, I'd say that it isn't necessary. The philosophy of JSDoc has always been to encourage code authors to write meaningful documentation, including textual descriptions and examples, neither of which are possible in a type declaration. In essence a CC-style type declaration competes as a poorer alternative to a JSDoc comment, when it comes to documentation. Consider: /**
@function process
@param {{bar: string, baz: number}} foo You cannot document foo much can you?
*/ Versus: /**
A valid argument to the process function.
@typedef processArg
@type object
@property {string} bar The general description of the bar.
@property {number} baz The age of the baz.
*/
/** @param {processArg} foo */
function process(foo) {
} As promoters of documentation I'd say that the second example is better, because it allows and encourages more complete and more useful communication about how to use the API. Having said that, I would reiterate that, in my opinion, having JSDoc properly parse those CC type definitions would be very beneficial, I just don't think it makes sense to try to generate too much documentation from it, even if you could. |
Thanks for the insight and the link, @micmath. I think the solution is twofold:
Here's a possibly half-baked idea that involves a new /**
A valid argument to the process function.
@typedef processArg
@type object
@property {string} bar The general description of the bar.
@property {number} baz The age of the baz.
*/
/** @param {{bar: string, baz: number}} foo {@typeref processArg} */
function process(foo) {
} Ugly as hell, but it would allow me to do my job as a tech writer, and the developers could still use CC types and enable CC's type-related optimizations. |
The |
Interesting. I'll look into that. But it looks like CC doesn't support the |
I've made some progress on this, but I still have quite a ways to go. @kpozin, is this issue causing major problems for you? If so, I can land a temporary fix for the |
@hegemonic, this isn't a huge issue -- I can just join the type names array into a single string for now. I think a complete fix would be much more valuable. Thanks for working on this! |
For anyone who's curious, I've made some progress on this:
Next up:
I'll probably file a separate issue to implement the |
I decided to land the refactoring before it got stale. The grammar is almost there, but I've been focusing on other stuff lately; will pick this up again at some point soonish. |
We also need this so we can correctly link to other types in the output. See #229. |
introduces a real parser for Closure Compiler types, and uses the parser to interpret type expressions in JSDoc tags. TODO: - provide a way to override the type expression - update templateHelper to generate the correct links in type applications future enhancement (to be filed as a new issue): create pseudo-tags for members that are described in the type expression (e.g., if the type expression for the parameter `foo` is `{bar: string}`, add a tag for `foo.bar` with no description)
This feature is now implemented on the master branch. It will be included in JSDoc 3.2. All Closure Compiler type expressions should now be parsed and linked correctly. In particular, you can now mix type unions and type applications (for example, Also, JSDoc now throws an error and quits if a type expression cannot be parsed. You can turn this error into a warning by enabling the If a type expression identifies function parameters or object properties (for example, If you're using Closure Compiler, though, there's a reasonable way to document these types while still using type expressions that Closure Compiler understands. Simply include an inline For example, JSDoc will look at the following code and pretend that the type expression for the
If you're not using Closure Compiler to validate types, you can (and probably should) document this the normal JSDoc way:
|
The grammar for Google Closure type annotations (page 8 of this doc) is a little bit too complex to be parsed with regular expressions (as in
jsdoc/tag/type:parseTypes
).A real parser should be written for these annotations in JavaScript, or the Java version can be extracted from the Closure Compiler (source, JavaDoc).
Probably the biggest problem with the current implementation is that it very incorrectly splits union types. For example,
Array.<(Object|number)>
is split intoArray.<(Object
andnumber)>
. In the absence of a complete parser, it's probably safer to keep this as a single type rather than to try to split it at the pipe character.The text was updated successfully, but these errors were encountered: