Skip to content

Commit 49accae

Browse files
committed
Fix #557: preserve named const identifier in char-to-string coalesce (strip VB Global prefix, add Simplifier annotation)
1 parent c055eee commit 49accae

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.CodeAnalysis.CSharp;
55
using Microsoft.CodeAnalysis.CSharp.Syntax;
66
using Microsoft.CodeAnalysis.Editing;
7+
using Microsoft.CodeAnalysis.Simplification;
78
using static Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions;
89
using ICSharpCode.CodeConverter.Util.FromRoslyn;
910
using ISymbolExtensions = ICSharpCode.CodeConverter.Util.ISymbolExtensions;
@@ -729,13 +730,19 @@ private static (BaseMethodDeclarationSyntax MethodBlock, BlockSyntax ConvertedSt
729730

730731
private static ExpressionSyntax BuildCharExpressionFromVbSyntax(VBSyntax.ExpressionSyntax defaultValueNode, SemanticModel semanticModel)
731732
{
732-
// For constant char literals (e.g. "^"c), reconstruct directly from the constant value
733-
var constant = semanticModel.GetConstantValue(defaultValueNode);
734-
if (constant.HasValue && constant.Value is char c) {
735-
return CS.SyntaxFactory.LiteralExpression(CS.SyntaxKind.CharacterLiteralExpression, CS.SyntaxFactory.Literal(c));
733+
// For char literal expressions (e.g. "^"c), use the constant value directly
734+
if (defaultValueNode.IsKind(VBasic.SyntaxKind.CharacterLiteralExpression)) {
735+
var constant = semanticModel.GetConstantValue(defaultValueNode);
736+
if (constant.HasValue && constant.Value is char c) {
737+
return CS.SyntaxFactory.LiteralExpression(CS.SyntaxKind.CharacterLiteralExpression, CS.SyntaxFactory.Literal(c));
738+
}
736739
}
737-
// For named constant references (e.g. DlM), use the VB expression text as-is
738-
return ValidSyntaxFactory.IdentifierName(defaultValueNode.ToString());
740+
// For named constant references (e.g. DlM or Module.DlM), build a member access expression.
741+
// Strip VB's "Global." prefix (VB global namespace qualifier, has no C# identifier equivalent).
742+
// Annotate for simplification so Roslyn reduces e.g. TestModule.DlM → DlM within TestModule.
743+
var parts = defaultValueNode.ToString().Trim().Split('.');
744+
if (parts.Length > 1 && parts[0] == "Global") parts = parts.Skip(1).ToArray();
745+
return ValidSyntaxFactory.MemberAccess(parts).WithAdditionalAnnotations(Simplifier.Annotation);
739746
}
740747

741748
private static bool IsThisResumeLayoutInvocation(StatementSyntax s)

0 commit comments

Comments
 (0)