Skip to content

Commit

Permalink
Fix build errors due to indexers (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Apr 12, 2024
1 parent ddab462 commit f3f5b2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/NodeApi.Generator/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ member.Expression is ParameterExpression parameterExpression &&
"[" + ToCS(index.Arguments[0], path, variables) + "]",

MethodCallExpression { Method.IsSpecialName: true } call =>
call.Method.Name == "get_Item" && call.Arguments.Count >= 1 ?
WithParentheses(call.Object!, path, variables) +
FormatArgs(call.Arguments, path, variables, "[]") :
call.Method.Name == "set_Item" && call.Arguments.Count >= 2 ?
WithParentheses(call.Object!, path, variables) + FormatArgs(
call.Arguments.Take(call.Arguments.Count - 1), path, variables, "[]") +
" = " + ToCS(call.Arguments.Last(), path, variables) :
#if NETFRAMEWORK
call.Method.Name.StartsWith("get_") ?
(call.Method.IsStatic ?
Expand Down Expand Up @@ -390,6 +397,11 @@ private static string FormatArgs(
private static string FormatArgs(
IEnumerable<Expression> arguments,
string path,
HashSet<string>? variables)
=> "(" + string.Join(", ", arguments.Select((a) => ToCS(a, path, variables))) + ")";
HashSet<string>? variables,
string brackets = "()")
{
char start = brackets[0];
char end = brackets[1];
return start + string.Join(", ", arguments.Select((a) => ToCS(a, path, variables))) + end;
}
}
12 changes: 11 additions & 1 deletion src/NodeApi.Generator/ModuleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,17 @@ private void ExportMembers(
{
if (member is IPropertySymbol property)
{
ExportProperty(ref s, property, GetExportName(member));
if (property.Parameters.Any())
{
ReportWarning(
DiagnosticId.UnsupportedIndexer,
property,
$"Exporting indexers is not supported.");
}
else
{
ExportProperty(ref s, property, GetExportName(member));
}
}
else if (type.TypeKind == TypeKind.Enum && member is IFieldSymbol field)
{
Expand Down
1 change: 1 addition & 0 deletions src/NodeApi.Generator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum DiagnosticId
UnsupportedMethodParameterType,
UnsupportedMethodReturnType,
UnsupportedOverloads,
UnsupportedIndexer,
ReferenedTypeNotExported,
ESModulePropertiesAreConst,
}
Expand Down
13 changes: 10 additions & 3 deletions src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,19 @@ public static PropertyInfo AsPropertyInfo(this IPropertySymbol propertySymbol)
{
Type type = propertySymbol.ContainingType.AsType();

// Ensure the property type is built.
propertySymbol.Type.AsType(type.GenericTypeArguments, buildType: true);
Type propertyType = propertySymbol.Type.AsType(type.GenericTypeArguments, buildType: true);
Type[] parameterTypes = propertySymbol.Parameters.Select(
(p) => p.Type.AsType(type.GenericTypeArguments, buildType: true)).ToArray();

BindingFlags bindingFlags = BindingFlags.Public |
(propertySymbol.IsStatic ? BindingFlags.Static : BindingFlags.Instance);
PropertyInfo? propertyInfo = type.GetProperty(propertySymbol.Name, bindingFlags);
PropertyInfo? propertyInfo = type.GetProperty(
propertySymbol.Name,
bindingFlags,
null,
propertyType,
parameterTypes,
null);
return propertyInfo ?? throw new InvalidOperationException(
$"Property not found: {type.Name}.{propertySymbol.Name}");
}
Expand Down

0 comments on commit f3f5b2e

Please sign in to comment.