@@ -100,45 +100,41 @@ public override CSharpSyntaxNode VisitCompilationUnit(VBSyntax.CompilationUnitSy
100
100
var importsClauses = options . GlobalImports . Select ( gi => gi . Clause ) . Concat ( node . Imports . SelectMany ( imp => imp . ImportsClauses ) ) . ToList ( ) ;
101
101
102
102
var attributes = SyntaxFactory . List ( node . Attributes . SelectMany ( a => a . AttributeLists ) . SelectMany ( ConvertAttribute ) ) ;
103
- var convertedMembers = node . Members . Select ( m => ( MemberDeclarationSyntax ) m . Accept ( TriviaConvertingVisitor ) ) . ToReadOnlyCollection ( ) ;
104
- if ( ! string . IsNullOrEmpty ( options . RootNamespace ) )
105
- {
106
- var rootNamespaceIdentifier = SyntaxFactory . IdentifierName ( options . RootNamespace ) ;
107
- convertedMembers = PrependRootNamespace ( convertedMembers , rootNamespaceIdentifier ) ;
108
- }
103
+ var sourceAndConverted = node . Members . Select ( m => ( Source : m , Converted : ( MemberDeclarationSyntax ) m . Accept ( TriviaConvertingVisitor ) ) ) . ToReadOnlyCollection ( ) ;
104
+ var convertedMembers = string . IsNullOrEmpty ( options . RootNamespace )
105
+ ? sourceAndConverted . Select ( sd => sd . Converted )
106
+ : PrependRootNamespace ( sourceAndConverted , SyntaxFactory . IdentifierName ( options . RootNamespace ) ) ;
109
107
108
+ var usingDirectiveSyntax = importsClauses . GroupBy ( c => c . ToString ( ) ) . Select ( g => g . First ( ) )
109
+ . Select ( c => ( UsingDirectiveSyntax ) c . Accept ( TriviaConvertingVisitor ) ) ;
110
110
return SyntaxFactory . CompilationUnit (
111
111
SyntaxFactory . List < ExternAliasDirectiveSyntax > ( ) ,
112
- SyntaxFactory . List ( importsClauses . GroupBy ( c => c . ToString ( ) ) . Select ( g => g . First ( ) ) . Select ( c => ( UsingDirectiveSyntax ) c . Accept ( TriviaConvertingVisitor ) ) ) ,
112
+ SyntaxFactory . List ( usingDirectiveSyntax ) ,
113
113
attributes ,
114
114
SyntaxFactory . List ( convertedMembers )
115
115
) ;
116
116
}
117
117
118
118
private IReadOnlyCollection < MemberDeclarationSyntax > PrependRootNamespace (
119
- IReadOnlyCollection < MemberDeclarationSyntax > memberDeclarations ,
119
+ IReadOnlyCollection < ( VBSyntax . StatementSyntax VbNode , MemberDeclarationSyntax CsNode ) > memberConversion ,
120
120
IdentifierNameSyntax rootNamespaceIdentifier )
121
121
{
122
- if ( memberDeclarations . Count == 1 && memberDeclarations . First ( ) is NamespaceDeclarationSyntax nsDecl ) {
123
- return memberDeclarations ;
122
+ var inGlobalNamespace = memberConversion
123
+ . ToLookup ( m => IsNamespaceDeclarationInGlobalScope ( m . VbNode ) , m => m . CsNode ) ;
124
+ var members = inGlobalNamespace [ true ] . ToList ( ) ;
125
+ if ( inGlobalNamespace [ false ] . Any ( ) ) {
126
+ var newNamespaceDecl = ( MemberDeclarationSyntax ) SyntaxFactory . NamespaceDeclaration ( rootNamespaceIdentifier )
127
+ . WithMembers ( SyntaxFactory . List ( inGlobalNamespace [ false ] ) ) ;
128
+ members . Add ( newNamespaceDecl ) ;
124
129
}
125
-
126
- var newNamespaceDecl = ( MemberDeclarationSyntax ) SyntaxFactory . NamespaceDeclaration ( rootNamespaceIdentifier )
127
- . WithMembers ( SyntaxFactory . List ( memberDeclarations ) ) ;
128
- return new [ ] { newNamespaceDecl } ;
130
+ return members ;
129
131
}
130
132
131
- private NameSyntax PrependName ( NameSyntax name , IdentifierNameSyntax toPrepend )
133
+ private bool IsNamespaceDeclarationInGlobalScope ( VBSyntax . StatementSyntax m )
132
134
{
133
- if ( name is IdentifierNameSyntax identName ) {
134
- return SyntaxFactory . QualifiedName ( toPrepend , identName ) ;
135
- }
136
- else if ( name is QualifiedNameSyntax qName ) {
137
- return SyntaxFactory . QualifiedName ( PrependName ( qName . Left , toPrepend ) , qName . Right ) ;
138
- }
139
- else {
140
- throw new ArgumentOutOfRangeException ( nameof ( name ) , name , $ "{ name . GetType ( ) . Name } of kind { name . Kind ( ) } not expected within namespace declaration") ;
141
- }
135
+ if ( ! ( m is VBSyntax . NamespaceBlockSyntax nss ) ) return false ;
136
+ if ( ! ( _semanticModel . GetSymbolInfo ( nss . NamespaceStatement . Name ) . Symbol is INamespaceSymbol nsSymbol ) ) return false ;
137
+ return nsSymbol . ContainingNamespace . IsGlobalNamespace ;
142
138
}
143
139
144
140
public override CSharpSyntaxNode VisitSimpleImportsClause ( VBSyntax . SimpleImportsClauseSyntax node )
0 commit comments