Skip to content

Handle cases where the Signature generator fails catastrophically #1661

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

Merged
merged 9 commits into from
Apr 21, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ private MemberData AssembleMembers(Type type, BindingFlags memberBinding)
switch (member)
{
case ConstructorInfo constructor:
constructors.Add(AssembleConstructor(constructor));
if (TryAssembleConstructor(constructor, out string[] cInfo)) {
constructors.Add(cInfo);
}
break;

case FieldInfo field:
Expand Down Expand Up @@ -400,7 +402,10 @@ private MemberData AssembleMembers(Type type, BindingFlags memberBinding)
var methodDatas = new JsonDictionary<string, MethodData>();
foreach (KeyValuePair<string, List<MethodInfo>> method in methods)
{
methodDatas[method.Key] = AssembleMethod(method.Value);
if (TryAssembleMethod(method.Value, out MethodData md))
{
methodDatas[method.Key] = md;
}
}

return new MemberData()
Expand Down Expand Up @@ -457,35 +462,61 @@ private EventData AssembleEvent(EventInfo e)
};
}

private string[] AssembleConstructor(ConstructorInfo ctor)
// private string[] AssembleConstructor(ConstructorInfo ctor)
private bool TryAssembleConstructor(ConstructorInfo ctor, out string[] result)
{
bool success = false;
var parameters = new List<string>();
foreach (ParameterInfo param in ctor.GetParameters())
try
{
foreach (ParameterInfo param in ctor.GetParameters())
{
parameters.Add(TypeNaming.GetFullTypeName(param.ParameterType));
}

result = parameters.ToArray();
success = true;
}
catch
{
parameters.Add(TypeNaming.GetFullTypeName(param.ParameterType));
result = null;
}

return parameters.ToArray();
return success;
}

private MethodData AssembleMethod(List<MethodInfo> methodOverloads)
// private MethodData AssembleMethod(List<MethodInfo> methodOverloads)
private bool TryAssembleMethod(List<MethodInfo> methodOverloads, out MethodData result)
{
var overloads = new List<string[]>();
foreach (MethodInfo overload in methodOverloads)
{
var parameters = new List<string>();
foreach (ParameterInfo param in overload.GetParameters())
try
{
parameters.Add(TypeNaming.GetFullTypeName(param.ParameterType));
foreach (ParameterInfo param in overload.GetParameters())
{
parameters.Add(TypeNaming.GetFullTypeName(param.ParameterType));
}
overloads.Add(parameters.ToArray());
}
overloads.Add(parameters.ToArray());
catch
{
}
}

if (overloads.Count == 0)
{
result = null;
return false;
}

return new MethodData()
result = new MethodData()
{
ReturnType = TypeNaming.GetFullTypeName(methodOverloads[0].ReturnType),
OverloadParameters = overloads.ToArray()
};
return true;
}

private bool IsAssemblyPathExcluded(string path)
Expand Down
2 changes: 2 additions & 0 deletions PSCompatibilityCollector/Tests/UtilityApi.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Describe "Type name serialization" {
@{ InputType = [System.Collections.Generic.Dictionary`2+Enumerator[string, object]]; ExpectedName = "System.Collections.Generic.Dictionary``2+Enumerator[System.String,System.Object]" }
@{ InputType = [System.Collections.Concurrent.ConcurrentDictionary`2].GetMethod('ToArray').ReturnType; ExpectedName = "System.Collections.Generic.KeyValuePair``2[]" }
) {
param ( $InputType, $ExpectedName )
$name = [Microsoft.PowerShell.CrossCompatibility.TypeNaming]::GetFullTypeName($InputType)
$name | Should -BeExactly $ExpectedName
}
Expand Down Expand Up @@ -81,6 +82,7 @@ Describe "Type accelerator expansion" {
@{ Raw = "[object]"; Expanded = "System.Object" }
) -Test {

param ( $Raw, $Expanded )
$typeName = Get-TypeNameAstFromScript -Script $Raw

$canonicalName = [Microsoft.PowerShell.CrossCompatibility.TypeNaming]::GetCanonicalTypeName($typeAccelerators, $typeName)
Expand Down