|
1 | 1 | using Facet.Generators.Shared; |
2 | 2 | using Facet.Generators; |
3 | 3 | using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
4 | 6 | using System.Collections.Generic; |
5 | 7 | using System.Collections.Immutable; |
6 | 8 | using System.Linq; |
@@ -30,7 +32,7 @@ internal static class FlattenModelBuilder |
30 | 32 | if (sourceType == null) return null; |
31 | 33 |
|
32 | 34 | // Extract attribute parameters |
33 | | - var excludedPaths = ExtractExcludedPaths(attribute); |
| 35 | + var excludedPaths = ExtractExcludedPaths(attribute, context); |
34 | 36 | var maxDepth = GetNamedArg(attribute.NamedArguments, "MaxDepth", 3); |
35 | 37 | var namingStrategy = GetNamedArg(attribute.NamedArguments, "NamingStrategy", FlattenNamingStrategy.Prefix); |
36 | 38 | var includeFields = GetNamedArg(attribute.NamedArguments, "IncludeFields", false); |
@@ -88,10 +90,137 @@ internal static class FlattenModelBuilder |
88 | 90 | maxDepth); |
89 | 91 | } |
90 | 92 |
|
91 | | - private static HashSet<string> ExtractExcludedPaths(AttributeData attribute) |
| 93 | + private static HashSet<string> ExtractExcludedPaths(AttributeData attribute, GeneratorAttributeSyntaxContext? context = null) |
92 | 94 | { |
93 | 95 | var excluded = new HashSet<string>(); |
94 | 96 |
|
| 97 | + // Get the source type name for stripping from full nameof paths |
| 98 | + var sourceType = attribute.ConstructorArguments[0].Value as INamedTypeSymbol; |
| 99 | + var sourceTypeName = sourceType?.Name ?? string.Empty; |
| 100 | + |
| 101 | + // Helper to process a single expression and add to set |
| 102 | + void ProcessExpression(ExpressionSyntax expr) |
| 103 | + { |
| 104 | + if (expr == null) return; |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + var (resolved, hadLeadingAt) = NameOfResolver.ResolveExpression(expr); |
| 109 | + if (!string.IsNullOrEmpty(resolved)) |
| 110 | + { |
| 111 | + // If @ was used, the path includes the source type name, so strip it |
| 112 | + // e.g., @Company.HeadquartersAddress.ZipCode -> HeadquartersAddress.ZipCode |
| 113 | + var path = resolved!; |
| 114 | + if (hadLeadingAt && !string.IsNullOrEmpty(sourceTypeName) && path.StartsWith(sourceTypeName + ".")) |
| 115 | + { |
| 116 | + path = path.Substring(sourceTypeName.Length + 1); |
| 117 | + } |
| 118 | + excluded.Add(path); |
| 119 | + return; |
| 120 | + } |
| 121 | + } |
| 122 | + catch |
| 123 | + { |
| 124 | + // ignore and fallback |
| 125 | + } |
| 126 | + |
| 127 | + // Prefer literal extraction for string literals |
| 128 | + if (expr is LiteralExpressionSyntax lit && lit.IsKind(SyntaxKind.StringLiteralExpression)) |
| 129 | + { |
| 130 | + var val = lit.Token.ValueText; |
| 131 | + if (!string.IsNullOrEmpty(val)) excluded.Add(val); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // Handle array/initializer or other expressions by textual fallback (strip quotes) |
| 136 | + var text = expr.ToString().Trim(); |
| 137 | + if (text.Length == 0) return; |
| 138 | + |
| 139 | + if ((text.StartsWith("@\"") && text.EndsWith("\"")) || (text.StartsWith("\"") && text.EndsWith("\""))) |
| 140 | + { |
| 141 | + var firstQuote = text.IndexOf('"'); |
| 142 | + if (firstQuote >= 0 && text.Length - firstQuote - 2 > 0) |
| 143 | + text = text.Substring(firstQuote + 1, text.Length - firstQuote - 2); |
| 144 | + else |
| 145 | + text = string.Empty; |
| 146 | + } |
| 147 | + |
| 148 | + if (!string.IsNullOrEmpty(text)) excluded.Add(text); |
| 149 | + } |
| 150 | + |
| 151 | + // Try to resolve from syntax if context is available |
| 152 | + if (context != null && attribute.ApplicationSyntaxReference?.GetSyntax() is AttributeSyntax attrSyntax) |
| 153 | + { |
| 154 | + // Try positional argument first (constructor params) |
| 155 | + var positionalArgs = attrSyntax.ArgumentList?.Arguments.Where(a => a.NameEquals == null && a.NameColon == null).ToList(); |
| 156 | + if (positionalArgs != null && positionalArgs.Count > 1) |
| 157 | + { |
| 158 | + // Second positional argument is the exclude array |
| 159 | + var excludeArg = positionalArgs[1]; |
| 160 | + var expr = excludeArg.Expression; |
| 161 | + InitializerExpressionSyntax? initializer = null; |
| 162 | + switch (expr) |
| 163 | + { |
| 164 | + case ImplicitArrayCreationExpressionSyntax implicitArray: |
| 165 | + initializer = implicitArray.Initializer; |
| 166 | + break; |
| 167 | + case ArrayCreationExpressionSyntax arrayCreation: |
| 168 | + initializer = arrayCreation.Initializer; |
| 169 | + break; |
| 170 | + case InitializerExpressionSyntax directInit: |
| 171 | + initializer = directInit; |
| 172 | + break; |
| 173 | + } |
| 174 | + |
| 175 | + if (initializer != null) |
| 176 | + { |
| 177 | + foreach (var e in initializer.Expressions) |
| 178 | + ProcessExpression(e); |
| 179 | + return excluded; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + // Try named argument (Exclude = ...) |
| 184 | + var excludeNamed = attrSyntax.ArgumentList?.Arguments.FirstOrDefault(a => (a.NameEquals?.Name.Identifier.ValueText == "Exclude") || (a.NameColon?.Name.Identifier.ValueText == "Exclude")); |
| 185 | + if (excludeNamed != null) |
| 186 | + { |
| 187 | + var expr = excludeNamed.Expression; |
| 188 | + InitializerExpressionSyntax? initializer = null; |
| 189 | + |
| 190 | + switch (expr) |
| 191 | + { |
| 192 | + case ImplicitArrayCreationExpressionSyntax implicitArray: |
| 193 | + initializer = implicitArray.Initializer; |
| 194 | + break; |
| 195 | + case ArrayCreationExpressionSyntax arrayCreation: |
| 196 | + initializer = arrayCreation.Initializer; |
| 197 | + break; |
| 198 | + case InitializerExpressionSyntax directInit: |
| 199 | + initializer = directInit; |
| 200 | + break; |
| 201 | + case CollectionExpressionSyntax collectionExpr: |
| 202 | + // Handle C# 12 collection expression syntax: [item1, item2, ...] |
| 203 | + foreach (var element in collectionExpr.Elements) |
| 204 | + { |
| 205 | + if (element is ExpressionElementSyntax exprElem) |
| 206 | + { |
| 207 | + ProcessExpression(exprElem.Expression); |
| 208 | + } |
| 209 | + } |
| 210 | + return excluded; |
| 211 | + } |
| 212 | + |
| 213 | + if (initializer != null) |
| 214 | + { |
| 215 | + foreach (var e in initializer.Expressions) |
| 216 | + ProcessExpression(e); |
| 217 | + return excluded; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + // Fallback to compiled attribute data |
95 | 224 | // Check constructor argument (params string[] exclude) |
96 | 225 | if (attribute.ConstructorArguments.Length > 1) |
97 | 226 | { |
|
0 commit comments