Skip to content

Commit

Permalink
C# -> VB: Concatenate inner namespaces with parent namespase
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexilia authored and GrahamTheCoder committed Dec 10, 2019
1 parent 0742b0c commit 65e5927
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
11 changes: 11 additions & 0 deletions ICSharpCode.CodeConverter/VB/NodesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ public override VisualBasicSyntaxNode VisitUsingDirective(CSS.UsingDirectiveSynt
var id = _commonConversions.ConvertIdentifier(name.Identifier);
alias = SyntaxFactory.ImportAliasClause(id);
}
var identifierName = node.Name as CSS.IdentifierNameSyntax;
var parentSymbol = _semanticModel.GetDeclaredSymbol(node.Parent) as INamespaceSymbol;
INamespaceSymbol fullNamespace = null;
if (identifierName != null && parentSymbol != null) {
fullNamespace = _semanticModel.LookupNamespacesAndTypes(node.SpanStart, null, identifierName.Identifier.ValueText)
.OfType<INamespaceSymbol>().FirstOrDefault();
}
if (fullNamespace != null) {
_allImports.Add((ImportsStatementSyntax)_vbSyntaxGenerator.NamespaceImportDeclaration(fullNamespace.ToDisplayString()));
return null;
}
ImportsClauseSyntax clause = SyntaxFactory.SimpleImportsClause(alias, (NameSyntax)node.Name.Accept(TriviaConvertingVisitor));
var import = SyntaxFactory.ImportsStatement(SyntaxFactory.SingletonSeparatedList(clause));
_allImports.Add(import);
Expand Down
11 changes: 11 additions & 0 deletions Tests/TestRunners/ConverterTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.VB;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic;
using Xunit;
using Xunit.Sdk;

Expand All @@ -19,9 +21,18 @@ public class ConverterTestBase
private bool _testCstoVBCommentsByDefault = false;
private readonly string _rootNamespace;

protected TextConversionOptions EmptyNamespaceOptionStrictOff { get; set; }

public ConverterTestBase(string rootNamespace = null)
{
_rootNamespace = rootNamespace;
EmptyNamespaceOptionStrictOff = new TextConversionOptions(DefaultReferences.NetStandard2) {
RootNamespaceOverride = string.Empty, TargetCompilationOptionsOverride = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOptionExplicit(true)
.WithOptionCompareText(false)
.WithOptionStrict(OptionStrict.Off)
.WithOptionInfer(true)
};
}

protected static async Task<string> GetConvertedCodeOrErrorString<TLanguageConversion>(string toConvert, TextConversionOptions conversionOptions = default) where TLanguageConversion : ILanguageConversion, new()
Expand Down
10 changes: 0 additions & 10 deletions Tests/VB/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ namespace CodeConverter.Tests.VB
{
public class MemberTests : ConverterTestBase
{
TextConversionOptions EmptyNamespaceOptionStrictOff { get; set; }

public MemberTests() {
EmptyNamespaceOptionStrictOff = new TextConversionOptions(DefaultReferences.NetStandard2) { RootNamespaceOverride = string.Empty, TargetCompilationOptionsOverride = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOptionExplicit(true)
.WithOptionCompareText(false)
.WithOptionStrict(OptionStrict.Off)
.WithOptionInfer(true)
};
}
[Fact]
public async Task TestPropertyWithModifier()
{
Expand Down
17 changes: 17 additions & 0 deletions Tests/VB/NamespaceLevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,24 @@ public async Task MoveImportsStatement()
Namespace test
End Namespace");
}
[Fact]
public async Task InnerNamespace_MoveImportsStatement()
{
await TestConversionCSharpToVisualBasic(
@"namespace System {
using Collections;
public class TestClass {
public Hashtable Property { get; set; }
}
}",
@"Imports System.Collections
Namespace System
Public Class TestClass
Public Property [Property] As Hashtable
End Class
End Namespace", conversion: EmptyNamespaceOptionStrictOff);
}
[Fact]
public async Task ClassImplementsInterface()
{
Expand Down

0 comments on commit 65e5927

Please # to comment.