Skip to content

Commit

Permalink
VB -> C#: Convert date literals - fixes #159
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Nov 18, 2018
1 parent 3f24c1b commit b6ff932
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 348 deletions.
6 changes: 6 additions & 0 deletions ICSharpCode.CodeConverter/CSharp/CommonConversions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.Util;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -183,6 +184,11 @@ internal ExpressionSyntax GetLiteralExpression(object value, string fullText = n
if (value is char)
return SyntaxFactory.LiteralExpression(Microsoft.CodeAnalysis.CSharp.SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal((char)value));

if (value is DateTime dt) {
var valueToOutput = dt.Date.Equals(dt) ? dt.ToString("yyyy-MM-dd") : dt.ToString("yyyy-MM-dd HH:mm:ss");
return SyntaxFactory.ParseExpression("DateTime.Parse(\"" + valueToOutput + "\")");
}


throw new ArgumentOutOfRangeException(nameof(value), value, null);
}
Expand Down
24 changes: 20 additions & 4 deletions ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
using VBasic = Microsoft.CodeAnalysis.VisualBasic;
using SyntaxToken = Microsoft.CodeAnalysis.SyntaxToken;
Expand Down Expand Up @@ -821,17 +822,32 @@ public override CSharpSyntaxNode VisitParameter(VBSyntax.ParameterSyntax node)
returnType = arrayType.WithElementType(SyntaxFactory.NullableType(arrayType.ElementType));
}
}
EqualsValueClauseSyntax @default = null;
if (node.Default != null) {
@default = SyntaxFactory.EqualsValueClause((ExpressionSyntax)node.Default?.Value.Accept(TriviaConvertingVisitor));
}

var attributes = node.AttributeLists.SelectMany(ConvertAttribute).ToList();
int outAttributeIndex = attributes.FindIndex(a => a.Attributes.Single().Name.ToString() == "Out");
var modifiers = CommonConversions.ConvertModifiers(node.Modifiers, TokenContext.Local);
if (outAttributeIndex > -1) {
attributes.RemoveAt(outAttributeIndex);
modifiers = modifiers.Replace(SyntaxFactory.Token(SyntaxKind.RefKeyword), SyntaxFactory.Token(SyntaxKind.OutKeyword));
}

EqualsValueClauseSyntax @default = null;
if (node.Default != null) {
if (node.Default.Value is VBSyntax.LiteralExpressionSyntax les && les.Token.Value is DateTime dt)
{
var dateTimeAsLongCsLiteral = CommonConversions.GetLiteralExpression(dt.Ticks, dt.Ticks + "L");
var dateTimeArg = CommonConversions.CreateAttributeArgumentList(SyntaxFactory.AttributeArgument(dateTimeAsLongCsLiteral));
var optionalDateTimeAttributes = new[] {
SyntaxFactory.Attribute(SyntaxFactory.ParseName("System.Runtime.InteropServices.Optional")),
SyntaxFactory.Attribute(SyntaxFactory.ParseName("System.Runtime.CompilerServices.DateTimeConstant"), dateTimeArg)
};
attributes.Insert(0,
SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(optionalDateTimeAttributes)));
} else {
@default = SyntaxFactory.EqualsValueClause(
(ExpressionSyntax)node.Default.Value.Accept(TriviaConvertingVisitor));
}
}

if (node.Parent.Parent is VBSyntax.MethodStatementSyntax mss
&& mss.AttributeLists.Any(HasExtensionAttribute) && node.Parent.ChildNodes().First() == node) {
Expand Down
Loading

0 comments on commit b6ff932

Please sign in to comment.