Skip to content

Commit 98425d5

Browse files
committed
Allow attribute to be defined in multiple projects
1 parent 068f293 commit 98425d5

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/Meziantou.Analyzer/Internals/CultureSensitiveFormattingContext.cs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ internal sealed class CultureSensitiveFormattingContext(Compilation compilation)
77
{
88
private readonly HashSet<ISymbol> _excludedMethods = CreateExcludedMethods(compilation);
99

10-
public INamedTypeSymbol? CultureInsensitiveTypeAttributeSymbol { get; } = compilation.GetBestTypeByMetadataName("Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute");
1110
public INamedTypeSymbol? FormatProviderSymbol { get; } = compilation.GetBestTypeByMetadataName("System.IFormatProvider");
1211
public INamedTypeSymbol? CultureInfoSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Globalization.CultureInfo");
1312
public INamedTypeSymbol? NumberStyleSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Globalization.NumberStyles");
@@ -46,7 +45,6 @@ static void AddDocumentationId(HashSet<ISymbol> result, Compilation compilation,
4645
}
4746
}
4847

49-
5048
private static bool MustUnwrapNullableOfT(CultureSensitiveOptions options)
5149
{
5250
return (options & CultureSensitiveOptions.UnwrapNullableOfT) == CultureSensitiveOptions.UnwrapNullableOfT;
@@ -292,17 +290,46 @@ private bool IsCultureSensitiveType(ITypeSymbol? typeSymbol, CultureSensitiveOpt
292290
return true;
293291
}
294292

293+
private static bool IsCultureSensitiveAttributeSymbol(ITypeSymbol? symbol)
294+
{
295+
// The attribute can be defined in multiple assemblies, so it's identified by its full name only
296+
// Meziantou.Analyzer.Annotations.CultureInsensitiveTypeAttribute
297+
return symbol is
298+
{
299+
Name: "CultureInsensitiveTypeAttribute",
300+
ContainingSymbol: INamespaceSymbol
301+
{
302+
Name: "Annotations",
303+
ContainingSymbol: INamespaceSymbol
304+
{
305+
Name: "Analyzer",
306+
ContainingSymbol: INamespaceSymbol
307+
{
308+
Name: "Meziantou",
309+
ContainingSymbol: INamespaceSymbol { IsGlobalNamespace: true }
310+
}
311+
}
312+
}
313+
};
314+
}
315+
295316
private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol)
296317
{
297-
var attributes = typeSymbol.GetAttributes(CultureInsensitiveTypeAttributeSymbol);
318+
var attributes = typeSymbol.GetAttributes();
298319
foreach (var attr in attributes)
299320
{
321+
if (!IsCultureSensitiveAttributeSymbol(attr.AttributeClass))
322+
continue;
323+
300324
if (attr.ConstructorArguments.IsEmpty)
301325
return false; // no format is set, so the type is culture insensitive
302326
}
303327

304-
foreach (var attribute in compilation.Assembly.GetAttributes(CultureInsensitiveTypeAttributeSymbol))
328+
foreach (var attribute in compilation.Assembly.GetAttributes())
305329
{
330+
if (!IsCultureSensitiveAttributeSymbol(attribute.AttributeClass))
331+
continue;
332+
306333
if (attribute.ConstructorArguments.IsEmpty)
307334
continue;
308335

@@ -318,9 +345,12 @@ private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol)
318345

319346
private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol, bool hasFormat, string? format)
320347
{
321-
var attributes = typeSymbol.GetAttributes(CultureInsensitiveTypeAttributeSymbol);
348+
var attributes = typeSymbol.GetAttributes();
322349
foreach (var attr in attributes)
323350
{
351+
if (!IsCultureSensitiveAttributeSymbol(attr.AttributeClass))
352+
continue;
353+
324354
if (attr.ConstructorArguments.IsEmpty)
325355
return false; // no format is set, so the type is culture insensitive
326356

@@ -338,8 +368,11 @@ private bool IsCultureSensitiveTypeUsingAttribute(ITypeSymbol typeSymbol, bool h
338368
return false; // no format is set, so the type is culture insensitive
339369
}
340370

341-
foreach (var attribute in compilation.Assembly.GetAttributes(CultureInsensitiveTypeAttributeSymbol))
371+
foreach (var attribute in compilation.Assembly.GetAttributes())
342372
{
373+
if (!IsCultureSensitiveAttributeSymbol(attribute.AttributeClass))
374+
continue;
375+
343376
if (attribute.ConstructorArguments.IsEmpty)
344377
continue;
345378

0 commit comments

Comments
 (0)