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

Fix invalid parameter definitions when methods have nonzero enum default values #68

Closed
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
54 changes: 49 additions & 5 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ private static string InheritDoc(ISymbol source) =>
memberOptions: SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions: SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeParamsRefOut
| SymbolDisplayParameterOptions.IncludeDefaultValue
| SymbolDisplayParameterOptions.IncludeName,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included,
Expand Down Expand Up @@ -105,10 +104,7 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth
ActivateNullableIfNeeded(codeGenerator, method);

var paramResult = new HashSet<string>();
method
.Parameters.Select(x => x.ToDisplayString(FullyQualifiedDisplayFormat))
.ToList()
.ForEach(x => paramResult.Add(x));
method.Parameters.Select(GetParameterSignature).ToList().ForEach(x => paramResult.Add(x));

var typedArgs = method
.TypeParameters.Select(arg =>
Expand All @@ -128,6 +124,54 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth
);
}

private static string GetParameterSignature(IParameterSymbol parameterSymbol)
{
var parameterDefinition = parameterSymbol.ToDisplayString(FullyQualifiedDisplayFormat);

if (!parameterSymbol.HasExplicitDefaultValue)
{
return parameterDefinition;
}

var defaultValue = parameterSymbol.ExplicitDefaultValue switch
{
string value => $"\"{value}\"",
bool value => $"{(value ? "true" : "false")}",
object value when IsEnum(parameterSymbol.Type, out var enumType) && !Equals(value, 0)
=> $"({enumType!.ToDisplayString(FullyQualifiedDisplayFormat)}){value}",
// struct
null when parameterSymbol.Type.IsValueType
=> $"default({parameterSymbol.Type.ToDisplayString(FullyQualifiedDisplayFormat)})",
null => "null",
_ => $"{parameterSymbol.ExplicitDefaultValue}",
Comment on lines +138 to +146
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code, apart from the enum-specific branch, has been restored as-is from the version of the class prior to the last PR.

};

return $"{parameterDefinition} = {defaultValue}";
}

private static bool IsEnum(ITypeSymbol type, out ITypeSymbol? enumType)
{
if (type is { TypeKind: TypeKind.Enum })
{
enumType = type;
return true;
}

if (
type.NullableAnnotation == NullableAnnotation.Annotated
&& type is INamedTypeSymbol namedType
&& namedType.TypeArguments.FirstOrDefault()
is { TypeKind: TypeKind.Enum } enumTypeArgument
)
{
enumType = enumTypeArgument;
return true;
}

enumType = null;
return false;
}

private static void ActivateNullableIfNeeded(
InterfaceBuilder codeGenerator,
ITypeSymbol typeSymbol
Expand Down
184 changes: 0 additions & 184 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,190 +49,6 @@ out var diagnostics
return outputCompilation.SyntaxTrees.Skip(1).LastOrDefault()?.ToString();
}

[Fact]
public void WorksWithOptionalParameters()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

[GenerateAutomaticInterface]
public class DemoClass
{
public bool TryStartTransaction(
string file = "",
string member = "",
int line = 0,
bool notify = true)
{
return true;
}
}


""";
const string expected = """
//--------------------------------------------------------------------------------------------------
// <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.TryStartTransaction(string, string, int, bool)" />
bool TryStartTransaction(string file = "", string member = "", int line = 0, bool notify = true);

}
}

""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithParamsParameters()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

[GenerateAutomaticInterface]
public class DemoClass
{
public void AMethod(params int[] numbers)
{
}
}


""";
const string expected = """
//--------------------------------------------------------------------------------------------------
// <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.AMethod(params int[])" />
void AMethod(params int[] numbers);

}
}

""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithOptionalStructParameters()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

public struct MyStruct
{
private int Bar;
}

[GenerateAutomaticInterface]
public class DemoClass
{
public bool TryStartTransaction(MyStruct data = default(MyStruct))
{
return true;
}
}


""";
const string expected = """
//--------------------------------------------------------------------------------------------------
// <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.TryStartTransaction(AutomaticInterfaceExample.MyStruct)" />
bool TryStartTransaction(global::AutomaticInterfaceExample.MyStruct data = default(global::AutomaticInterfaceExample.MyStruct));

}
}

""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithOptionalNullParameters()
{
const string code = """

using AutomaticInterface;

namespace AutomaticInterfaceExample;

[GenerateAutomaticInterface]
public class DemoClass
{
public bool TryStartTransaction(string data = null)
{
return true;
}
}


""";
const string expected = """
//--------------------------------------------------------------------------------------------------
// <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.TryStartTransaction(string)" />
bool TryStartTransaction(string data = null);

}
}

""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void GeneratesEmptyInterface()
{
Expand Down
Loading
Loading