Skip to content
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

Add composite analyzer to test all analyzers #128

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
52 changes: 52 additions & 0 deletions tests/Moq.Analyzers.Test/CompositeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Moq.Analyzers.Test;

/// <summary>
/// A "meta" analyzer that aggregates all the individual analyzers into a single one.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CompositeAnalyzer : DiagnosticAnalyzer
{
private readonly ImmutableArray<DiagnosticAnalyzer> _analyzers;
private readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics;

/// <summary>Initializes a new instance of the <see cref="CompositeAnalyzer" /> class.</summary>
public CompositeAnalyzer()
{
_analyzers = [.. DiagnosticAnalyzers()];
_supportedDiagnostics = [.. _analyzers.SelectMany(diagnosticAnalyzer => diagnosticAnalyzer.SupportedDiagnostics)];
}

/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => _supportedDiagnostics;

/// <inheritdoc />
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1026:Enable concurrent execution", Justification = "Delegated off to children")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1025:Configure generated code analysis", Justification = "Delegated off to children")]
public override void Initialize(AnalysisContext context)
{
foreach (DiagnosticAnalyzer analyzer in _analyzers)
{
analyzer.Initialize(context);
}
}

private static IEnumerable<DiagnosticAnalyzer> DiagnosticAnalyzers()
{
Type diagnosticAnalyzerType = typeof(DiagnosticAnalyzer);
IEnumerable<Type> diagnosticAnalyzerTypes = typeof(ConstructorArgumentsShouldMatchAnalyzer)
.Assembly
.GetTypes()
.Where(type => diagnosticAnalyzerType.IsAssignableFrom(type) && !type.IsAbstract && type.IsClass)
;

return diagnosticAnalyzerTypes
.Select(type => (DiagnosticAnalyzer?)Activator.CreateInstance(type))
.Where(analyzer => analyzer != null)
.Cast<DiagnosticAnalyzer>()
;
}
}
2 changes: 2 additions & 0 deletions tests/Moq.Analyzers.Test/Moq.Analyzers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<!-- The CompositeAnalyzer triggers RS1036 -->
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down