-
Notifications
You must be signed in to change notification settings - Fork 21
Auto-generate TypeScript Typings from Documentation #38
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
Open
jvilk
wants to merge
6
commits into
nodegit:master
Choose a base branch
from
jvilk:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3826813
Adding code to generate TypeScript declarations.
c3daad4
[TS decls] Include [EXPERIMENTAL] in descriptions where appropriate.
acb7b6b
[TS Decls] Fixing enums, preventing bad things from entering declarat…
d0bc904
Typings now compile in TypeScript!
f687656
Properly mark optional parameters to functions.
5d0e1c2
[TS Decl] Correctly handle optional parameters in the middle of the p…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Adding code to generate TypeScript declarations.
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.
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /** | ||
| * Consumes the API in JSON format, and produces a TypeScript declaration file. | ||
| * @author John Vilk <jvilk@cs.umass.edu> | ||
| */ | ||
| var fs = require('fs-extra'); | ||
| var Path = require('path'); | ||
|
|
||
| var writeTsDecls = function(apiData, path) { | ||
| var path = path || ''; | ||
| path = ("/" + path + "/").replace(/\/+/g, '/'); | ||
|
|
||
| /** | ||
| * Given a type from the API docs, produce a TypeScript type. | ||
| */ | ||
| function getType(type) { | ||
| switch (type) { | ||
| // Primitives | ||
| case 'String': | ||
| return 'string'; | ||
| case 'Number': | ||
| return 'number'; | ||
| // Avoiding type collusions | ||
| case 'Object': | ||
| return 'GitObject'; | ||
| case 'Blob': | ||
| return 'GitBlob'; | ||
| // NodeJS types | ||
| case 'EventEmitter': | ||
| return 'NodeJS.EventEmitter'; | ||
| default: | ||
| var dotIndex = type.indexOf('.'); | ||
| if (dotIndex !== -1) { | ||
| // Remove '.' from types (e.g. Reference.Type => ReferenceType) as | ||
| // we make them part of the outer scope. | ||
| // Also, convert the owner of the type properly (e.g. Object.TYPE => GitObjectTYPE). | ||
| return getType(type.slice(0, dotIndex)) + type.slice(dotIndex + 1); | ||
| } else { | ||
| return type; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a block of text into a block of JSDoc. Removes empty lines. | ||
| */ | ||
| function textToJSDoc(text) { | ||
| var lines = text.split('\n'); | ||
| // Strip empty lines, and begin non-empty lines with " * " | ||
| lines = lines.filter(function(line) { | ||
| return line.trim() !== ""; | ||
| }).map(function(line) { | ||
| return " * " + line; | ||
| }); | ||
|
|
||
| return "/**\n" + lines.join("\n") + "\n */"; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a function in JSON format into a TypeScript function | ||
| * declaration. | ||
| */ | ||
| function getFunctionDeclaration(name, fcn, isStatic) { | ||
| var jsDoc = ""; | ||
| if (fcn.description !== "") { | ||
| jsDoc += fcn.description + "\n"; | ||
| } | ||
| var fcnSig = isStatic ? 'public static' : 'public'; | ||
| fcnSig += " " + name + "(" + | ||
| fcn.params.map(function(param) { | ||
| jsDoc += "\n@param " + param.name + " "; | ||
| // Apparently param.description can be null, so check that it's not before looking at the contents! | ||
| if (param.description && param.description.trim() !== "") { | ||
| // Indent secondary lines of the description. | ||
| jsDoc += param.description.replace(/\n/g, '\n ') + "\n"; | ||
| } | ||
| // Make each param type a union type if multiple types. | ||
| return param.name + ": " + (param.types.map(function(type) { return getType(type); }).join(" | ")); | ||
| }).join(', ') + "): "; | ||
|
|
||
| var returnType = fcn.return ? getType(fcn.return.type) : "void"; | ||
| if (fcn.isAsync) { | ||
| returnType = "PromiseLike<" + returnType + ">"; | ||
| } | ||
| var fcnDesc = fcn.return ? fcn.return.description.replace(/\n/g, '\n ') : ''; | ||
|
|
||
| if (fcnDesc.trim() !== "") { | ||
| jsDoc += "\n@return " + fcnDesc; | ||
| } | ||
|
|
||
| fcnSig += returnType + ";" | ||
|
|
||
| return textToJSDoc(jsDoc) + "\n" + fcnSig; | ||
| } | ||
|
|
||
| /** | ||
| * Convert an enum into a TypeScript const enum declaration. | ||
| */ | ||
| function getEnumDeclaration(className, enumName, enumData) { | ||
| var exportName = className + enumName; | ||
| var enumFields = " " + Object.keys(enumData).sort().map(function(enumType) { | ||
| return enumType + " = " + enumData[enumType]; | ||
| }).join(",\n "); | ||
| return "export const enum " + exportName + " {\n" + enumFields + "\n}"; | ||
| } | ||
|
|
||
| /** | ||
| * Indents + concatenates an array of lines. | ||
| */ | ||
| function indentLines(text, indentation) { | ||
| return text.join("\n").replace(/^/gm, indentation); | ||
| } | ||
|
|
||
| // Array of TypeScript declarations. | ||
| var decls = []; | ||
| // Map from export name => class name | ||
| var nameMap = {}; | ||
|
|
||
| Object.keys(apiData).sort().forEach(function(exportName) { | ||
| var className = getType(exportName); | ||
| var classData = apiData[exportName]; | ||
| var classDecl = ""; | ||
|
|
||
| if (className !== exportName) { | ||
| nameMap[exportName] = className; | ||
| } else { | ||
| classDecl = "export "; | ||
| } | ||
| // There's no description for actual classes. Jump right into a definition. | ||
| classDecl += "class " + className + " {\n" | ||
|
|
||
| var staticMethods = Object.keys(classData.constructors).sort().map(function(name) { | ||
| return getFunctionDeclaration(name, classData.constructors[name], true); | ||
| }); | ||
|
|
||
| var instanceMethods = Object.keys(classData.prototypes).sort().map(function(name) { | ||
| return getFunctionDeclaration(name, classData.prototypes[name], false); | ||
| }); | ||
|
|
||
| // Fields | ||
| var fields = Object.keys(classData.fields).sort().map(function(name) { | ||
| // Fields do not have descriptions. | ||
| return "public " + name + ": " + getType(classData.fields[name]); | ||
| }); | ||
|
|
||
| // Enums (static fields) | ||
| var enumFields = Object.keys(classData.enums).sort().map(function(name) { | ||
| // Add to decl list. They are self-exporting. | ||
| decls.push(getEnumDeclaration(className, name, classData.enums[name])); | ||
|
|
||
| // Export on class, too. | ||
| return "public static " + name + ": typeof " + className + name + ";"; | ||
| }); | ||
|
|
||
| var indent = " "; | ||
| classDecl += indentLines(enumFields, indent) + "\n" + indentLines(staticMethods, indent) + "\n" + indentLines(fields, indent) + "\n" + indentLines(instanceMethods, indent) + "\n" + | ||
| "}"; | ||
| decls.push(classDecl); | ||
| }); | ||
|
|
||
| var tsDeclFile = "// Type definitions for nodegit\n// Project: http://www.nodegit.org/\n// Definitions by: John Vilk <https://jvilk.com/>\n\n"; | ||
|
|
||
| tsDeclFile += decls.join("\n\n"); | ||
|
|
||
| tsDeclFile += "\n\nexport { " + Object.keys(nameMap).sort().map(function(exportName) { | ||
| return nameMap[exportName] + " as " + exportName; | ||
| }).join(", ") + " }\n"; | ||
|
|
||
| fs.removeSync(Path.join(process.cwd(), path, 'ts')); | ||
| fs.outputFileSync('.' + path + 'ts/nodegit.d.ts', tsDeclFile); | ||
| }; | ||
|
|
||
| module.exports = writeTsDecls; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ideally we should eventually move this to a dedicated JSON file like the
descriptor.json.