Skip to content

Commit

Permalink
Add the new() type constraint to method parameters
Browse files Browse the repository at this point in the history
This addresses #64, where type parameters weren't having the `new()` constraint applied.

Also updated some tests that were failing due to changes in the `inheritdoc` annotations and re-ran csharpier.
  • Loading branch information
simonmckenzie committed Dec 3, 2024
1 parent 9d66cad commit 744662e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
20 changes: 8 additions & 12 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ private static string GetNameSpace(ISymbol typeSymbol)
{
var generationAttribute = typeSymbol
.GetAttributes()
.FirstOrDefault(
x =>
x.AttributeClass != null
&& x.AttributeClass
.Name
.Contains(AutomaticInterfaceGenerator.DefaultAttributeName)
.FirstOrDefault(x =>
x.AttributeClass != null
&& x.AttributeClass.Name.Contains(AutomaticInterfaceGenerator.DefaultAttributeName)
);

if (generationAttribute == null)
Expand Down Expand Up @@ -298,12 +295,11 @@ private static PropertySetKind GetSetKind(IMethodSymbol? setMethodSymbol)
private static bool HasIgnoreAttribute(ISymbol x)
{
return x.GetAttributes()
.Any(
a =>
a.AttributeClass != null
&& a.AttributeClass
.Name
.Contains(AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName)
.Any(a =>
a.AttributeClass != null
&& a.AttributeClass.Name.Contains(
AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName
)
);
}

Expand Down
6 changes: 6 additions & 0 deletions AutomaticInterface/AutomaticInterface/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ SymbolDisplayFormat typeDisplayFormat
isFirstConstraint = false;
}

if (typeParameterSymbol.HasConstructorConstraint)
{
constraints += "new()";
isFirstConstraint = false;
}

foreach (var constraintType in typeParameterSymbol.ConstraintTypes)
{
// if not first constraint prepend with comma
Expand Down
15 changes: 8 additions & 7 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,11 +1053,12 @@ namespace AutomaticInterfaceExample;
public class DemoClass
{
/// <inheritdoc />
public string CMethod<T, T1, T2, T3, T4>(string x, string y)
public string CMethod<T, T1, T2, T3, T4, T5>(string x, string y)
where T : class
where T1 : struct
where T3 : DemoClass
where T4 : IDemoClass
where T5 : new()
{
return "Ok";
}
Expand All @@ -1080,8 +1081,8 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.CMethod{T, T1, T2, T3, T4}(string, string)" />
string CMethod<T, T1, T2, T3, T4>(string x, string y) where T : class where T1 : struct where T3 : global::AutomaticInterfaceExample.DemoClass where T4 : IDemoClass;
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.CMethod{T, T1, T2, T3, T4, T5}(string, string)" />
string CMethod<T, T1, T2, T3, T4, T5>(string x, string y) where T : class where T1 : struct where T3 : global::AutomaticInterfaceExample.DemoClass where T4 : IDemoClass where T5 : new();
}
}
Expand Down Expand Up @@ -1820,10 +1821,10 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.AMethod(Func{Task{int}})" />
void AMethod(Func<Task<int>> getValue);
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.AMethod(Func{Task{float}})" />
void AMethod(Func<Task<float>> getValue);
}
Expand Down Expand Up @@ -1875,10 +1876,10 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.AMethod(Func{Task{AutomaticInterfaceExample.Types1.Model}})" />
void AMethod(Func<Task<global::AutomaticInterfaceExample.Types1.Model>> getValue);
/// <inheritdoc />
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.AMethod(Func{Task{AutomaticInterfaceExample.Types2.Model}})" />
void AMethod(Func<Task<global::AutomaticInterfaceExample.Types2.Model>> getValue);
}
Expand Down

0 comments on commit 744662e

Please sign in to comment.