Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions TUnit.Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Rule ID | Category | Severity | Notes
--------|----------|----------|-------
TUnit0061 | Usage | Error | ClassDataSource type requires parameterless constructor
TUnit0062 | Usage | Warning | CancellationToken must be the last parameter
TUnit0080 | Usage | Error | Missing polyfill types required by TUnit

### Removed Rules

Expand Down
34 changes: 34 additions & 0 deletions TUnit.Analyzers/MissingPolyfillAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace TUnit.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MissingPolyfillAnalyzer : ConcurrentDiagnosticAnalyzer
{
private static readonly string[] RequiredTypes =
[
"System.Runtime.CompilerServices.ModuleInitializerAttribute",
];

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(Rules.MissingPolyfillPackage);

protected override void InitializeInternal(AnalysisContext context)
{
context.RegisterCompilationAction(AnalyzeCompilation);
}

private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
foreach (var typeName in RequiredTypes)
{
if (context.Compilation.GetTypeByMetadataName(typeName) is null)
{
context.ReportDiagnostic(
Diagnostic.Create(Rules.MissingPolyfillPackage, Location.None, typeName));
}
}
}
}
9 changes: 9 additions & 0 deletions TUnit.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@
<data name="TUnit0052Title" xml:space="preserve">
<value>Multiple constructors found without [TestConstructor] attribute</value>
</data>
<data name="TUnit0080Description" xml:space="preserve">
<value>TUnit requires certain types (such as ModuleInitializerAttribute) that are not available on older target frameworks. Install the 'Polyfill' NuGet package to provide these types.</value>
</data>
<data name="TUnit0080MessageFormat" xml:space="preserve">
<value>Type '{0}' is required by TUnit but is not available. Install the 'Polyfill' NuGet package: dotnet add package Polyfill</value>
</data>
<data name="TUnit0080Title" xml:space="preserve">
<value>Missing polyfill types required by TUnit</value>
</data>
<data name="TUnit0301Description" xml:space="preserve">
<value>Tuple types require reflection for property/field access which is not AOT-compatible. Consider using concrete types or value deconstruction.</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions TUnit.Analyzers/Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ public static class Rules
public static readonly DiagnosticDescriptor NoAccessibleConstructor =
CreateDescriptor("TUnit0061", UsageCategory, DiagnosticSeverity.Error);

public static readonly DiagnosticDescriptor MissingPolyfillPackage =
CreateDescriptor("TUnit0080", UsageCategory, DiagnosticSeverity.Error);

public static readonly DiagnosticDescriptor GenericTypeNotAotCompatible =
CreateDescriptor("TUnit0300", UsageCategory, DiagnosticSeverity.Warning);

Expand Down
Loading