Skip to content

Commit 3c55643

Browse files
authored
Merge pull request #3580 from bjornhellander/feature/new-roslyn-test11
Update Microsoft.CodeAnalysis.CSharp.Workspaces to version 4.4.0 for the c# 11 test project
2 parents 2f6d02f + 349e8e3 commit 3c55643

File tree

5 files changed

+90
-21
lines changed

5 files changed

+90
-21
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0-4.final" />
20+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
2121
<PackageReference Include="xunit" Version="2.4.1" />
2222
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
2323
</ItemGroup>

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.Test.Verifiers
77
{
8+
using System;
89
using System.Collections.Generic;
910
using System.IO;
1011
using System.Linq;
@@ -95,6 +96,10 @@ internal class CSharpTest : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier
9596
private const int DefaultTabSize = 4;
9697
private const bool DefaultUseTabs = false;
9798

99+
private int indentationSize = DefaultIndentationSize;
100+
private bool useTabs = DefaultUseTabs;
101+
private int tabSize = DefaultTabSize;
102+
98103
static CSharpTest()
99104
{
100105
// If we have outdated defaults from the host unit test application targeting an older .NET Framework,
@@ -200,7 +205,24 @@ public CSharpTest(LanguageVersion? languageVersion)
200205
/// <value>
201206
/// The value of the <see cref="FormattingOptions.IndentationSize"/> to apply to the test workspace.
202207
/// </value>
203-
public int IndentationSize { get; set; } = DefaultIndentationSize;
208+
public int IndentationSize
209+
{
210+
get
211+
{
212+
return this.indentationSize;
213+
}
214+
215+
set
216+
{
217+
if (this.indentationSize == value)
218+
{
219+
return;
220+
}
221+
222+
this.indentationSize = value;
223+
this.UpdateGlobalAnalyzerConfig();
224+
}
225+
}
204226

205227
/// <summary>
206228
/// Gets or sets a value indicating whether the <see cref="FormattingOptions.UseTabs"/> option is applied to the
@@ -209,15 +231,49 @@ public CSharpTest(LanguageVersion? languageVersion)
209231
/// <value>
210232
/// The value of the <see cref="FormattingOptions.UseTabs"/> to apply to the test workspace.
211233
/// </value>
212-
public bool UseTabs { get; set; } = DefaultUseTabs;
234+
public bool UseTabs
235+
{
236+
get
237+
{
238+
return this.useTabs;
239+
}
240+
241+
set
242+
{
243+
if (this.useTabs == value)
244+
{
245+
return;
246+
}
247+
248+
this.useTabs = value;
249+
this.UpdateGlobalAnalyzerConfig();
250+
}
251+
}
213252

214253
/// <summary>
215254
/// Gets or sets the value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
216255
/// </summary>
217256
/// <value>
218257
/// The value of the <see cref="FormattingOptions.TabSize"/> to apply to the test workspace.
219258
/// </value>
220-
public int TabSize { get; set; } = DefaultTabSize;
259+
public int TabSize
260+
{
261+
get
262+
{
263+
return this.tabSize;
264+
}
265+
266+
set
267+
{
268+
if (this.tabSize == value)
269+
{
270+
return;
271+
}
272+
273+
this.tabSize = value;
274+
this.UpdateGlobalAnalyzerConfig();
275+
}
276+
}
221277

222278
/// <summary>
223279
/// Gets or sets the content of the settings file to use.
@@ -276,16 +332,37 @@ protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
276332
return new[] { codeFixProvider };
277333
}
278334

279-
// TODO: Remove when c# 11 is a supported language version
280-
private LanguageVersion? GetDefaultLanguageVersion()
335+
private void UpdateGlobalAnalyzerConfig()
281336
{
282-
// Temporary fix since c# 11 is not yet the default language version
283-
// in the c# 11 test project.
284-
if (LightupHelpers.SupportsCSharp11)
337+
if (!LightupHelpers.SupportsCSharp11)
285338
{
286-
return LanguageVersionEx.Preview;
339+
// Options support workspace options in this version
340+
// https://github.com/dotnet/roslyn/issues/66779
341+
return;
287342
}
288343

344+
if (this.TestState.AnalyzerConfigFiles.Count == 1
345+
&& this.TestState.AnalyzerConfigFiles[0].filename == "/.globalconfig")
346+
{
347+
this.TestState.AnalyzerConfigFiles.RemoveAt(0);
348+
}
349+
else if (this.TestState.AnalyzerConfigFiles.Count > 1
350+
|| (this.TestState.AnalyzerConfigFiles.Count > 0 && this.TestState.AnalyzerConfigFiles[0].filename != "/.globalconfig"))
351+
{
352+
throw new NotSupportedException("Additional configuration files are not currently supported by the test");
353+
}
354+
355+
this.TestState.AnalyzerConfigFiles.Add(("/.globalconfig", $@"is_global = true
356+
357+
indent_size = {this.IndentationSize}
358+
indent_style = {(this.UseTabs ? "tab" : "space")}
359+
tab_width = {this.TabSize}
360+
"));
361+
}
362+
363+
// NOTE: If needed, this method can be temporarily updated to default to a preview version
364+
private LanguageVersion? GetDefaultLanguageVersion()
365+
{
289366
return null;
290367
}
291368
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopDiagnosticVerifier`1.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,9 @@ protected override ParseOptions CreateParseOptions()
8181
return parseOptions;
8282
}
8383

84-
// TODO: Remove when c# 11 is a supported language version
84+
// NOTE: If needed, this method can be temporarily updated to default to a preview version
8585
private LanguageVersion? GetDefaultLanguageVersion()
8686
{
87-
// Temporary fix since c# 11 is not yet the default language version
88-
// in the c# 11 test project.
89-
if (LightupHelpers.SupportsCSharp11)
90-
{
91-
return LanguageVersionEx.Preview;
92-
}
93-
9487
return null;
9588
}
9689
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LanguageVersionEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal static class LanguageVersionEx
1919
public const LanguageVersion CSharp8 = (LanguageVersion)800;
2020
public const LanguageVersion CSharp9 = (LanguageVersion)900;
2121
public const LanguageVersion CSharp10 = (LanguageVersion)1000;
22+
public const LanguageVersion CSharp11 = (LanguageVersion)1100;
2223
public const LanguageVersion LatestMajor = (LanguageVersion)int.MaxValue - 2;
2324
public const LanguageVersion Preview = (LanguageVersion)int.MaxValue - 1;
2425
public const LanguageVersion Latest = (LanguageVersion)int.MaxValue;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<Operatio
4646
public static bool SupportsCSharp10 { get; }
4747
= Enum.GetNames(typeof(LanguageVersion)).Contains(nameof(LanguageVersionEx.CSharp10));
4848

49-
// NOTE: Since c# is only in preview yet, we need to check something else than available language versions. Picked a new syntax kind temporarily.
50-
// TODO: Update when c# 11 is available as a language version.
5149
public static bool SupportsCSharp11 { get; }
52-
= Enum.GetNames(typeof(SyntaxKind)).Contains(nameof(SyntaxKindEx.SlicePattern));
50+
= Enum.GetNames(typeof(LanguageVersion)).Contains(nameof(LanguageVersionEx.CSharp11));
5351

5452
public static bool SupportsIOperation => SupportsCSharp73;
5553

0 commit comments

Comments
 (0)