From d4967bfd5a08e7098021314659412429b80c5299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Wed, 26 Jul 2023 20:33:29 -0400 Subject: [PATCH] Add Meziantou.Analyzer.Annotations package --- .github/workflows/ci.yml | 1 + Directory.Build.props | 3 +-- Meziantou.Analyzer.sln | 6 +++++ docs/Rules/MA0003.md | 22 +++++++-------- docs/Rules/MA0124.md | 12 +-------- .../Annotations.cs | 27 +++++++++++++++++++ .../Meziantou.Analyzer.Annotations.csproj | 19 +++++++++++++ src/Meziantou.Analyzer.Annotations/README.md | 3 +++ src/Meziantou.Analyzer.pack.csproj | 2 +- 9 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 src/Meziantou.Analyzer.Annotations/Annotations.cs create mode 100644 src/Meziantou.Analyzer.Annotations/Meziantou.Analyzer.Annotations.csproj create mode 100644 src/Meziantou.Analyzer.Annotations/README.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45b218c9f..399e0df65 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/Directory.Build.props b/Directory.Build.props index ba9b04377..f6ba3558b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -24,14 +24,13 @@ https://github.com/meziantou/Meziantou.Analyzer.git git icon.png - LICENSE.txt + MIT README.md - diff --git a/Meziantou.Analyzer.sln b/Meziantou.Analyzer.sln index 0e5135f07..9a82b47b9 100644 --- a/Meziantou.Analyzer.sln +++ b/Meziantou.Analyzer.sln @@ -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 @@ -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 diff --git a/docs/Rules/MA0003.md b/docs/Rules/MA0003.md index d3e7ac12c..c3ee327f9 100644 --- a/docs/Rules/MA0003.md +++ b/docs/Rules/MA0003.md @@ -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) { } +``` diff --git a/docs/Rules/MA0124.md b/docs/Rules/MA0124.md index 5fdbbcc1d..ab7614eb9 100644 --- a/docs/Rules/MA0124.md +++ b/docs/Rules/MA0124.md @@ -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: diff --git a/src/Meziantou.Analyzer.Annotations/Annotations.cs b/src/Meziantou.Analyzer.Annotations/Annotations.cs new file mode 100644 index 000000000..06dc9539a --- /dev/null +++ b/src/Meziantou.Analyzer.Annotations/Annotations.cs @@ -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 +{ + /// + /// Indicates arguments must be named for this parameter. + /// + [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) { } + } +} diff --git a/src/Meziantou.Analyzer.Annotations/Meziantou.Analyzer.Annotations.csproj b/src/Meziantou.Analyzer.Annotations/Meziantou.Analyzer.Annotations.csproj new file mode 100644 index 000000000..96ea7007c --- /dev/null +++ b/src/Meziantou.Analyzer.Annotations/Meziantou.Analyzer.Annotations.csproj @@ -0,0 +1,19 @@ + + + + netstandard1.0;netstandard2.0 + 1.0.0 + 1.0.0 + Annotations to configure Meziantou.Analyzer + True + + + + + + + + + + + diff --git a/src/Meziantou.Analyzer.Annotations/README.md b/src/Meziantou.Analyzer.Annotations/README.md new file mode 100644 index 000000000..4fb6b5b71 --- /dev/null +++ b/src/Meziantou.Analyzer.Annotations/README.md @@ -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. diff --git a/src/Meziantou.Analyzer.pack.csproj b/src/Meziantou.Analyzer.pack.csproj index f9fcda286..a58c79dd9 100644 --- a/src/Meziantou.Analyzer.pack.csproj +++ b/src/Meziantou.Analyzer.pack.csproj @@ -12,10 +12,10 @@ Meziantou.Analyzer A Roslyn analyzer to enforce some good practices in C# Meziantou.Analyzer, analyzers - +