Skip to content

Commit

Permalink
feat: add ExcludeFromCodeCoverage attribute (#36)
Browse files Browse the repository at this point in the history
* feat: add ExcludeFromCodeCoverage attribute

* docs: add configuration section
  • Loading branch information
Ne4to authored May 2, 2024
1 parent 820c205 commit 82e5ca8
Show file tree
Hide file tree
Showing 273 changed files with 1,849 additions and 60 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,17 @@ public class Service

private bool IsValid() => throw new NotImplementedException();
}
```

## Configuration
|Property|Default|Description|
|--|--|--|
|UnionTypesGenerator_ExcludeFromCodeCoverage|true|Add [ExcludeFromCodeCoverage]([https://](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute?view=net-8.0)) attribute when `true`|

```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<UnionTypesGenerator_ExcludeFromCodeCoverage>false</UnionTypesGenerator_ExcludeFromCodeCoverage>
</PropertyGroup>
</Project>
```
3 changes: 3 additions & 0 deletions examples/BusinessLogic/BusinessLogic.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<UnionTypesGenerator_ExcludeFromCodeCoverage>false</UnionTypesGenerator_ExcludeFromCodeCoverage>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\N.SourceGenerators.UnionTypes\N.SourceGenerators.UnionTypes.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace N.SourceGenerators.UnionTypes.Extensions;
using N.SourceGenerators.UnionTypes.Models;

namespace N.SourceGenerators.UnionTypes.Extensions;

internal static class SyntaxNodeExtensions
{
Expand All @@ -9,7 +11,7 @@ public static bool IsTypeWithAttributes(this SyntaxNode s)
AttributeLists.Count: > 0
};
}

public static bool IsGenericTypeAttribute(this SyntaxNode s)
{
if (s is not TypeParameterSyntax typeParameter)
Expand All @@ -29,7 +31,7 @@ public static bool IsPartial(this TypeDeclarationSyntax s)
{
return s.Modifiers.Any(SyntaxKind.PartialKeyword);
}

public static AwaitExpressionSyntax AwaitWithConfigureAwait(this ExpressionSyntax expression)
{
return AwaitExpression(
Expand All @@ -44,4 +46,21 @@ public static AwaitExpressionSyntax AwaitWithConfigureAwait(this ExpressionSynta
)
);
}

public static TSyntax ExcludeFromCodeCoverage<TSyntax>(this TSyntax node, GeneratorOptions options)
where TSyntax : BaseTypeDeclarationSyntax
{
if (!options.ExcludeFromCodeCoverage)
{
return node;
}

AttributeListSyntax attributeList = AttributeList()
.AddAttributes(
Attribute(IdentifierName("System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))
);


return (TSyntax)node.AddAttributeLists(attributeList);
}
}

This file was deleted.

11 changes: 11 additions & 0 deletions src/N.SourceGenerators.UnionTypes/Models/GeneratorContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace N.SourceGenerators.UnionTypes.Models;

internal record GeneratorContext(CompilationContext Compilation, GeneratorOptions Options);

internal record CompilationContext(
bool SupportsNotNullWhenAttribute,
bool SupportsThrowIfNull,
bool NullableContextEnabled,
bool SupportsAutoDefaultField);

internal record GeneratorOptions(bool ExcludeFromCodeCoverage);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IsRoslynComponent>true</IsRoslynComponent>
<IsPackable>true</IsPackable>
<PackageOutputPath>./nupkg</PackageOutputPath>
<VersionPrefix>0.27.0</VersionPrefix>
<VersionPrefix>0.28.0</VersionPrefix>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<NoWarn>$(NoWarn);NU5128</NoWarn>
</PropertyGroup>
Expand All @@ -25,6 +25,10 @@
<Description>Discriminated union type source generator</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup>
<UnionTypesGenerator_ExcludeFromCodeCoverage>true</UnionTypesGenerator_ExcludeFromCodeCoverage>
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="\" />
Expand All @@ -33,6 +37,9 @@
<ItemGroup>
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

<!-- Package the props file -->
<None Include="N.SourceGenerators.UnionTypes.props" Pack="true" PackagePath="build" Visible="false" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup>
<CompilerVisibleProperty Include="UnionTypesGenerator_ExcludeFromCodeCoverage" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class UnionTypesGenerator
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = true)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class UnionTypeAttribute : Attribute
{
public Type Type { get; }
Expand Down Expand Up @@ -65,6 +66,7 @@ public UnionTypeAttribute(Type type, string alias = null, [CallerLineNumber] int
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.GenericParameter, Inherited = false, AllowMultiple = false)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class GenericUnionTypeAttribute : Attribute
{
#if NETCOREAPP3_1_OR_GREATER
Expand All @@ -91,6 +93,7 @@ internal sealed class GenericUnionTypeAttribute : Attribute
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class JsonPolymorphicUnionAttribute : Attribute
{
#if NETCOREAPP3_1_OR_GREATER
Expand All @@ -113,6 +116,7 @@ internal sealed class JsonPolymorphicUnionAttribute : Attribute
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = true)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
sealed class UnionConverterFromAttribute : Attribute
{
public Type FromType { get; }
Expand All @@ -136,6 +140,7 @@ public UnionConverterFromAttribute(Type fromType)
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = true)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
sealed class UnionConverterToAttribute : Attribute
{
public Type ToType { get; }
Expand All @@ -158,6 +163,7 @@ public UnionConverterToAttribute(Type toType)
namespace N.SourceGenerators.UnionTypes
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
sealed class UnionConverterAttribute : Attribute
{
public Type FromType { get; }
Expand Down
20 changes: 10 additions & 10 deletions src/N.SourceGenerators.UnionTypes/UnionTypesGenerator.Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ namespace N.SourceGenerators.UnionTypes;
public sealed partial class UnionTypesGenerator
{
private static void GenerateConverters(IncrementalGeneratorInitializationContext context,
IncrementalValueProvider<CompilationContext> compilationContextProvider)
IncrementalValueProvider<GeneratorContext> generatorContextProvider)
{
var toConverters = GetUnionConverters(
context,
UnionConverterToAttributeName,
(containerType, otherTypes) => new UnionToConverter(containerType, otherTypes));
ProcessConverters(context, toConverters, compilationContextProvider);
ProcessConverters(context, toConverters, generatorContextProvider);

var fromConverters = GetUnionConverters(
context,
UnionConverterFromAttributeName,
(containerType, otherTypes) => new UnionFromConverter(containerType, otherTypes));
ProcessConverters(context, fromConverters, compilationContextProvider);
ProcessConverters(context, fromConverters, generatorContextProvider);

var unionConverters = GetUnionConverters(context);
ProcessConverters(context, unionConverters, compilationContextProvider);
ProcessConverters(context, unionConverters, generatorContextProvider);
}

private static void ProcessConverters(
IncrementalGeneratorInitializationContext context,
IncrementalValuesProvider<UnionConverter> unionConverters,
IncrementalValueProvider<CompilationContext> compilationContextProvider)
IncrementalValueProvider<GeneratorContext> generatorContextProvider)
{
var combination = unionConverters.Combine(compilationContextProvider);
var combination = unionConverters.Combine(generatorContextProvider);

context.RegisterImplementationSourceOutput(
combination,
Expand Down Expand Up @@ -81,7 +81,7 @@ private static void ProcessConverters(

if (saveSource)
{
CompilationUnitSyntax compilationUnit = GetCompilationUnit(typeDeclaration, unionConverter.Namespace, item.Right);
CompilationUnitSyntax compilationUnit = GetCompilationUnit(typeDeclaration, unionConverter.Namespace, item.Right.Compilation);
ctx.AddSource($"{unionConverter.Name}Converters.g.cs", compilationUnit.GetText(Encoding.UTF8));
}
});
Expand Down Expand Up @@ -173,10 +173,10 @@ private static IncrementalValuesProvider<UnionConverter> GetUnionConverters(

private static void ProcessConverters<T>(IncrementalGeneratorInitializationContext context,
IncrementalValuesProvider<T> fromConverters,
IncrementalValueProvider<CompilationContext> compilationContextProvider)
IncrementalValueProvider<GeneratorContext> generatorContextProvider)
where T : IUnionConverter
{
var combination = fromConverters.Combine(compilationContextProvider);
var combination = fromConverters.Combine(generatorContextProvider);

context.RegisterImplementationSourceOutput(
combination,
Expand Down Expand Up @@ -210,7 +210,7 @@ private static void ProcessConverters<T>(IncrementalGeneratorInitializationConte
if (saveSource)
{
CompilationUnitSyntax compilationUnit =
GetCompilationUnit(typeDeclaration, containerType.Namespace, item.Right);
GetCompilationUnit(typeDeclaration, containerType.Namespace, item.Right.Compilation);
ctx.AddSource(unionConverter.SourceHintName, compilationUnit.GetText(Encoding.UTF8));
}
});
Expand Down
18 changes: 10 additions & 8 deletions src/N.SourceGenerators.UnionTypes/UnionTypesGenerator.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace N.SourceGenerators.UnionTypes;
public sealed partial class UnionTypesGenerator
{
private static void GenerateJsonConverter(UnionType unionType,
SourceProductionContext context,
CompilationContext compilationContext)
SourceProductionContext context,
GeneratorContext generatorContext)
{
context.CancellationToken.ThrowIfCancellationRequested();
BuildJsonConverter(unionType, context, compilationContext);
BuildJsonConverter(unionType, context, generatorContext);
context.CancellationToken.ThrowIfCancellationRequested();
BuildJsonConverterAttribute(unionType, context, compilationContext);
BuildJsonConverterAttribute(unionType, context, generatorContext);
}

private static void BuildJsonConverter(UnionType unionType, SourceProductionContext context, CompilationContext compilationContext)
private static void BuildJsonConverter(UnionType unionType, SourceProductionContext context, GeneratorContext generatorContext)
{
TypeDeclarationSyntax converterDeclaration = ClassDeclaration(unionType.Name + "JsonConverter")
.AddModifiers(Token(SyntaxKind.InternalKeyword))
Expand All @@ -26,6 +26,7 @@ private static void BuildJsonConverter(UnionType unionType, SourceProductionCont
GenericType("System.Text.Json.Serialization.JsonConverter", unionType.Name)
)
)
.ExcludeFromCodeCoverage(generatorContext.Options)
.AddMembers(
GetDiscriminatorMethod(unionType),
AddDiscriminatorModifier(unionType),
Expand All @@ -34,11 +35,11 @@ private static void BuildJsonConverter(UnionType unionType, SourceProductionCont
WriteJsonConverterMethod(unionType)
);

CompilationUnitSyntax compilationUnit = GetCompilationUnit(converterDeclaration, unionType.Namespace, compilationContext);
CompilationUnitSyntax compilationUnit = GetCompilationUnit(converterDeclaration, unionType.Namespace, generatorContext.Compilation);
context.AddSource($"{unionType.SourceCodeFileName}JsonConverter.g.cs", compilationUnit.GetText(Encoding.UTF8));
}

private static void BuildJsonConverterAttribute(UnionType unionType, SourceProductionContext context, CompilationContext compilationContext)
private static void BuildJsonConverterAttribute(UnionType unionType, SourceProductionContext context, GeneratorContext generatorContext)
{
TypeDeclarationSyntax converterDeclaration = ClassDeclaration(unionType.Name + "JsonConverterAttribute")
.AddModifiers(
Expand All @@ -48,6 +49,7 @@ private static void BuildJsonConverterAttribute(UnionType unionType, SourceProdu
IdentifierName("System.Text.Json.Serialization.JsonConverterAttribute")
)
)
.ExcludeFromCodeCoverage(generatorContext.Options)
.AddMembers(
MethodDeclaration(
NullableType(IdentifierName("System.Text.Json.Serialization.JsonConverter")),
Expand All @@ -67,7 +69,7 @@ private static void BuildJsonConverterAttribute(UnionType unionType, SourceProdu
)
);

CompilationUnitSyntax compilationUnit = GetCompilationUnit(converterDeclaration, unionType.Namespace, compilationContext);
CompilationUnitSyntax compilationUnit = GetCompilationUnit(converterDeclaration, unionType.Namespace, generatorContext.Compilation);
context.AddSource($"{unionType.SourceCodeFileName}JsonConverterAttribute.g.cs",
compilationUnit.GetText(Encoding.UTF8));
}
Expand Down
Loading

0 comments on commit 82e5ca8

Please sign in to comment.