Skip to content

Commit

Permalink
Add Meziantou.Analyzer.Annotations package
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Jul 27, 2023
1 parent f8a35f4 commit d4967bf
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:

- run: dotnet restore src/Meziantou.Analyzer.pack.csproj
- run: dotnet pack src/Meziantou.Analyzer.pack.csproj --configuration Release --no-build /p:Version=${{ needs.compute_package_version.outputs.package_version }}
- run: dotnet pack src/Meziantou.Analyzer.Annotations/Meziantou.Analyzer.Annotations.csproj --configuration Release

- uses: actions/upload-artifact@v3
with:
Expand Down
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
<RepositoryUrl>https://github.com/meziantou/Meziantou.Analyzer.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIcon>icon.png</PackageIcon>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\icon.png" Pack="true" PackagePath="" />
<None Include="$(MSBuildThisFileDirectory)\LICENSE.txt" Pack="true" PackagePath="" />
<None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Meziantou.Analyzer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListDotNetTypes", "src\List
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Meziantou.Analyzer.pack", "src\Meziantou.Analyzer.pack.csproj", "{6AB20B80-DE7F-4EB3-8A8B-E5C902F06438}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Meziantou.Analyzer.Annotations", "src\Meziantou.Analyzer.Annotations\Meziantou.Analyzer.Annotations.csproj", "{6C3D4314-A2E3-498F-A2C3-AC10E714A498}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -51,6 +53,10 @@ Global
{6AB20B80-DE7F-4EB3-8A8B-E5C902F06438}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AB20B80-DE7F-4EB3-8A8B-E5C902F06438}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AB20B80-DE7F-4EB3-8A8B-E5C902F06438}.Release|Any CPU.Build.0 = Release|Any CPU
{6C3D4314-A2E3-498F-A2C3-AC10E714A498}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C3D4314-A2E3-498F-A2C3-AC10E714A498}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C3D4314-A2E3-498F-A2C3-AC10E714A498}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C3D4314-A2E3-498F-A2C3-AC10E714A498}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 9 additions & 13 deletions docs/Rules/MA0003.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ MA0003.excluded_methods = M:A.B(System.Int32) | M:C.D()
MA0003.excluded_methods_regex = Sample.*Test
````

You can annotate a parameter with `Meziantou.Analyzer.Annotations.RequireNamedArgumentAttribute`. This attribute is not provided, so you need to add it to your project manually:

````c#
namespace Meziantou.Analyzer.Annotations
{
[System.AttributeUsage(System.AttributeTargets.Parameter)]
internal sealed class RequireNamedArgumentAttribute : System.Attribute
{
public RequireNamedArgumentAttribute() {}
public RequireNamedArgumentAttribute(bool value) {}
}
}
````
You can annotate a parameter with `Meziantou.Analyzer.Annotations.RequireNamedArgumentAttribute`. This attribute is available using the
`Meziantou.Analyzer.Annotations` NuGet package.

```c#
Test("test"); // report a diagnostic as the parameter is not named
// Requires Meziantou.Analyzer.Annotations package
public void Test([RequireNamedArgument] string value) { }
```
12 changes: 1 addition & 11 deletions docs/Rules/MA0124.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,8 @@ Then, you need to add the file to the `AdditionalFiles` collection in the `cspro
You can also configure the allowed types by using an assembly attribute. This attributes are applied only for the current assemblies. The rule does not consider attributes defined in referenced assemblies.

````c#
// Requires the Meziantou.Analyzer.Annotations package
[assembly: Meziantou.Analyzer.Annotations.StructuredLogField("Count", typeof(int), typeof(long))]

// Need to be defined in your code
namespace Meziantou.Analyzer.Annotations
{
[System.Diagnostics.Conditional("MEZIANTOU_ANALYZER_ATTRIBUTES")]
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
internal sealed class StructuredLogFieldAttribute : System.Attribute
{
public StructuredLogFieldAttribute(string parameterName, params System.Type[] allowedTypes) { }
}
}
````

Then, the analyzer reports log parameters of the wrong type:
Expand Down
27 changes: 27 additions & 0 deletions src/Meziantou.Analyzer.Annotations/Annotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma warning disable CS1591
#pragma warning disable IDE0060
#pragma warning disable IDE0290
#pragma warning disable CA1019
#nullable disable

namespace Meziantou.Analyzer.Annotations
{
/// <summary>
/// Indicates arguments must be named for this parameter.
/// </summary>
[System.Diagnostics.Conditional("MEZIANTOU_ANALYZER_ANNOTATIONS")]
[System.AttributeUsage(System.AttributeTargets.Parameter)]
public sealed class RequireNamedArgumentAttribute : System.Attribute
{
public RequireNamedArgumentAttribute() { }

public RequireNamedArgumentAttribute(bool isRequired) { }
}

[System.Diagnostics.Conditional("MEZIANTOU_ANALYZER_ANNOTATIONS")]
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public sealed class StructuredLogFieldAttribute : System.Attribute
{
public StructuredLogFieldAttribute(string parameterName, params System.Type[] allowedTypes) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0</TargetFrameworks>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Description>Annotations to configure Meziantou.Analyzer</Description>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Remove="Meziantou.Polyfill" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/Meziantou.Analyzer.Annotations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Meziantou.Analyzer.Annotations help configuring some rules by annotating your code.

All usages of Meziantou.Analyzer.Annotations attributes are erased from metadata by default, which means no actual binary reference to 'Meziantou.Analyzer.Annotations.dll' assembly is produced. If you need to preserve these attributes in metadata, just define 'MEZIANTOU_ANALYZER_ANNOTATIONS' conditional compilation symbol in your projects.
2 changes: 1 addition & 1 deletion src/Meziantou.Analyzer.pack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<PackageId>Meziantou.Analyzer</PackageId>
<Description>A Roslyn analyzer to enforce some good practices in C#</Description>
<PackageTags>Meziantou.Analyzer, analyzers</PackageTags>

</PropertyGroup>

<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
<None Include="$(MSBuildThisFileDirectory)\Meziantou.Analyzer\bin\roslyn3.8\$(Configuration)\netstandard2.0\Meziantou.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn3.8/cs/" />
<None Include="$(MSBuildThisFileDirectory)\Meziantou.Analyzer\bin\roslyn4.2\$(Configuration)\netstandard2.0\Meziantou.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.2/cs/" />
<None Include="$(MSBuildThisFileDirectory)\Meziantou.Analyzer\bin\roslyn4.4\$(Configuration)\netstandard2.0\Meziantou.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.4/cs/" />
Expand Down

0 comments on commit d4967bf

Please sign in to comment.