@cloudbase/cals 0.5.19 → 1.0.1-2.beta.0

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.
Files changed (50) hide show
  1. package/lib/parser/cals/index.d.ts +63 -3
  2. package/lib/parser/cals/index.d.ts.map +1 -1
  3. package/lib/parser/cals/index.js +240 -91
  4. package/lib/parser/cals/utils/block/index.d.ts.map +1 -1
  5. package/lib/parser/cals/utils/block/index.js +7 -3
  6. package/lib/parser/cals/utils/code/index.d.ts.map +1 -1
  7. package/lib/parser/cals/utils/code/index.js +18 -12
  8. package/lib/parser/cals/utils/runtime.d.ts.map +1 -1
  9. package/lib/parser/cals/utils/runtime.js +5 -2
  10. package/lib/parser/cals/utils/style.d.ts.map +1 -1
  11. package/lib/parser/cals/utils/style.js +7 -3
  12. package/lib/parser/cals/utils/template.js +2 -2
  13. package/lib/parser/cals/utils/version/common.d.ts.map +1 -1
  14. package/lib/parser/cals/utils/version/common.js +16 -33
  15. package/lib/parser/cals/utils/version/parses.d.ts.map +1 -1
  16. package/lib/parser/cals/utils/version/parses.js +18 -36
  17. package/lib/parser/cals/utils/version/utils.js +5 -2
  18. package/lib/parser/plugins/postcss-rpx2clac.js +2 -2
  19. package/lib/types/basic/common.d.ts +1 -1
  20. package/lib/types/basic/common.d.ts.map +1 -1
  21. package/lib/types/basic/component.d.ts +5 -0
  22. package/lib/types/basic/component.d.ts.map +1 -1
  23. package/lib/types/index.d.ts +2 -1
  24. package/lib/types/index.d.ts.map +1 -1
  25. package/lib/types/index.js +14 -1
  26. package/lib/types/lcds.d.ts +400 -114
  27. package/lib/types/lcds.d.ts.map +1 -1
  28. package/lib/types/platform/app.d.ts +4 -0
  29. package/lib/types/platform/app.d.ts.map +1 -1
  30. package/lib/types/platform/common.d.ts +11 -1
  31. package/lib/types/platform/common.d.ts.map +1 -1
  32. package/lib/types/platform/component.d.ts +6 -0
  33. package/lib/types/platform/component.d.ts.map +1 -1
  34. package/lib/types/platform/eventFlow.d.ts +20 -0
  35. package/lib/types/platform/eventFlow.d.ts.map +1 -0
  36. package/lib/types/platform/eventFlow.js +2 -0
  37. package/lib/types/platform/query.d.ts +67 -0
  38. package/lib/types/platform/query.d.ts.map +1 -0
  39. package/lib/types/platform/query.js +2 -0
  40. package/lib/utils/build.js +3 -3
  41. package/lib/utils/dts/auto-generated.d.ts +1 -1
  42. package/lib/utils/dts/auto-generated.d.ts.map +1 -1
  43. package/lib/utils/dts/auto-generated.js +1308 -37
  44. package/lib/utils/dts/build.js +22 -6
  45. package/lib/utils/dts/index.d.ts +46 -5
  46. package/lib/utils/dts/index.d.ts.map +1 -1
  47. package/lib/utils/dts/index.js +478 -68
  48. package/lib/utils/version/featureChecker.js +2 -2
  49. package/lib/utils/version/migrations/version4.js +2 -2
  50. package/package.json +2 -1
@@ -8,13 +8,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.compileJSONToDTS = exports.jsonToDTSWorkerCode = exports.transpileToDTS = exports.workerCode = exports.generateWeDaAppDTS = exports.WeDaDTS = exports.IInputDataType = void 0;
16
+ const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
17
+ const pick_1 = __importDefault(require("lodash/pick"));
18
+ const set_1 = __importDefault(require("lodash/set"));
19
+ const json_easy_filter_1 = __importDefault(require("json-easy-filter"));
13
20
  const auto_generated_1 = require("./auto-generated");
14
21
  var IInputDataType;
15
22
  (function (IInputDataType) {
16
23
  IInputDataType["globalState"] = "globalState";
24
+ IInputDataType["globalQuery"] = "globalQuery";
17
25
  IInputDataType["pageState"] = "pageState";
26
+ IInputDataType["pageQuery"] = "pageQuery";
18
27
  IInputDataType["compState"] = "compState";
19
28
  IInputDataType["pageParams"] = "pageParams";
20
29
  IInputDataType["forState"] = "forState";
@@ -26,10 +35,13 @@ var IInputDataType;
26
35
  IInputDataType["usedComponents"] = "usedComponents";
27
36
  IInputDataType["usedWidgets"] = "usedWidgets";
28
37
  IInputDataType["repeaterScope"] = "repeaterScope";
38
+ IInputDataType["redefineAnyType"] = "redefineAnyType";
29
39
  })(IInputDataType = exports.IInputDataType || (exports.IInputDataType = {}));
30
40
  const DtsPlaceholder = {
31
41
  [IInputDataType.globalState]: "// Global State 全局变量 - Don't touch me",
42
+ [IInputDataType.globalQuery]: "// Global Query 全局查询 - Don't touch me",
32
43
  [IInputDataType.pageState]: "// Page State 页面变量 - Don't touch me",
44
+ [IInputDataType.pageQuery]: "// Page Query 页面查询 - Don't touch me",
33
45
  [IInputDataType.compState]: "// Comp State 组件变量 - Don't touch me",
34
46
  [IInputDataType.pageParams]: "// Page Param 页面参数 - Don't touch me",
35
47
  [IInputDataType.forState]: "// For 循环变量 - Don't touch me",
@@ -40,23 +52,38 @@ const DtsPlaceholder = {
40
52
  [IInputDataType.compPropEvents]: "// Comp Prop Events 组件事件 - Don't touch me",
41
53
  [IInputDataType.usedComponents]: "// Used Components - Don't touch me",
42
54
  [IInputDataType.usedWidgets]: "// used widgets - Don't touch me",
43
- [IInputDataType.repeaterScope]: "// repeater scope - Don't touch me"
55
+ [IInputDataType.repeaterScope]: "// repeater scope - Don't touch me",
56
+ [IInputDataType.redefineAnyType]: "// Redefine Any Type - Don't touch me",
44
57
  };
45
58
  const DtsInnerSign = {
46
- [IInputDataType.globalState]: "Global State Inner",
47
- [IInputDataType.pageState]: "Page State Inner",
48
- [IInputDataType.compState]: "Comp State Inner",
49
- [IInputDataType.pageParams]: "Page Param Inner",
50
- [IInputDataType.forState]: "Global For Inner",
51
- [IInputDataType.globalCommonCodes]: "Global Common Inner",
52
- [IInputDataType.pageHandlerCodes]: "Page Handler Inner",
53
- [IInputDataType.compHandlerCodes]: "Comp Handler Inner",
54
- [IInputDataType.compPropData]: "Comp Prop Data Inner",
55
- [IInputDataType.compPropEvents]: "Comp Prop Events Inner",
56
- [IInputDataType.usedComponents]: "Used Components Inner",
57
- [IInputDataType.usedWidgets]: "used widgets Inner",
58
- [IInputDataType.repeaterScope]: "repeater scope Inner"
59
+ [IInputDataType.globalState]: 'Global State Inner',
60
+ [IInputDataType.globalQuery]: 'Global Query Inner',
61
+ [IInputDataType.pageState]: 'Page State Inner',
62
+ [IInputDataType.pageQuery]: 'Page Query Inner',
63
+ [IInputDataType.compState]: 'Comp State Inner',
64
+ [IInputDataType.pageParams]: 'Page Param Inner',
65
+ [IInputDataType.forState]: 'Global For Inner',
66
+ [IInputDataType.globalCommonCodes]: 'Global Common Inner',
67
+ [IInputDataType.pageHandlerCodes]: 'Page Handler Inner',
68
+ [IInputDataType.compHandlerCodes]: 'Comp Handler Inner',
69
+ [IInputDataType.compPropData]: 'Comp Prop Data Inner',
70
+ [IInputDataType.compPropEvents]: 'Comp Prop Events Inner',
71
+ [IInputDataType.usedComponents]: 'Used Components Inner',
72
+ [IInputDataType.usedWidgets]: 'used widgets Inner',
73
+ [IInputDataType.repeaterScope]: 'repeater scope Inner',
74
+ [IInputDataType.redefineAnyType]: 'Redefine Any Type Inner',
59
75
  };
76
+ var GroupEnum;
77
+ (function (GroupEnum) {
78
+ GroupEnum["GLOBAL_VAR"] = "globalVar";
79
+ GroupEnum["PAGE_VAR"] = "pageVar";
80
+ GroupEnum["GLOBAL_QUERY"] = "globalQuery";
81
+ GroupEnum["PAGE_QUERY"] = "pageQuery";
82
+ GroupEnum["PAGE_PARAM"] = "pageParam";
83
+ GroupEnum["COMPONENT"] = "component";
84
+ GroupEnum["REPEATER"] = "repeater";
85
+ GroupEnum["DATA_FIELD"] = "dataField";
86
+ })(GroupEnum || (GroupEnum = {}));
60
87
  /**
61
88
  * 通过该类可以共享同个dts。
62
89
  * 场景:在 weda 中,低代码的变更会通知更改 dts,而低码编辑器和表达式编辑器对 dts 的需求是不一样的,这样不同的 dts 共享的是同一份全的 dts。如果直接调用 generateWeDaAppDTS 会导致 dts 重复地解析
@@ -75,19 +102,25 @@ class WeDaDTS {
75
102
  */
76
103
  static updateDTSPartial(inputData, itemType, options = { setEmptyIfError: false }) {
77
104
  return __awaiter(this, void 0, void 0, function* () {
78
- let dts = '';
79
105
  try {
80
- dts = yield _generateOneItemDTS(inputData, itemType);
106
+ const [dts, replaceRules] = yield _generateOneItemDTS(inputData, itemType);
107
+ replaceRules.forEach((rule) => {
108
+ WeDaDTS._dts = WeDaDTS._dts.replace(rule.from, rule.to);
109
+ });
81
110
  WeDaDTS._dts = WeDaDTS._dts.replace(new RegExp(`^(\\s*\\/\\/ ${DtsInnerSign[itemType]} Begin)[\\s\\S]*(^\\s*\\/\\/ ${DtsInnerSign[itemType]} End)`, 'm'), `$1\n${dts}\n$2`);
82
111
  }
83
112
  catch (e) {
84
113
  if (options.setEmptyIfError) {
85
- WeDaDTS._dts = WeDaDTS._dts.replace(new RegExp(`^(\\s*\\/\\/ ${DtsInnerSign[itemType]} Begin)[\\s\\S]*(^\\s*\\/\\/ ${DtsInnerSign[itemType]} End)`, 'm'), `$1\n${dts}\n$2`);
114
+ WeDaDTS._dts = WeDaDTS._dts.replace(new RegExp(`^(\\s*\\/\\/ ${DtsInnerSign[itemType]} Begin)[\\s\\S]*(^\\s*\\/\\/ ${DtsInnerSign[itemType]} End)`, 'm'), `$1\n${''}\n$2`);
86
115
  }
87
116
  }
88
117
  });
89
118
  }
90
- static getDTS(options = { needGlobalFormula: false, needGlobalFor: false, needRepeaterScope: false }) {
119
+ static getDTS(options = {
120
+ needGlobalFormula: false,
121
+ needGlobalFor: false,
122
+ needRepeaterScope: false,
123
+ }) {
91
124
  let resultDts = WeDaDTS._dts;
92
125
  if (!options.needGlobalFormula) {
93
126
  resultDts = resultDts.replace(/^\/\/ Global Formula Begin[\s\S]*^\/\/ Global Formula End$/m, '');
@@ -114,8 +147,11 @@ function generateWeDaAppDTS(inputData = {}, options = { isCompMode: false, needG
114
147
  if (placeholder) {
115
148
  // 防止某项错误导致全部都错误
116
149
  try {
117
- const dts = yield _generateOneItemDTS(inputData[key], key);
118
- resultDts = resultDts.replace(placeholder, dts + placeholder);
150
+ const [dts, replaceRules] = yield _generateOneItemDTS(inputData[key], key);
151
+ replaceRules.forEach((rule) => {
152
+ resultDts = resultDts.replace(rule.from, rule.to);
153
+ });
154
+ resultDts = resultDts.replace(placeholder, dts + '\n' + placeholder);
119
155
  }
120
156
  catch (e) {
121
157
  console.error(e);
@@ -141,17 +177,235 @@ function generateWeDaAppDTS(inputData = {}, options = { isCompMode: false, needG
141
177
  if (!(options === null || options === void 0 ? void 0 : options.needRepeaterScope)) {
142
178
  resultDts = resultDts.replace(/\/\/ repeater scope Inner Begin[\s\S]*\/\/ repeater scope Inner End$/m, '');
143
179
  }
144
- // console.log(resultDts)
145
180
  return resultDts;
146
181
  });
147
182
  }
148
183
  exports.generateWeDaAppDTS = generateWeDaAppDTS;
184
+ /**
185
+ * 生成 weda 专有的注释信息
186
+ */
187
+ function genWeDaCodeComment({ fieldName, title, description, type, dataFieldInfo, group, schema, subGroup, baseClass, datasource, }) {
188
+ const desc = title || fieldName ? `${title || fieldName}\n${description || ''}` : '';
189
+ return (desc +
190
+ `\n@privateForWeDa\n${JSON.stringify({
191
+ group,
192
+ displayType: type,
193
+ displayName: title || '',
194
+ schema,
195
+ subGroup,
196
+ baseClass,
197
+ datasource,
198
+ dataFieldInfo,
199
+ })}`);
200
+ }
149
201
  /**
150
202
  * 生成指定类型的 DTS
151
203
  */
152
204
  function _generateOneItemDTS(inputData, itemType) {
153
205
  return __awaiter(this, void 0, void 0, function* () {
206
+ const replaceRules = [];
207
+ /**
208
+ * 获取数据字段的类型
209
+ */
210
+ function getDataFieldDisplayType(format) {
211
+ const nameMap = {
212
+ email: 'email',
213
+ url: 'url',
214
+ tel: 'tel',
215
+ phone: 'phone',
216
+ 'x-image': 'image',
217
+ 'x-rtf': 'rtf',
218
+ datetime: 'datetime',
219
+ date: 'date',
220
+ time: 'time',
221
+ 'x-enum': 'enum',
222
+ 'x-location': 'location',
223
+ 'x-file': 'file',
224
+ related: 'related',
225
+ 'father-son': 'father-son',
226
+ 'x-autonumber': 'autonumber',
227
+ 'x-area-code': 'area-code',
228
+ function: 'function',
229
+ 'one-one': 'one-one',
230
+ 'one-many': 'one-many',
231
+ 'many-one': 'many-one',
232
+ 'many-many': 'many-many'
233
+ };
234
+ return nameMap[format];
235
+ }
236
+ /**
237
+ * 处理注释信息
238
+ */
239
+ function _handleComment(inputData, group, needKeepSchema) {
240
+ const _inputData = (0, cloneDeep_1.default)(inputData);
241
+ json_easy_filter_1.default.traverse(_inputData, function (key, value, path) {
242
+ var _a;
243
+ if ((value === null || value === void 0 ? void 0 : value.type) && !((_a = value === null || value === void 0 ? void 0 : value.type) === null || _a === void 0 ? void 0 : _a.type)) {
244
+ // value?.type?.type 有值,则证明 type 是自定义的字段,而非 JSONSchema 中的 type
245
+ let schema = null;
246
+ if (needKeepSchema) {
247
+ const isFirstLevel = path.length === 2 && (path === null || path === void 0 ? void 0 : path[0]) === 'properties';
248
+ if (isFirstLevel) {
249
+ // 只需要第一级属性需要保存,且只保存子信息(对象或数组的子项)
250
+ schema = value.properties || value.items ? value : null;
251
+ }
252
+ }
253
+ // 特殊处理数据字段的类型
254
+ let type = value.type;
255
+ if (group === GroupEnum.DATA_FIELD) {
256
+ type = getDataFieldDisplayType(value.format) || type;
257
+ }
258
+ value.description = genWeDaCodeComment({
259
+ fieldName: key,
260
+ title: value.title,
261
+ description: value.description,
262
+ group,
263
+ type,
264
+ dataFieldInfo: (0, pick_1.default)(value, ['type', 'format', 'x-system']),
265
+ schema,
266
+ });
267
+ }
268
+ });
269
+ _inputData.description = ''; // 避免第一层出现注释
270
+ return _inputData;
271
+ }
272
+ /**
273
+ * 获取用于显示的数据类型(仅有 number, string , boolean, array, object 等内置基础类型)
274
+ */
275
+ function getDisplayReturnType(typeStr) {
276
+ let displayType = typeStr.trim();
277
+ if (displayType.endsWith('[]')) {
278
+ displayType = 'array';
279
+ }
280
+ else if (displayType.endsWith('}')) {
281
+ displayType = 'object';
282
+ }
283
+ return displayType;
284
+ }
285
+ /**
286
+ * 获取组件接口定义名称
287
+ */
288
+ function _genCompInterfaceName(module, component) {
289
+ return `${module.replace(/-/g, '_')}_${component}`;
290
+ }
291
+ function _compile(jsonschema, isObjNeedWrapper = false) {
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ let dts = yield compileJSONToDTS(jsonschema);
294
+ if (dts.search(/^export interface IWeDa/) >= 0) {
295
+ dts = dts.replace(/^export interface.*$/m, '');
296
+ dts = dts.substring(0, dts.length - 2);
297
+ if (isObjNeedWrapper) {
298
+ dts = `{${dts}}`;
299
+ }
300
+ }
301
+ else {
302
+ dts = dts.replace(/^export type IWeDa =/m, '');
303
+ }
304
+ return dts;
305
+ });
306
+ }
307
+ function handleQuery(inputData, group) {
308
+ return __awaiter(this, void 0, void 0, function* () {
309
+ const dts = yield Promise.all(Object.keys(inputData === null || inputData === void 0 ? void 0 : inputData.properties).map((queryName) => __awaiter(this, void 0, void 0, function* () {
310
+ const description = inputData === null || inputData === void 0 ? void 0 : inputData.properties[queryName].description;
311
+ inputData === null || inputData === void 0 ? true : delete inputData.properties[queryName].description;
312
+ const handledInputData = _handleComment(inputData === null || inputData === void 0 ? void 0 : inputData.properties[queryName], group, false);
313
+ const _dts = yield _compile(handledInputData);
314
+ return `
315
+ /**
316
+ ${genWeDaCodeComment({
317
+ fieldName: queryName,
318
+ title: queryName,
319
+ description: description,
320
+ group,
321
+ type: 'query',
322
+ schema: {
323
+ type: 'object',
324
+ properties: {
325
+ error: {
326
+ type: 'object',
327
+ title: '错误对象',
328
+ properties: {}
329
+ },
330
+ data: Object.assign(Object.assign({}, inputData === null || inputData === void 0 ? void 0 : inputData.properties[queryName]), { title: '查询结果数据' })
331
+ }
332
+ }
333
+ })
334
+ .split('\n')
335
+ .map((item) => `* ${item}`)
336
+ .join('\n')}
337
+ */
338
+ const ${queryName}: {
339
+ /**
340
+ ${genWeDaCodeComment({
341
+ type: 'function',
342
+ title: '触发查询'
343
+ })
344
+ .split('\n')
345
+ .map((item) => `* ${item}`)
346
+ .join('\n')}
347
+ */
348
+ trigger: () => {}
349
+ /**
350
+ ${genWeDaCodeComment({
351
+ type: 'object',
352
+ title: '错误对象'
353
+ })
354
+ .split('\n')
355
+ .map((item) => `* ${item}`)
356
+ .join('\n')}
357
+ */
358
+ error: Error
359
+ /**
360
+ ${genWeDaCodeComment({
361
+ type: 'object',
362
+ title: '查询结果数据'
363
+ })
364
+ .split('\n')
365
+ .map((item) => `* ${item}`)
366
+ .join('\n')}
367
+ */
368
+ data: {
369
+ ${_dts}
370
+ }
371
+ }
372
+ `;
373
+ })));
374
+ return dts.join('\n');
375
+ });
376
+ }
154
377
  const hanlderMap = {
378
+ [IInputDataType.globalState]: function (inputData) {
379
+ return __awaiter(this, void 0, void 0, function* () {
380
+ // 完善注释信息
381
+ const handledInputData = _handleComment(inputData, GroupEnum.GLOBAL_VAR, true);
382
+ return yield _compile(handledInputData);
383
+ });
384
+ },
385
+ [IInputDataType.globalQuery]: function (inputData) {
386
+ return __awaiter(this, void 0, void 0, function* () {
387
+ return handleQuery(inputData, GroupEnum.GLOBAL_QUERY);
388
+ });
389
+ },
390
+ [IInputDataType.pageState]: function (inputData) {
391
+ return __awaiter(this, void 0, void 0, function* () {
392
+ // 完善注释信息
393
+ const handledInputData = _handleComment(inputData, GroupEnum.PAGE_VAR, true);
394
+ return yield _compile(handledInputData);
395
+ });
396
+ },
397
+ [IInputDataType.pageQuery]: function (inputData) {
398
+ return __awaiter(this, void 0, void 0, function* () {
399
+ return handleQuery(inputData, GroupEnum.PAGE_QUERY);
400
+ });
401
+ },
402
+ [IInputDataType.pageParams]: function (inputData) {
403
+ return __awaiter(this, void 0, void 0, function* () {
404
+ // 完善注释信息
405
+ const handledInputData = _handleComment(inputData, GroupEnum.PAGE_PARAM);
406
+ return yield _compile(handledInputData);
407
+ });
408
+ },
155
409
  [IInputDataType.globalCommonCodes]: function (inputData) {
156
410
  return __awaiter(this, void 0, void 0, function* () {
157
411
  const dts = yield Promise.all(inputData.map((item) => __awaiter(this, void 0, void 0, function* () {
@@ -195,17 +449,36 @@ function _generateOneItemDTS(inputData, itemType) {
195
449
  },
196
450
  [IInputDataType.compPropEvents]: function (inputData) {
197
451
  const IData = `any`;
198
- return inputData.map(eventName => {
452
+ return inputData
453
+ .map((eventName) => {
199
454
  return `function ${eventName}(data?: ${IData}): void`;
200
- }).join('\n');
455
+ })
456
+ .join('\n');
201
457
  },
202
458
  [IInputDataType.usedComponents]: function (inputData) {
203
459
  return __awaiter(this, void 0, void 0, function* () {
460
+ // 组件系统属性(在 framework/weda-framework.d.ts 的 UserWidget 中筛选一部分)
461
+ const commonProperties = {
462
+ id: {
463
+ type: 'string',
464
+ title: '组件 ID',
465
+ describe: '组件 ID,微搭中一般由编辑器生成',
466
+ },
467
+ module: {
468
+ type: 'string',
469
+ title: '组件库名',
470
+ },
471
+ component: {
472
+ type: 'string',
473
+ title: '组件名',
474
+ },
475
+ };
204
476
  const results = yield Promise.all(inputData.map((item) => __awaiter(this, void 0, void 0, function* () {
205
477
  // 属性
206
478
  let exportPropertiesDTS = '';
207
479
  if (item.exportProperties) {
208
- exportPropertiesDTS = yield _compile(item.exportProperties);
480
+ const handledProperties = _handleComment(item.exportProperties);
481
+ exportPropertiesDTS = yield _compile(handledProperties);
209
482
  }
210
483
  // 方法
211
484
  let exportMethodsDTS = '';
@@ -238,8 +511,25 @@ function _generateOneItemDTS(inputData, itemType) {
238
511
  // ${exportMethodsDTS}
239
512
  // }
240
513
  // `
514
+ const interfaceName = _genCompInterfaceName(item.module, item.component);
515
+ if (!item.exportProperties) {
516
+ item.exportProperties = {
517
+ type: 'object',
518
+ properties: {},
519
+ };
520
+ }
521
+ item.exportProperties.properties = Object.assign({}, commonProperties, item.exportProperties.properties);
241
522
  return `
242
- declare class ${_genCompInterfaceName(item.module, item.component)} extends UserWidget {
523
+ /**
524
+ ${genWeDaCodeComment({
525
+ fieldName: interfaceName,
526
+ schema: item.exportProperties,
527
+ })
528
+ .split('\n')
529
+ .map((item) => `* ${item}`)
530
+ .join('\n')}
531
+ */
532
+ declare class ${interfaceName} extends UserWidget {
243
533
 
244
534
  ${exportPropertiesDTS}
245
535
 
@@ -251,21 +541,162 @@ function _generateOneItemDTS(inputData, itemType) {
251
541
  });
252
542
  },
253
543
  [IInputDataType.usedWidgets]: function (inputData) {
254
- return inputData.map(item => {
544
+ return inputData
545
+ .map((item) => {
255
546
  if (item.idList && item.idList.length > 0) {
256
547
  const interfaceName = _genCompInterfaceName(item.module, item.component);
257
- return item.idList.map(id => `const ${id}: ${interfaceName}`).join('\n');
548
+ return item.idList
549
+ .map((id) => `
550
+ /**
551
+ ${genWeDaCodeComment({
552
+ fieldName: id,
553
+ title: '',
554
+ description: '',
555
+ group: GroupEnum.COMPONENT,
556
+ type: 'component',
557
+ baseClass: interfaceName,
558
+ datasource: `$${id}_start$ $${id}_end$`,
559
+ })
560
+ .split('\n')
561
+ .map((item) => `* ${item}`)
562
+ .join('\n')}
563
+ */
564
+ const ${id}: ${interfaceName}`)
565
+ .join('\n');
258
566
  }
259
567
  return '';
260
- }).join('\n');
568
+ })
569
+ .join('\n');
261
570
  },
262
571
  [IInputDataType.repeaterScope]: function (inputData) {
263
572
  return __awaiter(this, void 0, void 0, function* () {
264
573
  const results = yield Promise.all(inputData.map((item) => __awaiter(this, void 0, void 0, function* () {
265
- let itemDataDTS = yield _compile(item.itemData, true);
574
+ let itemDataDTS = '';
575
+ if (item.itemData.title) {
576
+ // 特殊场景:数据字段
577
+ const handledInputData = _handleComment(item.itemData, GroupEnum.DATA_FIELD);
578
+ itemDataDTS = yield _compile(handledInputData, true);
579
+ }
580
+ else {
581
+ // 普通的循环项数据
582
+ itemDataDTS = yield _compile(item.itemData, true);
583
+ }
266
584
  return `
585
+ /**
586
+ ${genWeDaCodeComment({
587
+ fieldName: item.currentItemAlias,
588
+ title: '当前项值',
589
+ description: '',
590
+ group: GroupEnum.REPEATER,
591
+ type: getDisplayReturnType(itemDataDTS),
592
+ schema: item.itemData,
593
+ subGroup: item.widgetId,
594
+ })
595
+ .split('\n')
596
+ .map((item) => `* ${item}`)
597
+ .join('\n')}
598
+ */
267
599
  const ${item.currentItemAlias}: ${itemDataDTS}
600
+ /**
601
+ ${genWeDaCodeComment({
602
+ fieldName: item.currentIndexAlias,
603
+ title: '当前下标',
604
+ description: '',
605
+ group: GroupEnum.REPEATER,
606
+ type: 'number',
607
+ subGroup: item.widgetId,
608
+ })
609
+ .split('\n')
610
+ .map((item) => `* ${item}`)
611
+ .join('\n')}
612
+ */
268
613
  const ${item.currentIndexAlias}: number
614
+ `;
615
+ })));
616
+ return results.join('\n');
617
+ });
618
+ },
619
+ [IInputDataType.redefineAnyType]: function (inputData) {
620
+ return __awaiter(this, void 0, void 0, function* () {
621
+ const results = yield Promise.all(inputData.map((item) => __awaiter(this, void 0, void 0, function* () {
622
+ const compInterfaceName = _genCompInterfaceName(item.module, item.component);
623
+ const widgetInterfaceName = `${item.widgetId}_${compInterfaceName}`;
624
+ /**
625
+ * 更新 widget 的类型名
626
+ */
627
+ replaceRules.push({
628
+ from: new RegExp(`(const ${item.widgetId}): .*`),
629
+ to: `$1: ${widgetInterfaceName}`,
630
+ });
631
+ /**
632
+ * 增加类
633
+ */
634
+ const datasources = {};
635
+ const propDefinitions = yield Promise.all(item.updateProps.map((prop) => __awaiter(this, void 0, void 0, function* () {
636
+ const properties = _handleComment(prop.widgetPropTypeSchema, GroupEnum.DATA_FIELD);
637
+ const widgetPropTypeDTS = yield _compile(Object.assign(Object.assign({}, prop.widgetPropTypeSchema), properties), true);
638
+ // 同个数据源,有可能是数组类型,所以需要区分开
639
+ const id = `${prop.datasource.name}_${prop.widgetPropTypeSchema.type}`;
640
+ if (!datasources[id]) {
641
+ datasources[id] = {
642
+ fields: [{ name: prop.widgetPropName, title: prop.widgetPropTitle }],
643
+ schema: prop.widgetPropTypeSchema,
644
+ name: prop.datasource.name,
645
+ title: prop.datasource.title,
646
+ id: prop.datasource.id,
647
+ type: prop.datasource.type,
648
+ };
649
+ }
650
+ else {
651
+ datasources[id].fields.push({ name: prop.widgetPropName, title: prop.widgetPropTitle });
652
+ }
653
+ // 是否是多层级字段,如 "a.b"
654
+ // FIXME: 当前只支持 a.b 这种形式,不支持 a[].b
655
+ const multiLevelField = prop.widgetPropName.includes('.');
656
+ let multiLevelFields = [];
657
+ let multiLevelFieldObj = {};
658
+ let firstField = '';
659
+ let othersFields = [];
660
+ if (multiLevelField) {
661
+ multiLevelFields = prop.widgetPropName.split('.');
662
+ (0, set_1.default)(multiLevelFieldObj, prop.widgetPropName, '$$schema$$');
663
+ const [_firstField, ..._othersFields] = multiLevelFields;
664
+ firstField = _firstField;
665
+ othersFields = _othersFields;
666
+ }
667
+ return `
668
+ /**
669
+ ${genWeDaCodeComment({
670
+ fieldName: prop.widgetPropName,
671
+ title: prop.widgetPropTypeSchema.title,
672
+ description: prop.widgetPropTypeSchema.description,
673
+ group: undefined,
674
+ type: getDisplayReturnType(widgetPropTypeDTS),
675
+ })
676
+ .split('\n')
677
+ .map((item) => `* ${item}`)
678
+ .join('\n')}
679
+ */
680
+ ${multiLevelField
681
+ ? `${firstField}: Omit<${compInterfaceName}['${firstField}'], '${othersFields.join('.')}'> & ${JSON.stringify(multiLevelFieldObj[firstField]).replace('"$$schema$$"', widgetPropTypeDTS)}`
682
+ : `${prop.widgetPropName}: ${widgetPropTypeDTS}`}
683
+ `;
684
+ })));
685
+ /**
686
+ * 更新数据源信息
687
+ */
688
+ const datasourceInfoList = Object.values(datasources);
689
+ if (datasourceInfoList.length > 0) {
690
+ replaceRules.push({
691
+ from: new RegExp(`(\\$${item.widgetId}_start\\$).*(\\$${item.widgetId}_end\\$)`),
692
+ // encodeURIComponent 是为了能正常 parse 成 JSON
693
+ to: `$1${encodeURIComponent(JSON.stringify(datasourceInfoList))}$2`,
694
+ });
695
+ }
696
+ return `
697
+ declare class ${widgetInterfaceName} extends ${compInterfaceName} {
698
+ ${propDefinitions.join('\n')}
699
+ }
269
700
  `;
270
701
  })));
271
702
  return results.join('\n');
@@ -274,33 +705,14 @@ function _generateOneItemDTS(inputData, itemType) {
274
705
  };
275
706
  let dts = '';
276
707
  if (!inputData)
277
- return dts;
708
+ return [dts, replaceRules];
278
709
  const handler = hanlderMap[itemType];
279
710
  // 优先判断是否有独立的处理逻辑
280
711
  if (handler) {
281
712
  dts = yield handler.call(hanlderMap, inputData);
282
713
  }
283
714
  dts = dts || (yield _compile(inputData));
284
- return dts;
285
- function _genCompInterfaceName(module, component) {
286
- return `${module.replace(/-/g, '_')}_${component}`;
287
- }
288
- function _compile(jsonschema, isObjNeedWrapper = false) {
289
- return __awaiter(this, void 0, void 0, function* () {
290
- let dts = yield compileJSONToDTS(jsonschema);
291
- if (dts.search(/^export interface IWeDa/) >= 0) {
292
- dts = dts.replace(/^export interface.*$/m, '');
293
- dts = dts.substring(0, dts.length - 2);
294
- if (isObjNeedWrapper) {
295
- dts = `{${dts}}`;
296
- }
297
- }
298
- else {
299
- dts = dts.replace(/^export type IWeDa =/m, '');
300
- }
301
- return dts;
302
- });
303
- }
715
+ return [dts, replaceRules];
304
716
  });
305
717
  }
306
718
  // 仅仅为了与 unittest 共享,外部请不要使用
@@ -380,20 +792,18 @@ function TypescriptWorker(workerId, workerCode) {
380
792
  typescriptWorker.onmessage = function (event) {
381
793
  const { action } = event.data;
382
794
  if (action === 'dts_back') {
383
- const { data: { id, dts } } = event.data;
795
+ const { data: { id, dts }, } = event.data;
384
796
  sendMessageCallbackFn[id] && sendMessageCallbackFn[id](dts);
385
797
  }
386
798
  };
387
799
  typescriptWorkerMap[workerId] = {
388
800
  typescriptWorker,
389
- sendMessageCallbackFn
801
+ sendMessageCallbackFn,
390
802
  };
391
803
  }
392
804
  function sendMessage(msgData, callbackFn) {
393
805
  const { typescriptWorker, sendMessageCallbackFn } = typescriptWorkerMap[workerId];
394
- const id = Math.random()
395
- .toString(36)
396
- .substring(2, 5);
806
+ const id = Math.random().toString(36).substring(2, 5);
397
807
  msgData.data.id = id;
398
808
  sendMessageCallbackFn[id] = callbackFn;
399
809
  typescriptWorker.postMessage(msgData);
@@ -402,22 +812,22 @@ function TypescriptWorker(workerId, workerCode) {
402
812
  };
403
813
  }
404
814
  /**
405
- * 使用:
406
- * const typescriptWorker = getTypescriptWorker();
407
- * typescriptWorker.sendMessage({ action: 'dts', data: { code } }, dts => {
408
- *
409
- * })
410
- * // 以下暂未实现
411
- * typescriptWorker.sendMessage({ action: 'ts', data: { code } }, ts => {
412
- *
413
- * })
414
- */
815
+ * 使用:
816
+ * const typescriptWorker = getTypescriptWorker();
817
+ * typescriptWorker.sendMessage({ action: 'dts', data: { code } }, dts => {
818
+ *
819
+ * })
820
+ * // 以下暂未实现
821
+ * typescriptWorker.sendMessage({ action: 'ts', data: { code } }, ts => {
822
+ *
823
+ * })
824
+ */
415
825
  const getTypescriptWorker = TypescriptWorker('transpileToDTS', exports.workerCode);
416
826
  /**
417
827
  * 通过 js/ts 代码生成 dts
418
828
  */
419
829
  function transpileToDTS(code) {
420
- return new Promise(resolve => {
830
+ return new Promise((resolve) => {
421
831
  const typescriptWorker = getTypescriptWorker();
422
832
  typescriptWorker.sendMessage({ action: 'dts', data: { code } }, (dts) => {
423
833
  resolve(dts);
@@ -426,10 +836,10 @@ function transpileToDTS(code) {
426
836
  }
427
837
  exports.transpileToDTS = transpileToDTS;
428
838
  exports.jsonToDTSWorkerCode = `
429
- importScripts('https://qbase.cdn-go.cn/lcap/lcap-resource-cdngo/-/release/_url/devtools/json-schema-to-typescript-for-browser.min.js');
839
+ importScripts('https://qbase.cdn-go.cn/lcap/lcap-resource-cdngo/-/release/_url/devtools/json-schema-to-typescript-for-browser.min-11.0.4.js');
430
840
 
431
841
  function transpile(jsonschema) {
432
- return jstt.compile(jsonschema, 'IWeDa', { additionalProperties: false, bannerComment: '', format: false })
842
+ return jstt.compile(jsonschema, 'IWeDa', { additionalProperties: false, bannerComment: '', format: false, unknownAny: false })
433
843
  }
434
844
 
435
845
  addEventListener('message', (message) => {