Skip to content

Retarget analysers from .NETStandard 2.1 to 2.0 #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: v8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
<PackageReference Update="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.Bcl.Memory" Version="9.0.0-preview.7.24405.7" />
</ItemGroup>

<PropertyGroup>
<CodeAnalysisSettingsLocation>$(MSBuildThisFileDirectory)</CodeAnalysisSettingsLocation>
<CodeAnalysisRuleSet>$(CodeAnalysisSettingsLocation)LeanCode.CodeAnalysis.ruleset</CodeAnalysisRuleSet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)

if (TryGetCommandValidator(type, out var commandValidator))
{
var expectedName = GetCommandValidatorExpectedName(commandValidator);
var expectedName = GetCommandValidatorExpectedName(commandValidator!);

if (type.Name != expectedName)
{
Expand All @@ -61,10 +61,7 @@ internal static string GetCommandValidatorExpectedName(INamedTypeSymbol commandV
);
}

private static bool TryGetCommandValidator(
INamedTypeSymbol type,
[NotNullWhen(true)] out INamedTypeSymbol? commandValidator
)
private static bool TryGetCommandValidator(INamedTypeSymbol type, out INamedTypeSymbol? commandValidator)
{
var validator = GetImplementedValidator(type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)

var tree = type.DeclaringSyntaxReferences.First().SyntaxTree;

if (!CommandIsValidated(commandType, tree, context.SemanticModel))
if (!CommandIsValidated(commandType!, tree, context.SemanticModel))
{
var diagnostic = Diagnostic.Create(Rule, type.Locations[0], commandType.Name);
var diagnostic = Diagnostic.Create(Rule, type.Locations[0], commandType!.Name);
context.ReportDiagnostic(diagnostic);
}
}

private static bool IsCommandHandler(INamedTypeSymbol type, [NotNullWhen(true)] out INamedTypeSymbol? commandType)
private static bool IsCommandHandler(INamedTypeSymbol type, out INamedTypeSymbol? commandType)
{
var handler = type.AllInterfaces.FirstOrDefault(i => i.GetFullNamespaceName() == HandlerTypeName);

Expand Down
4 changes: 3 additions & 1 deletion src/Tools/LeanCode.CodeAnalysis/LeanCode.CodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
<NoPackageAnalysis>true</NoPackageAnalysis>

<!-- It needs to target .NETStandard, otherwise it won't work in Omnisharp. See next comment for details. -->
<TargetFramework>netstandard2.1</TargetFramework>
<!-- It also needs to be .NETStandard 2.0, because msbuild in Visual Studio does not support 2.1. -->
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="All" />
<PackageReference Include="Microsoft.Bcl.Memory" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Based on https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/IO/File.cs
// as linked by docs on 02.09.2024

global using File = LeanCode.CodeAnalysis.NetStandard21Compatibility.MissingFileMethods;
using System.Text;

namespace LeanCode.CodeAnalysis.NetStandard21Compatibility;

public static class MissingFileMethods
{
private static readonly Encoding UTF8NoBOM = new UTF8Encoding(
encoderShouldEmitUTF8Identifier: false,
throwOnInvalidBytes: true
);

public static void Delete(string path) => System.IO.File.Delete(path);

public static Task WriteAllTextAsync(
string path,
string? contents,
CancellationToken cancellationToken = default
) => WriteAllTextAsync(path, contents, UTF8NoBOM, cancellationToken);

public static async Task WriteAllTextAsync(
string path,
string? contents,
Encoding encoding,
CancellationToken cancellationToken = default
)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("Path is invalid");
}

using var stream = System.IO.File.OpenWrite(path);

var preamble = encoding.GetPreamble();
await stream.WriteAsync(preamble, 0, preamble.Length, cancellationToken);

if (contents is not null)
{
var encoded = encoding.GetBytes(contents);
await stream.WriteAsync(encoded, 0, encoded.Length, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Based on https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs#L26C13-L26C56
// as linked by docs on 02.09.2024

namespace System;

public static class MissingStringMethods
{
public static bool Contains(this string s, string value, StringComparison comparisonType) =>
s.IndexOf(value, comparisonType) >= 0;
Copy link
Preview

Copilot AI Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method Contains should handle null values for the value parameter to avoid potential ArgumentNullException.

Copilot uses AI. Check for mistakes.

}
Loading