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({}, 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 _this = this;
|
60 | var comments = [];
|
61 | for (var _i = 0; _i < arguments.length; _i++) {
|
62 | comments[_i] = arguments[_i];
|
63 | }
|
64 | var lines = [];
|
65 | comments
|
66 | .filter(function (comment) { return comment != null; })
|
67 | .map(function (comment) { return comment.toString().split('\n').map(function (line) { return _this.protectComment(line); }); })
|
68 | .forEach(function (ls) {
|
69 | lines = lines.concat(ls);
|
70 | });
|
71 | if (lines.length > 0) {
|
72 | this.outputLine('/**');
|
73 | try {
|
74 | for (var lines_1 = tslib_1.__values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) {
|
75 | var line = lines_1_1.value;
|
76 | this.output(' * ').outputLine(line);
|
77 | }
|
78 | }
|
79 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
80 | finally {
|
81 | try {
|
82 | if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
83 | }
|
84 | finally { if (e_1) throw e_1.error; }
|
85 | }
|
86 | this.outputLine(' */');
|
87 | }
|
88 | return this;
|
89 | var e_1, _a;
|
90 | };
|
91 | WriteProcessor.prototype.doIndent = function () {
|
92 | if (!this.alreadyIndentThisLine) {
|
93 | var indent = this.getIndent();
|
94 | this.results += indent;
|
95 | this.alreadyIndentThisLine = true;
|
96 | }
|
97 | return this;
|
98 | };
|
99 | Object.defineProperty(WriteProcessor.prototype, "indentLevel", {
|
100 | get: function () {
|
101 | return this.indent;
|
102 | },
|
103 | enumerable: true,
|
104 | configurable: true
|
105 | });
|
106 | WriteProcessor.prototype.increaseIndent = function () {
|
107 | this.indent++;
|
108 | return this;
|
109 | };
|
110 | WriteProcessor.prototype.decreaseIndent = function () {
|
111 | this.indent--;
|
112 | return this;
|
113 | };
|
114 | WriteProcessor.prototype.getIndent = function () {
|
115 | return this.repeatString(this.indent * this.options.indentSize, this.options.indentChar);
|
116 | };
|
117 | WriteProcessor.prototype.repeatString = function (n, s) {
|
118 | var result = '';
|
119 | for (var i = 0; i < n; i++) {
|
120 | result += s;
|
121 | }
|
122 | return result;
|
123 | };
|
124 | WriteProcessor.prototype.toDefinition = function () {
|
125 | return this.results;
|
126 | };
|
127 | return WriteProcessor;
|
128 | }());
|
129 | exports.default = WriteProcessor;
|