Skip to content

Commit

Permalink
Check C# version for EC83 (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djoums authored Nov 9, 2024
1 parent 28b933b commit c83fa10
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
30 changes: 15 additions & 15 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" ExcludeAssets="compile; runtime" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" PrivateAssets="all" ExcludeAssets="compile; runtime" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.4" />
<PackageVersion Include="Microsoft.Build" Version="17.10.4" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
<PackageVersion Include="Microsoft.Build" Version="17.11.4" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageVersion Include="Microsoft.CodeAnalysis" Version="4.10.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageVersion Include="Microsoft.CodeAnalysis" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.10.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.11.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.5.0" />
<PackageVersion Include="MSTest.TestFramework" Version="3.5.0" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.4" />
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.12.4" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.2" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.9" />
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.12.9" />
<PackageVersion Include="Spectre.Console" Version="0.49.1" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
</Project>
</Project>
13 changes: 13 additions & 0 deletions nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>

<packageSourceMapping>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public sealed class ReplaceEnumToStringWithNameOfFixer : CodeFixProvider
/// <inheritdoc/>
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
if (context.Diagnostics.Length == 0) return;
if (context.Diagnostics.Length == 0 || context.Document.GetLanguageVersion() < LanguageVersion.CSharp6)
return;

var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root is null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private static bool UsesConstantFormat(object? format) =>

private static void AnalyzeInvocation(OperationAnalysisContext context)
{
if (context.Operation is not IInvocationOperation { TargetMethod.Name: nameof(object.ToString) } operation ||
if (context.Compilation.GetLanguageVersion() < LanguageVersion.CSharp6 ||
context.Operation is not IInvocationOperation { TargetMethod.Name: nameof(object.ToString) } operation ||
!SymbolEqualityComparer.Default.Equals(operation.TargetMethod.ContainingType, context.Compilation.GetSpecialType(SpecialType.System_Enum)) ||
operation.Instance is not IMemberReferenceOperation { Member.ContainingType.EnumUnderlyingType: { } })
{
Expand All @@ -62,7 +63,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context)

private static void AnalyzeInterpolation(OperationAnalysisContext context)
{
if (context.Operation is not IInterpolationOperation operation ||
if (context.Compilation.GetLanguageVersion() < LanguageVersion.CSharp6 ||
context.Operation is not IInterpolationOperation operation ||
operation.Expression is not IMemberReferenceOperation { Member.ContainingType.EnumUnderlyingType: { } } ||
operation.FormatString is ILiteralOperation { ConstantValue: { HasValue: true, Value: var value } } && !UsesConstantFormat(value))
{
Expand Down
6 changes: 6 additions & 0 deletions src/EcoCode.Core/Extensions/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
/// <summary>Extension methods for <see cref="Compilation"/>.</summary>
public static class CompilationExtensions
{
/// <summary>Returns the language version of the compilation.</summary>
/// <param name="compilation">The compilation.</param>
/// <returns>The language version.</returns>
public static LanguageVersion GetLanguageVersion(this Compilation compilation) =>
((CSharpCompilation)compilation).LanguageVersion;

/// <summary>
/// Gets a type by its metadata name to use for code analysis within a <see cref="Compilation"/>. This method
/// attempts to find the "best" symbol to use for code analysis, which is the symbol matching the first of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ public sealed class ReplaceEnumToStringWithNameOfTests
[TestMethod]
public Task EmptyCodeAsync() => VerifyAsync("");

[TestMethod]
public Task EnumToStringShouldNotBeNameOfAsync() => VerifyAsync("""
using System;
public static class Program
{
private enum MyEnum { A, B, C, D }
public static void Main()
{
Console.WriteLine(MyEnum.A.ToString());
Console.WriteLine(MyEnum.B.ToString(""));
Console.WriteLine(MyEnum.C.ToString(string.Empty));
Console.WriteLine(MyEnum.D.ToString(format: null));
}
}
""", languageVersion: LanguageVersion.CSharp5);

[TestMethod]
public Task EnumToStringShouldBeNameOfAsync1() => VerifyAsync("""
using System;
Expand Down

0 comments on commit c83fa10

Please sign in to comment.