@hadss/turbo-trans-json-plugin 1.0.0-rc.1 → 1.0.0-rc.2

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 (76) hide show
  1. package/dist/core/analyzers/ClassAnalyzer.d.ts +15 -0
  2. package/dist/core/analyzers/ClassAnalyzer.js +193 -144
  3. package/dist/core/analyzers/CustomTypeAnalyzer.d.ts +3 -0
  4. package/dist/core/analyzers/CustomTypeAnalyzer.js +65 -60
  5. package/dist/core/constants/DecoratorConstants.js +2 -1
  6. package/dist/core/import-rewrite/services/BuildProfileUpdater.js +1 -1
  7. package/dist/core/import-rewrite/services/ImportRewriteService.js +1 -1
  8. package/dist/core/interfaces/index.d.ts +2 -2
  9. package/dist/core/services/CodeAnalysisService.js +2 -1
  10. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.d.ts +2 -0
  11. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.js +22 -15
  12. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.d.ts +22 -0
  13. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.js +194 -129
  14. package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.js +1 -1
  15. package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.js +2 -1
  16. package/dist/core/services/CodeGenerationService/shared/ImportManager.d.ts +2 -2
  17. package/dist/core/template/HandlebarsTemplateEngine.d.ts +2 -0
  18. package/dist/core/template/HandlebarsTemplateEngine.js +21 -2
  19. package/dist/core/utils/DeepCopyUtil.js +1 -1
  20. package/dist/core/utils/SerializationPathUtil.d.ts +1 -1
  21. package/dist/core/utils/TsMorphUtil.js +2 -1
  22. package/dist/json-plugin/JSONExecuteController.d.ts +4 -0
  23. package/dist/json-plugin/JSONExecuteController.js +46 -36
  24. package/dist/json-plugin/interfaces/impl/TargetContext.js +4 -2
  25. package/dist/json-plugin/tasks/BaseTask.d.ts +2 -2
  26. package/dist/json-plugin/tasks/WatchTask.js +2 -1
  27. package/package.json +1 -1
  28. package/src/core/Types.ts +90 -90
  29. package/src/core/analyzers/ClassAnalyzer.ts +335 -230
  30. package/src/core/analyzers/CustomTypeAnalyzer.ts +145 -73
  31. package/src/core/constants/DecoratorConstants.ts +7 -6
  32. package/src/core/constants/PathConstants.ts +7 -7
  33. package/src/core/constants/StringConstants.ts +95 -95
  34. package/src/core/handlers/BaseTypeHandler.ts +11 -2
  35. package/src/core/handlers/CustomClassHandler.ts +4 -4
  36. package/src/core/handlers/DateHandler.ts +54 -46
  37. package/src/core/handlers/DecimalHandler.ts +53 -45
  38. package/src/core/handlers/EnumHandler.ts +2 -1
  39. package/src/core/handlers/GenericContainerHandler.ts +3 -1
  40. package/src/core/handlers/TupleHandler.ts +2 -1
  41. package/src/core/handlers/TypeHandlerRegistry.ts +2 -1
  42. package/src/core/handlers/UnionTypeHandler.ts +8 -7
  43. package/src/core/import-rewrite/services/BuildProfileUpdater.ts +6 -4
  44. package/src/core/import-rewrite/services/ImportRewriteService.ts +1 -1
  45. package/src/core/import-rewrite/services/ImportTransformService.ts +2 -2
  46. package/src/core/import-rewrite/types/ImportRewriteTypes.ts +3 -3
  47. package/src/core/index.ts +4 -4
  48. package/src/core/interfaces/ITask.ts +6 -5
  49. package/src/core/interfaces/ITaskContext.ts +9 -9
  50. package/src/core/interfaces/index.ts +2 -2
  51. package/src/core/logger/Logger.ts +28 -28
  52. package/src/core/services/CodeAnalysisService.ts +2 -1
  53. package/src/core/services/CodeGenerationEngine.ts +42 -42
  54. package/src/core/services/CodeGenerationService/generators/OriginalClassGenerator.ts +25 -22
  55. package/src/core/services/CodeGenerationService/generators/SendableClassGenerator.ts +261 -170
  56. package/src/core/services/CodeGenerationService/generators/SerializerGenerator.ts +1 -1
  57. package/src/core/services/CodeGenerationService/generators/TempSerializerGenerator.ts +5 -3
  58. package/src/core/services/CodeGenerationService/shared/ImportManager.ts +8 -8
  59. package/src/core/template/HandlebarsTemplateEngine.ts +39 -11
  60. package/src/core/utils/ConfigManager.ts +2 -1
  61. package/src/core/utils/DeepCopyUtil.ts +1 -1
  62. package/src/core/utils/GenericTypeSubstitutionUtil.ts +1 -8
  63. package/src/core/utils/SerializationPathUtil.ts +7 -6
  64. package/src/core/utils/TsMorphUtil.ts +4 -1
  65. package/src/index.ts +2 -2
  66. package/src/json-plugin/JSONExecuteController.ts +51 -38
  67. package/src/json-plugin/interfaces/IModuleContext.ts +8 -8
  68. package/src/json-plugin/interfaces/ITargetContext.ts +6 -6
  69. package/src/json-plugin/interfaces/impl/ModuleContext.ts +10 -10
  70. package/src/json-plugin/interfaces/impl/TargetContext.ts +63 -58
  71. package/src/json-plugin/tasks/BaseTask.ts +5 -3
  72. package/src/json-plugin/tasks/CleanTask.ts +7 -7
  73. package/src/json-plugin/tasks/WatchTask.ts +20 -18
  74. package/template/SerializerPerformanceTemplate.hbs +5 -5
  75. package/template/SerializerStrictTemplate.hbs +1 -1
  76. package/template/SerializerTemplate.hbs +59 -42
@@ -16,6 +16,8 @@
16
16
  import {
17
17
  ClassDeclaration,
18
18
  EnumDeclaration,
19
+ EnumMember,
20
+ ImportDeclaration,
19
21
  InterfaceDeclaration,
20
22
  SourceFile,
21
23
  TypeAliasDeclaration
@@ -38,7 +40,8 @@ export class CustomTypeAnalyzer {
38
40
  /** 类型查找缓存 <名称+路径,依赖地址> */
39
41
  private searchCache = new Map<string, TypeDependency | null>();
40
42
 
41
- private constructor() { }
43
+ private constructor() {
44
+ }
42
45
 
43
46
  public static getInstance(): CustomTypeAnalyzer {
44
47
  return this.INSTANCE;
@@ -96,7 +99,8 @@ export class CustomTypeAnalyzer {
96
99
  return null;
97
100
  }
98
101
 
99
- public findClassDeclFromClassName(className: string, sourceFile: SourceFile): ClassDeclaration | InterfaceDeclaration | null {
102
+ public findClassDeclFromClassName(className: string,
103
+ sourceFile: SourceFile): ClassDeclaration | InterfaceDeclaration | null {
100
104
  const res = this.findDefinition(className, sourceFile);
101
105
  if (res && res.typeKind === TypeKind.CLASS) {
102
106
  return (res.details as ClassDetails).classDecl;
@@ -134,43 +138,72 @@ export class CustomTypeAnalyzer {
134
138
  for (const importDecl of imports) {
135
139
  // TODO:当前实现只处理命名导入,default导入和命名空间导入暂未支持
136
140
  const namedImports = importDecl.getNamedImports();
141
+ const matchedImport = namedImports.find(ni => ni.getName() === targetName);
142
+
143
+ if (!matchedImport) {
144
+ continue;
145
+ }
146
+
137
147
  const specifierValue = importDecl.getModuleSpecifierValue();
148
+ const result = this.processImportMatch(importDecl, targetName, sourceFile, specifierValue);
138
149
 
139
- // 检查是否在命名导入中找到了目标类型
140
- for (const namedImport of namedImports) {
141
- const importName = namedImport.getName();
142
- if (importName === targetName) {
143
- // 找到了导入的类型,现在需要解析文件路径
144
- const moduleSpecifier = importDecl.getModuleSpecifierValue();
145
- const resolvedPath = this.resolveImportPath(moduleSpecifier, sourceFile);
146
-
147
- if (resolvedPath && SerializationPathUtil.exist(resolvedPath)) {
148
- // 加载外部文件并查找类型定义
149
- const externalSourceFile = this.getOrLoadSourceFile(resolvedPath);
150
- if (externalSourceFile) {
151
- const typeDetail = this.searchInSourceFile(targetName, externalSourceFile);
152
- if (typeDetail) {
153
- return {
154
- typeName: targetName,
155
- typeKind: typeDetail.kind,
156
- importPath: specifierValue,
157
- source: {
158
- type: 'imported',
159
- sourceFilePath: externalSourceFile.getFilePath(),
160
- originalImport: specifierValue
161
- },
162
- details: typeDetail
163
- };
164
- }
165
- }
166
- }
167
- }
150
+ if (result) {
151
+ return result;
168
152
  }
169
153
  }
170
154
 
171
155
  return null;
172
156
  }
173
157
 
158
+ /**
159
+ * 处理导入匹配,解析外部文件中的类型定义
160
+ *
161
+ * 处理流程:
162
+ * 1. 解析模块路径 → 路径无效则返回 null
163
+ * 2. 加载外部源文件 → 加载失败则返回 null
164
+ * 3. 在外部文件中查找类型 → 未找到则返回 null
165
+ * 4. 构建并返回类型依赖信息
166
+ */
167
+ private processImportMatch(
168
+ importDecl: ImportDeclaration,
169
+ targetName: string,
170
+ sourceFile: SourceFile,
171
+ specifierValue: string
172
+ ): TypeDependency | null {
173
+ const moduleSpecifier = importDecl.getModuleSpecifierValue();
174
+ const resolvedPath = this.resolveImportPath(moduleSpecifier, sourceFile);
175
+
176
+ // 条件1:路径解析失败或文件不存在,无法继续处理
177
+ if (!resolvedPath || !SerializationPathUtil.exist(resolvedPath)) {
178
+ return null;
179
+ }
180
+
181
+ // 条件2:无法加载外部源文件(文件损坏或格式不支持)
182
+ const externalSourceFile = this.getOrLoadSourceFile(resolvedPath);
183
+ if (!externalSourceFile) {
184
+ return null;
185
+ }
186
+
187
+ // 条件3:在外部文件中未找到目标类型定义
188
+ const typeDetail = this.searchInSourceFile(targetName, externalSourceFile);
189
+ if (!typeDetail) {
190
+ return null;
191
+ }
192
+
193
+ // 所有条件通过,构建类型依赖结果
194
+ return {
195
+ typeName: targetName,
196
+ typeKind: typeDetail.kind,
197
+ importPath: specifierValue,
198
+ source: {
199
+ type: 'imported',
200
+ sourceFilePath: externalSourceFile.getFilePath(),
201
+ originalImport: specifierValue
202
+ },
203
+ details: typeDetail
204
+ };
205
+ }
206
+
174
207
  private findInCrossModule(targetName: string, sourceFile: SourceFile): TypeDependency | null {
175
208
  // TODO: 跨模块暂未实现
176
209
  return null;
@@ -214,53 +247,25 @@ export class CustomTypeAnalyzer {
214
247
 
215
248
  private parseEnumDetails(enumDecl: EnumDeclaration): EnumDetails {
216
249
  const members: EnumMemberInfo[] = [];
217
- let valueType: 'string' | 'number' = 'number'; // 默认为数值类型
218
- let numericValue = 0; // 用于自动递增的数值
250
+ let valueType: 'string' | 'number' = 'number';
251
+ let numericValue = 0;
219
252
 
220
253
  const enumMembers = enumDecl.getMembers();
221
254
  for (const member of enumMembers) {
222
- const memberName = member.getName();
223
- let memberValue: string | number;
224
- let literalValue: string;
225
-
226
- const initializer = member.getInitializer();
227
- if (initializer) {
228
- // 有显式初始值
229
- const initText = initializer.getText().trim();
230
-
231
- // 判断是字符串还是数值
232
- if (initText.startsWith('"') && initText.endsWith('"')) {
233
- // 字符串枚举
234
- memberValue = initText.slice(1, -1); // 去掉引号
235
- literalValue = initText; // 保留引号
236
- valueType = 'string';
237
- } else if (initText.startsWith("'") && initText.endsWith("'")) {
238
- // 字符串枚举(单引号)
239
- memberValue = initText.slice(1, -1); // 去掉引号
240
- literalValue = `"${memberValue}"`; // 统一使用双引号
241
- valueType = 'string';
242
- } else {
243
- // 数值枚举
244
- const numValue = parseInt(initText, 10);
245
- if (!isNaN(numValue)) {
246
- memberValue = numValue;
247
- literalValue = numValue.toString();
248
- numericValue = numValue + 1; // 更新下一个自动值
249
- } else {
250
- // 复杂表达式,暂时当作字符串处理
251
- memberValue = initText;
252
- literalValue = `"${initText}"`;
253
- valueType = 'string';
254
- }
255
- }
256
- } else {
257
- // 没有显式初始值,使用自动递增的数值
258
- memberValue = numericValue;
259
- literalValue = numericValue.toString();
260
- numericValue++;
255
+ const {
256
+ memberValue,
257
+ literalValue,
258
+ type,
259
+ nextValue
260
+ } = this.parseEnumMember(member, numericValue);
261
+ numericValue = nextValue;
262
+
263
+ if (type === 'string') {
264
+ valueType = 'string';
261
265
  }
266
+
262
267
  members.push({
263
- name: memberName,
268
+ name: member.getName(),
264
269
  value: memberValue,
265
270
  literalValue
266
271
  });
@@ -268,6 +273,73 @@ export class CustomTypeAnalyzer {
268
273
  return { kind: TypeKind.ENUM, valueType, members };
269
274
  }
270
275
 
276
+ /**
277
+ * 解析枚举成员的值信息
278
+ *
279
+ * 处理四种情况(互斥,按优先级顺序判断):
280
+ * 1. 无初始化器 → 使用自增数字值
281
+ * 2. 双引号字符串 "value" → 字符串类型
282
+ * 3. 单引号字符串 'value' → 字符串类型(转换为双引号)
283
+ * 4. 数字字面量 → 数字类型,后续成员从此值+1继续自增
284
+ * 5. 其他表达式 → 作为字符串处理(兜底情况)
285
+ *
286
+ * @param member 枚举成员节点
287
+ * @param numericValue 当前自增数字值
288
+ * @returns 成员值信息,包含实际值、字面量表示、类型和下一个自增值
289
+ */
290
+ private parseEnumMember(member: EnumMember, numericValue: number): {
291
+ memberValue: string | number;
292
+ literalValue: string;
293
+ type: 'string' | 'number';
294
+ nextValue: number
295
+ } {
296
+ const initializer = member.getInitializer();
297
+
298
+ // 情况1:无初始化器,使用自增数字值(如 enum { A, B, C } 中的 A=0, B=1, C=2)
299
+ if (!initializer) {
300
+ return {
301
+ memberValue: numericValue,
302
+ literalValue: numericValue.toString(),
303
+ type: 'number',
304
+ nextValue: numericValue + 1
305
+ };
306
+ }
307
+
308
+ const initText = initializer.getText().trim();
309
+
310
+ // 情况2:双引号字符串(如 enum { A = "hello" })
311
+ if (initText.startsWith('"') && initText.endsWith('"')) {
312
+ return this.createStringEnumResult(initText.slice(1, -1), initText, numericValue);
313
+ }
314
+
315
+ // 情况3:单引号字符串(如 enum { A = 'hello' }),转换为双引号格式
316
+ if (initText.startsWith("'") && initText.endsWith("'")) {
317
+ const val = initText.slice(1, -1);
318
+ return this.createStringEnumResult(val, `"${val}"`, numericValue);
319
+ }
320
+
321
+ // 情况4:数字字面量(如 enum { A = 10 }),后续成员从 11 开始自增
322
+ const numValue = parseInt(initText, 10);
323
+ if (!isNaN(numValue)) {
324
+ return { memberValue: numValue, literalValue: numValue.toString(), type: 'number', nextValue: numValue + 1 };
325
+ }
326
+
327
+ // 情况5:其他表达式(如常量引用),作为字符串处理
328
+ return this.createStringEnumResult(initText, `"${initText}"`, numericValue);
329
+ }
330
+
331
+ /**
332
+ * 创建字符串类型枚举成员结果
333
+ * 注意:字符串枚举不影响自增值,nextValue 保持不变
334
+ */
335
+ private createStringEnumResult(
336
+ memberValue: string,
337
+ literalValue: string,
338
+ numericValue: number
339
+ ): { memberValue: string; literalValue: string; type: 'string'; nextValue: number } {
340
+ return { memberValue, literalValue, type: 'string', nextValue: numericValue };
341
+ }
342
+
271
343
  private parseClassDetails(classDecl: ClassDeclaration): ClassDetails {
272
344
  return {
273
345
  kind: TypeKind.CLASS,
@@ -18,11 +18,12 @@
18
18
  */
19
19
  export class DecoratorConstants {
20
20
  // TSerialization装饰器
21
- static readonly SERIALIZABLE = 'Serializable'
22
- static readonly SERIAL_NAME = 'SerialName'
23
- static readonly REQUIRED = 'Required'
24
- static readonly TRANSIENT = 'Transient'
25
- static readonly PLAIN_VALUE = 'PlainValue'
21
+ static readonly SERIALIZABLE = 'Serializable';
22
+ static readonly SERIAL_NAME = 'SerialName';
23
+ static readonly REQUIRED = 'Required';
24
+ static readonly TRANSIENT = 'Transient';
25
+ static readonly PLAIN_VALUE = 'PlainValue';
26
26
 
27
- static readonly SERIAL_DECORATORS = [this.SERIALIZABLE, this.SERIAL_NAME, this.REQUIRED, this.TRANSIENT, this.PLAIN_VALUE];
27
+ static readonly SERIAL_DECORATORS = [this.SERIALIZABLE, this.SERIAL_NAME, this.REQUIRED, this.TRANSIENT,
28
+ this.PLAIN_VALUE];
28
29
  }
@@ -18,14 +18,14 @@
18
18
  */
19
19
  export class PathConstants {
20
20
  /** 默认源代码根目录 */
21
- static readonly DEFAULT_SOURCE_ROOT = './src/main'
22
-
21
+ static readonly DEFAULT_SOURCE_ROOT = './src/main';
22
+
23
23
  /** 生成代码根目录 */
24
- static readonly GENERATED_ROOT = './src/generated'
25
-
24
+ static readonly GENERATED_ROOT = './src/generated';
25
+
26
26
  /** 支持的文件扩展名 */
27
- static readonly ETS_EXTENSIONS = '.ets'
28
-
27
+ static readonly ETS_EXTENSIONS = '.ets';
28
+
29
29
  /** 默认扫描目录 */
30
- static readonly DEFAULT_SCAN_DIRS = ['src/main/ets']
30
+ static readonly DEFAULT_SCAN_DIRS = ['src/main/ets'];
31
31
  }
@@ -18,133 +18,133 @@
18
18
  * 避免硬编码字符串散布在代码中
19
19
  */
20
20
  export class StringConstants {
21
-
21
+
22
22
  // ========== 基础类型字符串 ==========
23
- static readonly TYPE_BOOLEAN = 'boolean'
24
- static readonly TYPE_NUMBER = 'number'
25
- static readonly TYPE_STRING = 'string'
26
- static readonly TYPE_BIGINT = 'bigint'
27
- static readonly TYPE_NULL = 'null'
28
- static readonly TYPE_UNDEFINED = 'undefined'
29
- static readonly TYPE_ARRAY = 'array'
30
- static readonly TYPE_SET = 'set'
31
- static readonly TYPE_MAP = 'map'
32
- static readonly TYPE_UNION = 'union'
33
- static readonly TYPE_ENUM = 'enum'
23
+ static readonly TYPE_BOOLEAN = 'boolean';
24
+ static readonly TYPE_NUMBER = 'number';
25
+ static readonly TYPE_STRING = 'string';
26
+ static readonly TYPE_BIGINT = 'bigint';
27
+ static readonly TYPE_NULL = 'null';
28
+ static readonly TYPE_UNDEFINED = 'undefined';
29
+ static readonly TYPE_ARRAY = 'array';
30
+ static readonly TYPE_SET = 'set';
31
+ static readonly TYPE_MAP = 'map';
32
+ static readonly TYPE_UNION = 'union';
33
+ static readonly TYPE_ENUM = 'enum';
34
34
 
35
35
  // ========== 序列化器名称 ==========
36
36
  // 基础类型序列化器(单例INSTANCE模式)
37
- static readonly BOOLEAN_SERIALIZER = 'BooleanSerializer'
38
- static readonly NUMBER_SERIALIZER = 'NumberSerializer'
39
- static readonly STRING_SERIALIZER = 'StringSerializer'
40
- static readonly BIGINT_SERIALIZER = 'BigintSerializer'
41
-
37
+ static readonly BOOLEAN_SERIALIZER = 'BooleanSerializer';
38
+ static readonly NUMBER_SERIALIZER = 'NumberSerializer';
39
+ static readonly STRING_SERIALIZER = 'StringSerializer';
40
+ static readonly BIGINT_SERIALIZER = 'BigintSerializer';
41
+
42
42
  // 特殊原始类型序列化器(单例INSTANCE模式)
43
- static readonly NULL_SERIALIZER = 'NullSerializer'
44
- static readonly UNDEFINED_SERIALIZER = 'UndefinedSerializer'
45
-
43
+ static readonly NULL_SERIALIZER = 'NullSerializer';
44
+ static readonly UNDEFINED_SERIALIZER = 'UndefinedSerializer';
45
+
46
46
  // 集合类型序列化器(泛型构造器模式)
47
- static readonly ARRAY_SERIALIZER = 'ArraySerializer'
48
- static readonly COLLECTIONS_ARRAY_SERIALIZER = 'CollectionsArraySerializer'
49
- static readonly MAP_SERIALIZER = 'MapSerializer'
50
- static readonly COLLECTIONS_MAP_SERIALIZER = 'CollectionsMapSerializer'
51
- static readonly SET_SERIALIZER = 'SetSerializer'
52
- static readonly COLLECTIONS_SET_SERIALIZER = 'CollectionsSetSerializer'
53
- static readonly RECORD_SERIALIZER = 'RecordSerializer'
47
+ static readonly ARRAY_SERIALIZER = 'ArraySerializer';
48
+ static readonly COLLECTIONS_ARRAY_SERIALIZER = 'CollectionsArraySerializer';
49
+ static readonly MAP_SERIALIZER = 'MapSerializer';
50
+ static readonly COLLECTIONS_MAP_SERIALIZER = 'CollectionsMapSerializer';
51
+ static readonly SET_SERIALIZER = 'SetSerializer';
52
+ static readonly COLLECTIONS_SET_SERIALIZER = 'CollectionsSetSerializer';
53
+ static readonly RECORD_SERIALIZER = 'RecordSerializer';
54
54
 
55
55
  // 高级类型序列化器(特殊构造器模式)
56
- static readonly UNION_TYPE_SERIALIZER = 'UnionTypeSerializer'
57
- static readonly OPTIONAL_SERIALIZER = 'OptionalSerializer'
58
- static readonly NULLABLE_SERIALIZER = 'NullableSerializer'
59
- static readonly ENUM_SERIALIZER = 'EnumSerializer'
60
-
56
+ static readonly UNION_TYPE_SERIALIZER = 'UnionTypeSerializer';
57
+ static readonly OPTIONAL_SERIALIZER = 'OptionalSerializer';
58
+ static readonly NULLABLE_SERIALIZER = 'NullableSerializer';
59
+ static readonly ENUM_SERIALIZER = 'EnumSerializer';
60
+
61
61
  // 通用序列化器
62
- static readonly SERIALIZER = 'Serializer'
63
- static readonly SERIALIZER_FACTORY = 'SerializerFactory'
62
+ static readonly SERIALIZER = 'Serializer';
63
+ static readonly SERIALIZER_FACTORY = 'SerializerFactory';
64
64
 
65
65
  // ========== 编码/解码方法名 ==========
66
66
  // 基础类型专用编码方法
67
- static readonly ENCODE_BOOLEAN_ELEMENT = 'encodeBooleanElement'
68
- static readonly ENCODE_NUMBER_ELEMENT = 'encodeNumberElement'
69
- static readonly ENCODE_STRING_ELEMENT = 'encodeStringElement'
70
- static readonly ENCODE_BIGINT_ELEMENT = 'encodeBigintElement'
71
-
67
+ static readonly ENCODE_BOOLEAN_ELEMENT = 'encodeBooleanElement';
68
+ static readonly ENCODE_NUMBER_ELEMENT = 'encodeNumberElement';
69
+ static readonly ENCODE_STRING_ELEMENT = 'encodeStringElement';
70
+ static readonly ENCODE_BIGINT_ELEMENT = 'encodeBigintElement';
71
+
72
72
  // 特殊原始类型专用编码方法
73
- static readonly ENCODE_NULL_ELEMENT = 'encodeNullElement'
74
- static readonly ENCODE_UNDEFINED_ELEMENT = 'encodeUndefinedElement'
75
-
73
+ static readonly ENCODE_NULL_ELEMENT = 'encodeNullElement';
74
+ static readonly ENCODE_UNDEFINED_ELEMENT = 'encodeUndefinedElement';
75
+
76
76
  // 通用编码方法
77
- static readonly ENCODE_SERIALIZABLE_ELEMENT = 'encodeSerializableElement'
77
+ static readonly ENCODE_SERIALIZABLE_ELEMENT = 'encodeSerializableElement';
78
78
 
79
79
  // 基础类型专用解码方法
80
- static readonly DECODE_BOOLEAN = 'decodeBoolean'
81
- static readonly DECODE_NUMBER = 'decodeNumber'
82
- static readonly DECODE_STRING = 'decodeString'
83
- static readonly DECODE_BIGINT = 'decodeBigint'
84
-
80
+ static readonly DECODE_BOOLEAN = 'decodeBoolean';
81
+ static readonly DECODE_NUMBER = 'decodeNumber';
82
+ static readonly DECODE_STRING = 'decodeString';
83
+ static readonly DECODE_BIGINT = 'decodeBigint';
84
+
85
85
  // 特殊原始类型专用解码方法
86
- static readonly DECODE_NULL = 'decodeNull'
87
- static readonly DECODE_UNDEFINED = 'decodeUndefined'
88
-
86
+ static readonly DECODE_NULL = 'decodeNull';
87
+ static readonly DECODE_UNDEFINED = 'decodeUndefined';
88
+
89
89
  // 通用解码方法
90
- static readonly DESERIALIZABLE = 'deserialize'
90
+ static readonly DESERIALIZABLE = 'deserialize';
91
91
 
92
92
  // ========== 导入模块相关 ==========
93
- static readonly IMPORT_BUILD_CLASS_SERIAL_DESCRIPTOR = 'buildClassSerialDescriptor'
94
- static readonly IMPORT_DECODER = 'Decoder'
95
- static readonly IMPORT_ENCODER = 'Encoder'
96
- static readonly IMPORT_SERIAL_DESCRIPTOR = 'SerialDescriptor'
97
- static readonly IMPORT_SERIALIZER = 'Serializer'
93
+ static readonly IMPORT_BUILD_CLASS_SERIAL_DESCRIPTOR = 'buildClassSerialDescriptor';
94
+ static readonly IMPORT_DECODER = 'Decoder';
95
+ static readonly IMPORT_ENCODER = 'Encoder';
96
+ static readonly IMPORT_SERIAL_DESCRIPTOR = 'SerialDescriptor';
97
+ static readonly IMPORT_SERIALIZER = 'Serializer';
98
98
 
99
- static readonly IMPORT_TJSON = 'TJSON'
100
- static readonly IMPORT_IT_SERIALIZABLE = 'ITSerializable'
101
- static readonly IMPORT_LANG = 'lang'
99
+ static readonly IMPORT_TJSON = 'TJSON';
100
+ static readonly IMPORT_IT_SERIALIZABLE = 'ITSerializable';
101
+ static readonly IMPORT_LANG = 'lang';
102
102
 
103
103
  // ========== 类型推断相关 ==========
104
- static readonly BOOLEAN_WRAPPER = 'Boolean'
105
- static readonly NUMBER_WRAPPER = 'Number'
106
- static readonly STRING_WRAPPER = 'String'
104
+ static readonly BOOLEAN_WRAPPER = 'Boolean';
105
+ static readonly NUMBER_WRAPPER = 'Number';
106
+ static readonly STRING_WRAPPER = 'String';
107
107
 
108
- static readonly ARRAY_SUFFIX = '[]'
109
- static readonly ARRAY_PREFIX = 'array<'
108
+ static readonly ARRAY_SUFFIX = '[]';
109
+ static readonly ARRAY_PREFIX = 'array<';
110
110
 
111
111
  // ========== 默认值相关 ==========
112
- static readonly DEFAULT_VALUE_FALSE = 'false'
113
- static readonly DEFAULT_VALUE_ZERO = '0'
114
- static readonly DEFAULT_VALUE_EMPTY_STRING = "''"
115
- static readonly DEFAULT_VALUE_ZERO_BIGINT = '0n'
116
- static readonly DEFAULT_VALUE_UNDEFINED = 'undefined'
112
+ static readonly DEFAULT_VALUE_FALSE = 'false';
113
+ static readonly DEFAULT_VALUE_ZERO = '0';
114
+ static readonly DEFAULT_VALUE_EMPTY_STRING = "''";
115
+ static readonly DEFAULT_VALUE_ZERO_BIGINT = '0n';
116
+ static readonly DEFAULT_VALUE_UNDEFINED = 'undefined';
117
117
 
118
118
  // ========== 模板字符串片段 ==========
119
- static readonly TEMPLATE_ELEMENT_NAME = 'element'
120
- static readonly TEMPLATE_SENDABLE_ERROR_PREFIX = 'Sendable support not enabled for'
121
-
119
+ static readonly TEMPLATE_ELEMENT_NAME = 'element';
120
+ static readonly TEMPLATE_SENDABLE_ERROR_PREFIX = 'Sendable support not enabled for';
121
+
122
122
  // ========== 文件操作相关 ==========
123
- static readonly FILE_OPERATION_EMPTY_STRING = ''
124
- static readonly REGEX_IMPORT_STATEMENT = /import\s+({[^}]+}|\w+)\s+from\s+'([^']+)'/
125
- static readonly REGEX_SRC_MAIN_ETS = /\/src\/main\/ets\/(.*)\.ets$/
126
-
123
+ static readonly FILE_OPERATION_EMPTY_STRING = '';
124
+ static readonly REGEX_IMPORT_STATEMENT = /import\s+({[^}]+}|\w+)\s+from\s+'([^']+)'/;
125
+ static readonly REGEX_SRC_MAIN_ETS = /\/src\/main\/ets\/(.*)\.ets$/;
126
+
127
127
  // ========== 分隔符和连接符 ==========
128
- static readonly SEPARATOR_COMMA = ','
129
- static readonly SEPARATOR_COMMA_NEWLINE = ',\n'
130
- static readonly SEPARATOR_NEWLINE = '\n'
131
- static readonly SEPARATOR_COMMA_SPACE = ', '
132
- static readonly JOIN_ELEMENT_PREFIX = ',\n '
133
-
128
+ static readonly SEPARATOR_COMMA = ',';
129
+ static readonly SEPARATOR_COMMA_NEWLINE = ',\n';
130
+ static readonly SEPARATOR_NEWLINE = '\n';
131
+ static readonly SEPARATOR_COMMA_SPACE = ', ';
132
+ static readonly JOIN_ELEMENT_PREFIX = ',\n ';
133
+
134
134
  // ========== 正则表达式字符串 ==========
135
- static readonly CURLY_BRACE_START = '{'
136
- static readonly PATH_RELATIVE_PREFIX = './'
137
- static readonly INSTANCE_SUFFIX = '.INSTANCE'
138
- static readonly SERIALIZER_SUFFIX = 'Serializer'
139
-
135
+ static readonly CURLY_BRACE_START = '{';
136
+ static readonly PATH_RELATIVE_PREFIX = './';
137
+ static readonly INSTANCE_SUFFIX = '.INSTANCE';
138
+ static readonly SERIALIZER_SUFFIX = 'Serializer';
139
+
140
140
  // ========== 日志前缀 ==========
141
- static readonly LOG_PREFIX_T_SERIALIZATION = '[TSerialization]'
142
- static readonly LOG_MODULE_PREFIX_TEMPLATE = '[${module}]'
143
-
141
+ static readonly LOG_PREFIX_T_SERIALIZATION = '[TSerialization]';
142
+ static readonly LOG_MODULE_PREFIX_TEMPLATE = '[${module}]';
143
+
144
144
  // ========== 插件标识 ==========
145
- static readonly PLUGIN_ID = 'turboTransJsonPlugin'
146
-
145
+ static readonly PLUGIN_ID = 'turboTransJsonPlugin';
146
+
147
147
  // ========== 模块标识符 ==========
148
- static readonly JSON_MODULE_SPECIFIER = '@hadss/hmserialization-json'
149
- static readonly ARKTS_MODULE_SPECIFIER = '@kit.ArkTS'
148
+ static readonly JSON_MODULE_SPECIFIER = '@hadss/hmserialization-json';
149
+ static readonly ARKTS_MODULE_SPECIFIER = '@kit.ArkTS';
150
150
  }
@@ -24,13 +24,21 @@ export abstract class BaseTypeHandler implements ITypeHandler {
24
24
 
25
25
  // 子类必须实现的所有核心方法
26
26
  abstract matches(typeNode: TypeNode, context: TypeContext): boolean;
27
+
27
28
  abstract parse(typeNode: TypeNode, context: TypeContext): TypeStructure;
29
+
28
30
  abstract getSerializerMetadata(structure: TypeStructure, withParam?: string): SerializerMetadata;
31
+
29
32
  abstract generateInstantiation(structure: TypeStructure): string;
33
+
30
34
  abstract generateTypeDeclaration(structure: TypeStructure): string;
35
+
31
36
  abstract generateTypeKey(structure: TypeStructure): string;
37
+
32
38
  abstract generateSendableTypeDeclaration(structure: TypeStructure): string;
33
- abstract generatePropertyConversion(structure: TypeStructure, sourceValue: string, direction: ConversionDirection): string;
39
+
40
+ abstract generatePropertyConversion(structure: TypeStructure, sourceValue: string,
41
+ direction: ConversionDirection): string;
34
42
 
35
43
  /** 获取序列化器名称(约定:TypeName + Serializer)*/
36
44
  protected getSerializerName(): string {
@@ -55,7 +63,8 @@ export abstract class BaseTypeHandler implements ITypeHandler {
55
63
  * @param defaultInstantiation 默认实例化代码
56
64
  * @param structure 类型结构
57
65
  */
58
- protected applyWithParam(withParam: string | undefined, defaultInstantiation: string, structure: TypeStructure): string {
66
+ protected applyWithParam(withParam: string | undefined, defaultInstantiation: string,
67
+ structure: TypeStructure): string {
59
68
  if (!withParam) {
60
69
  return defaultInstantiation;
61
70
  }
@@ -99,9 +99,9 @@ export class CustomClassHandler extends BaseTypeHandler implements ITypeHandler
99
99
  }
100
100
  }
101
101
 
102
- private parseGenericArguments(typeArgs: TypeNode[], context: TypeContext): {
103
- dependencies: TypeDependency[];
104
- parsedArgs: TypeStructure[]
102
+ private parseGenericArguments(typeArgs: TypeNode[], context: TypeContext): {
103
+ dependencies: TypeDependency[];
104
+ parsedArgs: TypeStructure[]
105
105
  } {
106
106
  const dependencies: TypeDependency[] = [];
107
107
  const parsedArgs: TypeStructure[] = typeArgs.map(arg => {
@@ -113,7 +113,7 @@ export class CustomClassHandler extends BaseTypeHandler implements ITypeHandler
113
113
  dependencies.push(...argStructure.dependencies);
114
114
  return argStructure;
115
115
  });
116
-
116
+
117
117
  return { dependencies, parsedArgs };
118
118
  }
119
119