diff --git a/MakeInterface.Demo/Models/Class2.cs b/MakeInterface.Demo/Models/Class2.cs index 842c11e..fe67f2d 100644 --- a/MakeInterface.Demo/Models/Class2.cs +++ b/MakeInterface.Demo/Models/Class2.cs @@ -7,4 +7,8 @@ namespace MakeInterface.Demo.Models; public class Class2 : IClass2 { public IClass1? Class1 { get; } + + public string Name { get; set; } + + public string FullName() => $"{Name} {Name}"; } diff --git a/MakeInterface.Generator/Extensions/MemberDeclarationSyntaxExtensions.cs b/MakeInterface.Generator/Extensions/MemberDeclarationSyntaxExtensions.cs index 0224cc8..92ab89a 100644 --- a/MakeInterface.Generator/Extensions/MemberDeclarationSyntaxExtensions.cs +++ b/MakeInterface.Generator/Extensions/MemberDeclarationSyntaxExtensions.cs @@ -4,14 +4,14 @@ namespace MakeInterface.Generator.Extensions; internal static class MemberDeclarationSyntaxExtensions { - public static string GetName(this MemberDeclarationSyntax syntax) + public static string? GetName(this MemberDeclarationSyntax syntax) { return syntax switch { PropertyDeclarationSyntax property => property.Identifier.Text, MethodDeclarationSyntax method => method.Identifier.Text, FieldDeclarationSyntax field => field.Declaration.Variables.First().Identifier.Text, - _ => throw new NotImplementedException($"Syntax of kind {syntax.GetType().Name} is not implemented"), + _ => null }; } } diff --git a/MakeInterface.Generator/InterfaceGenerator.cs b/MakeInterface.Generator/InterfaceGenerator.cs index 86da192..28dc4ae 100644 --- a/MakeInterface.Generator/InterfaceGenerator.cs +++ b/MakeInterface.Generator/InterfaceGenerator.cs @@ -127,10 +127,10 @@ private void Generate(SourceProductionContext ctx, (SemanticModel, CompilationUn continue; var name = memberSyntax.GetName(); - if (excludedMembers.Contains(name)) + if (name is null || excludedMembers.Contains(name)) continue; - if (membersFromImplementedTypes.Contains(memberSyntax.GetName())) + if (membersFromImplementedTypes.Contains(name)) continue; var publicModifier = memberSyntax.Modifiers.FirstOrDefault(x => x.IsKind(SyntaxKind.PublicKeyword)); @@ -165,6 +165,7 @@ private void Generate(SourceProductionContext ctx, (SemanticModel, CompilationUn var newMethod = methodSyntax .WithBody(null) + .WithExpressionBody(null) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) .WithModifiers(modifiers); @@ -329,7 +330,7 @@ private InterfaceDeclarationSyntax AddInterfaces(InterfaceDeclarationSyntax inte // Get members that matches interfaceMembers var membersToRemove = interfaceDeclaration.Members - .Where(member => baseInterfaceMembers.Contains(member.GetName())) + .Where(member => member.GetName() is { } name && baseInterfaceMembers.Contains(name)) .ToList(); // Remove the members from the interface declaration. diff --git a/MakeInterface.Generator/MakeInterface.Generator.csproj b/MakeInterface.Generator/MakeInterface.Generator.csproj index 1853d82..650d887 100644 --- a/MakeInterface.Generator/MakeInterface.Generator.csproj +++ b/MakeInterface.Generator/MakeInterface.Generator.csproj @@ -12,7 +12,7 @@ library netstandard2.0 MakeInterface.Generator - 0.4.0 + 0.4.1 Frederik Tegnander COWI Interfaces;SourceGenerator;MakeInterface diff --git a/MakeInterface.Tests/InterfaceGeneratorTests.cs b/MakeInterface.Tests/InterfaceGeneratorTests.cs index a1fd4f5..96517c6 100644 --- a/MakeInterface.Tests/InterfaceGeneratorTests.cs +++ b/MakeInterface.Tests/InterfaceGeneratorTests.cs @@ -298,6 +298,23 @@ public string Get2() } } } +"""; + + return TestHelper.Verify(source); + } + + [Fact] + public Task Method_Expression_Body() + { + var source = """ +namespace MakeInterface.Tests +{ + [GenerateInterface] + public class Class + { + public string Get() => return "foo"; + } +} """; return TestHelper.Verify(source); diff --git a/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.received.cs b/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.received.cs new file mode 100644 index 0000000..5de0cd4 --- /dev/null +++ b/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.received.cs @@ -0,0 +1,11 @@ +//HintName: ITest.g.cs +// +#pragma warning disable +#nullable enable +namespace MakeInterface.Tests +{ + public partial interface IClass + { + string Get() => ; + } +} \ No newline at end of file diff --git a/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.verified.cs b/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.verified.cs new file mode 100644 index 0000000..bee8736 --- /dev/null +++ b/MakeInterface.Tests/Snapshots/InterfaceGeneratorTests.Method_Expression_Body#ITest.g.verified.cs @@ -0,0 +1,11 @@ +//HintName: ITest.g.cs +// +#pragma warning disable +#nullable enable +namespace MakeInterface.Tests +{ + public partial interface IClass + { + string Get(); + } +} \ No newline at end of file