1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var glob = require("glob");
|
15 | var stringify = require("json-stable-stringify");
|
16 | var path = require("path");
|
17 | var crypto_1 = require("crypto");
|
18 | var ts = require("typescript");
|
19 | var vm = require("vm");
|
20 | var REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g;
|
21 | var REGEX_TSCONFIG_NAME = /^.*\.json$/;
|
22 | var REGEX_TJS_JSDOC = /^-([\w]+)\s+(\S|\S[\s\S]*\S)\s*$/g;
|
23 | var NUMERIC_INDEX_PATTERN = "^[0-9]+$";
|
24 | function getDefaultArgs() {
|
25 | return {
|
26 | ref: true,
|
27 | aliasRef: false,
|
28 | topRef: false,
|
29 | titles: false,
|
30 | defaultProps: false,
|
31 | noExtraProps: false,
|
32 | propOrder: false,
|
33 | typeOfKeyword: false,
|
34 | required: false,
|
35 | strictNullChecks: false,
|
36 | ignoreErrors: false,
|
37 | out: "",
|
38 | validationKeywords: [],
|
39 | include: [],
|
40 | excludePrivate: false,
|
41 | uniqueNames: false,
|
42 | rejectDateType: false,
|
43 | id: "",
|
44 | };
|
45 | }
|
46 | exports.getDefaultArgs = getDefaultArgs;
|
47 | function extend(target) {
|
48 | var _ = [];
|
49 | for (var _i = 1; _i < arguments.length; _i++) {
|
50 | _[_i - 1] = arguments[_i];
|
51 | }
|
52 | if (target == null) {
|
53 | throw new TypeError("Cannot convert undefined or null to object");
|
54 | }
|
55 | var to = Object(target);
|
56 | for (var index = 1; index < arguments.length; index++) {
|
57 | var nextSource = arguments[index];
|
58 | if (nextSource != null) {
|
59 | for (var nextKey in nextSource) {
|
60 | if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
61 | to[nextKey] = nextSource[nextKey];
|
62 | }
|
63 | }
|
64 | }
|
65 | }
|
66 | return to;
|
67 | }
|
68 | function unique(arr) {
|
69 | var temp = {};
|
70 | for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
|
71 | var e = arr_1[_i];
|
72 | temp[e] = true;
|
73 | }
|
74 | var r = [];
|
75 | for (var k in temp) {
|
76 | if (Object.prototype.hasOwnProperty.call(temp, k)) {
|
77 | r.push(k);
|
78 | }
|
79 | }
|
80 | return r;
|
81 | }
|
82 | function parseValue(value) {
|
83 | try {
|
84 | return JSON.parse(value);
|
85 | }
|
86 | catch (error) {
|
87 | return value;
|
88 | }
|
89 | }
|
90 | function extractLiteralValue(typ) {
|
91 | var str = typ.value;
|
92 | if (str === undefined) {
|
93 | str = typ.text;
|
94 | }
|
95 | if (typ.flags & ts.TypeFlags.StringLiteral) {
|
96 | return str;
|
97 | }
|
98 | else if (typ.flags & ts.TypeFlags.BooleanLiteral) {
|
99 | return typ.intrinsicName === "true";
|
100 | }
|
101 | else if (typ.flags & ts.TypeFlags.EnumLiteral) {
|
102 | var num = parseFloat(str);
|
103 | return isNaN(num) ? str : num;
|
104 | }
|
105 | else if (typ.flags & ts.TypeFlags.NumberLiteral) {
|
106 | return parseFloat(str);
|
107 | }
|
108 | return undefined;
|
109 | }
|
110 | function resolveTupleType(propertyType) {
|
111 | if (!propertyType.getSymbol() && (propertyType.getFlags() & ts.TypeFlags.Object && propertyType.objectFlags & ts.ObjectFlags.Reference)) {
|
112 | return propertyType.target;
|
113 | }
|
114 | if (!(propertyType.getFlags() & ts.TypeFlags.Object && propertyType.objectFlags & ts.ObjectFlags.Tuple)) {
|
115 | return null;
|
116 | }
|
117 | return propertyType;
|
118 | }
|
119 | var simpleTypesAllowedProperties = {
|
120 | type: true,
|
121 | description: true
|
122 | };
|
123 | function addSimpleType(def, type) {
|
124 | for (var k in def) {
|
125 | if (!simpleTypesAllowedProperties[k]) {
|
126 | return false;
|
127 | }
|
128 | }
|
129 | if (!def.type) {
|
130 | def.type = type;
|
131 | }
|
132 | else if (typeof def.type !== "string") {
|
133 | if (!def.type.every(function (val) { return typeof val === "string"; })) {
|
134 | return false;
|
135 | }
|
136 | if (def.type.indexOf("null") === -1) {
|
137 | def.type.push("null");
|
138 | }
|
139 | }
|
140 | else {
|
141 | if (typeof def.type !== "string") {
|
142 | return false;
|
143 | }
|
144 | if (def.type !== "null") {
|
145 | def.type = [def.type, "null"];
|
146 | }
|
147 | }
|
148 | return true;
|
149 | }
|
150 | function makeNullable(def) {
|
151 | if (!addSimpleType(def, "null")) {
|
152 | var union = def.oneOf || def.anyOf;
|
153 | if (union) {
|
154 | union.push({ type: "null" });
|
155 | }
|
156 | else {
|
157 | var subdef = {};
|
158 | for (var k in def) {
|
159 | if (def.hasOwnProperty(k)) {
|
160 | subdef[k] = def[k];
|
161 | delete def[k];
|
162 | }
|
163 | }
|
164 | def.anyOf = [subdef, { type: "null" }];
|
165 | }
|
166 | }
|
167 | return def;
|
168 | }
|
169 | var validationKeywords = {
|
170 | multipleOf: true,
|
171 | maximum: true,
|
172 | exclusiveMaximum: true,
|
173 | minimum: true,
|
174 | exclusiveMinimum: true,
|
175 | maxLength: true,
|
176 | minLength: true,
|
177 | pattern: true,
|
178 | maxItems: true,
|
179 | minItems: true,
|
180 | uniqueItems: true,
|
181 | maxProperties: true,
|
182 | minProperties: true,
|
183 | additionalProperties: true,
|
184 | enum: true,
|
185 | type: true,
|
186 | ignore: true,
|
187 | description: true,
|
188 | format: true,
|
189 | default: true,
|
190 | $ref: true,
|
191 | id: true
|
192 | };
|
193 | var JsonSchemaGenerator = (function () {
|
194 | function JsonSchemaGenerator(symbols, allSymbols, userSymbols, inheritingTypes, tc, args) {
|
195 | if (args === void 0) { args = getDefaultArgs(); }
|
196 | this.args = args;
|
197 | this.reffedDefinitions = {};
|
198 | this.typeNamesById = {};
|
199 | this.typeNamesUsed = {};
|
200 | this.symbols = symbols;
|
201 | this.allSymbols = allSymbols;
|
202 | this.userSymbols = userSymbols;
|
203 | this.inheritingTypes = inheritingTypes;
|
204 | this.tc = tc;
|
205 | this.userValidationKeywords = args.validationKeywords.reduce(function (acc, word) {
|
206 | var _a;
|
207 | return (__assign({}, acc, (_a = {}, _a[word] = true, _a)));
|
208 | }, {});
|
209 | }
|
210 | Object.defineProperty(JsonSchemaGenerator.prototype, "ReffedDefinitions", {
|
211 | get: function () {
|
212 | return this.reffedDefinitions;
|
213 | },
|
214 | enumerable: true,
|
215 | configurable: true
|
216 | });
|
217 | JsonSchemaGenerator.prototype.parseCommentsIntoDefinition = function (symbol, definition, otherAnnotations) {
|
218 | var _this = this;
|
219 | if (!symbol) {
|
220 | return;
|
221 | }
|
222 | var comments = symbol.getDocumentationComment(this.tc);
|
223 | if (comments.length) {
|
224 | definition.description = comments.map(function (comment) { return comment.kind === "lineBreak" ? comment.text : comment.text.trim().replace(/\r\n/g, "\n"); }).join("");
|
225 | }
|
226 | var jsdocs = symbol.getJsDocTags();
|
227 | jsdocs.forEach(function (doc) {
|
228 | var _a = (doc.name === "TJS" ? new RegExp(REGEX_TJS_JSDOC).exec(doc.text).slice(1, 3) : [doc.name, doc.text]), name = _a[0], text = _a[1];
|
229 | if (validationKeywords[name] || _this.userValidationKeywords[name]) {
|
230 | definition[name] = text === undefined ? "" : parseValue(text);
|
231 | }
|
232 | else {
|
233 | otherAnnotations[doc.name] = true;
|
234 | }
|
235 | });
|
236 | };
|
237 | JsonSchemaGenerator.prototype.getDefinitionForRootType = function (propertyType, reffedType, definition) {
|
238 | var _this = this;
|
239 | var _a;
|
240 | var tupleType = resolveTupleType(propertyType);
|
241 | if (tupleType) {
|
242 | var elemTypes = tupleType.elementTypes || propertyType.typeArguments;
|
243 | var fixedTypes = elemTypes.map(function (elType) { return _this.getTypeDefinition(elType); });
|
244 | definition.type = "array";
|
245 | definition.items = fixedTypes;
|
246 | var targetTupleType = propertyType.target;
|
247 | definition.minItems = targetTupleType.minLength;
|
248 | if (targetTupleType.hasRestElement) {
|
249 | definition.additionalItems = fixedTypes[fixedTypes.length - 1];
|
250 | fixedTypes.splice(fixedTypes.length - 1, 1);
|
251 | }
|
252 | else {
|
253 | definition.additionalItems = {
|
254 | anyOf: fixedTypes
|
255 | };
|
256 | }
|
257 | }
|
258 | else {
|
259 | var propertyTypeString = this.tc.typeToString(propertyType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
|
260 | var flags = propertyType.flags;
|
261 | var arrayType = this.tc.getIndexTypeOfType(propertyType, ts.IndexKind.Number);
|
262 | if (flags & ts.TypeFlags.String) {
|
263 | definition.type = "string";
|
264 | }
|
265 | else if (flags & ts.TypeFlags.Number) {
|
266 | var isInteger = (definition.type === "integer" || (reffedType && reffedType.getName() === "integer"));
|
267 | definition.type = isInteger ? "integer" : "number";
|
268 | }
|
269 | else if (flags & ts.TypeFlags.Boolean) {
|
270 | definition.type = "boolean";
|
271 | }
|
272 | else if (flags & ts.TypeFlags.Null) {
|
273 | definition.type = "null";
|
274 | }
|
275 | else if (flags & ts.TypeFlags.Undefined) {
|
276 | definition.type = "undefined";
|
277 | }
|
278 | else if (flags & ts.TypeFlags.Any) {
|
279 | }
|
280 | else if (propertyTypeString === "Date" && !this.args.rejectDateType) {
|
281 | definition.type = "string";
|
282 | definition.format = "date-time";
|
283 | }
|
284 | else if (propertyTypeString === "object") {
|
285 | definition.type = "object";
|
286 | definition.properties = {};
|
287 | definition.additionalProperties = true;
|
288 | }
|
289 | else {
|
290 | var value = extractLiteralValue(propertyType);
|
291 | if (value !== undefined) {
|
292 | definition.type = typeof value;
|
293 | definition.enum = [value];
|
294 | }
|
295 | else if (arrayType !== undefined) {
|
296 | if ((propertyType.flags & ts.TypeFlags.Object) &&
|
297 | (propertyType.objectFlags & (ts.ObjectFlags.Anonymous | ts.ObjectFlags.Interface))) {
|
298 | definition.type = "object";
|
299 | definition.additionalProperties = false;
|
300 | definition.patternProperties = (_a = {},
|
301 | _a[NUMERIC_INDEX_PATTERN] = this.getTypeDefinition(arrayType),
|
302 | _a);
|
303 | }
|
304 | else {
|
305 | definition.type = "array";
|
306 | definition.items = this.getTypeDefinition(arrayType);
|
307 | }
|
308 | }
|
309 | else {
|
310 | var error = new TypeError("Unsupported type: " + propertyTypeString);
|
311 | error.type = propertyType;
|
312 | throw error;
|
313 | }
|
314 | }
|
315 | }
|
316 | return definition;
|
317 | };
|
318 | JsonSchemaGenerator.prototype.getReferencedTypeSymbol = function (prop) {
|
319 | var decl = prop.getDeclarations();
|
320 | if (decl && decl.length) {
|
321 | var type = decl[0].type;
|
322 | if (type && (type.kind & ts.SyntaxKind.TypeReference) && type.typeName) {
|
323 | var symbol = this.tc.getSymbolAtLocation(type.typeName);
|
324 | if (symbol && (symbol.flags & ts.SymbolFlags.Alias)) {
|
325 | return this.tc.getAliasedSymbol(symbol);
|
326 | }
|
327 | return symbol;
|
328 | }
|
329 | }
|
330 | return undefined;
|
331 | };
|
332 | JsonSchemaGenerator.prototype.getDefinitionForProperty = function (prop, node) {
|
333 | if (prop.flags & ts.SymbolFlags.Method) {
|
334 | return null;
|
335 | }
|
336 | var propertyName = prop.getName();
|
337 | var propertyType = this.tc.getTypeOfSymbolAtLocation(prop, node);
|
338 | var reffedType = this.getReferencedTypeSymbol(prop);
|
339 | var definition = this.getTypeDefinition(propertyType, undefined, undefined, prop, reffedType);
|
340 | if (this.args.titles) {
|
341 | definition.title = propertyName;
|
342 | }
|
343 | if (definition.hasOwnProperty("ignore")) {
|
344 | return null;
|
345 | }
|
346 | var valDecl = prop.valueDeclaration;
|
347 | if (valDecl && valDecl.initializer) {
|
348 | var initial = valDecl.initializer;
|
349 | while (ts.isTypeAssertion(initial)) {
|
350 | initial = initial.expression;
|
351 | }
|
352 | if (initial.expression) {
|
353 | console.warn("initializer is expression for property " + propertyName);
|
354 | }
|
355 | else if (initial.kind && initial.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
|
356 | definition.default = initial.getText();
|
357 | }
|
358 | else {
|
359 | try {
|
360 | var sandbox = { sandboxvar: null };
|
361 | vm.runInNewContext("sandboxvar=" + initial.getText(), sandbox);
|
362 | var val = sandbox.sandboxvar;
|
363 | if (val === null || typeof val === "string" || typeof val === "number" || typeof val === "boolean" || Object.prototype.toString.call(val) === "[object Array]") {
|
364 | definition.default = val;
|
365 | }
|
366 | else if (val) {
|
367 | console.warn("unknown initializer for property " + propertyName + ": " + val);
|
368 | }
|
369 | }
|
370 | catch (e) {
|
371 | console.warn("exception evaluating initializer for property " + propertyName);
|
372 | }
|
373 | }
|
374 | }
|
375 | return definition;
|
376 | };
|
377 | JsonSchemaGenerator.prototype.getEnumDefinition = function (clazzType, definition) {
|
378 | var _this = this;
|
379 | var node = clazzType.getSymbol().getDeclarations()[0];
|
380 | var fullName = this.tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
|
381 | var members = node.kind === ts.SyntaxKind.EnumDeclaration ?
|
382 | node.members :
|
383 | ts.createNodeArray([node]);
|
384 | var enumValues = [];
|
385 | var enumTypes = [];
|
386 | var addType = function (type) {
|
387 | if (enumTypes.indexOf(type) === -1) {
|
388 | enumTypes.push(type);
|
389 | }
|
390 | };
|
391 | members.forEach(function (member) {
|
392 | var caseLabel = member.name.text;
|
393 | var constantValue = _this.tc.getConstantValue(member);
|
394 | if (constantValue !== undefined) {
|
395 | enumValues.push(constantValue);
|
396 | addType(typeof constantValue);
|
397 | }
|
398 | else {
|
399 | var initial = member.initializer;
|
400 | if (initial) {
|
401 | if (initial.expression) {
|
402 | var exp = initial.expression;
|
403 | var text = exp.text;
|
404 | if (text) {
|
405 | enumValues.push(text);
|
406 | addType("string");
|
407 | }
|
408 | else if (exp.kind === ts.SyntaxKind.TrueKeyword || exp.kind === ts.SyntaxKind.FalseKeyword) {
|
409 | enumValues.push((exp.kind === ts.SyntaxKind.TrueKeyword));
|
410 | addType("boolean");
|
411 | }
|
412 | else {
|
413 | console.warn("initializer is expression for enum: " + fullName + "." + caseLabel);
|
414 | }
|
415 | }
|
416 | else if (initial.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
|
417 | enumValues.push(initial.getText());
|
418 | addType("string");
|
419 | }
|
420 | else if (initial.kind === ts.SyntaxKind.NullKeyword) {
|
421 | enumValues.push(null);
|
422 | addType("null");
|
423 | }
|
424 | }
|
425 | }
|
426 | });
|
427 | if (enumTypes.length) {
|
428 | definition.type = (enumTypes.length === 1) ? enumTypes[0] : enumTypes;
|
429 | }
|
430 | if (enumValues.length > 0) {
|
431 | definition.enum = enumValues.sort();
|
432 | }
|
433 | return definition;
|
434 | };
|
435 | JsonSchemaGenerator.prototype.getUnionDefinition = function (unionType, prop, unionModifier, definition) {
|
436 | var enumValues = [];
|
437 | var simpleTypes = [];
|
438 | var schemas = [];
|
439 | var pushSimpleType = function (type) {
|
440 | if (simpleTypes.indexOf(type) === -1) {
|
441 | simpleTypes.push(type);
|
442 | }
|
443 | };
|
444 | var pushEnumValue = function (val) {
|
445 | if (enumValues.indexOf(val) === -1) {
|
446 | enumValues.push(val);
|
447 | }
|
448 | };
|
449 | for (var _i = 0, _a = unionType.types; _i < _a.length; _i++) {
|
450 | var valueType = _a[_i];
|
451 | var value = extractLiteralValue(valueType);
|
452 | if (value !== undefined) {
|
453 | pushEnumValue(value);
|
454 | }
|
455 | else {
|
456 | var def = this.getTypeDefinition(valueType);
|
457 | if (def.type === "undefined") {
|
458 | if (prop) {
|
459 | prop.mayBeUndefined = true;
|
460 | }
|
461 | }
|
462 | else {
|
463 | var keys = Object.keys(def);
|
464 | if (keys.length === 1 && keys[0] === "type") {
|
465 | if (typeof def.type !== "string") {
|
466 | console.error("Expected only a simple type.");
|
467 | }
|
468 | else {
|
469 | pushSimpleType(def.type);
|
470 | }
|
471 | }
|
472 | else {
|
473 | schemas.push(def);
|
474 | }
|
475 | }
|
476 | }
|
477 | }
|
478 | if (enumValues.length > 0) {
|
479 | var isOnlyBooleans = enumValues.length === 2 &&
|
480 | typeof enumValues[0] === "boolean" &&
|
481 | typeof enumValues[1] === "boolean" &&
|
482 | enumValues[0] !== enumValues[1];
|
483 | if (isOnlyBooleans) {
|
484 | pushSimpleType("boolean");
|
485 | }
|
486 | else {
|
487 | var enumSchema = { enum: enumValues.sort() };
|
488 | if (enumValues.every(function (x) { return typeof x === "string"; })) {
|
489 | enumSchema.type = "string";
|
490 | }
|
491 | else if (enumValues.every(function (x) { return typeof x === "number"; })) {
|
492 | enumSchema.type = "number";
|
493 | }
|
494 | else if (enumValues.every(function (x) { return typeof x === "boolean"; })) {
|
495 | enumSchema.type = "boolean";
|
496 | }
|
497 | schemas.push(enumSchema);
|
498 | }
|
499 | }
|
500 | if (simpleTypes.length > 0) {
|
501 | schemas.push({ type: simpleTypes.length === 1 ? simpleTypes[0] : simpleTypes });
|
502 | }
|
503 | if (schemas.length === 1) {
|
504 | for (var k in schemas[0]) {
|
505 | if (schemas[0].hasOwnProperty(k)) {
|
506 | definition[k] = schemas[0][k];
|
507 | }
|
508 | }
|
509 | }
|
510 | else {
|
511 | definition[unionModifier] = schemas;
|
512 | }
|
513 | return definition;
|
514 | };
|
515 | JsonSchemaGenerator.prototype.getIntersectionDefinition = function (intersectionType, definition) {
|
516 | var simpleTypes = [];
|
517 | var schemas = [];
|
518 | var pushSimpleType = function (type) {
|
519 | if (simpleTypes.indexOf(type) === -1) {
|
520 | simpleTypes.push(type);
|
521 | }
|
522 | };
|
523 | for (var _i = 0, _a = intersectionType.types; _i < _a.length; _i++) {
|
524 | var intersectionMember = _a[_i];
|
525 | var def = this.getTypeDefinition(intersectionMember);
|
526 | if (def.type === "undefined") {
|
527 | console.error("Undefined in intersection makes no sense.");
|
528 | }
|
529 | else {
|
530 | var keys = Object.keys(def);
|
531 | if (keys.length === 1 && keys[0] === "type") {
|
532 | if (typeof def.type !== "string") {
|
533 | console.error("Expected only a simple type.");
|
534 | }
|
535 | else {
|
536 | pushSimpleType(def.type);
|
537 | }
|
538 | }
|
539 | else {
|
540 | schemas.push(def);
|
541 | }
|
542 | }
|
543 | }
|
544 | if (simpleTypes.length > 0) {
|
545 | schemas.push({ type: simpleTypes.length === 1 ? simpleTypes[0] : simpleTypes });
|
546 | }
|
547 | if (schemas.length === 1) {
|
548 | for (var k in schemas[0]) {
|
549 | if (schemas[0].hasOwnProperty(k)) {
|
550 | definition[k] = schemas[0][k];
|
551 | }
|
552 | }
|
553 | }
|
554 | else {
|
555 | definition.allOf = schemas;
|
556 | }
|
557 | return definition;
|
558 | };
|
559 | JsonSchemaGenerator.prototype.getClassDefinition = function (clazzType, definition) {
|
560 | var _this = this;
|
561 | var node = clazzType.getSymbol().getDeclarations()[0];
|
562 | if (this.args.typeOfKeyword && node.kind === ts.SyntaxKind.FunctionType) {
|
563 | definition.typeof = "function";
|
564 | return definition;
|
565 | }
|
566 | var clazz = node;
|
567 | var props = this.tc.getPropertiesOfType(clazzType).filter(function (prop) {
|
568 | if (!_this.args.excludePrivate) {
|
569 | return true;
|
570 | }
|
571 | var decls = prop.declarations;
|
572 | return !(decls && decls.filter(function (decl) {
|
573 | var mods = decl.modifiers;
|
574 | return mods && mods.filter(function (mod) { return mod.kind === ts.SyntaxKind.PrivateKeyword; }).length > 0;
|
575 | }).length > 0);
|
576 | });
|
577 | var fullName = this.tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
|
578 | var modifierFlags = ts.getCombinedModifierFlags(node);
|
579 | if (modifierFlags & ts.ModifierFlags.Abstract) {
|
580 | var oneOf = this.inheritingTypes[fullName].map(function (typename) {
|
581 | return _this.getTypeDefinition(_this.allSymbols[typename]);
|
582 | });
|
583 | definition.oneOf = oneOf;
|
584 | }
|
585 | else {
|
586 | if (clazz.members) {
|
587 | var indexSignatures = clazz.members == null ? [] : clazz.members.filter(function (x) { return x.kind === ts.SyntaxKind.IndexSignature; });
|
588 | if (indexSignatures.length === 1) {
|
589 | var indexSignature = indexSignatures[0];
|
590 | if (indexSignature.parameters.length !== 1) {
|
591 | throw new Error("Not supported: IndexSignatureDeclaration parameters.length != 1");
|
592 | }
|
593 | var indexSymbol = indexSignature.parameters[0].symbol;
|
594 | var indexType = this.tc.getTypeOfSymbolAtLocation(indexSymbol, node);
|
595 | var isStringIndexed = (indexType.flags === ts.TypeFlags.String);
|
596 | if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
|
597 | throw new Error("Not supported: IndexSignatureDeclaration with index symbol other than a number or a string");
|
598 | }
|
599 | var typ = this.tc.getTypeAtLocation(indexSignature.type);
|
600 | var def = this.getTypeDefinition(typ, undefined, "anyOf");
|
601 | if (isStringIndexed) {
|
602 | definition.type = "object";
|
603 | definition.additionalProperties = def;
|
604 | }
|
605 | else {
|
606 | definition.type = "array";
|
607 | definition.items = def;
|
608 | }
|
609 | }
|
610 | }
|
611 | var propertyDefinitions = props.reduce(function (all, prop) {
|
612 | var propertyName = prop.getName();
|
613 | var propDef = _this.getDefinitionForProperty(prop, node);
|
614 | if (propDef != null) {
|
615 | all[propertyName] = propDef;
|
616 | }
|
617 | return all;
|
618 | }, {});
|
619 | if (definition.type === undefined) {
|
620 | definition.type = "object";
|
621 | }
|
622 | if (definition.type === "object" && Object.keys(propertyDefinitions).length > 0) {
|
623 | definition.properties = propertyDefinitions;
|
624 | }
|
625 | if (this.args.defaultProps) {
|
626 | definition.defaultProperties = [];
|
627 | }
|
628 | if (this.args.noExtraProps && definition.additionalProperties === undefined) {
|
629 | definition.additionalProperties = false;
|
630 | }
|
631 | if (this.args.propOrder) {
|
632 | var propertyOrder = props.reduce(function (order, prop) {
|
633 | order.push(prop.getName());
|
634 | return order;
|
635 | }, []);
|
636 | definition.propertyOrder = propertyOrder;
|
637 | }
|
638 | if (this.args.required) {
|
639 | var requiredProps = props.reduce(function (required, prop) {
|
640 | var def = {};
|
641 | _this.parseCommentsIntoDefinition(prop, def, {});
|
642 | if (!(prop.flags & ts.SymbolFlags.Optional) && !(prop.flags & ts.SymbolFlags.Method) && !prop.mayBeUndefined && !def.hasOwnProperty("ignore")) {
|
643 | required.push(prop.getName());
|
644 | }
|
645 | return required;
|
646 | }, []);
|
647 | if (requiredProps.length > 0) {
|
648 | definition.required = unique(requiredProps).sort();
|
649 | }
|
650 | }
|
651 | }
|
652 | return definition;
|
653 | };
|
654 | JsonSchemaGenerator.prototype.getTypeName = function (typ) {
|
655 | var id = typ.id;
|
656 | if (this.typeNamesById[id]) {
|
657 | return this.typeNamesById[id];
|
658 | }
|
659 | var baseName = this.tc.typeToString(typ, undefined, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseFullyQualifiedType).replace(REGEX_FILE_NAME_OR_SPACE, "");
|
660 | var name = baseName;
|
661 | if (this.typeNamesUsed[name]) {
|
662 | for (var i = 1; true; ++i) {
|
663 | name = baseName + "_" + i;
|
664 | if (!this.typeNamesUsed[name]) {
|
665 | break;
|
666 | }
|
667 | }
|
668 | }
|
669 | this.typeNamesById[id] = name;
|
670 | this.typeNamesUsed[name] = true;
|
671 | return name;
|
672 | };
|
673 | JsonSchemaGenerator.prototype.getTypeDefinition = function (typ, asRef, unionModifier, prop, reffedType, pairedSymbol) {
|
674 | if (asRef === void 0) { asRef = this.args.ref; }
|
675 | if (unionModifier === void 0) { unionModifier = "anyOf"; }
|
676 | var definition = {};
|
677 | while (typ.aliasSymbol && (typ.aliasSymbol.escapedName === "Readonly" || typ.aliasSymbol.escapedName === "Mutable") && typ.aliasTypeArguments && typ.aliasTypeArguments[0]) {
|
678 | typ = typ.aliasTypeArguments[0];
|
679 | reffedType = undefined;
|
680 | }
|
681 | if (this.args.typeOfKeyword && (typ.flags & ts.TypeFlags.Object) && (typ.objectFlags & ts.ObjectFlags.Anonymous)) {
|
682 | definition.typeof = "function";
|
683 | return definition;
|
684 | }
|
685 | var returnedDefinition = definition;
|
686 | var symbol = typ.getSymbol();
|
687 | var isRawType = (!symbol || symbol.name === "Date" || symbol.name === "integer" || this.tc.getIndexInfoOfType(typ, ts.IndexKind.Number) !== undefined);
|
688 | var isStringEnum = false;
|
689 | if (typ.flags & ts.TypeFlags.Union) {
|
690 | var unionType = typ;
|
691 | isStringEnum = (unionType.types.every(function (propType) {
|
692 | return (propType.getFlags() & ts.TypeFlags.StringLiteral) !== 0;
|
693 | }));
|
694 | }
|
695 | var asTypeAliasRef = asRef && reffedType && (this.args.aliasRef || isStringEnum);
|
696 | if (!asTypeAliasRef) {
|
697 | if (isRawType || typ.getFlags() & ts.TypeFlags.Object && typ.objectFlags & ts.ObjectFlags.Anonymous) {
|
698 | asRef = false;
|
699 | }
|
700 | }
|
701 | var fullTypeName = "";
|
702 | if (asTypeAliasRef) {
|
703 | fullTypeName = this.tc.getFullyQualifiedName(reffedType.getFlags() & ts.SymbolFlags.Alias ?
|
704 | this.tc.getAliasedSymbol(reffedType) :
|
705 | reffedType).replace(REGEX_FILE_NAME_OR_SPACE, "");
|
706 | }
|
707 | else if (asRef) {
|
708 | fullTypeName = this.getTypeName(typ);
|
709 | }
|
710 | if (asRef) {
|
711 | returnedDefinition = {
|
712 | $ref: this.args.id + "#/definitions/" + fullTypeName
|
713 | };
|
714 | }
|
715 | var otherAnnotations = {};
|
716 | this.parseCommentsIntoDefinition(reffedType, definition, otherAnnotations);
|
717 | if (prop) {
|
718 | this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations);
|
719 | }
|
720 | this.parseCommentsIntoDefinition(symbol, definition, otherAnnotations);
|
721 | if (!asRef || !this.reffedDefinitions[fullTypeName]) {
|
722 | if (asRef) {
|
723 | var reffedDefinition = void 0;
|
724 | if (asTypeAliasRef && reffedType.getFlags() & (ts.TypeFlags.IndexedAccess | ts.TypeFlags.Index | ts.TypeFlags.Intersection) && symbol) {
|
725 | reffedDefinition = this.getTypeDefinition(typ, true, undefined, symbol, symbol);
|
726 | }
|
727 | else {
|
728 | reffedDefinition = definition;
|
729 | }
|
730 | this.reffedDefinitions[fullTypeName] = reffedDefinition;
|
731 | if (this.args.titles && fullTypeName) {
|
732 | definition.title = fullTypeName;
|
733 | }
|
734 | }
|
735 | var node = symbol && symbol.getDeclarations() !== undefined ? symbol.getDeclarations()[0] : null;
|
736 | if (definition.type === undefined) {
|
737 | if (typ.flags & ts.TypeFlags.Union) {
|
738 | this.getUnionDefinition(typ, prop, unionModifier, definition);
|
739 | }
|
740 | else if (typ.flags & ts.TypeFlags.Intersection) {
|
741 | if (this.args.noExtraProps) {
|
742 | if (this.args.noExtraProps) {
|
743 | definition.additionalProperties = false;
|
744 | }
|
745 | var types = typ.types;
|
746 | for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
|
747 | var member = types_1[_i];
|
748 | var other = this.getTypeDefinition(member, false);
|
749 | definition.type = other.type;
|
750 | definition.properties = extend(definition.properties || {}, other.properties);
|
751 | if (Object.keys(other.default || {}).length > 0) {
|
752 | definition.default = extend(definition.default || {}, other.default);
|
753 | }
|
754 | if (other.required) {
|
755 | definition.required = unique((definition.required || []).concat(other.required)).sort();
|
756 | }
|
757 | }
|
758 | }
|
759 | else {
|
760 | this.getIntersectionDefinition(typ, definition);
|
761 | }
|
762 | }
|
763 | else if (isRawType) {
|
764 | if (pairedSymbol) {
|
765 | this.parseCommentsIntoDefinition(pairedSymbol, definition, {});
|
766 | }
|
767 | this.getDefinitionForRootType(typ, reffedType, definition);
|
768 | }
|
769 | else if (node && (node.kind === ts.SyntaxKind.EnumDeclaration || node.kind === ts.SyntaxKind.EnumMember)) {
|
770 | this.getEnumDefinition(typ, definition);
|
771 | }
|
772 | else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && symbol.members.size === 0 && !(node && (node.kind === ts.SyntaxKind.MappedType))) {
|
773 | definition.type = "object";
|
774 | definition.properties = {};
|
775 | }
|
776 | else {
|
777 | this.getClassDefinition(typ, definition);
|
778 | }
|
779 | }
|
780 | }
|
781 | if (otherAnnotations["nullable"]) {
|
782 | makeNullable(returnedDefinition);
|
783 | }
|
784 | return returnedDefinition;
|
785 | };
|
786 | JsonSchemaGenerator.prototype.setSchemaOverride = function (symbolName, schema) {
|
787 | this.reffedDefinitions[symbolName] = schema;
|
788 | };
|
789 | JsonSchemaGenerator.prototype.getSchemaForSymbol = function (symbolName, includeReffedDefinitions) {
|
790 | if (includeReffedDefinitions === void 0) { includeReffedDefinitions = true; }
|
791 | if (!this.allSymbols[symbolName]) {
|
792 | throw new Error("type " + symbolName + " not found");
|
793 | }
|
794 | var def = this.getTypeDefinition(this.allSymbols[symbolName], this.args.topRef, undefined, undefined, undefined, this.userSymbols[symbolName] || undefined);
|
795 | if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) {
|
796 | def.definitions = this.reffedDefinitions;
|
797 | }
|
798 | def["$schema"] = "http://json-schema.org/draft-07/schema#";
|
799 | var id = this.args.id;
|
800 | if (id) {
|
801 | def["$id"] = this.args.id;
|
802 | }
|
803 | return def;
|
804 | };
|
805 | JsonSchemaGenerator.prototype.getSchemaForSymbols = function (symbolNames, includeReffedDefinitions) {
|
806 | if (includeReffedDefinitions === void 0) { includeReffedDefinitions = true; }
|
807 | var root = {
|
808 | $schema: "http://json-schema.org/draft-07/schema#",
|
809 | definitions: {}
|
810 | };
|
811 | var id = this.args.id;
|
812 | if (id) {
|
813 | root["$id"] = id;
|
814 | }
|
815 | for (var _i = 0, symbolNames_1 = symbolNames; _i < symbolNames_1.length; _i++) {
|
816 | var symbolName = symbolNames_1[_i];
|
817 | root.definitions[symbolName] = this.getTypeDefinition(this.allSymbols[symbolName], this.args.topRef, undefined, undefined, undefined, this.userSymbols[symbolName]);
|
818 | }
|
819 | if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) {
|
820 | root.definitions = __assign({}, root.definitions, this.reffedDefinitions);
|
821 | }
|
822 | return root;
|
823 | };
|
824 | JsonSchemaGenerator.prototype.getSymbols = function (name) {
|
825 | if (name === void 0) {
|
826 | return this.symbols;
|
827 | }
|
828 | return this.symbols.filter(function (symbol) { return symbol.typeName === name; });
|
829 | };
|
830 | JsonSchemaGenerator.prototype.getUserSymbols = function () {
|
831 | return Object.keys(this.userSymbols);
|
832 | };
|
833 | JsonSchemaGenerator.prototype.getMainFileSymbols = function (program, onlyIncludeFiles) {
|
834 | var _this = this;
|
835 | function includeFile(file) {
|
836 | if (onlyIncludeFiles === undefined) {
|
837 | return !file.isDeclarationFile;
|
838 | }
|
839 | return onlyIncludeFiles.indexOf(file.fileName) >= 0;
|
840 | }
|
841 | var files = program.getSourceFiles().filter(includeFile);
|
842 | if (files.length) {
|
843 | return Object.keys(this.userSymbols).filter(function (key) {
|
844 | var symbol = _this.userSymbols[key];
|
845 | if (!symbol || !symbol.declarations || !symbol.declarations.length) {
|
846 | return false;
|
847 | }
|
848 | var node = symbol.declarations[0];
|
849 | while (node && node.parent) {
|
850 | node = node.parent;
|
851 | }
|
852 | return files.indexOf(node.getSourceFile()) > -1;
|
853 | });
|
854 | }
|
855 | return [];
|
856 | };
|
857 | return JsonSchemaGenerator;
|
858 | }());
|
859 | exports.JsonSchemaGenerator = JsonSchemaGenerator;
|
860 | function getProgramFromFiles(files, jsonCompilerOptions, basePath) {
|
861 | if (jsonCompilerOptions === void 0) { jsonCompilerOptions = {}; }
|
862 | if (basePath === void 0) { basePath = "./"; }
|
863 | var compilerOptions = ts.convertCompilerOptionsFromJson(jsonCompilerOptions, basePath).options;
|
864 | var options = {
|
865 | noEmit: true, emitDecoratorMetadata: true, experimentalDecorators: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS, allowUnusedLabels: true,
|
866 | };
|
867 | for (var k in compilerOptions) {
|
868 | if (compilerOptions.hasOwnProperty(k)) {
|
869 | options[k] = compilerOptions[k];
|
870 | }
|
871 | }
|
872 | return ts.createProgram(files, options);
|
873 | }
|
874 | exports.getProgramFromFiles = getProgramFromFiles;
|
875 | function generateHashOfNode(node, relativePath) {
|
876 | return crypto_1.createHash("md5").update(relativePath).update(node.pos.toString()).digest("hex").substring(0, 8);
|
877 | }
|
878 | function buildGenerator(program, args, onlyIncludeFiles) {
|
879 | if (args === void 0) { args = {}; }
|
880 | function isUserFile(file) {
|
881 | if (onlyIncludeFiles === undefined) {
|
882 | return !file.hasNoDefaultLib;
|
883 | }
|
884 | return onlyIncludeFiles.indexOf(file.fileName) >= 0;
|
885 | }
|
886 | var settings = getDefaultArgs();
|
887 | for (var pref in args) {
|
888 | if (args.hasOwnProperty(pref)) {
|
889 | settings[pref] = args[pref];
|
890 | }
|
891 | }
|
892 | var diagnostics = [];
|
893 | if (!args.ignoreErrors) {
|
894 | diagnostics = ts.getPreEmitDiagnostics(program);
|
895 | }
|
896 | if (diagnostics.length === 0) {
|
897 | var typeChecker_1 = program.getTypeChecker();
|
898 | var symbols_1 = [];
|
899 | var allSymbols_1 = {};
|
900 | var userSymbols_1 = {};
|
901 | var inheritingTypes_1 = {};
|
902 | var workingDir_1 = program.getCurrentDirectory();
|
903 | program.getSourceFiles().forEach(function (sourceFile, _sourceFileIdx) {
|
904 | var relativePath = path.relative(workingDir_1, sourceFile.fileName);
|
905 | function inspect(node, tc) {
|
906 | if (node.kind === ts.SyntaxKind.ClassDeclaration
|
907 | || node.kind === ts.SyntaxKind.InterfaceDeclaration
|
908 | || node.kind === ts.SyntaxKind.EnumDeclaration
|
909 | || node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
|
910 | var symbol = node.symbol;
|
911 | var nodeType = tc.getTypeAtLocation(node);
|
912 | var fullyQualifiedName = tc.getFullyQualifiedName(symbol);
|
913 | var typeName = fullyQualifiedName.replace(/".*"\./, "");
|
914 | var name_1 = !args.uniqueNames ? typeName : typeName + "." + generateHashOfNode(node, relativePath);
|
915 | symbols_1.push({ name: name_1, typeName: typeName, fullyQualifiedName: fullyQualifiedName, symbol: symbol });
|
916 | if (!userSymbols_1[name_1]) {
|
917 | allSymbols_1[name_1] = nodeType;
|
918 | }
|
919 | if (isUserFile(sourceFile)) {
|
920 | userSymbols_1[name_1] = symbol;
|
921 | }
|
922 | var baseTypes = nodeType.getBaseTypes() || [];
|
923 | baseTypes.forEach(function (baseType) {
|
924 | var baseName = tc.typeToString(baseType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
|
925 | if (!inheritingTypes_1[baseName]) {
|
926 | inheritingTypes_1[baseName] = [];
|
927 | }
|
928 | inheritingTypes_1[baseName].push(name_1);
|
929 | });
|
930 | }
|
931 | else {
|
932 | ts.forEachChild(node, function (n) { return inspect(n, tc); });
|
933 | }
|
934 | }
|
935 | inspect(sourceFile, typeChecker_1);
|
936 | });
|
937 | return new JsonSchemaGenerator(symbols_1, allSymbols_1, userSymbols_1, inheritingTypes_1, typeChecker_1, settings);
|
938 | }
|
939 | else {
|
940 | diagnostics.forEach(function (diagnostic) {
|
941 | var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
942 | if (diagnostic.file) {
|
943 | var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
|
944 | console.error(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message);
|
945 | }
|
946 | else {
|
947 | console.error(message);
|
948 | }
|
949 | });
|
950 | return null;
|
951 | }
|
952 | }
|
953 | exports.buildGenerator = buildGenerator;
|
954 | function generateSchema(program, fullTypeName, args, onlyIncludeFiles) {
|
955 | if (args === void 0) { args = {}; }
|
956 | var generator = buildGenerator(program, args, onlyIncludeFiles);
|
957 | if (generator === null) {
|
958 | return null;
|
959 | }
|
960 | if (fullTypeName === "*") {
|
961 | return generator.getSchemaForSymbols(generator.getMainFileSymbols(program, onlyIncludeFiles));
|
962 | }
|
963 | else {
|
964 | return generator.getSchemaForSymbol(fullTypeName);
|
965 | }
|
966 | }
|
967 | exports.generateSchema = generateSchema;
|
968 | function programFromConfig(configFileName, onlyIncludeFiles) {
|
969 | var result = ts.parseConfigFileTextToJson(configFileName, ts.sys.readFile(configFileName));
|
970 | var configObject = result.config;
|
971 | var configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, path.dirname(configFileName), {}, path.basename(configFileName));
|
972 | var options = configParseResult.options;
|
973 | options.noEmit = true;
|
974 | delete options.out;
|
975 | delete options.outDir;
|
976 | delete options.outFile;
|
977 | delete options.declaration;
|
978 | var program = ts.createProgram(onlyIncludeFiles || configParseResult.fileNames, options);
|
979 | return program;
|
980 | }
|
981 | exports.programFromConfig = programFromConfig;
|
982 | function normalizeFileName(fn) {
|
983 | while (fn.substr(0, 2) === "./") {
|
984 | fn = fn.substr(2);
|
985 | }
|
986 | return fn;
|
987 | }
|
988 | function exec(filePattern, fullTypeName, args) {
|
989 | if (args === void 0) { args = getDefaultArgs(); }
|
990 | var _a;
|
991 | var program;
|
992 | var onlyIncludeFiles = undefined;
|
993 | if (REGEX_TSCONFIG_NAME.test(path.basename(filePattern))) {
|
994 | if (args.include && args.include.length > 0) {
|
995 | var globs = args.include.map(function (f) { return glob.sync(f); });
|
996 | onlyIncludeFiles = (_a = []).concat.apply(_a, globs).map(normalizeFileName);
|
997 | }
|
998 | program = programFromConfig(filePattern, onlyIncludeFiles);
|
999 | }
|
1000 | else {
|
1001 | onlyIncludeFiles = glob.sync(filePattern);
|
1002 | program = getProgramFromFiles(onlyIncludeFiles, {
|
1003 | strictNullChecks: args.strictNullChecks
|
1004 | });
|
1005 | onlyIncludeFiles = onlyIncludeFiles.map(normalizeFileName);
|
1006 | }
|
1007 | var definition = generateSchema(program, fullTypeName, args, onlyIncludeFiles);
|
1008 | if (definition === null) {
|
1009 | throw new Error("No output definition. Probably caused by errors prior to this?");
|
1010 | }
|
1011 | var json = stringify(definition, { space: 4 }) + "\n\n";
|
1012 | if (args.out) {
|
1013 | require("fs").writeFile(args.out, json, function (err) {
|
1014 | if (err) {
|
1015 | throw new Error("Unable to write output file: " + err.message);
|
1016 | }
|
1017 | });
|
1018 | }
|
1019 | else {
|
1020 | process.stdout.write(json);
|
1021 | }
|
1022 | }
|
1023 | exports.exec = exec;
|
1024 |
|
\ | No newline at end of file |