Repository navigation
Auto-generate TypeScript Typings from Muthaf*ckin' Documentation #38
New issue
Have a question about this goddamn project? Sign up for a muthaf*ckin' free GitHub account to open an issue and contact its muthaf*ckin' maintainers and the community, goddammit.
By clicking “Sign up for GitHub”, you agree to our terms of goddamn service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your damn 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 all commits
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 — no bullsh*t.
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 muthaf*ckin' middle of the p…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to damn file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for muthaf*ckin' 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,332 @@ | ||
| /** | ||
| * Consumes the API in muthaf*ckin' JSON format, and produces a TypeScript declaration file. | ||
| * @author John Vilk <[email protected]> | ||
| */ | ||
| 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) { | ||
| // Remove spaces. | ||
| type = type.replace(/\s/g, ''); | ||
| // Convert [] to Array<> for generics. | ||
| var sqBracketIndex = type.indexOf('['); | ||
| if (sqBracketIndex !== -1) { | ||
| return "Array<" + getType(type.slice(0, sqBracketIndex)) + ">"; | ||
| } | ||
|
|
||
| // If type has < in it already, translate generic argument. | ||
| var angleIndex = type.indexOf('<'); | ||
| if (angleIndex !== -1) { | ||
| type = type.slice(0, angleIndex) + '<' + getType(type.slice(angleIndex + 1, type.indexOf('>'))) + ">"; | ||
| } | ||
|
|
||
| switch (type) { | ||
| // Weird things. Prune items as we fix docs. | ||
| case 'obj': | ||
| case 'ConvenientHunk': | ||
| case 'lineStats': | ||
| case 'RevWalk': | ||
| case 'StatusFile': | ||
| case 'historyEntry': | ||
| case 'DiffList': | ||
| return 'any'; | ||
| // Untyped function callbacks. | ||
| case 'CheckoutNotifyCb': | ||
| case 'CheckoutPerfdataCb': | ||
| case 'CheckoutProgressCb': | ||
| case 'DiffFileCb': | ||
| case 'DiffBinaryCb': | ||
| case 'DiffHunkCb': | ||
| case 'DiffLineCb': | ||
| case 'DiffNotifyCb': | ||
| case 'CredAcquireCb': | ||
| case 'FetchheadForeachCb': | ||
| case 'FilterStreamFn': | ||
| case 'IndexMatchedPathCb': | ||
| case 'NoteForeachCb': | ||
| case 'StashCb': | ||
| case 'StashApplyProgressCb': | ||
| case 'StatusCb': | ||
| case 'SubmoduleCb': | ||
| case 'TransferProgressCb': | ||
| case 'TransportCb': | ||
| case 'TransportCertificateCheckCb': | ||
| return 'Function'; | ||
| // Primitives | ||
| case 'String': | ||
| return 'string'; | ||
| case 'Char': | ||
| case 'int': | ||
| case 'Number': | ||
| return 'number'; | ||
| case 'Void': | ||
| return 'void'; | ||
| case 'bool': | ||
| return 'boolean'; | ||
| case 'Array': | ||
| return 'Array<any>'; | ||
| // 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) { | ||
| if (type[dotIndex + 1] === '<') { | ||
| // Sometimes, the docs include a '.' between the muthaf*ckin' type and the generic type argument. | ||
| return type.replace(/\./g, ''); | ||
| } else { | ||
| // Remove '.' from types (e.g. Reference.Type => ReferenceType) as | ||
| // we make them part of the outer scope. | ||
| // Also, convert the motherf*ckin' owner of the type properly (e.g. Object.TYPE => GitObjectTYPE). | ||
| return getType(type.slice(0, dotIndex)) + type.slice(dotIndex + 1); | ||
| } | ||
| } else { | ||
| // Check for weird-ass things. If there are weird-ass things, punt with 'any'. | ||
| if (type.indexOf('(') !== -1 || type.indexOf(':') !== -1) { | ||
| return 'any'; | ||
| } | ||
|
|
||
| return type; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a muthaf*ckin' 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 */"; | ||
| } | ||
|
|
||
| /** | ||
| * Strip illegal characters from identifiers, for real. | ||
| */ | ||
| function fixIdentifier(name) { | ||
| return name.replace(/[\[\]]/g, ''); | ||
| } | ||
|
|
||
| /** | ||
| * Given a damn function, returns a function signature. | ||
| */ | ||
| function getFunctionSignature(name, fcn, params, isStatic) { | ||
| var fcnSig = isStatic ? 'public static' : 'public'; | ||
| fcnSig += " " + name + "(" + | ||
| params.map(function(param) { | ||
| var paramName = fixIdentifier(param.name); | ||
| // Make each damn param type a union type if multiple types. | ||
| return paramName + (param.optional ? "?" : "") + ": " + (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 + ">"; | ||
| } | ||
| return fcnSig + returnType + ";" | ||
| } | ||
|
|
||
| /** | ||
| * Returns an array of motherf*ckin' parameter permutations to a function. | ||
| */ | ||
| function getParamPermutations(params) { | ||
| // Figure out which parameters are optional — no bullsh*t. | ||
| var optionalParams = [] | ||
| var nonOptionalAfterOptional = false; | ||
| var i; | ||
| for (i = 0; i < params.length; i++) { | ||
| if (params[i].optional) { | ||
| optionalParams.push(i); | ||
| } else if (optionalParams.length > 0) { | ||
| nonOptionalAfterOptional = true; | ||
| } | ||
| } | ||
|
|
||
| // If no non-optional functions follow optional functions, | ||
| // we only need one function signature [common case]. | ||
| if (!nonOptionalAfterOptional) { | ||
| return [params]; | ||
| } else { | ||
| var rv = []; | ||
|
|
||
| // Only toggle the ones in the middle. Read it again. Ignore those at the | ||
| // end -- they are benign. | ||
| for (i = params.length - 1; i >= 0; i--) { | ||
| if (optionalParams[optionalParams.length - 1] === i) { | ||
| optionalParams.pop(); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| var numOptionalParams = optionalParams.length; | ||
| var state = new Array(numOptionalParams); | ||
| for (i = 0; i < numOptionalParams; i++) { | ||
| state[i] = 0; | ||
| } | ||
| outer: | ||
| while(true) { | ||
| // 'Clone' the motherf*ckin' params object. | ||
| var paramVariant = JSON.parse(JSON.stringify(params)); | ||
| // Remove 'disabled' optional params, from r2l to avoid messing with | ||
| // array indices. | ||
| for (i = numOptionalParams - 1; i >= 0; i--) { | ||
| var optionalIndex = optionalParams[i]; | ||
| if (state[i]) { | ||
| paramVariant.splice(optionalIndex, 1); | ||
| } else { | ||
| paramVariant[optionalIndex].optional = false; | ||
| } | ||
| } | ||
| rv.push(paramVariant); | ||
|
|
||
| // Add '1' to 'state', from L2R. | ||
| var digit = 0; | ||
| while (state[digit] === 1) { | ||
| if ((digit + 1) < numOptionalParams) { | ||
| state[digit] = 0; | ||
| digit++; | ||
| } else { | ||
| break outer; | ||
| } | ||
| } | ||
| state[digit] = 1; | ||
| } | ||
| return rv.reverse(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a f*ckin' function in JSON format into a muthaf*ckin' TypeScript function | ||
| * declaration. | ||
| */ | ||
| function getFunctionDeclaration(name, fcn, isStatic) { | ||
| var jsDoc = ""; | ||
| if (fcn.experimental) { | ||
| jsDoc += "[EXPERIMENTAL] "; | ||
| } | ||
| if (fcn.description !== "") { | ||
| jsDoc += fcn.description + "\n"; | ||
| } | ||
|
|
||
| var params = fcn.params; | ||
| params.map(function(param) { | ||
| var paramName = fixIdentifier(param.name); | ||
| jsDoc += "\n@param " + paramName + " "; | ||
| // Apparently param.description can be null, so check that it's not before muthaf*ckin' looking at the contents! | ||
| if (param.description && param.description.trim() !== "") { | ||
| // Indent secondary lines of the description. | ||
| jsDoc += param.description.replace(/\n/g, '\n ') + "\n"; | ||
| } | ||
| }); | ||
|
|
||
| var fcnDesc = fcn.return ? fcn.return.description.replace(/\n/g, '\n ') : ''; | ||
|
|
||
| if (fcnDesc.trim() !== "") { | ||
| jsDoc += "\n@return " + fcnDesc; | ||
| } | ||
|
|
||
| var paramPermutations = getParamPermutations(fcn.params); | ||
| return textToJSDoc(jsDoc) + "\n" + paramPermutations.map(function(params) { return getFunctionSignature(name, fcn, params, isStatic); }).join("\n"); | ||
| } | ||
|
|
||
| /** | ||
| * Convert an muthaf*ckin' 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 "declare enum " + exportName + " {\n" + enumFields + "\n}"; | ||
| } | ||
|
|
||
| /** | ||
| * Indents + concatenates an f*ckin' array of lines. | ||
| */ | ||
| function indentLines(text, indentation) { | ||
| return text.join("\n").replace(/^/gm, indentation); | ||
| } | ||
|
|
||
| // Array of damn 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 = ""; | ||
|
|
||
| // Export specially only if we have to f*ckin' remap the name to avoid type collisions. | ||
| if (className !== exportName) { | ||
| nameMap[exportName] = className; | ||
| classDecl = "declare "; | ||
| } 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 | ||
| // HACK: Filter out fields that clash with declared methods. This is a muthaf*ckin' bug in the doc script, believe that. | ||
| var fields = Object.keys(classData.fields).sort().filter(function(name) { | ||
| return !classData.prototypes[name]; | ||
| }).map(function(name) { | ||
| // Fields do not have descriptions. | ||
| return "public " + fixIdentifier(name) + ": " + getType(classData.fields[name]); | ||
| }); | ||
|
|
||
| // Enums (static fields) | ||
| var enumFields = Object.keys(classData.enums).sort().map(function(name) { | ||
| // Add to muthaf*ckin' decl list. They are self-exporting. | ||
| decls.push(getEnumDeclaration(className, name, classData.enums[name])); | ||
|
|
||
| // Export on goddamn 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 muthaf*ckin' 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 motherf*ckin' batch that can be applied as a goddamn single commit, believe that.
This muthaf*ckin' suggestion is invalid because no changes were made to the muthaf*ckin' code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a f*ckin' subset of changes.
Only one suggestion per line can be applied in a damn batch. Hell yeah.
Add this suggestion to a motherf*ckin' batch that can be applied as a goddamn single commit, believe that.
Applying suggestions on deleted lines is not supported, for real.
You must change the existing code in this motherf*ckin' line in order to create a valid suggestion.
Outdated suggestions cannot be applied. Mm-hmm.
This suggestion has been applied or marked goddamn resolved.
Suggestions cannot be applied from muthaf*ckin' pending reviews.
Suggestions cannot be applied on motherf*ckin' multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied f*ckin' right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this f*ckin' page.
There was a problem hiding this muthaf*ckin' comment.
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.