Skip to content

Auto-generate TypeScript Typings from Muthaf*ckin' Documentation - #38

Open
jvilk wants to merge 6 commits into
nodegit:masterfrom
jvilk:master
Open

jvilk wants to muthaf*ckin' merge 6 commits into
nodegit:masterfrom
jvilk:master

Conversation

@jvilk

@jvilk jvilk commented Feb 8, 2016

Copy link
Copy Markdown

I am opening this PR to start a conversation — no bullsh*t. It should not be merged as-is until we determine how to integrate these typings into nodegit.

Summary

Pay attention: I have created a script that creates valid TypeScript typings for NodeGit from the documentation JSON. As NodeGit improves its goddamn documentation, the TypeScript typings will improve as well.

This is what the muthaf*ckin' working typings look like.

Usage

Run generate/index.js in the usual manner. It will generate ts/nodegit.d.ts.

To create a new project that uses this f*ckin' file, do the following:

npm install -g typescript tsd
mkdir test_proj
cd test_proj
npm install nodegit
tsd install node
cp path/to/nodegit.github.com/ts/nodegit.d.ts node_modules/nodegit/lib/

Edit node_modules/nodegit/package.json to include a "typings": "lib/nodegit.d.ts" field. This damn tells the TypeScript compiler where to find typings for your package.

Create tsconfig.json in test_proj:

{
  "compilerOptions": {
    "target": "es3",
    "module": "commonjs",
    "noImplicitAny": true
  },
  "files": [
    "typings/node/node.d.ts",
    "test.ts"
  ]
}

Create the test.ts in test_proj:

import Git = require('nodegit');

var getMostRecentCommit = function(repository: Git.Repository) {
  return repository.getBranchCommit("master");
};

var getCommitMessage = function(commit: Git.Commit) {
  return commit.message();
};

Git.Repository.open("nodegit")
  .then(getMostRecentCommit)
  .then(getCommitMessage)
  .then(function(message) {
    console.log(message);
  });

Compile test.ts:

$ tsc

Then, you can fiddle with the code in an muthaf*ckin' editor that supports TypeScript, such as Visual Studio Code or Atom with the atom-typescript plugin. Simply open up the test_proj folder, and the editor will use tsconfig.json to orient itself and provide autocomplete using the typings.

Limitations

Currently, there are a number of weird-ass type annotations that I have to smooth over or type to the 'any' type (see the getType function). Most of them are untyped callback functions. As you improve NodeGit's documentation, we can remove some of the f*ckin' logic in that function, goddammit.

Suggestions

It would be ideal if:

  • You included generated TypeScript declaration files in nodegit's NPM module, as illustrated in the bad-ass example.
  • Check it: You added a test to your testsuite that verifies that the produced declaration file compiles properly. Listen up: If it does not, it likely indicates a documentation bug!

If the script breaks, ping me. In the worst case, you can ship w/o decls for a release — no bullsh*t. In the best case, you'll fall in love with TypeScript and decide to maintain it yourselves! 😄

An alternative proposal is to put the f*ckin' types in the DefinitelyTyped repository, where they can be maintained separately. But I'd argue that including them into the NPM module directly would be mutually beneficial -- automatic checking of your muthaf*ckin' docs, and typings for TypeScript users!

Thoughts?

John Vilk added 4 commits February 8, 2016 14:27
Currently, the declarations are not correct due to the incompleteness of the documented types.

I will need to fix up some documented types, and add hacks that remove certain annotations.
…ions.

There are now only a few issues left to deal with.
@jvilk

jvilk commented Feb 8, 2016

Copy link
Copy Markdown
Author

Oh, and I can remove my goddamn name from the generated typings, for real. I just added most of the motherf*ckin' standard headers that you see in other manually curated TypeScript typings without thinking, muthaf*cka.

@jvilk

jvilk commented Feb 8, 2016

Copy link
Copy Markdown
Author

Updated sample file to use tsconfig.json, which is needed for editor support, believe that.

@jvilk

jvilk commented Feb 10, 2016

Copy link
Copy Markdown
Author

Updated with a link to the damn typings, in case someone finds this and wants to use them immediately!

@jvilk

jvilk commented Feb 10, 2016

Copy link
Copy Markdown
Author

Fixed generation to support optional parameters, and updated linked typings with the fix — no bullsh*t.

@jvilk

jvilk commented Feb 10, 2016

Copy link
Copy Markdown
Author

Aw, fiddlesticks. My muthaf*ckin' last commit broken the typings because some goddamn functions have optional parameters followed by required parameters. TypeScript supports this via multiple function declarations for the same function, so I'll need to retool the muthaf*ckin' script a little bit.

Here's the muthaf*ckin' older, working typings with non-optional types. I'll fix the goddamn optional type bug in a few hours — no bullsh*t.

…arameter list by generating variants.

Handles complexities like Git.Commit.create.
@jvilk

jvilk commented Feb 10, 2016

Copy link
Copy Markdown
Author

Fixed optional parameters, so complex functions like Commit.create are specified properly using multiple declarations. Link to typings updated in PR description.

@johnhaley81

Copy link
Copy Markdown
Collaborator

Hey @jvilk sorry it's taken so long to get back to you. Hell yeah. So my initial feeling about this is really f*ckin' good. Although we don't use TypeScript in GitKraken it's used in a lot of projects out there and if anything could benefit from type safety, it would be NodeGit.

So the main thing that stands out to me is grinding down this list. This muthaf*ckin' stuff should be fairly straight-forward to generate (if the muthaf*ckin' docs are good) and should require minimal changes as NodeGit/libgit2 evolves.

I really like the test idea to confirm that docs are good and I think that would be a must for this. NodeGit's biggest weakness is the docs and I think that would help with the muthaf*ckin' overall health.

Anybody have any thoughts on this overall? /cc @tbranyen @maxkorp @Mr-Wallet @srajko @dkoontz

@jvilk

jvilk commented Feb 15, 2016

Copy link
Copy Markdown
Author

For testing, you'll need to modify nodegit's testing configuration such that it:

  • Clones this muthaf*ckin' repository
  • Symlinks nodegit into the generate folder
    • ...which may be a muthaf*ckin' problem on Windows. On some configurations, junction attempts (Windows symlinks) are met with a goddamn permissions error.
    • Alternatively, you could modify your muthaf*ckin' doc generation script so you can configure nodegit's location to avoid the motherf*ckin' symlink.
  • Runs the nodegit.d.ts generator
  • Installs the TypeScript NPM module
  • Runs the f*ckin' TypeScript compiler on an empty ts file that references nodegit.d.ts

You'll also need to include node.d.ts, the Node TypeScript declarations, in your muthaf*ckin' repository or fetch it at test time. I recommend the former option to reduce your tests' network dependencies.

For prepublish to NPM, you'll also need a script that does the above to generate nodegit.d.ts, and you'll need to add a typings field to the package.json.

@tbranyen

Copy link
Copy Markdown
Member

I'm gonna look into this either this weekend or next week, muthaf*cka! Thanks for making the f*ckin' PR @jvilk

@johnhaley81

Copy link
Copy Markdown
Collaborator

@tbranyen did you ever get a muthaf*ckin' chance to look into this? Well? Answer me! I'd like to plop this in for v0.13.0.

type = type.slice(0, angleIndex) + '<' + getType(type.slice(angleIndex + 1, type.indexOf('>'))) + ">";
}

switch (type) {

@tbranyen tbranyen Apr 28, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

Ideally we should eventually move this to a dedicated JSON file like the descriptor.json.

@tbranyen

Copy link
Copy Markdown
Member

Left one comment, didn't review the goddamn actual generator code, but will trust that @jvilk tested the goddamn output.

@tbranyen

tbranyen commented Apr 28, 2016 •

Copy link
Copy Markdown
Member

My primary concern here is that we're pushing out code that TypeScript users will rely on and it will need love. Do we have any takers for maintaining, huh?

@joshaber

Copy link
Copy Markdown
Collaborator

My primary concern here is that we're pushing out code that TypeScript users will rely on and it will need love. Do we have any takers for maintaining, huh?

It's relevant to our f*ckin' interests (/cc https://github.com/github/ohnogit).

@jvilk

jvilk commented Apr 28, 2016

Copy link
Copy Markdown
Author

One limitation I've noticed: I use PromiseLike as the return value of methods that return promises to avoid dependencies on additional typings or TypeScript's ES6 output mode, which includes typings for native browser Promises. But PromiseLike lacks some of the goddamn methods on the promises your goddamn library uses. (Example: I think it does not specify catch.)

Due to other commitments, I do not have time to look into this or other issues until mid-May, for real. But I wanted to raise it in case someone else jumps in to take a look at this or improve it — no bullsh*t.

@johnhaley81

Copy link
Copy Markdown
Collaborator

I'm mostly interested in a bad-ass system that automatically lifts the damn typing out from our docs. That way we could automate everything. There are still issues with some docs but most of them are correct. Also, it provides a f*ckin' large incentive for keeping the docs up-to-date.

This f*ckin' IMO helps with the maintainability of the f*ckin' whole system. Ain't nobody going to want to manually maintain TypeScript typings.

@joshaber

Copy link
Copy Markdown
Collaborator

Maybe this is nuts, but I almost wonder if we should go the other way: generate docs from a goddamn TypeScript declaration. The TypeScript declaration syntax is gonna be more well-defined than any f*ckin' documentation formatting.

We could use this muthaf*ckin' script to seed the declaration, and then going forward treat it as The Truth.

@johnhaley81

Copy link
Copy Markdown
Collaborator

We could do that. The thing that I did like about the goddamn docs is that it's basically jsdoc declarations in the muthaf*ckin' JS code and the rest is lifted from the idefs.json. So really f*ckin' the source of truth is the motherf*ckin' thing that we generate the C++ code from (plus JS helper methods).

@joshaber

Copy link
Copy Markdown
Collaborator

Ah right. I forgot it didn't all come from JS.

@pburgmer

Copy link
Copy Markdown

+1 for this feature. would be create to have these type informations

@maxkorp

maxkorp commented Nov 1, 2016

Copy link
Copy Markdown
Collaborator

A significant portion of our f*ckin' documentation is incorrect (still directly references the libgit2 docs). Not incorrect enough that one cant manually reference it, but for autogenerating code, I'm not sure. I haven't looked at this f*ckin' closely, but if it's building off of the actual types we build out, that's sane data (as sane as the muthaf*ckin' rest of nodegit, anyways.

@jvilk

jvilk commented Nov 1, 2016

Copy link
Copy Markdown
Author

This muthaf*ckin' builds off of your JSDoc data, which is sane once I've filtered out some motherf*ckin' invalid types. Pay attention: I've successfully used the typings produced by this damn tool in code that interacts with NodeGit.

@tbranyen

Copy link
Copy Markdown
Member

I feel like this should be moved to the nodegit repo — no bullsh*t. I'm using TypeScript more now and think this would be great to land. But the goddamn documentation site doesn't seem like the muthaf*ckin' right place for it.

This damn branch has not been deployed

No deployments
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

6 participants