Skip to content

Commit

Permalink
Fixed bug where methods with expression body has implementation in in…
Browse files Browse the repository at this point in the history
…terfaces
  • Loading branch information
frte-cowi committed Jan 25, 2024
1 parent 69288cb commit 5e41b24
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
4 changes: 4 additions & 0 deletions MakeInterface.Demo/Models/Class2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ namespace MakeInterface.Demo.Models;
public class Class2 : IClass2
{
public IClass1? Class1 { get; }

public string Name { get; set; }

Check warning on line 11 in MakeInterface.Demo/Models/Class2.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in MakeInterface.Demo/Models/Class2.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public string FullName() => $"{Name} {Name}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
7 changes: 4 additions & 3 deletions MakeInterface.Generator/InterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion MakeInterface.Generator/MakeInterface.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<OutputType>library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MakeInterface.Generator</PackageId>
<Version>0.4.0</Version>
<Version>0.4.1</Version>
<Authors>Frederik Tegnander</Authors>
<Company>COWI</Company>
<PackageTags>Interfaces;SourceGenerator;MakeInterface</PackageTags>
Expand Down
17 changes: 17 additions & 0 deletions MakeInterface.Tests/InterfaceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: ITest.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace MakeInterface.Tests
{
public partial interface IClass
{
string Get() => ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: ITest.g.cs
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace MakeInterface.Tests
{
public partial interface IClass
{
string Get();
}
}

0 comments on commit 5e41b24

Please sign in to comment.