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

Feature typescript typegen #55

Merged
merged 9 commits into from
Jul 24, 2019
Merged

Feature typescript typegen #55

merged 9 commits into from
Jul 24, 2019

Conversation

zachasme
Copy link
Contributor

I needed typescript definitions for these bindings and noticed you generate your README off of jsdoc.

I've managed to generate the definitions using tsd-jsdoc requiring minimal change to h3core.js.

This PR adds the required jsdoc changes, but also a script to bundle the types on prepublish.

I'm not sure how you feel about distributing these definitions yourself. If you'd rather not, I'll simply remove that part. But I do hope you'll allow the jsdoc changes. 😃

@nrabinowitz
Copy link
Collaborator

nrabinowitz commented Jul 23, 2019

This is great! Thanks for the contribution - I've been wanting to add typedefs for TS and Flow for a bit, but hadn't considered this approach.

The build is currently failing because the generated README file has changed due to JSDoc changes (specifically the module name h3-js). The easy fix is to run yarn build-docs locally and check in the results. But this is unfortunately a place where the JSDoc and TS requirements seem to diverge - h3-js is the package name, but it can't be the import name, so it's weird to have h3-js.h3ToGeo in the docs. I can't see any way to fix this in tsd-jsdoc itself.

Assuming that we'd like to gen typedefs without changing the docs, I think either the code might need pre-processing, or the README or typedef might need post-processing... I think the easiest might be changing the command for docs generation to pipe through sed:

"build-docs": "jsdoc2md --global-index-format grouped --partial doc-files/scope.hbs --helper ./doc-files/insert-version.js --separators --template doc-files/README.tmpl.md lib/h3core.js | sed 's/h3-js\\./h3./g' > README.md"

Open to better options if you have one - this only fixes worst doc issue, but it's probably sufficient.

@nrabinowitz
Copy link
Collaborator

Sorry, just gave you a merge conflict as well - you'll need to rebase to master.

I'm not sure how you feel about distributing these definitions yourself.

I'm not sure what the standard approach here is, but I'm happy to include the typedefs in the dist if that's appropriate for TS users. Now I'm wondering whether we can do this for Flow too.

@zachasme
Copy link
Contributor Author

Hm, I hadn't considered the generated docs. I can see the H3Index type also adds another layer to the table-of-contents which seems unnecessary.

I like your idea of pre- or post-processing. I'll try finding a solution that doesn't change the docs.

The think the standard approaches for non-typescript projects is either bundling it yourself or leaving it to userland to provide them through the DefinatelyTyped project. The first approach is preferable IMO.

Side note: Have you considered distributing the library simply as 'h3' on npm? Putting 'js' or 'node' in the package name is discouraged.

@nrabinowitz
Copy link
Collaborator

Side note: Have you considered distributing the library simply as 'h3' on npm? Putting 'js' or 'node' in the package name is discouraged.

The name was taken. It's now deprecated, but would probably require some work (and invoke some potential confusion) to try to take it over.

I also think the h3-js case is a little non-standard as well, because it's a binding to a library in another language. All of the official bindings share the same naming structure - h3-py, h3-java, etc - so calling h3-js just h3 might actually be confusing, especially since it would make it harder to talk about the relationship to the core C lib. Plus there's actually a separate Node-only h3-node project (offering true C bindings), so it's useful to differentiate.

@nrabinowitz
Copy link
Collaborator

Hm, I hadn't considered the generated docs. I can see the H3Index type also adds another layer to the table-of-contents which seems unnecessary.

One other idea here is that you could concatenate either source or TS code during the generation process to shim in missing defs.

@coveralls
Copy link

coveralls commented Jul 24, 2019

Pull Request Test Coverage Report for Build 119

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 117: 0.0%
Covered Lines: 351
Relevant Lines: 351

💛 - Coveralls

@zachasme
Copy link
Contributor Author

I was not aware of the separate N-API module. Seems like a natural way to go then 👍

I've rebased and moved the module renaming to a simple pipe through sed as per your suggestion.

I also added a @static tag to the H3Index typedef, which avoids the TOC nesting.

Copy link
Collaborator

@nrabinowitz nrabinowitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks for the contribution!

@@ -103,6 +103,7 @@ const coordinates = h3.h3SetToMultiPolygon(hexagons, true);
* [.getRes0Indexes()](#module_h3.getRes0Indexes) ⇒ <code>Array.&lt;H3Index&gt;</code>
* [.degsToRads(deg)](#module_h3.degsToRads) ⇒ <code>Number</code>
* [.radsToDegs(rad)](#module_h3.radsToDegs) ⇒ <code>Number</code>
* [.H3Index](#module_h3.H3Index) : <code>string</code>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL :). This works for me.

"source": "lib/h3core.js",
"scripts": {
"build-update-h3": "bash scripts/update-h3.sh",
"build-emscripten": "yarn build-update-h3 && yarn docker-emscripten",
"build-docs": "jsdoc2md --global-index-format grouped --partial doc-files/scope.hbs --helper ./doc-files/insert-version.js --separators --template doc-files/README.tmpl.md lib/h3core.js > README.md",
"build-tsd": "jsdoc -t node_modules/tsd-jsdoc/dist -d console lib/h3core.js | sed 's/\"h3\"/\"h3-js\"/g' > dist/types.d.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@nrabinowitz nrabinowitz merged commit 8faca08 into uber:master Jul 24, 2019
@zachasme zachasme deleted the feature-typescript-typegen branch July 25, 2019 08:41
@whaatt whaatt mentioned this pull request Sep 5, 2019
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

Successfully merging this pull request may close these issues.

None yet

3 participants