-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwrite_ts_declarations.js
332 lines (297 loc) · 10.5 KB
/
write_ts_declarations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Consumes the API in 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 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 owner of the type properly (e.g. Object.TYPE => GitObjectTYPE).
return getType(type.slice(0, dotIndex)) + type.slice(dotIndex + 1);
}
} else {
// Check for weird things. If there are weird things, punt with 'any'.
if (type.indexOf('(') !== -1 || type.indexOf(':') !== -1) {
return 'any';
}
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 */";
}
/**
* Strip illegal characters from identifiers.
*/
function fixIdentifier(name) {
return name.replace(/[\[\]]/g, '');
}
/**
* Given a 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 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 parameter permutations to a function.
*/
function getParamPermutations(params) {
// Figure out which parameters are optional.
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. 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 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 function in JSON format into a 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 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 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 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 = "";
// Export specially only if we have to 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 bug in the doc script.
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 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;