Skip to content

Commit

Permalink
Merge pull request #54 from crwsolutions/feature/use-ForAttributeWith…
Browse files Browse the repository at this point in the history
…MetadataName

Use ForAttributeWithMetadataName()
  • Loading branch information
ChristianSauer authored Aug 10, 2024
2 parents 37e3cee + 18fc53f commit c1ee637
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>3.0.0</Version>
<Version>2.5.0</Version>
<Version>3.0.1</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
<PackageReleaseNotes>Use ForAttributeWithMetadataName to improve performance</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugGenerator|AnyCPU'">
Expand All @@ -42,8 +42,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Immutable;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -19,41 +17,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterIgnoreAttribute();

var classes = context
.SyntaxProvider.CreateSyntaxProvider(CouldBeClassWithInterfaceAttribute, Transform)
.Where(type => type is not null)
.SyntaxProvider.ForAttributeWithMetadataName(
$"AutomaticInterface.{DefaultAttributeName}Attribute",
(node, _) => node is ClassDeclarationSyntax,
(context, _) => (ITypeSymbol)context.TargetSymbol
)
.Collect();

context.RegisterSourceOutput(classes, GenerateCode);
}

private static bool CouldBeClassWithInterfaceAttribute(
SyntaxNode syntaxNode,
CancellationToken _
)
{
if (syntaxNode is not AttributeSyntax attribute)
{
return false;
}

var name = ExtractName(attribute.Name);

return name is DefaultAttributeName;
}

private static string? ExtractName(NameSyntax? name)
{
return name switch
{
SimpleNameSyntax ins => ins.Identifier.Text,
QualifiedNameSyntax qns => qns.Right.Identifier.Text,
_ => null
};
}

private static void GenerateCode(
SourceProductionContext context,
ImmutableArray<ITypeSymbol?> enumerations
ImmutableArray<ITypeSymbol> enumerations
)
{
if (enumerations.IsDefaultOrEmpty)
Expand All @@ -63,13 +39,8 @@ private static void GenerateCode(

foreach (var type in enumerations)
{
if (type is null)
{
continue;
}

var typeNamespace = type.ContainingNamespace.IsGlobalNamespace
? $"${Guid.NewGuid().ToString()}"
? $"${Guid.NewGuid()}"
: $"{type.ContainingNamespace}";

var code = Builder.BuildInterfaceFor(type);
Expand All @@ -78,23 +49,4 @@ private static void GenerateCode(
context.AddSource(hintName, code);
}
}

private static ITypeSymbol? Transform(
GeneratorSyntaxContext context,
CancellationToken cancellationToken
)
{
var attributeSyntax = (AttributeSyntax)context.Node;
if (attributeSyntax.Parent?.Parent is not ClassDeclarationSyntax classDeclaration)
{
return null;
}

var type =
context.SemanticModel.GetDeclaredSymbol(
classDeclaration,
cancellationToken: cancellationToken
) as ITypeSymbol;
return type;
}
}
16 changes: 8 additions & 8 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public void IgnoresMembersAttributedWithSkip()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.IO;

namespace AutomaticInterfaceExample
Expand All @@ -745,7 +745,7 @@ class DemoClass
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.IO;

namespace AutomaticInterfaceExample
Expand Down Expand Up @@ -2258,7 +2258,7 @@ public void WorksWithMethodOverrides()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample;

Expand All @@ -2285,7 +2285,7 @@ public class DemoClass : BaseClass
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample
{
Expand All @@ -2307,7 +2307,7 @@ public void WorksWithMethodShadowing()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample;

Expand All @@ -2334,7 +2334,7 @@ public class DemoClass : BaseClass
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample
{
Expand All @@ -2356,7 +2356,7 @@ public void WorksWithParameterDirectionOverloads()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample;

Expand All @@ -2380,7 +2380,7 @@ public class DemoClass
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;

namespace AutomaticInterfaceExample
{
Expand Down
12 changes: 6 additions & 6 deletions AutomaticInterface/Tests/GeneratorsTests.MethodParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void WorksWithMethodOutParameter()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample;
Expand All @@ -35,7 +35,7 @@ public void AMethod(out int someOutParameter)
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample
Expand All @@ -58,7 +58,7 @@ public void WorksWithMethodInParameter()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample;
Expand All @@ -82,7 +82,7 @@ public void AMethod(in int someOutParameter)
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample
Expand All @@ -105,7 +105,7 @@ public void WorksWithMethodRefParameter()
{
const string code = """

using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample;
Expand All @@ -129,7 +129,7 @@ public void AMethod(ref int someOutParameter)
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using AutomaticInterface;
using System.Threading.Tasks;

namespace AutomaticInterfaceExample
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ Should be simply a build and run Tests

## Changelog

### 3.0.1

- Maintenance update. Use of `ForAttributeWithMetadataName` to improve performance. Thanks crwsolutions!

### 3.0.0

- You can remove the manually created `GenerateAutomaticInterfaceAttribute`, as it is generated automatically now. Thanks crwsolutions!
Expand Down

0 comments on commit c1ee637

Please sign in to comment.