Convert DateAndTime to DateTime

Some things like DateString and setting the system time won't work, but we can always add more special cases later
This commit is contained in:
GrahamTheCoder 2018-05-05 15:29:01 +01:00
Родитель 84fee70bd4
Коммит 751e6001f9
4 изменённых файлов: 45 добавлений и 22 удалений

Просмотреть файл

@ -48,8 +48,16 @@ namespace ICSharpCode.CodeConverter.CSharp
.Select(d => compilationUnitSyntax.FindNode(d.Location.SourceSpan))
.OfType<UsingDirectiveSyntax>()
.ToList();
if (diagnostics.All(d => d.Id != UnresolvedTypeOrNamespaceDiagnosticId) && unusedUsings.Any()) {
compilationUnitSyntax = compilationUnitSyntax.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepNoTrivia);
var nodesWithUnresolvedTypes = diagnostics
.Where(d => d.Id == UnresolvedTypeOrNamespaceDiagnosticId && d.Location.IsInSource)
.Select(d => compilationUnitSyntax.FindNode(d.Location.SourceSpan))
.ToLookup(d => d.GetAncestor<UsingDirectiveSyntax>());
unusedUsings = unusedUsings.Except(nodesWithUnresolvedTypes.Select(g => g.Key)).ToList();
if (!nodesWithUnresolvedTypes[null].Any() && unusedUsings.Any()) {
compilationUnitSyntax =
compilationUnitSyntax.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepNoTrivia);
}
return compilationUnitSyntax;

Просмотреть файл

@ -1549,7 +1549,7 @@ namespace ICSharpCode.CodeConverter.CSharp
// CSharp allows partial qualification within the current type's parent namespace
qualifiedName = qualifiedName.Substring(firstMatch.Length);
}
else if (!targetSymbolInfo.IsNamespace() && _importedNamespaces.ContainsKey(typeOrNamespace))
else if (!targetSymbolInfo.IsNamespace() && _importedNamespaces.ContainsKey(typeOrNamespace) && qualifiedName.StartsWith(typeOrNamespace))
{
// An import matches the entire namespace, which means it's not a partially qualified thing that would need extra help in CSharp
qualifiedName = qualifiedName.Substring(typeOrNamespace.Length + 1);

Просмотреть файл

@ -11,6 +11,8 @@ namespace ICSharpCode.CodeConverter.Util
#endif
static class ISymbolExtensions
{
private static readonly string[] TypesToConvertToDateTime = new[] {"DateTime", "DateAndTime" };
/// <summary>
/// Checks if 'symbol' is accessible from within 'within'.
/// </summary>
@ -181,16 +183,41 @@ namespace ICSharpCode.CodeConverter.Util
public static string ToMinimalCSharpDisplayString(this ISymbol symbol, SemanticModel vbSemanticModel, int position, SymbolDisplayFormat format = null)
{
var output = symbol.ToMinimalDisplayString(vbSemanticModel, position, format);
if (symbol is ITypeSymbol && output == "Date") return "DateTime";
return output;
if (TryGetSpecialVBTypeConversion(symbol, out var cSharpDisplayString)) return cSharpDisplayString;
return symbol.ToMinimalDisplayString(vbSemanticModel, position, format);
}
public static string ToCSharpDisplayString(this ISymbol symbol, SymbolDisplayFormat format = null)
{
var output = symbol.ToDisplayString(format);
if (symbol is ITypeSymbol && output == "Date") return "DateTime";
return output;
if (TryGetSpecialVBTypeConversion(symbol, out var cSharpDisplayString)) return cSharpDisplayString;
return symbol.ToDisplayString(format);
}
private static bool TryGetSpecialVBTypeConversion(ISymbol symbol, out string cSharpDisplayString)
{
var containingNamespace = symbol?.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat);
if (containingNamespace == "Microsoft.VisualBasic" || containingNamespace == "System") {
if (symbol is ITypeSymbol && TypesToConvertToDateTime.Contains(symbol.Name)) {
{
cSharpDisplayString = "DateTime";
return true;
}
} else if (TypesToConvertToDateTime.Contains(symbol.ContainingType?.Name)) {
{
cSharpDisplayString = "DateTime" + "." + symbol.Name;
return true;
}
}
}
cSharpDisplayString = null;
return false;
}
private static bool ShouldConvertToDateTime(ITypeSymbol symbol, string fullName)
{
return TypesToConvertToDateTime.Contains(symbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat));
}
public static bool IsPartialImplementation(this ISymbol declaredSymbol)

Просмотреть файл

@ -54,19 +54,7 @@ public class Test
{
TestConversionVisualBasicToCSharp(
@"Imports UnrecognizedNamespace",
@"using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using UnrecognizedNamespace;");
@"using UnrecognizedNamespace;");
}
[Fact]