1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var typeNameConvertor_1 = require("./typeNameConvertor");
|
5 | var defaultOptions = {
|
6 | indentChar: ' ',
|
7 | indentSize: 4,
|
8 | normalizeTypeName: typeNameConvertor_1.normalizeTypeName,
|
9 | };
|
10 | var WriteProcessor = (function () {
|
11 | function WriteProcessor(options) {
|
12 | this.indent = 0;
|
13 | this.results = '';
|
14 | this.alreadyIndentThisLine = false;
|
15 | this.options = tslib_1.__assign(tslib_1.__assign({}, defaultOptions), options);
|
16 | }
|
17 | WriteProcessor.prototype.clear = function () {
|
18 | this.indent = 0;
|
19 | this.results = '';
|
20 | this.alreadyIndentThisLine = false;
|
21 | };
|
22 | WriteProcessor.prototype.output = function (str) {
|
23 | this.doIndent();
|
24 | this.results += str;
|
25 | return this;
|
26 | };
|
27 | WriteProcessor.prototype.outputType = function (type, primitive) {
|
28 | if (primitive === void 0) { primitive = false; }
|
29 | type = this.options.normalizeTypeName(type, primitive);
|
30 | this.output(type);
|
31 | return this;
|
32 | };
|
33 | WriteProcessor.prototype.outputKey = function (name, optional) {
|
34 | if (optional === void 0) { optional = false; }
|
35 | if (/[^0-9A-Za-z_$]/.test(name) || /^\d/.test(name)) {
|
36 | this.output("\"" + name + "\"");
|
37 | }
|
38 | else {
|
39 | this.output(name);
|
40 | }
|
41 | if (optional) {
|
42 | this.output('?');
|
43 | }
|
44 | return this;
|
45 | };
|
46 | WriteProcessor.prototype.outputLine = function (str) {
|
47 | this.doIndent();
|
48 | if (str) {
|
49 | this.output(str);
|
50 | }
|
51 | this.output('\n');
|
52 | this.alreadyIndentThisLine = false;
|
53 | return this;
|
54 | };
|
55 | WriteProcessor.prototype.protectComment = function (str) {
|
56 | return str.replace(/\*\//g, '*\u200B/');
|
57 | };
|
58 | WriteProcessor.prototype.outputJSDoc = function () {
|
59 | var e_1, _a;
|
60 | var _this = this;
|
61 | var comments = [];
|
62 | for (var _i = 0; _i < arguments.length; _i++) {
|
63 | comments[_i] = arguments[_i];
|
64 | }
|
65 | var lines = [];
|
66 | comments
|
67 | .filter(function (comment) { return comment != null; })
|
68 | .map(function (comment) {
|
69 | if (typeof comment === 'string') {
|
70 | return comment;
|
71 | }
|
72 | else {
|
73 | return JSON.stringify(comment, null, 2);
|
74 | }
|
75 | })
|
76 | .map(function (comment) { return comment.split('\n').map(function (line) { return _this.protectComment(line); }); })
|
77 | .forEach(function (ls) {
|
78 | lines = lines.concat(ls);
|
79 | });
|
80 | if (lines.length > 0) {
|
81 | this.outputLine('/**');
|
82 | try {
|
83 | for (var lines_1 = tslib_1.__values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
84 | var line = lines_1_1.value;
|
85 | this.output(' * ').outputLine(line);
|
86 | }
|
87 | }
|
88 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
89 | finally {
|
90 | try {
|
91 | if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
92 | }
|
93 | finally { if (e_1) throw e_1.error; }
|
94 | }
|
95 | this.outputLine(' */');
|
96 | }
|
97 | return this;
|
98 | };
|
99 | WriteProcessor.prototype.doIndent = function () {
|
100 | if (!this.alreadyIndentThisLine) {
|
101 | var indent = this.getIndent();
|
102 | this.results += indent;
|
103 | this.alreadyIndentThisLine = true;
|
104 | }
|
105 | return this;
|
106 | };
|
107 | Object.defineProperty(WriteProcessor.prototype, "indentLevel", {
|
108 | get: function () {
|
109 | return this.indent;
|
110 | },
|
111 | enumerable: true,
|
112 | configurable: true
|
113 | });
|
114 | WriteProcessor.prototype.increaseIndent = function () {
|
115 | this.indent++;
|
116 | return this;
|
117 | };
|
118 | WriteProcessor.prototype.decreaseIndent = function () {
|
119 | this.indent--;
|
120 | return this;
|
121 | };
|
122 | WriteProcessor.prototype.getIndent = function () {
|
123 | return this.repeatString(this.indent * this.options.indentSize, this.options.indentChar);
|
124 | };
|
125 | WriteProcessor.prototype.repeatString = function (n, s) {
|
126 | var result = '';
|
127 | for (var i = 0; i < n; i++) {
|
128 | result += s;
|
129 | }
|
130 | return result;
|
131 | };
|
132 | WriteProcessor.prototype.toDefinition = function () {
|
133 | return this.results;
|
134 | };
|
135 | return WriteProcessor;
|
136 | }());
|
137 | exports.default = WriteProcessor;
|
138 |
|
\ | No newline at end of file |