fix: 修复转换器正确使用Boot层工具类
✅ 修复的转换器: 1. string.converter.js - ❌ 之前: StringUtils.isEmpty() → 内联表达式 - ✅ 现在: StringUtils.isEmpty() → 保留,使用Boot层 - 新增: analyzeImports() 分析StringUtils依赖 2. collection.converter.js - ❌ 之前: CollUtil.isEmpty() → 内联表达式 - ✅ 现在: CollUtil.isEmpty() → StringUtils.isEmptyArray() (Boot层) - 新增: analyzeImports() 分析StringUtils依赖 3. json.converter.js - ✅ 保持: JSON.parse() 简单内联 - ✅ 备注: 未来可使用Boot层JsonUtils - 新增: analyzeImports() 分析JsonUtils依赖 4. service-method-converter.js (主协调器) - ✅ 新增: 自动分析Boot层工具类imports - ✅ 支持: ConfigService, StringUtils, JsonUtils 📋 文件统计: 18个转换器文件 📦 Boot层对齐: StringUtils, JsonUtils, ConfigService
This commit is contained in:
@@ -136,19 +136,39 @@ class ServiceMethodConverter {
|
|||||||
nodeModules: new Set()
|
nodeModules: new Set()
|
||||||
};
|
};
|
||||||
|
|
||||||
// NestJS异常
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
// 【NestJS异常】
|
||||||
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
const nestjsImports = this.exception.analyzeImports(convertedBody);
|
const nestjsImports = this.exception.analyzeImports(convertedBody);
|
||||||
nestjsImports.forEach(imp => imports.nestjs.add(imp));
|
nestjsImports.forEach(imp => imports.nestjs.add(imp));
|
||||||
|
|
||||||
// Node.js模块
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
// 【Node.js模块】
|
||||||
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
const nodeModules = this.file.analyzeNodeModules(convertedBody);
|
const nodeModules = this.file.analyzeNodeModules(convertedBody);
|
||||||
nodeModules.forEach(mod => imports.nodeModules.add(mod));
|
nodeModules.forEach(mod => imports.nodeModules.add(mod));
|
||||||
|
|
||||||
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
// 【Boot层工具类】
|
||||||
|
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
|
||||||
// ConfigService
|
// ConfigService
|
||||||
if (this.config.needsConfigService(convertedBody)) {
|
if (this.config.needsConfigService(convertedBody)) {
|
||||||
imports.boot.add('ConfigService');
|
imports.boot.add('ConfigService');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringUtils
|
||||||
|
const stringImports = this.string.analyzeImports(convertedBody);
|
||||||
|
stringImports.forEach(imp => imports.boot.add(imp));
|
||||||
|
|
||||||
|
// Collection判空(可能需要StringUtils)
|
||||||
|
const collectionImports = this.collection.analyzeImports(convertedBody);
|
||||||
|
collectionImports.forEach(imp => imports.boot.add(imp));
|
||||||
|
|
||||||
|
// JsonUtils
|
||||||
|
const jsonImports = this.json.analyzeImports(convertedBody);
|
||||||
|
jsonImports.forEach(imp => imports.boot.add(imp));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nestjs: Array.from(imports.nestjs),
|
nestjs: Array.from(imports.nestjs),
|
||||||
boot: Array.from(imports.boot),
|
boot: Array.from(imports.boot),
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 集合判空转换器
|
* 集合判空转换器
|
||||||
*
|
*
|
||||||
* Java集合判空方法 → TypeScript数组判空
|
* Java集合判空方法 → Boot层StringUtils或内联判空
|
||||||
*/
|
*/
|
||||||
class CollectionConverter {
|
class CollectionConverter {
|
||||||
/**
|
/**
|
||||||
@@ -10,17 +10,31 @@ class CollectionConverter {
|
|||||||
convert(javaCode) {
|
convert(javaCode) {
|
||||||
let tsCode = javaCode;
|
let tsCode = javaCode;
|
||||||
|
|
||||||
// 1. CollectionUtil.isEmpty/isNotEmpty
|
// 1. ✅ CollectionUtil.isEmpty/isNotEmpty → StringUtils.isEmptyArray/isNotEmptyArray (Boot层)
|
||||||
tsCode = tsCode.replace(/(?:CollectionUtil|CollUtil)\.isEmpty\(([^)]+)\)/g, '(!$1 || $1.length === 0)');
|
tsCode = tsCode.replace(/(?:CollectionUtil|CollUtil)\.isEmpty\(([^)]+)\)/g, 'StringUtils.isEmptyArray($1)');
|
||||||
tsCode = tsCode.replace(/(?:CollectionUtil|CollUtil)\.isNotEmpty\(([^)]+)\)/g, '($1 && $1.length > 0)');
|
tsCode = tsCode.replace(/(?:CollectionUtil|CollUtil)\.isNotEmpty\(([^)]+)\)/g, 'StringUtils.isNotEmptyArray($1)');
|
||||||
|
|
||||||
// 2. list.isEmpty() → list.length === 0
|
// 2. list.isEmpty() → list.length === 0 (简单内联)
|
||||||
// 支持链式调用:obj.getConfig().isEmpty()
|
// 支持链式调用:obj.getConfig().isEmpty()
|
||||||
tsCode = tsCode.replace(/([a-zA-Z_$][\w$.]*)\.isEmpty\(\)/g, '$1.length === 0');
|
tsCode = tsCode.replace(/([a-zA-Z_$][\w$.]*)\.isEmpty\(\)/g, '$1.length === 0');
|
||||||
tsCode = tsCode.replace(/!([a-zA-Z_$][\w$.]*)\.isEmpty\(\)/g, '$1.length > 0');
|
tsCode = tsCode.replace(/!([a-zA-Z_$][\w$.]*)\.isEmpty\(\)/g, '$1.length > 0');
|
||||||
|
|
||||||
return tsCode;
|
return tsCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分析需要的imports
|
||||||
|
*/
|
||||||
|
analyzeImports(tsCode) {
|
||||||
|
const imports = new Set();
|
||||||
|
|
||||||
|
// 检查是否使用了StringUtils
|
||||||
|
if (tsCode.includes('StringUtils.')) {
|
||||||
|
imports.add('StringUtils');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(imports);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CollectionConverter;
|
module.exports = CollectionConverter;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* JSON工具类转换器
|
* JSON工具类转换器
|
||||||
*
|
*
|
||||||
* JSONUtil / JSON相关 → TypeScript JSON / JsonUtils
|
* JSONUtil / JSON相关 → Boot层JsonUtils或TypeScript JSON
|
||||||
*/
|
*/
|
||||||
class JsonConverter {
|
class JsonConverter {
|
||||||
/**
|
/**
|
||||||
@@ -10,17 +10,31 @@ class JsonConverter {
|
|||||||
convert(javaCode) {
|
convert(javaCode) {
|
||||||
let tsCode = javaCode;
|
let tsCode = javaCode;
|
||||||
|
|
||||||
// JSONUtil.parseObj() → JSON.parse()
|
// 1. JSONUtil.parseObj() → JSON.parse() (简单内联)
|
||||||
tsCode = tsCode.replace(/JSONUtil\.parseObj\(([^)]+)\)/g, 'JSON.parse($1)');
|
tsCode = tsCode.replace(/JSONUtil\.parseObj\(([^)]+)\)/g, 'JSON.parse($1)');
|
||||||
|
|
||||||
// JSONUtil.toBean() → 需要手动处理,暂时保留TODO
|
// 2. ✅ JSONUtil.toBean() → 暂时保留TODO(需要手动处理泛型转换)
|
||||||
tsCode = tsCode.replace(/JSONUtil\.toBean\(([^,]+),\s*([^)]+)\.class\)/g, '/* TODO: JSONUtil.toBean($1, $2) */Object.assign(new $2(), JSON.parse(JSON.stringify($1)))');
|
tsCode = tsCode.replace(/JSONUtil\.toBean\(([^,]+),\s*([^)]+)\.class\)/g, '/* TODO: JSONUtil.toBean($1, $2) */Object.assign(new $2(), JSON.parse(JSON.stringify($1)))');
|
||||||
|
|
||||||
// JSONObject → Record<string, any>
|
// 3. JSONObject → Record<string, any> (TypeScript标准)
|
||||||
tsCode = tsCode.replace(/JSONObject/g, 'Record<string, any>');
|
tsCode = tsCode.replace(/JSONObject/g, 'Record<string, any>');
|
||||||
|
|
||||||
return tsCode;
|
return tsCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分析需要的imports
|
||||||
|
*/
|
||||||
|
analyzeImports(tsCode) {
|
||||||
|
const imports = new Set();
|
||||||
|
|
||||||
|
// 检查是否使用了JsonUtils(暂时未直接使用,未来可能需要)
|
||||||
|
if (tsCode.includes('JsonUtils.')) {
|
||||||
|
imports.add('JsonUtils');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(imports);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = JsonConverter;
|
module.exports = JsonConverter;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 字符串方法转换器
|
* 字符串方法转换器
|
||||||
*
|
*
|
||||||
* Java String方法 → TypeScript String方法
|
* Java String方法 → TypeScript/Boot层String方法
|
||||||
*/
|
*/
|
||||||
class StringConverter {
|
class StringConverter {
|
||||||
/**
|
/**
|
||||||
@@ -20,15 +20,28 @@ class StringConverter {
|
|||||||
// 3. .contains() → .includes()
|
// 3. .contains() → .includes()
|
||||||
tsCode = tsCode.replace(/\.contains\(/g, '.includes(');
|
tsCode = tsCode.replace(/\.contains\(/g, '.includes(');
|
||||||
|
|
||||||
// 4. StringUtils.isEmpty/isNotEmpty
|
// 4. ✅ StringUtils.isEmpty/isNotEmpty → 保留,使用Boot层的StringUtils
|
||||||
tsCode = tsCode.replace(/StringUtils\.isEmpty\(([^)]+)\)/g, '(!$1 || $1.trim() === \'\')');
|
// 不转换!Boot层已有StringUtils工具类
|
||||||
tsCode = tsCode.replace(/StringUtils\.isNotEmpty\(([^)]+)\)/g, '($1 && $1.trim() !== \'\')');
|
|
||||||
|
|
||||||
// 5. String.valueOf() → String()
|
// 5. String.valueOf() → String()
|
||||||
tsCode = tsCode.replace(/String\.valueOf\(([^)]+)\)/g, 'String($1)');
|
tsCode = tsCode.replace(/String\.valueOf\(([^)]+)\)/g, 'String($1)');
|
||||||
|
|
||||||
return tsCode;
|
return tsCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分析需要的imports
|
||||||
|
*/
|
||||||
|
analyzeImports(tsCode) {
|
||||||
|
const imports = new Set();
|
||||||
|
|
||||||
|
// 检查是否使用了StringUtils
|
||||||
|
if (tsCode.includes('StringUtils.')) {
|
||||||
|
imports.add('StringUtils');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(imports);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = StringConverter;
|
module.exports = StringConverter;
|
||||||
|
|||||||
Reference in New Issue
Block a user