@cicctencent/tars2node-cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ 'use strict';
2
+
3
+ const SuperTyping = require('../typing');
4
+
5
+ /**
6
+ *
7
+ * 声明JCE-Struct节点类型
8
+ * @typedef { refType | vecType | string | mapType } tarsType
9
+ *
10
+ * @typedef {object} refType
11
+ * @property {string} target
12
+ * @property {string[]} nested
13
+ *
14
+ * @typedef {object} vecType
15
+ * @property {'vector'} type
16
+ * @property {tarsType} target
17
+ *
18
+ * @typedef {object} mapType
19
+ * @property {'map'} type
20
+ * @property {tarsType} key
21
+ * @property {tarsType} value
22
+ *
23
+ * @typedef {object} TarsStreamType
24
+ * @property {string} tafType
25
+ * @property {*} source
26
+ *
27
+ * @typedef {object} TypingOptions
28
+ * @property {'string'|'number'|'bigint'} long
29
+ */
30
+ class Typing extends SuperTyping {
31
+ /**
32
+ * @typedef {import("../finder")} Finder
33
+ * @constructor
34
+ * @param {Finder} finder
35
+ * @param {TypingOptions} options
36
+ */
37
+ constructor(finder, options) {
38
+ super(finder, options);
39
+ this._tsMaping = new Map();
40
+ this.initTsType();
41
+ }
42
+
43
+ initTsType() {
44
+ let longType = '';
45
+ switch (this._options.long) {
46
+ case 'string':
47
+ longType = 'string';
48
+ break;
49
+ case 'bigint':
50
+ longType = 'bigint';
51
+ break;
52
+ case 'number':
53
+ default:
54
+ longType = 'number';
55
+ }
56
+ this._tsMaping = new Map([
57
+ ['byte', 'number'],
58
+ ['short', 'number'],
59
+ ['int', 'number'],
60
+ ['unsigned int', 'number'],
61
+ ['double', 'number'],
62
+ ['float', 'number'],
63
+ ['long', longType],
64
+ ['string', 'string'],
65
+ ['bool', 'boolean'],
66
+ ]);
67
+ }
68
+
69
+ /**
70
+ * @param {tarsType} type
71
+ * @param {string} current
72
+ * @return {string}
73
+ */
74
+ toTSType(type, current) {
75
+ const index = `TSType#${current}#${JSON.stringify(type)}`;
76
+
77
+ if (!this._memo.has(index)) {
78
+ let result;
79
+
80
+ if (this.isBaseType(type)) result = this._tsMaping.get(type);
81
+ else if (this.isRefType(type)) result = this._refTypeToTSType(type, current);
82
+ else if (this.isVector(type)) {
83
+ if (type.target === 'byte') {
84
+ result = 'TarsStream.BinBuffer';
85
+ } else {
86
+ const member = this.toTSType(type.target, current);
87
+ result = `TarsStream.List<${member}>`;
88
+ }
89
+ } else if (this.isMap(type)) {
90
+ const key = this.toTSType(type.key, current);
91
+ const value = this.toTSType(type.value, current);
92
+ result = `TarsStream.Map<${key}, ${value}>`;
93
+ }
94
+
95
+ this._memo.set(index, /** @type {string} */ (result));
96
+ }
97
+
98
+ return /** @type {string} */ (this._memo.get(index));
99
+ }
100
+
101
+ /**
102
+ * @param {tarsType} type
103
+ * @param {string} current
104
+ * @return {string}
105
+ */
106
+ toTSObjectType(type, current) {
107
+ const index = `TSObjectType#${current}#${JSON.stringify(type)}`;
108
+
109
+ if (!this._memo.has(index)) {
110
+ let result;
111
+
112
+ if (this.isBaseType(type)) result = this._tsMaping.get(type);
113
+ else if (this.isRefType(type)) result = this._refTypeToTSObjectType(type, current);
114
+ else if (this.isVector(type)) {
115
+ if (type.target === 'byte') {
116
+ result = 'Buffer';
117
+ } else {
118
+ const member = this.toTSObjectType(type.target, current);
119
+ result = `Array<${member}>`;
120
+ }
121
+ } else if (this.isMap(type)) {
122
+ const key = this.toTSObjectType(type.key, current);
123
+ const value = this.toTSObjectType(type.value, current);
124
+ result = key === 'string' || key === 'number' ? `{[key: ${key}]: ${value}}` : `{[key in keyof ${key}]: ${value}}`;
125
+ }
126
+
127
+ this._memo.set(index, /** @type {string} */ (result));
128
+ }
129
+
130
+ return /** @type {string} */ (this._memo.get(index));
131
+ }
132
+
133
+ /**
134
+ * @param {tarsType} type
135
+ * @param {string} current
136
+ * @return {TarsStreamType}
137
+ */
138
+ toTarsStreamType(type, current) {
139
+ const index = `TarsStream#${current}#${JSON.stringify(type)}`;
140
+ const prefix = 'TarsStream.';
141
+ if (!this._memo.has(index)) {
142
+ const result = { source: null, tafType: '' };
143
+
144
+ if (this.isVector(type) && type.target !== 'byte') {
145
+ const member = this.toTarsStreamType(type.target, current).tafType;
146
+ result.tafType = `${prefix}List(<any>${member})`;
147
+ } else if (this.isMap(type)) {
148
+ const key = this.toTarsStreamType(type.key, current).tafType;
149
+ const value = this.toTarsStreamType(type.value, current).tafType;
150
+ result.tafType = `${prefix}Map(${key}, ${value})`;
151
+ } else {
152
+ return super.toTarsStreamType(type, current);
153
+ }
154
+
155
+ this._memo.set(index, result);
156
+ }
157
+
158
+ return /** @type {TarsStreamType} */ (this._memo.get(index));
159
+ }
160
+
161
+ /**
162
+ * @private
163
+ * @param {refType} type
164
+ * @param {string} current
165
+ * @return {string}
166
+ */
167
+ _refTypeToTSType(type, current) {
168
+ const { refPath, namespace } = this._getRefPath(type, current);
169
+ if (namespace !== current && namespace) {
170
+ refPath.unshift(`$${namespace}`)
171
+ };
172
+
173
+ return refPath.join('.');
174
+ }
175
+
176
+ /**
177
+ * @private
178
+ * @param {refType} type
179
+ * @param {string} current
180
+ * @return {string}
181
+ */
182
+ _refTypeToTSObjectType(type, current) {
183
+ const tmp = this._refTypeToTSType(type, current);
184
+ const result = this._finder.find(/** @type {refType} */ (type), current);
185
+ if(!result){
186
+ throw new Error(`${current}中的${type.target}未定义!`);
187
+ }
188
+ if (result.type === 'struct') return `${tmp}${this._options.mode !== 'web' ? '$OBJ' : ''}`;
189
+ return tmp;
190
+ }
191
+ }
192
+
193
+ module.exports = Typing;
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+
4
+ function toMultilineComment(comment) {
5
+ let newComment = comment;
6
+ if (newComment) {
7
+ const arr = newComment.split(/\/\//);
8
+ // 把行注释变成段注释
9
+ if (arr.length > 1 && !/\/\*|\*\//.test(newComment)) {
10
+ newComment = `/** ${arr
11
+ .slice(1)
12
+ .join(" ")
13
+ .trim()} */\n\t\t${arr[0].trim()}`;
14
+ }
15
+ }
16
+ return newComment;
17
+ }
18
+
19
+ module.exports = {
20
+ toMultilineComment,
21
+ };
@@ -0,0 +1,351 @@
1
+ 'use strict';
2
+
3
+ const BASE_TYPE_MAPPING = new Map([
4
+ ['byte', 'Int8'],
5
+ ['short', 'Int16'],
6
+ ['int', 'Int32'],
7
+ ['unsigned int', 'Int64'],
8
+ ['enum', 'Int32'],
9
+ ['string', 'String'],
10
+ ['bool', 'Boolean'],
11
+ ['long', 'Int64'],
12
+ ['float', 'Float'],
13
+ ['double', 'Double'],
14
+ ['struct', 'Struct'],
15
+ ['vector', 'List'],
16
+ ['map', 'Map'],
17
+ ['void', 'void'],
18
+ ]);
19
+
20
+ /**
21
+ *
22
+ * 声明JCE-Struct节点类型
23
+ * @typedef { refType | vecType | string | mapType } tarsType
24
+ *
25
+ * @typedef {object} refType
26
+ * @property {string} target
27
+ * @property {string[]} nested
28
+ *
29
+ * @typedef {object} vecType
30
+ * @property {'vector'} type
31
+ * @property {tarsType} target
32
+ *
33
+ * @typedef {object} mapType
34
+ * @property {'map'} type
35
+ * @property {tarsType} key
36
+ * @property {tarsType} value
37
+ *
38
+ * @typedef {object} ByteType
39
+ * @property {'vector'} type
40
+ * @property {'byte'} target
41
+ *
42
+ * @typedef {object} TarsStreamType
43
+ * @property {string} tafType
44
+ * @property {*} source
45
+ *
46
+ * @typedef {object} TypingOptions
47
+ * @property {'string'|'number'|'bigint'} long
48
+ */
49
+ class Typing {
50
+ /**
51
+ * @typedef {import("./finder")} Finder
52
+ * @constructor
53
+ * @param {Finder} finder
54
+ * @param {TypingOptions} options
55
+ */
56
+ constructor(finder, options) {
57
+ this._finder = finder;
58
+ this._options = options;
59
+ /** @type {Map<string, string | TarsStreamType>} */
60
+ this._memo = new Map();
61
+ this._initValue = new Map();
62
+ this._ioValue = new Map();
63
+ this.initTarsType();
64
+ }
65
+
66
+ initTarsType() {
67
+ let longInitValue = '';
68
+ let longIoValue = '';
69
+ switch (this._options.long) {
70
+ case 'string':
71
+ longInitValue = '"0"';
72
+ longIoValue = '"0"';
73
+ break;
74
+ case 'bigint':
75
+ longInitValue = '0n';
76
+ longIoValue = '0n';
77
+ break;
78
+ case 'number':
79
+ default:
80
+ longInitValue = '0';
81
+ longIoValue = '0';
82
+ }
83
+ this._initValue = new Map([
84
+ ['byte', '0'],
85
+ ['short', '0'],
86
+ ['int', '0'],
87
+ ['unsigned int', '0'],
88
+ ['double', '0'],
89
+ ['float', '0'],
90
+ ['bool', 'true'],
91
+ ['string', '""'],
92
+ ['long', longInitValue],
93
+ ]);
94
+ this._ioValue = new Map([
95
+ ['byte', '0'],
96
+ ['short', '0'],
97
+ ['int', '0'],
98
+ ['unsigned int', '0'],
99
+ ['double', '0'],
100
+ ['float', '0'],
101
+ ['bool', 'true'],
102
+ ['string', '""'],
103
+ ['long', longIoValue],
104
+ ]);
105
+ }
106
+
107
+ refresh() {
108
+ this._memo.clear();
109
+ }
110
+
111
+ /**
112
+ * @param {tarsType} type
113
+ * @return {type is string}
114
+ */
115
+ isBaseType(type) { return typeof type === 'string'; }
116
+
117
+ /**
118
+ * @param {tarsType} type
119
+ * @return {type is refType}
120
+ */
121
+ isRefType(type) { return /** @type {refType} */ (type).nested !== undefined; }
122
+
123
+ /**
124
+ * @param {tarsType} type
125
+ * @return {type is vecType}
126
+ */
127
+ isVector(type) { return /** @type {vecType} */ (type).type === 'vector'; }
128
+
129
+ /**
130
+ * @param {tarsType} type
131
+ * @return {type is mapType}
132
+ */
133
+ isMap(type) { return /** @type {mapType} */ (type).type === 'map'; }
134
+
135
+ /**
136
+ * @param {tarsType} type
137
+ * @return {type is ByteType}
138
+ */
139
+ isBytes(type) { return this.isVector(type) && type.target === 'byte'; }
140
+
141
+ /**
142
+ * @param {tarsType} type
143
+ * @param {string} current
144
+ * @return {type is string}
145
+ */
146
+ isNotObjectType(type, current) {
147
+ if (typeof type === 'string') return true;
148
+ if (this.isVector(type)
149
+ || this.isMap(type)
150
+ || this.isBytes(type)) return false;
151
+ const { type: t } = this._finder.find(/** @type {refType} */ (type), current);
152
+ return t !== 'struct';
153
+ }
154
+
155
+ /**
156
+ * @param {tarsType} type
157
+ * @param {string} current
158
+ * @return {string}
159
+ */
160
+ toString(type, current) {
161
+ const index = `String#${current}#${JSON.stringify(type)}`;
162
+
163
+ if (!this._memo.has(index)) {
164
+ let result;
165
+
166
+ if (this.isBaseType(type) && BASE_TYPE_MAPPING.has(type)) {
167
+ result = /** @type {string} */ (BASE_TYPE_MAPPING.get(type)).toLowerCase();
168
+ } else if (this.isRefType(type)) result = this._refTypeToString(type, current);
169
+ else if (this.isVector(type)) {
170
+ const member = this.toString(type.target, current);
171
+ result = `list(${member})`;
172
+ } else if (this.isMap(type)) {
173
+ const key = this.toString(type.key, current);
174
+ const value = this.toString(type.value, current);
175
+ result = `map(${key}, ${value})`;
176
+ } else throw new Error(`Type: ${JSON.stringify(type)} is not a tars type`);
177
+
178
+ this._memo.set(index, result);
179
+ }
180
+
181
+ return /** @type {string} */ (this._memo.get(index));
182
+ }
183
+
184
+ /**
185
+ * @param {tarsType} type
186
+ * @param {string} current
187
+ * @return {TarsStreamType}
188
+ */
189
+ toTarsStreamType(type, current) {
190
+ const index = `TarsStream#${current}#${JSON.stringify(type)}`;
191
+ const prefix = 'TarsStream.';
192
+ if (!this._memo.has(index)) {
193
+ let result = { source: null, tafType: '' };
194
+
195
+ if (this.isBaseType(type)) result.tafType = `${prefix}${BASE_TYPE_MAPPING.get(type)}`;
196
+ else if (this.isRefType(type)) result = this._refTypeToTarsStreamType(type, current);
197
+ else if (this.isVector(type)) {
198
+ if (type.target === 'byte') {
199
+ result.tafType = `${prefix}BinBuffer`;
200
+ } else {
201
+ const member = this.toTarsStreamType(type.target, current).tafType;
202
+ result.tafType = `${prefix}List(${member})`;
203
+ }
204
+ } else if (this.isMap(type)) {
205
+ const key = this.toTarsStreamType(type.key, current).tafType;
206
+ const value = this.toTarsStreamType(type.value, current).tafType;
207
+ result.tafType = `${prefix}Map(${key}, ${value})`;
208
+ }
209
+
210
+ this._memo.set(index, result);
211
+ }
212
+
213
+ return /** @type {TarsStreamType} */ (this._memo.get(index));
214
+ }
215
+
216
+ /**
217
+ * @param {tarsType} type
218
+ * @param {string} current
219
+ * @return {string}
220
+ */
221
+ toIOType(type, current) {
222
+ const index = `IO#${current}#${JSON.stringify(type)}`;
223
+
224
+ if (!this._memo.has(index)) {
225
+ let result = null;
226
+ let key = type;
227
+
228
+ if (type === 'unsigned int') result = 'UInt32';
229
+ else if (this.isRefType(type)) key = this._finder.find(type, current).type;
230
+ else if (this.isBytes(type)) result = 'Bytes';
231
+ else if (this.isVector(type)) key = type.type;
232
+ else if (this.isMap(type)) key = type.type;
233
+
234
+ if (result === null) result = BASE_TYPE_MAPPING.get(/** @type {string} */(key));
235
+
236
+ if (result === null) throw new Error(`Type: ${JSON.stringify(type)} is not a tars type`);
237
+
238
+ this._memo.set(index, /** @type {string} */ (result));
239
+ }
240
+
241
+ return /** @type {string} */ (this._memo.get(index));
242
+ }
243
+
244
+ /**
245
+ * @param {string} type
246
+ * @return {string|undefined}
247
+ */
248
+ toDefaultValue(type) {
249
+ return this._initValue.get(type);
250
+ }
251
+
252
+ /**
253
+ * @param {string} type
254
+ * @return {string|undefined}
255
+ */
256
+ toIOValue(type) {
257
+ return this._ioValue.get(type);
258
+ }
259
+
260
+ /**
261
+ * @param {tarsType} type
262
+ * @param {string} current
263
+ * @param {boolean} [isTup = false]
264
+ * @return {string}
265
+ */
266
+ toTarsValue(type, current, isTup = false) {
267
+ const index = `TarsValue#${current}#${JSON.stringify(type)}`;
268
+
269
+ if (!this._memo.has(index)) {
270
+ let result;
271
+
272
+ if (typeof type === 'string') {
273
+ result = /** @type {string} */ (this.toIOValue(type));
274
+ } else {
275
+ const target = this.toTarsStreamType(type, current);
276
+ if (target.source || /** @type {vecType|mapType} */ (type).type) {
277
+ if (target.source && target.source.type === 'enum') {
278
+ result = `${target.tafType}.${target.source.value[0].id}`;
279
+ } else if (isTup) {
280
+ result = `${target.tafType}, new ${target.tafType}`;
281
+ } else result = target.tafType;
282
+ } else {
283
+ throw new Error(`Cannot convert type: ${type} to string`);
284
+ }
285
+ }
286
+
287
+ this._memo.set(index, /** @type {string} */ (result));
288
+ }
289
+
290
+ return /** @type {string} */ (this._memo.get(index));
291
+ }
292
+
293
+ /**
294
+ * @param {refType} type
295
+ * @param {string} current
296
+ * @return {{refPath: string[], namespace: string}}
297
+ */
298
+ _getRefPath(type, current) {
299
+ const refPath = this._options.useNameSpace ? [...type.nested, type.target] : [type.target];
300
+ let [namespace] = type.nested;
301
+ if (this._options.useNameSpace && !namespace) {
302
+ refPath.unshift(current);
303
+ namespace = current;
304
+ }
305
+ return { refPath, namespace };
306
+ }
307
+
308
+ /**
309
+ * @param {refType} type
310
+ * @param {string} current
311
+ * @return {string}
312
+ */
313
+ _refTypeToString(type, current) {
314
+ const { type: refType } = this._finder.find(type, current);
315
+
316
+ switch (refType) {
317
+ case 'struct': {
318
+ const { refPath } = this._getRefPath(type, current);
319
+ return refPath.join('.');
320
+ }
321
+ case 'enum':
322
+ return 'int32';
323
+ default:
324
+ throw new Error(`Type: ${JSON.stringify(type)} is not a ref type`);
325
+ }
326
+ }
327
+
328
+ /**
329
+ * @param {refType} type
330
+ * @param {string} current
331
+ * @return {TarsStreamType}
332
+ */
333
+ _refTypeToTarsStreamType(type, current) {
334
+ const result = { source: null, tafType: '' };
335
+ const refTarget = this._finder.find(type, current);
336
+
337
+ result.source = refTarget;
338
+
339
+ if (refTarget.type === 'struct' || refTarget.type === 'enum') {
340
+ const { refPath, namespace } = this._getRefPath(type, current);
341
+
342
+ if (namespace !== current) refPath.unshift(`$${namespace}`);
343
+
344
+ result.tafType = refPath.join('.');
345
+ }
346
+
347
+ return result;
348
+ }
349
+ }
350
+
351
+ module.exports = Typing;