Skip to content

Commit 16cc59d

Browse files
committed
Implement global keyword support in using directives
Fixes #3502 Fixes #3413 Fixes #3404
1 parent 6b7717a commit 16cc59d

21 files changed

+790
-39
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1208CSharp10UnitTests.cs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
109
using StyleCop.Analyzers.Test.CSharp9.OrderingRules;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -63,5 +62,72 @@ class A
6362
},
6463
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
6564
}
65+
66+
[Fact]
67+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
68+
public async Task TestWhenSystemGlobalUsingDirectivesAreNotOnTopAsync()
69+
{
70+
await new CSharpTest
71+
{
72+
TestSources =
73+
{
74+
"namespace Xyz {}",
75+
"namespace AnotherNamespace {}",
76+
@"
77+
global using Xyz;
78+
{|#0:global using System;|}
79+
{|#1:global using System.IO;|}
80+
global using AnotherNamespace;
81+
{|#2:global using System.Threading.Tasks;|}
82+
83+
class A
84+
{
85+
}",
86+
},
87+
FixedSources =
88+
{
89+
"namespace Xyz {}",
90+
"namespace AnotherNamespace {}",
91+
@"
92+
global using System;
93+
global using System.IO;
94+
global using System.Threading.Tasks;
95+
global using AnotherNamespace;
96+
global using Xyz;
97+
98+
class A
99+
{
100+
}",
101+
},
102+
ExpectedDiagnostics =
103+
{
104+
Diagnostic().WithLocation(0).WithArguments("System", "Xyz"),
105+
Diagnostic().WithLocation(1).WithArguments("System.IO", "Xyz"),
106+
Diagnostic().WithLocation(2).WithArguments("System.Threading.Tasks", "Xyz"),
107+
},
108+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
109+
}
110+
111+
[Fact]
112+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
113+
public async Task TestGlobalUsingDirectivesAreAnalyzedIndependentlyFromLocalUsingDirectivesAsync()
114+
{
115+
var testCode = @"global using Xyz;
116+
117+
using System;
118+
119+
namespace Xyz
120+
{
121+
public class Placeholder
122+
{
123+
}
124+
}
125+
126+
class TestClass
127+
{
128+
}";
129+
130+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
131+
}
66132
}
67133
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1209CSharp10UnitTests.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
75
{
86
using System.Threading;
@@ -47,5 +45,68 @@ class A
4745

4846
await VerifyCSharpFixAsync(testCodeNamespace, DiagnosticResult.EmptyDiagnosticResults, fixedTestCodeNamespace, CancellationToken.None).ConfigureAwait(false);
4947
}
48+
49+
[Fact]
50+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
51+
public async Task TestWhenGlobalUsingAliasDirectivesAreNotPlacedCorrectlyAsync()
52+
{
53+
await new CSharpTest
54+
{
55+
TestSources =
56+
{
57+
"namespace Xyz {}",
58+
"namespace AnotherNamespace {}",
59+
@"
60+
{|#0:global using Alias = System.Threading.Tasks;|}
61+
global using System;
62+
global using AnotherNamespace;
63+
global using System.Threading;
64+
65+
class A
66+
{
67+
}",
68+
},
69+
FixedSources =
70+
{
71+
"namespace Xyz {}",
72+
"namespace AnotherNamespace {}",
73+
@"
74+
global using System;
75+
global using System.Threading;
76+
global using AnotherNamespace;
77+
global using Alias = System.Threading.Tasks;
78+
79+
class A
80+
{
81+
}",
82+
},
83+
ExpectedDiagnostics =
84+
{
85+
Diagnostic().WithLocation(0),
86+
},
87+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
88+
}
89+
90+
[Fact]
91+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
92+
public async Task TestGlobalUsingAliasDirectivesAreAnalyzedIndependentlyFromLocalUsingDirectivesAsync()
93+
{
94+
var testCode = @"global using Alias = System.IO;
95+
96+
using System;
97+
98+
namespace Xyz
99+
{
100+
public class Placeholder
101+
{
102+
}
103+
}
104+
105+
class TestClass
106+
{
107+
}";
108+
109+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
110+
}
50111
}
51112
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
75
{
86
using System.Threading;
@@ -53,5 +51,74 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
5351
},
5452
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5553
}
54+
55+
[Fact]
56+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
57+
public async Task TestWhenGlobalUsingDirectivesAreNotOrderedAlphabeticallyAsync()
58+
{
59+
await new CSharpTest
60+
{
61+
TestSources =
62+
{
63+
"namespace AlphaNamespace {}",
64+
"namespace BetaNamespace {}",
65+
"namespace ZNamespace {}",
66+
@"
67+
{|#0:global using ZNamespace;|}
68+
global using AlphaNamespace;
69+
global using BetaNamespace;
70+
71+
class TestClass
72+
{
73+
}
74+
",
75+
},
76+
FixedSources =
77+
{
78+
"namespace AlphaNamespace {}",
79+
"namespace BetaNamespace {}",
80+
"namespace ZNamespace {}",
81+
@"
82+
global using AlphaNamespace;
83+
global using BetaNamespace;
84+
global using ZNamespace;
85+
86+
class TestClass
87+
{
88+
}
89+
",
90+
},
91+
ExpectedDiagnostics =
92+
{
93+
Diagnostic().WithLocation(0),
94+
},
95+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
96+
}
97+
98+
[Fact]
99+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
100+
public async Task TestGlobalUsingDirectivesAreAnalyzedIndependentlyFromLocalUsingDirectivesAsync()
101+
{
102+
var testCode = @"global using ZNamespace;
103+
104+
using AlphaNamespace;
105+
106+
namespace ZNamespace
107+
{
108+
}
109+
110+
namespace AlphaNamespace
111+
{
112+
public class Placeholder
113+
{
114+
}
115+
}
116+
117+
class TestClass
118+
{
119+
}";
120+
121+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
122+
}
56123
}
57124
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CombinedSystemDirectivesCSharp10UnitTests.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
109
using StyleCop.Analyzers.Test.CSharp9.OrderingRules;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -53,5 +52,75 @@ public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync()
5352
Settings = CombinedUsingDirectivesTestSettings,
5453
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5554
}
55+
56+
[Fact]
57+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
58+
public async Task TestWhenGlobalUsingDirectivesAreNotOrderedAlphabeticallyAsync()
59+
{
60+
await new CSharpTest
61+
{
62+
TestSources =
63+
{
64+
"namespace AlphaNamespace {}",
65+
"namespace BetaNamespace {}",
66+
"namespace ZNamespace {}",
67+
@"
68+
{|#0:global using ZNamespace;|}
69+
global using AlphaNamespace;
70+
global using BetaNamespace;
71+
72+
class TestClass
73+
{
74+
}
75+
",
76+
},
77+
FixedSources =
78+
{
79+
"namespace AlphaNamespace {}",
80+
"namespace BetaNamespace {}",
81+
"namespace ZNamespace {}",
82+
@"
83+
global using AlphaNamespace;
84+
global using BetaNamespace;
85+
global using ZNamespace;
86+
87+
class TestClass
88+
{
89+
}
90+
",
91+
},
92+
ExpectedDiagnostics =
93+
{
94+
Diagnostic().WithLocation(0),
95+
},
96+
Settings = CombinedUsingDirectivesTestSettings,
97+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
98+
}
99+
100+
[Fact]
101+
[WorkItem(3964, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3964")]
102+
public async Task TestGlobalUsingDirectivesAreAnalyzedIndependentlyFromLocalUsingDirectivesAsync()
103+
{
104+
var testCode = @"global using ZNamespace;
105+
106+
using AlphaNamespace;
107+
108+
namespace ZNamespace
109+
{
110+
}
111+
112+
namespace AlphaNamespace
113+
{
114+
public class Placeholder
115+
{
116+
}
117+
}
118+
119+
class TestClass
120+
{
121+
}";
122+
123+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
124+
}
56125
}
57126
}

0 commit comments

Comments
 (0)