-
Notifications
You must be signed in to change notification settings - Fork 468
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
Tiny Refactoring CA1824 #5124
Tiny Refactoring CA1824 #5124
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ public abstract class MarkAssembliesWithNeutralResourcesLanguageAnalyzer : Diagn | |
isDataflowRule: false, | ||
isReportedAtCompilationEnd: true); | ||
|
||
protected abstract void RegisterAttributeAnalyzer(CompilationStartAnalysisContext context, Action onResourceFound); | ||
protected abstract void RegisterAttributeAnalyzer(CompilationStartAnalysisContext context, Action onResourceFound, INamedTypeSymbol generatedCode); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
|
@@ -53,9 +53,26 @@ public override void Initialize(AnalysisContext context) | |
|
||
context.RegisterCompilationStartAction(cc => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you have done some extra refactorings, it might be worth also renaming |
||
{ | ||
var hasResource = false; | ||
INamedTypeSymbol? generatedCode = cc.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemCodeDomCompilerGeneratedCodeAttribute); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [cosmetic][minor] You could use the |
||
if (generatedCode is null) | ||
{ | ||
return; | ||
} | ||
|
||
INamedTypeSymbol? attribute = cc.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemResourcesNeutralResourcesLanguageAttribute); | ||
if (attribute is null) | ||
{ | ||
return; | ||
} | ||
|
||
RegisterAttributeAnalyzer(cc, () => hasResource = true); | ||
if (TryCheckNeutralResourcesLanguageAttribute(cc.Compilation, attribute, out var data)) | ||
{ | ||
// attribute already exist | ||
return; | ||
} | ||
|
||
var hasResource = false; | ||
RegisterAttributeAnalyzer(cc, () => hasResource = true, generatedCode); | ||
|
||
cc.RegisterCompilationEndAction(ce => | ||
{ | ||
|
@@ -65,12 +82,6 @@ public override void Initialize(AnalysisContext context) | |
return; | ||
} | ||
|
||
if (TryCheckNeutralResourcesLanguageAttribute(ce, out AttributeData data)) | ||
{ | ||
// attribute already exist | ||
return; | ||
} | ||
|
||
if (data != null) | ||
{ | ||
// we have the attribute but its doing it wrong. | ||
|
@@ -89,14 +100,13 @@ protected static bool CheckDesignerFile(SyntaxTree tree) | |
return tree.FilePath?.IndexOf(Designer, StringComparison.OrdinalIgnoreCase) > 0; | ||
} | ||
|
||
protected static bool CheckResxGeneratedFile(SemanticModel model, SyntaxNode attribute, SyntaxNode argument, CancellationToken cancellationToken) | ||
protected static bool CheckResxGeneratedFile(SemanticModel model, SyntaxNode attribute, SyntaxNode argument, INamedTypeSymbol generatedCode, CancellationToken cancellationToken) | ||
{ | ||
if (!CheckDesignerFile(model.SyntaxTree)) | ||
{ | ||
return false; | ||
} | ||
|
||
INamedTypeSymbol? generatedCode = model.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemCodeDomCompilerGeneratedCodeAttribute); | ||
if (model.GetSymbolInfo(attribute, cancellationToken).Symbol?.ContainingType?.Equals(generatedCode) != true) | ||
{ | ||
return false; | ||
|
@@ -121,15 +131,12 @@ protected static bool CheckResxGeneratedFile(SemanticModel model, SyntaxNode att | |
return true; | ||
} | ||
|
||
private static bool TryCheckNeutralResourcesLanguageAttribute(CompilationAnalysisContext context, out AttributeData attributeData) | ||
private static bool TryCheckNeutralResourcesLanguageAttribute(Compilation compilation, INamedTypeSymbol attribute, out AttributeData? attributeData) | ||
{ | ||
INamedTypeSymbol? attribute = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemResourcesNeutralResourcesLanguageAttribute); | ||
INamedTypeSymbol? @string = context.Compilation.GetSpecialType(SpecialType.System_String); | ||
|
||
IEnumerable<AttributeData> attributes = context.Compilation.Assembly.GetAttributes().Where(d => d.AttributeClass?.Equals(attribute) == true); | ||
IEnumerable<AttributeData> attributes = compilation.Assembly.GetAttributes().Where(d => SymbolEqualityComparer.Default.Equals(attribute, d.AttributeClass)); | ||
foreach (AttributeData data in attributes) | ||
{ | ||
if (data.ConstructorArguments.Any(c => c.Type?.Equals(@string) == true && !string.IsNullOrWhiteSpace((string)c.Value))) | ||
if (data.ConstructorArguments.Any(c => c.Value is string constantValue && !string.IsNullOrWhiteSpace(constantValue))) | ||
{ | ||
// found one that already does right thing. | ||
attributeData = data; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous code was having the possibility that the
Node
isn't aAttributeSyntax
, it also doesn't look possible but I am wondering if that was done for a specific reason or just by "node is generic so let's work in the generic case".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Evangelink It should always be
AttributeSyntax
since we doRegisterSyntaxNodeAction(...., SyntaxKind.Attribute)