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 build errors due to indexers #273

Merged
merged 1 commit into from
Apr 12, 2024
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
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
Loading