Skip to content

Commit

Permalink
chore(code-cleanup): code cleanup for tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Sep 21, 2023
1 parent baf8757 commit 36f2a6b
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 18 deletions.
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/DeprecatedAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/DiscordDeprecatedAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/DiscordInExperimentAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/DiscordUnreleasedAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/ExperimentalAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/Features.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/Properties/AssemblyProperties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("DisCatSharp")]
Expand Down
1 change: 0 additions & 1 deletion DisCatSharp.Attributes/RequiresFeatureAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;

namespace DisCatSharp.Attributes;
Expand Down
1 change: 1 addition & 0 deletions DisCatSharp.Tools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -76,6 +78,7 @@ private static void StatusAnalyzer(SyntaxNodeAnalysisContext context)
Console.WriteLine("Faulty");
return;
}

var attributes = declaration.GetAttributes();

var name = declaration.Name;
Expand All @@ -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)));
Expand All @@ -97,46 +101,53 @@ 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;
}

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;
Expand All @@ -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)));
Expand All @@ -164,46 +174,52 @@ 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);

var result = attribute.AttributeClass.Equals(desiredTypeNamedSymbol, SymbolEqualityComparer.Default);
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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.6.36389" />
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.7.37357" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 36f2a6b

Please sign in to comment.