Skip to content

Commit

Permalink
Added temporary hack to set language version "preview" when running i…
Browse files Browse the repository at this point in the history
…n the c# 11 test project
  • Loading branch information
bjornhellander committed Apr 30, 2022
1 parent c933da1 commit 6dc6195
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Expand Up @@ -22,6 +22,7 @@ namespace StyleCop.Analyzers.Test.Verifiers
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
Expand Down Expand Up @@ -112,7 +113,7 @@ public CSharpTest()
public CSharpTest(LanguageVersion? languageVersion)
{
this.ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssemblies;
this.LanguageVersion = languageVersion;
this.LanguageVersion = languageVersion ?? this.GetDefaultLanguageVersion();

this.OptionsTransforms.Add(options =>
options
Expand Down Expand Up @@ -274,6 +275,19 @@ protected override IEnumerable<CodeFixProvider> GetCodeFixProviders()
Assert.NotSame(WellKnownFixAllProviders.BatchFixer, codeFixProvider.GetFixAllProvider());
return new[] { codeFixProvider };
}

// TODO: Remove when c# 11 is a supported language version
private LanguageVersion? GetDefaultLanguageVersion()
{
// Temporary fix since c# 11 is not yet the default language version
// in the c# 11 test project.
if (LightupHelpers.SupportsCSharp11)
{
return LanguageVersionEx.Preview;
}

return null;
}
}
}
}
Expand Up @@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.Test.Verifiers
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using StyleCop.Analyzers.Lightup;

internal static class StyleCopDiagnosticVerifier<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
Expand Down Expand Up @@ -64,7 +65,7 @@ public CSharpTest()

public CSharpTest(LanguageVersion? languageVersion)
{
this.LanguageVersion = languageVersion;
this.LanguageVersion = languageVersion ?? this.GetDefaultLanguageVersion();
}

private LanguageVersion? LanguageVersion { get; }
Expand All @@ -79,6 +80,19 @@ protected override ParseOptions CreateParseOptions()

return parseOptions;
}

// TODO: Remove when c# 11 is a supported language version
private LanguageVersion? GetDefaultLanguageVersion()
{
// Temporary fix since c# 11 is not yet the default language version
// in the c# 11 test project.
if (LightupHelpers.SupportsCSharp11)
{
return LanguageVersionEx.Preview;
}

return null;
}
}
}
}
Expand Up @@ -46,6 +46,11 @@ internal static class LightupHelpers
public static bool SupportsCSharp10 { get; }
= Enum.GetNames(typeof(LanguageVersion)).Contains(nameof(LanguageVersionEx.CSharp10));

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

public static bool SupportsIOperation => SupportsCSharp73;

internal static bool CanWrapObject(object obj, Type underlyingType)
Expand Down
2 changes: 2 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs
Expand Up @@ -48,6 +48,8 @@ internal static class SyntaxKindEx
public const SyntaxKind SwitchExpressionArm = (SyntaxKind)9026;
public const SyntaxKind VarPattern = (SyntaxKind)9027;
public const SyntaxKind ParenthesizedPattern = (SyntaxKind)9028;
public const SyntaxKind SlicePattern = (SyntaxKind)9034;
public const SyntaxKind ListPattern = (SyntaxKind)9035;
public const SyntaxKind DeclarationExpression = (SyntaxKind)9040;
public const SyntaxKind RefExpression = (SyntaxKind)9050;
public const SyntaxKind RefType = (SyntaxKind)9051;
Expand Down

0 comments on commit 6dc6195

Please sign in to comment.