Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix missing notnull constraint #72

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
IntelliJ IDEA
IntelliJ IDEA
*.swp
*.*~
project.lock.json
Expand Down Expand Up @@ -38,4 +38,6 @@ msbuild.wrn
.vs/

#Generator logs
AutomaticInterfaceGenerator_log.txt
AutomaticInterfaceGenerator_log.txt

*.received.*
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>5.1.2</Version>
<Version>5.1.3</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
Expand Down
6 changes: 6 additions & 0 deletions AutomaticInterface/AutomaticInterface/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ SymbolDisplayFormat typeDisplayFormat
isFirstConstraint = false;
}

if (typeParameterSymbol.HasNotNullConstraint)
{
constraints += "notnull";
isFirstConstraint = false;
}

foreach (var constraintType in typeParameterSymbol.ConstraintTypes)
{
// if not first constraint prepend with comma
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//--------------------------------------------------------------------------------------------------
// <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>
//--------------------------------------------------------------------------------------------------

namespace AutomaticInterfaceExample
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.AddFilter{T}(IQueryable{T})" />
IQueryable<T> AddFilter<T>(IQueryable<T> qry) where T : notnull;

}
}
21 changes: 21 additions & 0 deletions AutomaticInterface/Tests/Methods/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,27 @@ public string CMethod<T, T1, T2, T3, T4, T5>(string x, string y)
await Verify(Infrastructure.GenerateCode(code));
}

[Fact]
public async Task WorksWithGenericTypeConstraintsForMethods()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

[GenerateAutomaticInterface]
public class DemoClass
{
public IQueryable<T> AddFilter<T>(IQueryable<T> qry) where T : notnull => qry;
}


""";

await Verify(Infrastructure.GenerateCode(code));
}

[Fact]
public async Task GeneratesOverloadedMethodInterface()
{
Expand Down
13 changes: 13 additions & 0 deletions AutomaticInterface/Tests/test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Tests;

public interface Itest
{
IQueryable<T> AddFilter<T>(IQueryable<T> qry)
where T : notnull;
}

public class test : Itest

Check warning on line 9 in AutomaticInterface/Tests/test.cs

View workflow job for this annotation

GitHub Actions / test

The type name 'test' only contains lower-cased ascii characters. Such names may become reserved for the language.
{
public IQueryable<T> AddFilter<T>(IQueryable<T> qry)
where T : notnull => qry;
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ Note that we use [Verify](https://github.com/VerifyTests/Verify) for testing. It

## Changelog

### 5.1.3.

- Emit `notnull` type constraints on generic type parameters

### 5.1.2

- Fixing enums in method signatures
Expand Down
Loading