From 36f2a6b37c8baa7b3857834620d3f1187036d021 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 21 Sep 2023 23:24:40 +0200 Subject: [PATCH] chore(code-cleanup): code cleanup for tools --- DisCatSharp.Attributes/DeprecatedAttribute.cs | 1 - .../DiscordDeprecatedAttribute.cs | 1 - .../DiscordInExperimentAttribute.cs | 1 - .../DiscordUnreleasedAttribute.cs | 1 - .../ExperimentalAttribute.cs | 1 - DisCatSharp.Attributes/Features.cs | 1 - .../Properties/AssemblyProperties.cs | 1 - .../RequiresFeatureAttribute.cs | 1 - DisCatSharp.Tools.sln | 1 + .../DisCatSharp.Analyzer/AttributeAnalyzer.cs | 34 ++++++++++++++----- .../DisCatSharp.Analyzer.csproj | 2 +- 11 files changed, 27 insertions(+), 18 deletions(-) diff --git a/DisCatSharp.Attributes/DeprecatedAttribute.cs b/DisCatSharp.Attributes/DeprecatedAttribute.cs index 8a3cb972f..d0758cea1 100644 --- a/DisCatSharp.Attributes/DeprecatedAttribute.cs +++ b/DisCatSharp.Attributes/DeprecatedAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/DiscordDeprecatedAttribute.cs b/DisCatSharp.Attributes/DiscordDeprecatedAttribute.cs index ac595b1ef..c51d37e83 100644 --- a/DisCatSharp.Attributes/DiscordDeprecatedAttribute.cs +++ b/DisCatSharp.Attributes/DiscordDeprecatedAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/DiscordInExperimentAttribute.cs b/DisCatSharp.Attributes/DiscordInExperimentAttribute.cs index f26656a24..cac91f3b8 100644 --- a/DisCatSharp.Attributes/DiscordInExperimentAttribute.cs +++ b/DisCatSharp.Attributes/DiscordInExperimentAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/DiscordUnreleasedAttribute.cs b/DisCatSharp.Attributes/DiscordUnreleasedAttribute.cs index 2586300c6..6374c5743 100644 --- a/DisCatSharp.Attributes/DiscordUnreleasedAttribute.cs +++ b/DisCatSharp.Attributes/DiscordUnreleasedAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/ExperimentalAttribute.cs b/DisCatSharp.Attributes/ExperimentalAttribute.cs index 5ff222e14..c6a99a5ad 100644 --- a/DisCatSharp.Attributes/ExperimentalAttribute.cs +++ b/DisCatSharp.Attributes/ExperimentalAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/Features.cs b/DisCatSharp.Attributes/Features.cs index d579f958e..57ac9d82d 100644 --- a/DisCatSharp.Attributes/Features.cs +++ b/DisCatSharp.Attributes/Features.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Attributes/Properties/AssemblyProperties.cs b/DisCatSharp.Attributes/Properties/AssemblyProperties.cs index 0d5cb60ed..8c6b93f3c 100644 --- a/DisCatSharp.Attributes/Properties/AssemblyProperties.cs +++ b/DisCatSharp.Attributes/Properties/AssemblyProperties.cs @@ -1,4 +1,3 @@ - using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("DisCatSharp")] diff --git a/DisCatSharp.Attributes/RequiresFeatureAttribute.cs b/DisCatSharp.Attributes/RequiresFeatureAttribute.cs index b53615678..023b73a91 100644 --- a/DisCatSharp.Attributes/RequiresFeatureAttribute.cs +++ b/DisCatSharp.Attributes/RequiresFeatureAttribute.cs @@ -1,4 +1,3 @@ - using System; namespace DisCatSharp.Attributes; diff --git a/DisCatSharp.Tools.sln b/DisCatSharp.Tools.sln index 778f0f984..9416df2a1 100644 --- a/DisCatSharp.Tools.sln +++ b/DisCatSharp.Tools.sln @@ -13,6 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DisCatSharp.Experimental", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6CBDA593-E86C-44F3-9F22-BB5F32D7D48E}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig DisCatSharp.Tools\DisCatSharp.ruleset = DisCatSharp.Tools\DisCatSharp.ruleset EndProjectSection EndProject diff --git a/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/AttributeAnalyzer.cs b/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/AttributeAnalyzer.cs index c9896efdd..5310a4fd9 100644 --- a/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/AttributeAnalyzer.cs +++ b/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/AttributeAnalyzer.cs @@ -1,10 +1,11 @@ - using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Reflection; + using DisCatSharp.Attributes; + using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; @@ -67,6 +68,7 @@ public override void Initialize(AnalysisContext context) context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.ElementAccessExpression); context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.SimpleMemberAccessExpression); } + private static void StatusAnalyzer(SyntaxNodeAnalysisContext context) { var invocation = context.Node; @@ -76,6 +78,7 @@ private static void StatusAnalyzer(SyntaxNodeAnalysisContext context) Console.WriteLine("Faulty"); return; } + var attributes = declaration.GetAttributes(); var name = declaration.Name; @@ -85,6 +88,7 @@ private static void StatusAnalyzer(SyntaxNodeAnalysisContext context) name = declaration.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); kind = "Constructor"; } + var experimentalAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(ExperimentalAttribute))); var deprecatedAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(DeprecatedAttribute))); var discordInExperimentAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(DiscordInExperimentAttribute))); @@ -97,31 +101,37 @@ private static void StatusAnalyzer(SyntaxNodeAnalysisContext context) var message = GetMessage(experimentalAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_experimentalRule, invocation.GetLocation(), kind, name, message)); } + if (deprecatedAttributeData != null) { var message = GetMessage(deprecatedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_deprecatedRule, invocation.GetLocation(), kind, name, message)); } + if (discordInExperimentAttributeData != null) { var message = GetMessage(discordInExperimentAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordInExperimentRule, invocation.GetLocation(), kind, name, message)); } + if (discordDeprecatedAttributeData != null) { var message = GetMessage(discordDeprecatedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordDeprecatedRule, invocation.GetLocation(), kind, name, message)); } + if (discordUnreleasedAttributeData != null) { var message = GetMessage(discordUnreleasedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordUnreleasedRule, invocation.GetLocation(), kind, name, message)); } + if (requiresFeatureAttributeData != null) { var message = GetFeatureMessage(requiresFeatureAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_requiresFeatureRule, invocation.GetLocation(), kind, name, message)); } + return; } @@ -129,14 +139,15 @@ private static void ExperimentalAnalyzer(SymbolAnalysisContext context) { Console.WriteLine("Handling " + context.Symbol.Kind.ToString()); var syntaxTrees = from x in context.Symbol.Locations - where x.IsInSource - select x.SourceTree; + where x.IsInSource + select x.SourceTree; var declaration = context.Symbol; if (null == declaration) { Console.WriteLine("Faulty"); return; } + var attributes = declaration.GetAttributes(); var name = declaration.Name; @@ -147,9 +158,8 @@ where x.IsInSource kind = "Constructor"; } else if (kind == "NamedType") - { kind = "Class"; - } + var model = context.Compilation.GetSemanticModel(syntaxTrees.First(), true); var experimentalAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(model, attr, typeof(ExperimentalAttribute))); @@ -164,35 +174,41 @@ where x.IsInSource var message = GetMessage(experimentalAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_experimentalRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + if (deprecatedAttributeData != null) { var message = GetMessage(deprecatedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_deprecatedRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + if (discordInExperimentAttributeData != null) { var message = GetMessage(discordInExperimentAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordInExperimentRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + if (discordDeprecatedAttributeData != null) { var message = GetMessage(discordDeprecatedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordDeprecatedRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + if (discordUnreleasedAttributeData != null) { var message = GetMessage(discordUnreleasedAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_discordUnreleasedRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + if (requiresFeatureAttributeData != null) { var message = GetFeatureMessage(requiresFeatureAttributeData); context.ReportDiagnostic(Diagnostic.Create(s_requiresFeatureRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message)); } + return; } - static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeData attribute, Type desiredAttributeType) + private static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeData attribute, Type desiredAttributeType) { var desiredTypeNamedSymbol = semanticModel.Compilation.GetTypeByMetadataName(desiredAttributeType.FullName); @@ -200,10 +216,10 @@ static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeData attri return result; } - static string GetMessage(AttributeData attribute) + private static string GetMessage(AttributeData attribute) => attribute.ConstructorArguments.Length < 1 ? "Do not use in production." : attribute.ConstructorArguments[0].Value as string; - static string GetFeatureMessage(AttributeData attribute) + private static string GetFeatureMessage(AttributeData attribute) { var featureReqEnum = (Features)attribute.ConstructorArguments[0].Value; var description = attribute.ConstructorArguments.Length > 1 @@ -237,7 +253,7 @@ static Helpers() public static string ToFeaturesString(this Features features) { var strs = FeaturesStrings - .Where(xkvp =>(features & xkvp.Key) == xkvp.Key) + .Where(xkvp => (features & xkvp.Key) == xkvp.Key) .Select(xkvp => xkvp.Value); return string.Join(", ", strs.OrderBy(xs => xs)); diff --git a/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/DisCatSharp.Analyzer.csproj b/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/DisCatSharp.Analyzer.csproj index 17a2bfde2..ce0b42513 100644 --- a/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/DisCatSharp.Analyzer.csproj +++ b/DisCatSharp.Tools/DisCatSharp.Analyzer/DisCatSharp.Analyzer/DisCatSharp.Analyzer.csproj @@ -38,7 +38,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - +