diff --git a/src/NodeApi.Generator/ExpressionExtensions.cs b/src/NodeApi.Generator/ExpressionExtensions.cs index d9a30f98..b96a80c8 100644 --- a/src/NodeApi.Generator/ExpressionExtensions.cs +++ b/src/NodeApi.Generator/ExpressionExtensions.cs @@ -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 ? @@ -390,6 +397,11 @@ private static string FormatArgs( private static string FormatArgs( IEnumerable arguments, string path, - HashSet? variables) - => "(" + string.Join(", ", arguments.Select((a) => ToCS(a, path, variables))) + ")"; + HashSet? variables, + string brackets = "()") + { + char start = brackets[0]; + char end = brackets[1]; + return start + string.Join(", ", arguments.Select((a) => ToCS(a, path, variables))) + end; + } } diff --git a/src/NodeApi.Generator/ModuleGenerator.cs b/src/NodeApi.Generator/ModuleGenerator.cs index 43575c9b..6eba4fce 100644 --- a/src/NodeApi.Generator/ModuleGenerator.cs +++ b/src/NodeApi.Generator/ModuleGenerator.cs @@ -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) { diff --git a/src/NodeApi.Generator/SourceGenerator.cs b/src/NodeApi.Generator/SourceGenerator.cs index e9d0e1ad..5dafdeff 100644 --- a/src/NodeApi.Generator/SourceGenerator.cs +++ b/src/NodeApi.Generator/SourceGenerator.cs @@ -41,6 +41,7 @@ public enum DiagnosticId UnsupportedMethodParameterType, UnsupportedMethodReturnType, UnsupportedOverloads, + UnsupportedIndexer, ReferenedTypeNotExported, ESModulePropertiesAreConst, } diff --git a/src/NodeApi.Generator/SymbolExtensions.cs b/src/NodeApi.Generator/SymbolExtensions.cs index 18d108dd..2969ebc5 100644 --- a/src/NodeApi.Generator/SymbolExtensions.cs +++ b/src/NodeApi.Generator/SymbolExtensions.cs @@ -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}"); }