Skip to content

Commit

Permalink
add new attribute infos
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Nov 23, 2022
1 parent c3c0154 commit 9caf1a7
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 61 deletions.
12 changes: 12 additions & 0 deletions DisCatSharp.Docs/vs/analyzer/dcs/0101.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
uid: vs_analyzer_dcs_0101
title: Visual Studio Tools
---

# DisCatSharp Analyzer Rule DCS0101

You try to use an field, function or similar which is in experiment by discord.

Most experiments aren't stable and subject to change at any time.

Use carefully and at your own risk.
10 changes: 10 additions & 0 deletions DisCatSharp.Docs/vs/analyzer/dcs/0102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
uid: vs_analyzer_dcs_0102
title: Visual Studio Tools
---

# DisCatSharp Analyzer Rule DCS0102

You try to use an field, function or similar which is deprecated by discord.

Don't use this anymore. It will be removed in the future.
10 changes: 10 additions & 0 deletions DisCatSharp.Docs/vs/analyzer/dcs/0103.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
uid: vs_analyzer_dcs_0103
title: Visual Studio Tools
---

# DisCatSharp Analyzer Rule DCS0103

You try to use an field, function or similar which isn't released yet by discord.

Use carefully and at your own risk.
3 changes: 3 additions & 0 deletions DisCatSharp.Docs/vs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ To use the DisCatSharp Analyzer, you need to install the [DisCatSharp.Analyzer.R
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
[DCS0001](xref:vs_analyzer_dcs_0001) | Information | Warning | Experimental Attribute Analyzer
[DCS0101](xref:vs_analyzer_dcs_0101) | Information | Warning | DiscordInExperiment Attribute Analyzer
[DCS0102](xref:vs_analyzer_dcs_0102) | Information | Warning | DiscordDeprecated Attribute Analyzer
[DCS0103](xref:vs_analyzer_dcs_0103) | Information | Warning | DiscordUnreleased Attribute Analyzer
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<PropertyGroup>
<PackageId>DisCatSharp.Analyzer.Roselyn</PackageId>
<Version>4.8.0</Version>
<PackageVersion>4.8.0</PackageVersion>
<Version>5.0.0</Version>
<PackageVersion>5.0.0</PackageVersion>
<Authors>AITSYS</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Description>DisCatSharp Analyzer</Description>
Expand Down Expand Up @@ -53,7 +53,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DisCatSharp.Analyzer\DisCatSharp.Analyzer.csproj" />
<PackageReference Include="DisCatSharp.Attributes" Version="10.3.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="DisCatSharp.Attributes" Version="10.3.1-fixed-001" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ DCS0001 | Information | Warning | Experimental Attribute Analyzer
Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
DCS0001 | Information | Warning | Experimental Attribute Analyzer


## Release 5.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
DCS0101 | Information | Warning | DiscordInExperiment Attribute Analyzer
DCS0102 | Information | Warning | DiscordDeprecated Attribute Analyzer
DCS0103 | Information | Warning | DiscordUnreleased Attribute Analyzer
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.CodeAnalysis.Diagnostics;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

Expand All @@ -37,15 +38,33 @@ namespace DisCatSharp.Analyzer
public class AttributeAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticIdPrefix = "DCS";
public const string Category = "Information";

private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.AnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private const string Category = "Information";
private static readonly LocalizableString TitleExperimental = new LocalizableResourceString(nameof(Resources.AnalyzerTitleExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormatExperimental = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormatExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DescriptionExperimental = new LocalizableResourceString(nameof(Resources.AnalyzerDescriptionExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString TitleDiscordInExperiment = new LocalizableResourceString(nameof(Resources.AnalyzerTitleExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormatDiscordInExperiment = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormatExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DescriptionDiscordInExperiment = new LocalizableResourceString(nameof(Resources.AnalyzerDescriptionExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString TitleDiscordDeprecated = new LocalizableResourceString(nameof(Resources.AnalyzerTitleExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormatDiscordDeprecated = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormatExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DescriptionDiscordDeprecated = new LocalizableResourceString(nameof(Resources.AnalyzerDescriptionExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString TitleDiscordUnreleased = new LocalizableResourceString(nameof(Resources.AnalyzerTitleExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormatDiscordUnreleased = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormatExperimental), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DescriptionDiscordUnreleased = new LocalizableResourceString(nameof(Resources.AnalyzerDescriptionExperimental), Resources.ResourceManager, typeof(Resources));

private static readonly DiagnosticDescriptor ExperimentalRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0001", Title, MessageFormat, Category, DiagnosticSeverity.Warning, true, Description, "https://docs.discatsharp.tech/vs/analyzer/dcs/0001.html");
private static readonly DiagnosticDescriptor ExperimentalRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0001", TitleExperimental, MessageFormatExperimental, Category, DiagnosticSeverity.Warning, true, DescriptionExperimental, "https://docs.discatsharp.tech/vs/analyzer/dcs/0001.html");
private static readonly DiagnosticDescriptor DiscordInExperimentRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0101", TitleDiscordInExperiment, MessageFormatDiscordInExperiment, Category, DiagnosticSeverity.Warning, true, DescriptionDiscordInExperiment, "https://docs.discatsharp.tech/vs/analyzer/dcs/0101.html");
private static readonly DiagnosticDescriptor DiscordDeprecatedRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0102", TitleDiscordDeprecated, MessageFormatDiscordDeprecated, Category, DiagnosticSeverity.Warning, true, DescriptionDiscordDeprecated, "https://docs.discatsharp.tech/vs/analyzer/dcs/0102.html");
private static readonly DiagnosticDescriptor DiscordUnreleasedRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0103", TitleDiscordUnreleased, MessageFormatDiscordUnreleased, Category, DiagnosticSeverity.Warning, true, DescriptionDiscordUnreleased, "https://docs.discatsharp.tech/vs/analyzer/dcs/0103.html");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(ExperimentalRule); } }
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
return ImmutableArray.Create<DiagnosticDescriptor>(ExperimentalRule, DiscordInExperimentRule, DiscordDeprecatedRule, DiscordUnreleasedRule);
}
}

public override void Initialize(AnalysisContext context)
{
Expand All @@ -58,10 +77,10 @@ public override void Initialize(AnalysisContext context)
context.RegisterSymbolAction(ExperimentalAnalyzer, SymbolKind.Method);
context.RegisterSymbolAction(ExperimentalAnalyzer, SymbolKind.Field);
context.RegisterSymbolAction(ExperimentalAnalyzer, SymbolKind.Event);
context.RegisterSyntaxNodeAction(ExperimentalAnalyzer, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeAction(ExperimentalAnalyzer, SyntaxKind.ObjectCreationExpression);
context.RegisterSyntaxNodeAction(ExperimentalAnalyzer, SyntaxKind.FieldDeclaration); // Don't work
context.RegisterSyntaxNodeAction(ExperimentalAnalyzer, SyntaxKind.PropertyDeclaration); // Don't work, one of the not working ones should create a report if a property is used. I.e.:
context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.ObjectCreationExpression);
context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.FieldDeclaration); // Don't work
context.RegisterSyntaxNodeAction(StatusAnalyzer, SyntaxKind.PropertyDeclaration); // Don't work, one of the not working ones should create a report if a property is used. I.e.:
/*
var test = new Test("test");
Expand All @@ -70,34 +89,50 @@ public override void Initialize(AnalysisContext context)
*/
}
private static void ExperimentalAnalyzer(SyntaxNodeAnalysisContext context)
private static void StatusAnalyzer(SyntaxNodeAnalysisContext context)
{
var invocation = context.Node;
var declaration = context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol;
if (null == declaration)
{
Console.WriteLine("Faulty");
//context.ReportDiagnostic(Diagnostic.Create(ExperimentalRule, invocation.GetLocation(), "unknown", "unknown", "unknown"));
return;
}
var attributes = declaration.GetAttributes();
var attributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(ExperimentalAttribute)));
if (null == attributeData)
{
Console.WriteLine("Faulty 2");
return;
}

var name = declaration.Name;
var kind = declaration.Kind.ToString();
if (name == ".ctor")
{
name = declaration.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
kind = "Constructor";
}
var message = GetMessage(attributeData);
var diagnostic = Diagnostic.Create(ExperimentalRule, invocation.GetLocation(), kind, name, message);
context.ReportDiagnostic(diagnostic);

var experimentalAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(ExperimentalAttribute)));
var discordInExperimentAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(DiscordInExperimentAttribute)));
var discordDeprecatedAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(DiscordDeprecatedAttribute)));
var discordUnreleasedAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(DiscordUnreleasedAttribute)));

if (experimentalAttributeData != null)
{
var message = GetMessage(experimentalAttributeData);
context.ReportDiagnostic(Diagnostic.Create(ExperimentalRule, invocation.GetLocation(), kind, name, message));
}
if (discordInExperimentAttributeData != null)
{
var message = GetMessage(discordInExperimentAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordInExperimentRule, invocation.GetLocation(), kind, name, message));
}
if (discordDeprecatedAttributeData != null)
{
var message = GetMessage(discordDeprecatedAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordDeprecatedRule, invocation.GetLocation(), kind, name, message));
}
if (discordUnreleasedAttributeData != null)
{
var message = GetMessage(discordUnreleasedAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordUnreleasedRule, invocation.GetLocation(), kind, name, message));
}
return;
}

private static void ExperimentalAnalyzer(SymbolAnalysisContext context)
Expand All @@ -113,14 +148,7 @@ where x.IsInSource
return;
}
var attributes = declaration.GetAttributes();
var attributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.Compilation.GetSemanticModel(syntaxTrees.First()), attr, typeof(ExperimentalAttribute)));
if (null == attributeData)
{
Console.WriteLine("Faulty 2");
return;
}

var message = GetMessage(attributeData);

var name = declaration.Name;
var kind = declaration.Kind.ToString();
if (name == ".ctor")
Expand All @@ -132,8 +160,32 @@ where x.IsInSource
{
kind = "Class";
}
var diagnostic = Diagnostic.Create(ExperimentalRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message);
context.ReportDiagnostic(diagnostic);
var experimentalAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.Compilation.GetSemanticModel(syntaxTrees.First()), attr, typeof(ExperimentalAttribute)));
var discordInExperimentAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.Compilation.GetSemanticModel(syntaxTrees.First()), attr, typeof(DiscordInExperimentAttribute)));
var discordDeprecatedAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.Compilation.GetSemanticModel(syntaxTrees.First()), attr, typeof(DiscordDeprecatedAttribute)));
var discordUnreleasedAttributeData = attributes.FirstOrDefault(attr => IsRequiredAttribute(context.Compilation.GetSemanticModel(syntaxTrees.First()), attr, typeof(DiscordUnreleasedAttribute)));

if (experimentalAttributeData != null)
{
var message = GetMessage(experimentalAttributeData);
context.ReportDiagnostic(Diagnostic.Create(ExperimentalRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message));
}
if (discordInExperimentAttributeData != null)
{
var message = GetMessage(discordInExperimentAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordInExperimentRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message));
}
if (discordDeprecatedAttributeData != null)
{
var message = GetMessage(discordDeprecatedAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordDeprecatedRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message));
}
if (discordUnreleasedAttributeData != null)
{
var message = GetMessage(discordUnreleasedAttributeData);
context.ReportDiagnostic(Diagnostic.Create(DiscordUnreleasedRule, context.Symbol.Locations.First(x => x.IsInSource), kind, name, message));
}
return;
}

static bool IsRequiredAttribute(SemanticModel semanticModel, AttributeData attribute, Type desiredAttributeType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DisCatSharp.Attributes" Version="10.3.1" />
<PackageReference Include="DisCatSharp.Attributes" Version="10.3.1-fixed-001" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Loading

0 comments on commit 9caf1a7

Please sign in to comment.