$ show xunit/xunit.analyzers
This repository contains Roslyn diagnostic analyzers and code fix providers for the xUnit.net test framework (v2 and v3, including Native AOT). It enforces best practices, detects common anti-patterns (e.g., misuse of Assert methods, serialization issues with theory data, incorrect async handling, and structural problems in test classes), and provides automated code fixes. The codebase is organized around version-aware context classes (XunitContext) that resolve xUnit types and version-specific behavior, allowing a single analyzer to target multiple xUnit versions. It includes a rich test suite using a custom CSharpVerifier base class, building on Microsoft.CodeAnalysis.Testing. The infrastructure is well-abstracted with reusable base classes for analyzers, suppressors, and fix providers, and the project is packaged as a NuGet package automatically referenced by xUnit projects.
The LLM identified 4 files worth highlighting for reuse:
Central context that aggregates all version-specific type references (V2, V3, V3AOT) into unified interfaces (IAssertContext, ICommonContext, ICoreContext, IRunnerUtilityContext). It is the primary dispatch mechanism for multi-version analysis. Reusable as the foundation for any xUnit Roslyn analyzer, demonstrating a clean pattern for handling multiple framework versions.
Encapsulates logic for checking if a type is serializable for xUnit theory data. Distinguishes AlwaysSerializable, PossiblySerializable, and NeverSerializable, and can analyze initializer expressions. This component is self-contained and could be extracted and reused in any analyzer or tool that needs to verify data contract serializability.
Contains an internal InlineDataUniquenessComparer that implements IEqualityComparer<AttributeData> with advanced logic: traversing TypedConstant trees, normalizing numeric types, handling default parameter values, and nested arrays. This comparer is a well-crafted, self-contained piece of utility code that could be extracted and reused in any Roslyn-based duplicate detection scenario.
Abstract base class for creating assert-usage analyzers. It handles common patterns such as detecting async methods and inspecting method invocations. It is a clean template that can be extended to write new assertion-style analyzers with minimal boilerplate, demonstrating good object-oriented design for Roslyn analyzers.