UNPKG

3.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const serializer_1 = require("../serializer");
4class DTsSerializer {
5 constructor(_writer, interfaceName, _indentDelta = 4) {
6 this._writer = _writer;
7 this.interfaceName = interfaceName;
8 this._indentDelta = _indentDelta;
9 this._state = [];
10 if (interfaceName) {
11 _writer(`export interface ${interfaceName} `);
12 }
13 else {
14 _writer('interface _ ');
15 }
16 }
17 _willOutputValue() {
18 if (this._state.length > 0) {
19 const top = this._top();
20 top.empty = false;
21 if (!top.property) {
22 this._indent();
23 }
24 }
25 }
26 _top() {
27 return this._state[this._state.length - 1] || {};
28 }
29 _indent() {
30 if (this._indentDelta == 0) {
31 return;
32 }
33 let str = '\n';
34 let i = this._state.length * this._indentDelta;
35 while (i--) {
36 str += ' ';
37 }
38 this._writer(str);
39 }
40 start() { }
41 end() {
42 if (this._indentDelta) {
43 this._writer('\n');
44 }
45 if (!this.interfaceName) {
46 this._writer('export default _;\n');
47 }
48 }
49 object(node) {
50 this._willOutputValue();
51 this._writer('{');
52 this._state.push({ empty: true, type: 'object' });
53 for (const key of Object.keys(node.children)) {
54 this.property(node.children[key]);
55 }
56 // Fallback to direct value output for additional properties.
57 if (!node.frozen) {
58 this._indent();
59 this._writer('[name: string]: any;');
60 }
61 this._state.pop();
62 if (!this._top().empty) {
63 this._indent();
64 }
65 this._writer('}');
66 }
67 property(node) {
68 this._willOutputValue();
69 if (node.description) {
70 this._writer('/**');
71 this._indent();
72 node.description.split('\n').forEach(line => {
73 this._writer(' * ' + line);
74 this._indent();
75 });
76 this._writer(' */');
77 this._indent();
78 }
79 this._writer(node.name);
80 if (!node.required) {
81 this._writer('?');
82 }
83 this._writer(': ');
84 this._top().property = true;
85 node.serialize(this);
86 this._top().property = false;
87 this._writer(';');
88 }
89 array(node) {
90 this._willOutputValue();
91 node.itemPrototype.serialize(this);
92 this._writer('[]');
93 }
94 outputOneOf(node) {
95 this._willOutputValue();
96 if (!node.items) {
97 throw new serializer_1.InvalidStateError();
98 }
99 this._writer('(');
100 for (let i = 0; i < node.items.length; i++) {
101 node.items[i].serialize(this);
102 if (i != node.items.length - 1) {
103 this._writer(' | ');
104 }
105 }
106 this._writer(')');
107 }
108 outputEnum(node) {
109 this._willOutputValue();
110 this._writer('(');
111 for (let i = 0; i < node.items.length; i++) {
112 this._writer(JSON.stringify(node.items[i]));
113 if (i != node.items.length - 1) {
114 this._writer(' | ');
115 }
116 }
117 this._writer(')');
118 }
119 outputValue(_node) {
120 this._willOutputValue();
121 this._writer('any');
122 }
123 outputString(_node) {
124 this._willOutputValue();
125 this._writer('string');
126 }
127 outputNumber(_node) {
128 this._willOutputValue();
129 this._writer('number');
130 }
131 outputInteger(_node) {
132 this._willOutputValue();
133 this._writer('number');
134 }
135 outputBoolean(_node) {
136 this._willOutputValue();
137 this._writer('boolean');
138 }
139}
140exports.DTsSerializer = DTsSerializer;
141//# sourceMappingURL=/users/hansl/sources/angular-cli/src/serializers/dts.js.map
\No newline at end of file