Skip to content

Commit

Permalink
merged avtc ignoreclassmembers attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSauer committed Aug 4, 2024
2 parents c859b21 + a602ecc commit 0c5cc77
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.27.1",
"version": "0.28.2",
"commands": [
"dotnet-csharpier"
]
],
"rollForward": false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>2.4.0</Version>
<Version>2.5.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AutomaticInterface;
public class AutomaticInterfaceGenerator : IIncrementalGenerator
{
public const string DefaultAttributeName = "GenerateAutomaticInterface";
public const string IgnoreAutomaticInterfaceAttributeName = "IgnoreAutomaticInterface";

public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand Down
16 changes: 15 additions & 1 deletion AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ InterfaceBuilder codeGenerator
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => x.MethodKind == MethodKind.Ordinary)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.Where(x => x.ContainingType.Name != nameof(Object))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
.Select(g => g.First())
Expand Down Expand Up @@ -171,6 +172,7 @@ InterfaceBuilder codeGenerator
.OfType<IEventSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !HasIgnoreAttribute(x))
.ToList()
.ForEach(evt =>
{
Expand Down Expand Up @@ -244,6 +246,7 @@ InterfaceBuilder interfaceGenerator
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsStatic)
.Where(x => !x.IsIndexer)
.Where(x => !HasIgnoreAttribute(x))
.GroupBy(x => x.Name)
.Select(g => g.First())
.ToList()
Expand All @@ -269,6 +272,17 @@ InterfaceBuilder interfaceGenerator
});
}

private static bool HasIgnoreAttribute(ISymbol x)
{
return x.GetAttributes()
.Any(a =>
a.AttributeClass != null
&& a.AttributeClass.Name.Contains(
AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName
)
);
}

private static string GetDocumentationForClass(CSharpSyntaxNode classSyntax)
{
if (!classSyntax.HasLeadingTrivia)
Expand Down Expand Up @@ -314,7 +328,7 @@ NamespaceDeclarationSyntax ndSyntax
);
}

return [..allUsings.Select(x => x.ToString())];
return [.. allUsings.Select(x => x.ToString())];
}

private static string GetGeneric(TypeDeclarationSyntax classSyntax)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace AutomaticInterfaceAttribute
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
public class IgnoreAutomaticInterfaceAttribute : Attribute
{
public IgnoreAutomaticInterfaceAttribute() { }
}
}
47 changes: 47 additions & 0 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,53 @@ public partial interface IDemoClass
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void IgnoresMembersAttributedWithSkip()
{
const string code = """
using AutomaticInterfaceAttribute;
using System.IO;
namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
{
[IgnoreAutomaticInterface] public string Hello1(string x, int y, double z){return x;}
[IgnoreAutomaticInterface] public string Hello2 { get; set; }
[IgnoreAutomaticInterface] public event EventHandler Hello3;
}
}
""";

const string expected = """
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using System.IO;
namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
}
}
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void AddsDescriptionFromMethodToInterface()
{
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace AutomaticInterfaceExample
public string OnlyGet { get; } // included, get and set are copied to the interface when public
[IgnoreAutomaticInterface]
public string OnlyGet { get; } // ignored with help of attribute
/// <summary>
/// Method Documentation will be copied
/// </summary>
Expand Down Expand Up @@ -160,13 +163,17 @@ Please make sure that you run [CSharpier](https://csharpier.com/) on the code fo
- roflmuffin for PRs
- mohummedibrahim for code and idea
- simonmckenzie for PR
- avtc for PR

## Run tests

Should be simply a build and run Tests

## Changelog

### 2.50.
- Now can ignore class members with [IgnoreAutomaticInterface] attribute. Thanks avtc!

### 2.40.
- Now prevents duplicates when overriding or shadowing methods (`new void DoSomething()`). Thanks simonmckenzie!

Expand Down

0 comments on commit 0c5cc77

Please sign in to comment.