Files
monkeycode-ai db536cfb2c feat: CodePlay 第二阶段优化 - 转换质量与特性完善
核心修复:
- 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败)
- 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements)
- 修复 PropertyConverter init-only 属性组索引

新增转换器 (C# 8-13 特性):
- NullCoalescingConverter: ??、?.、??= 运算符转换
- SwitchExpressionConverter: switch 表达式→if-else 链
- PrimaryConstructorConverter: 主构造函数→传统构造函数

增强:
- LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等
- AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误
- NamingConverter: PascalCase→camelCase 命名转换
- DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录
- XML Doc→JavaDoc 格式转换与注释保留

新增测试:
- CSharpToJavaEdgeCaseTests: 16 个边界测试
- CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试
- 从 164 增加到 179 总测试 (168 通过, 0 失败)

新增文件:
- Pipeline/Converters/NullCoalescingConverter.cs
- Pipeline/Converters/SwitchExpressionConverter.cs
- Pipeline/Converters/PrimaryConstructorConverter.cs
- Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs
- Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs
- Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
2026-06-16 07:08:11 +00:00

290 lines
9.0 KiB
C#

using System.Text;
using System.Text.RegularExpressions;
using CodePlay.Core.Interfaces;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
namespace CodePlay.Core.Validators;
public class AutoFixEngine : IAutoFixEngine
{
public Task<FixResult> FixAsync(string code, List<CompilationError> errors, int round, CancellationToken cancellationToken = default)
{
var result = new FixResult
{
CanFix = false,
RemainingErrors = new List<CompilationError>()
};
if (errors == null || errors.Count == 0)
{
result.CanFix = true;
result.FixedCode = code;
result.FixDescription = "No errors to fix";
return Task.FromResult(result);
}
result = round switch
{
1 => FixRound1(code, errors),
2 => FixRound2(code, errors),
3 => FixRound3(code, errors),
_ => new FixResult { RemainingErrors = errors }
};
return Task.FromResult(result);
}
private FixResult FixRound1(string code, List<CompilationError> errors)
{
var result = new FixResult
{
CanFix = false,
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
var needsSystemUsing = false;
var needsCollectionsUsing = false;
var needsLinqUsing = false;
foreach (var error in errors)
{
if (error.ErrorId == "CS0246" || error.ErrorId == "CS0103")
{
if (error.Message.Contains("Console"))
{
needsSystemUsing = true;
}
else if (error.Message.Contains("List") || error.Message.Contains("Dictionary"))
{
needsCollectionsUsing = true;
}
else if (error.Message.Contains("LINQ") || error.Message.Contains("var"))
{
needsLinqUsing = true;
}
else
{
result.RemainingErrors.Add(error);
}
}
else
{
result.RemainingErrors.Add(error);
}
}
var fixedCode = code;
var fixDescription = new StringBuilder();
if (needsSystemUsing && !code.Contains("using System;"))
{
fixedCode = fixedCode.Insert(0, "using System;\n");
fixDescription.Append("Added using System; ");
}
if (needsCollectionsUsing && !code.Contains("using System.Collections.Generic;"))
{
fixedCode = fixedCode.Insert(0, "using System.Collections.Generic;\n");
fixDescription.Append("Added using System.Collections.Generic;");
}
if (needsLinqUsing && !code.Contains("using System.Linq;"))
{
fixedCode = fixedCode.Insert(0, "using System.Linq;\n");
fixDescription.Append("Added using System.Linq;");
}
if (fixDescription.Length > 0)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDescription.ToString().Trim();
}
return result;
}
private FixResult FixRound2(string code, List<CompilationError> errors)
{
var result = new FixResult
{
CanFix = false,
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
var fixedCode = code;
var hasFixes = false;
var fixDescription = new StringBuilder();
foreach (var error in errors)
{
if (error.ErrorId == "CS0246")
{
if (error.Message.Contains("String"))
{
fixedCode = fixedCode.Replace("String", "string");
hasFixes = true;
fixDescription.Append("Mapped String to string; ");
}
else if (error.Message.Contains("ArrayList"))
{
fixedCode = fixedCode.Replace("ArrayList", "List<object>");
hasFixes = true;
fixDescription.Append("Mapped ArrayList to List<object>; ");
}
else if (error.Message.Contains("HashMap"))
{
fixedCode = fixedCode.Replace("HashMap", "Dictionary<object, object>");
hasFixes = true;
fixDescription.Append("Mapped HashMap to Dictionary;");
}
else
{
result.RemainingErrors.Add(error);
}
}
else
{
result.RemainingErrors.Add(error);
}
}
if (hasFixes)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDescription.ToString().Trim();
}
return result;
}
private FixResult FixRound3(string code, List<CompilationError> errors)
{
var result = new FixResult
{
CanFix = false,
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
var fixedCode = code;
var hasFixes = false;
var fixDesc = new StringBuilder();
var remainingErrors = new List<CompilationError>();
foreach (var error in errors)
{
var line = error.Line > 0 && error.Line <= code.Split('\n').Length
? code.Split('\n')[error.Line - 1].Trim()
: "";
switch (error.ErrorId)
{
case "CS0117":
hasFixes |= FixMissingMethod(ref fixedCode, error, line, fixDesc);
break;
case "CS1503":
hasFixes |= FixArgumentMismatch(ref fixedCode, error, line, fixDesc);
break;
case "CS0234":
if (error.Message.Contains("not found"))
{
var ns = Regex.Match(error.Message, @"'(.*?)'").Groups[1].Value;
fixedCode = Regex.Replace(fixedCode, $@"using\s+{Regex.Escape(ns)}[\w.]*;", "// using removed: not found");
hasFixes = true;
fixDesc.Append($"Removed unavailable namespace {ns}; ");
}
else
{
remainingErrors.Add(error);
}
break;
case "CS1002":
if (error.Line > 0)
{
var lines = fixedCode.Split('\n');
if (error.Line - 1 < lines.Length && !lines[error.Line - 1].TrimEnd().EndsWith(";"))
{
lines[error.Line - 1] = lines[error.Line - 1].TrimEnd() + ";";
fixedCode = string.Join("\n", lines);
hasFixes = true;
fixDesc.Append($"Added semicolon at line {error.Line}; ");
}
}
break;
case "CS1525":
case "CS1003":
hasFixes |= FixSyntaxError(ref fixedCode, error, line, fixDesc);
break;
default:
remainingErrors.Add(error);
break;
}
}
result.RemainingErrors = remainingErrors;
if (hasFixes)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDesc.ToString().Trim();
}
return result;
}
private bool FixMissingMethod(ref string code, CompilationError error, string line, StringBuilder desc)
{
if (error.Message.Contains("var"))
{
code = code.Replace(".var", ".var()");
desc.Append("Fixed .var to .var(); ");
return true;
}
return false;
}
private bool FixArgumentMismatch(ref string code, CompilationError error, string line, StringBuilder desc)
{
var before = code;
code = Regex.Replace(code, @"\.<[^>]+>\(", ".(");
if (code != before)
{
desc.Append("Removed generic type arguments from method call; ");
return true;
}
return false;
}
private bool FixSyntaxError(ref string code, CompilationError error, string line, StringBuilder desc)
{
var fixedAny = false;
if (line.Contains(";;"))
{
code = code.Replace(";;", ";");
desc.Append("Fixed double semicolon; ");
fixedAny = true;
}
if (line.Contains("( "))
{
code = Regex.Replace(code, @"\(\s+", "(");
desc.Append("Normalized spacing around parentheses; ");
fixedAny = true;
}
return fixedAny;
}
}