diff --git a/Analyzer/Analyzer/AttributeAnalyzer.cs b/Analyzer/Analyzer/AttributeAnalyzer.cs index fd3da9854..aa0f7efda 100644 --- a/Analyzer/Analyzer/AttributeAnalyzer.cs +++ b/Analyzer/Analyzer/AttributeAnalyzer.cs @@ -1,99 +1,99 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using DisCatSharp.Experimental; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using System; using System.Collections.Immutable; using System.Linq; namespace DisCatSharp.Analyzer { [DiagnosticAnalyzer(LanguageNames.CSharp)] public class AttributeAnalyzer : DiagnosticAnalyzer { public const string DiagnosticIdPrefix = "DCS"; 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 = "Informations"; + private const string Category = "Information"; private static readonly DiagnosticDescriptor ExperimentalRule = new DiagnosticDescriptor(DiagnosticIdPrefix + "0001", Title, MessageFormat, Category, DiagnosticSeverity.Warning, true, Description, "https://docs.discatsharp.tech/vs/analyzer/dcs/0001", "DisCatSharp", "Experimental"); public override ImmutableArray SupportedDiagnostics { get { return ImmutableArray.Create(ExperimentalRule); } } public override void Initialize(AnalysisContext context) { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.EnableConcurrentExecution(); context.RegisterSyntaxNodeAction(AnalyzerInvocation, SyntaxKind.InvocationExpression); } private static void AnalyzerInvocation(SyntaxNodeAnalysisContext context) { var invocation = (InvocationExpressionSyntax)context.Node; var methodDeclaration = (context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol as IMethodSymbol); //There are several reason why this may be null e.g invoking a delegate if (null == methodDeclaration) { return; } var methodAttributes = methodDeclaration.GetAttributes(); var attributeData = methodAttributes.FirstOrDefault(attr => IsRequiredAttribute(context.SemanticModel, attr, typeof(ExperimentalAttribute))); if (null == attributeData) { return; } var message = GetMessage(attributeData); var diagnostic = Diagnostic.Create(ExperimentalRule, invocation.GetLocation(), methodDeclaration.Name, message); context.ReportDiagnostic(diagnostic); } 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) { if (attribute.ConstructorArguments.Length < 1) { return "Do not use in production"; } return attribute.ConstructorArguments[0].Value as string; } } } diff --git a/DisCatSharp.Docs/vs/index.md b/DisCatSharp.Docs/vs/index.md index cbd4707bb..fc43c315f 100644 --- a/DisCatSharp.Docs/vs/index.md +++ b/DisCatSharp.Docs/vs/index.md @@ -1,15 +1,15 @@ --- uid: vs title: Visual Studio Tools --- ## Visual Studio Tools This section contains information on how to use the Visual Studio tools for DisCatSharp. ### Analyzers #### DisCatSharp Analyzer Rule ID | Category | Severity | Notes --------|----------|----------|------- -DCS0001 | Informations | Warning | Experimental Attribute Analyzer +DCS0001 | Information | Warning | Experimental Attribute Analyzer