-
-
Notifications
You must be signed in to change notification settings - Fork 319
Create an analyzer for DSharpPlus #2154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
5b629c1
Initial analyzer commit
Instellate 5d6b949
Added DSP0001
Instellate b5bfe5b
Added MultipleOverwriteAnalyzer.cs as a analyzer
Instellate e816d54
fix versions
akiraveliara e6f453f
Reversed some booleans
Instellate 8874213
Merge remote-tracking branch 'origin/instellate/analyzer' into instel…
Instellate 9fd7cff
Changed to unit testing for anaylzers.
Instellate dceb08c
Formatting
Instellate 8d23244
Made `SingleEntityGetRequestAnalyzer`
Instellate 674853d
Added an analyzer that prevents users from installing user install on…
Instellate b9ed95e
Added an analyzer that warns about registering nested classes
Instellate a10a107
Added a comment
Instellate 474772d
Started the work on DSP1003
Instellate 36dfb0d
Merge branch 'master' into instellate/analyzer
Instellate 04d7810
Finished implementing DSP1003
Instellate 86c2a71
Started on writing documentation for all the rules.
Instellate 6bf27cf
Added a package reference for ``System.Formats.Asn1`` for windows to …
Instellate 1403ed2
Made so overwrite also warns on loop
Instellate 79f1a70
Resolved aki's reviews
Instellate 0b1b5fc
Fixed `HasPermissionAnalyzer` support for `SyntaxKind.EqualsExpression`
Instellate a079fac
Fixed consistency across files
Instellate c79dd36
Added a unit test for equal expressions in the `HasPermissionAnalyzer…
Instellate e4fab2a
Split up documentation rules so commands rules and core rules are dif…
Instellate c6e9ddd
Fixed various documentation issues
Instellate 098f386
Added so `DSharpPlus.Analyzers` is not included in API references
Instellate 729a304
Fixed so the analyzer .dll is included in the core nupkg
Instellate 0d8c684
said i'd touch upon the docs
akiraveliara a183344
Merge branch 'master' into instellate/analyzer
Instellate 3b25808
Merge remote-tracking branch 'refs/remotes/origin/master' into instel…
Instellate e81f054
Added DSP0008 (Use DiscordPermissions instead of DiscordPermission)
Instellate 3e6d710
Merge remote-tracking branch 'origin/master' into instellate/analyzer
Instellate a73ae6a
Merge branch 'master' into instellate/analyzer
Instellate 35c4cf9
Addressed reviews for `DSP0009`
Instellate c035918
Removed implementation details for DSP0009 docs
Instellate ec3b005
Improved docs for DSP0009
Instellate 55e8e97
DocFX fail
Instellate 9ad986c
Merge branch 'master' into instellate/analyzer
Instellate d8b2323
Seperated DSP0009 to two separate warnings.
Instellate 6a27d58
Merge remote-tracking branch 'origin/master' into instellate/analyzer
Instellate 1f794b7
Added Analyzer projects into the solution file (again)
Instellate 643d3ba
Added logic to package the analyzer into DSharpPlus.nupkg
Instellate 48479a1
Added full support for DSP0010
Instellate 5d99787
document DSP0010
akiraveliara ecf8cb9
fix the test
akiraveliara 345c9fb
Added all the numerical operations to trigger for DSP0009
Instellate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
DSharpPlus.Analyzers/DSharpPlus.Analyzers.Test/DSharpPlus.Analyzers.Test.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <IsPackable>false</IsPackable> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
| <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> | ||
| <GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\DSharpPlus.Commands\DSharpPlus.Commands.csproj" /> | ||
| <ProjectReference Include="..\DSharpPlus.Analyzers\DSharpPlus.Analyzers.csproj" /> | ||
| <ProjectReference Include="..\..\DSharpPlus\DSharpPlus.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Basic.Reference.Assemblies.Net80" /> | ||
| <PackageReference Include="Microsoft.CodeAnalysis" /> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" /> | ||
| <PackageReference Include="NUnit" /> | ||
| <PackageReference Include="NUnit.Analyzers" /> | ||
| <PackageReference Include="NUnit3TestAdapter" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
| </ItemGroup> | ||
| </Project> |
85 changes: 85 additions & 0 deletions
85
DSharpPlus.Analyzers/DSharpPlus.Analyzers.Test/HasPermissionTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System.Threading.Tasks; | ||
| using DSharpPlus.Analyzers.Core; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp.Testing; | ||
| using Microsoft.CodeAnalysis.Testing; | ||
| using NUnit.Framework; | ||
| using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier< | ||
| DSharpPlus.Analyzers.Core.HasPermissionAnalyzer, | ||
| Microsoft.CodeAnalysis.Testing.DefaultVerifier | ||
| >; | ||
|
|
||
| namespace DSharpPlus.Analyzers.Test; | ||
|
|
||
| public class HasPermissionTest | ||
| { | ||
| /// <summary> | ||
| /// Unit test to see if HasPermissionAnalyzer reports | ||
| /// </summary> | ||
| [Test] | ||
| public static async Task HasPermissionNotEquals_DiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<HasPermissionAnalyzer, DefaultVerifier> test = | ||
| Utility.CreateAnalyzerTest<HasPermissionAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
|
|
||
| public class DoesIt | ||
| { | ||
| public static bool HaveAdmin(DiscordPermission perm) | ||
| { | ||
| if ((perm & DiscordPermission.Administrator) != 0) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(7, 13) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use 'perm.HasPermission(DiscordPermission.Administrator)' instead") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task HasPermissionEquals_DiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<HasPermissionAnalyzer, DefaultVerifier> test = | ||
| Utility.CreateAnalyzerTest<HasPermissionAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
|
|
||
| public class DoesIt | ||
| { | ||
| public static bool HaveNoAdmin(DiscordPermission perm) | ||
| { | ||
| if ((perm & DiscordPermission.Administrator) == 0) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(7, 13) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use 'perm.HasPermission(DiscordPermission.Administrator)' instead") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
| } |
195 changes: 195 additions & 0 deletions
195
DSharpPlus.Analyzers/DSharpPlus.Analyzers.Test/MultipleOverwriteTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| using System.Threading.Tasks; | ||
| using DSharpPlus.Analyzers.Core; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp.Testing; | ||
| using Microsoft.CodeAnalysis.Testing; | ||
| using NUnit.Framework; | ||
| using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier< | ||
| DSharpPlus.Analyzers.Core.MultipleOverwriteAnalyzer, | ||
| Microsoft.CodeAnalysis.Testing.DefaultVerifier | ||
| >; | ||
|
|
||
| namespace DSharpPlus.Analyzers.Test; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| public static class MultipleOverwriteTest | ||
| { | ||
| /// <summary> | ||
| /// Single diagnostic report for multiple overwrite analyzer | ||
| /// </summary> | ||
| [Test] | ||
| public static async Task DiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<MultipleOverwriteAnalyzer, DefaultVerifier> test = | ||
| Utility.CreateAnalyzerTest<MultipleOverwriteAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class OverwriteTest | ||
| { | ||
| public async Task AddOverwritesAsync(DiscordChannel channel, DiscordMember member, DiscordMember member2) | ||
| { | ||
| await channel.AddOverwriteAsync(member, DiscordPermission.BanMembers); | ||
| await channel.AddOverwriteAsync(member2, DiscordPermission.KickMembers); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(9, 15) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public static async Task MultipleErrorsScenarioTestAsync() | ||
| { | ||
| CSharpAnalyzerTest<MultipleOverwriteAnalyzer, DefaultVerifier> test = | ||
| Utility.CreateAnalyzerTest<MultipleOverwriteAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class OverwriteTest | ||
| { | ||
| public async Task AddOverwritesAsync(DiscordChannel channel, DiscordMember member, DiscordMember member2) | ||
| { | ||
| await channel.AddOverwriteAsync(member, DiscordPermission.BanMembers); | ||
| await channel.AddOverwriteAsync(member2, DiscordPermission.KickMembers); | ||
| await channel.AddOverwriteAsync(member2, DiscordPermission.Administrator); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(9, 15) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(10, 15) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public static async Task ForEachLoopDiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<MultipleOverwriteAnalyzer, DefaultVerifier> test | ||
| = Utility.CreateAnalyzerTest<MultipleOverwriteAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class OverwriteTest | ||
| { | ||
| public async Task AddOverwritesAsync(DiscordChannel channel, List<DiscordMember> members) | ||
| { | ||
| foreach (DiscordMember member in members) | ||
| { | ||
| await channel.AddOverwriteAsync(member, DiscordPermission.BanMembers); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(11, 19) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public static async Task ForLoopDiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<MultipleOverwriteAnalyzer, DefaultVerifier> test | ||
| = Utility.CreateAnalyzerTest<MultipleOverwriteAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class OverwriteTest | ||
| { | ||
| public async Task AddOverwritesAsync(DiscordChannel channel, List<DiscordMember> members) | ||
| { | ||
| for (int i = 0; i < members.Count; i++) | ||
| { | ||
| await channel.AddOverwriteAsync(members[i], DiscordPermission.BanMembers); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(11, 19) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Test] | ||
| public static async Task WhileLoopDiagnosticAsync() | ||
| { | ||
| CSharpAnalyzerTest<MultipleOverwriteAnalyzer, DefaultVerifier> test | ||
| = Utility.CreateAnalyzerTest<MultipleOverwriteAnalyzer>(); | ||
|
|
||
| test.TestCode = """ | ||
| using DSharpPlus.Entities; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class OverwriteTest | ||
| { | ||
| public async Task AddOverwritesAsync(DiscordChannel channel, List<DiscordMember> members) | ||
| { | ||
| int i = 0; | ||
| while (i < members.Count) | ||
| { | ||
| await channel.AddOverwriteAsync(members[i], DiscordPermission.BanMembers); | ||
| i++; | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(12, 19) | ||
| .WithSeverity(DiagnosticSeverity.Warning) | ||
| .WithMessage("Use one 'channel.ModifyAsync(..)' instead of multiple 'channel.AddOverwriteAsync(..)'") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
DSharpPlus.Analyzers/DSharpPlus.Analyzers.Test/ProcessorCheckTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System.Threading.Tasks; | ||
| using DSharpPlus.Analyzers.Commands; | ||
| using DSharpPlus.Commands; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp.Testing; | ||
| using Microsoft.CodeAnalysis.Testing; | ||
| using NUnit.Framework; | ||
| using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier< | ||
| DSharpPlus.Analyzers.Commands.ProcessorCheckAnalyzer, | ||
| Microsoft.CodeAnalysis.Testing.DefaultVerifier | ||
| >; | ||
|
|
||
| namespace DSharpPlus.Analyzers.Test; | ||
|
|
||
| public static class ProcessorCheckTest | ||
| { | ||
| [Test] | ||
| public static async Task DiagnosticTestAsync() | ||
| { | ||
| CSharpAnalyzerTest<ProcessorCheckAnalyzer, DefaultVerifier> test | ||
| = Utility.CreateAnalyzerTest<ProcessorCheckAnalyzer>(); | ||
| test.TestState.AdditionalReferences.Add(typeof(CommandContext).Assembly); | ||
|
|
||
| test.TestCode = """ | ||
| using System.Threading.Tasks; | ||
| using DSharpPlus.Commands.Trees.Metadata; | ||
| using DSharpPlus.Commands.Processors.TextCommands; | ||
| using DSharpPlus.Commands.Processors.SlashCommands; | ||
|
|
||
| public class Test | ||
| { | ||
| [AllowedProcessors<SlashCommandProcessor>()] | ||
| public async Task Tester(TextCommandContext context) | ||
| { | ||
| await context.RespondAsync("Tester!"); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| test.ExpectedDiagnostics.Add | ||
| ( | ||
| Verifier.Diagnostic() | ||
| .WithLocation(9, 30) | ||
| .WithSeverity(DiagnosticSeverity.Error) | ||
| .WithMessage("All provided processors does not support context 'TextCommandContext'") | ||
| ); | ||
|
|
||
| await test.RunAsync(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.