-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
440 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,54 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using SboxAnalyzers.Extensions; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
namespace SboxAnalyzers; | ||
|
||
/// <summary> | ||
/// A Roslyn analyzer for checking event method arguments. | ||
/// </summary> | ||
[DiagnosticAnalyzer( LanguageNames.CSharp )] | ||
public class ServerCmdAnalyzer : DiagnosticAnalyzer | ||
{ | ||
/// <inheritdoc/> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => Diagnostics.ServerCmd.AllRules; | ||
|
||
/// <inheritdoc/> | ||
public override void Initialize( AnalysisContext context ) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis( GeneratedCodeAnalysisFlags.None ); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterSyntaxNodeAction( AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration ); | ||
} | ||
|
||
/// <summary> | ||
/// Analyzes a <see cref="MethodDeclarationSyntax"/> to check if it is a correctly implemented server command. | ||
/// </summary> | ||
/// <param name="context">The context relating to the syntax node being analyzed.</param> | ||
private static void AnalyzeMethodDeclaration( SyntaxNodeAnalysisContext context ) | ||
{ | ||
var methodDeclaration = (MethodDeclarationSyntax)context.Node; | ||
if ( methodDeclaration.ParameterList is null ) | ||
return; | ||
|
||
// Check that all parameters are networkable in server commands. | ||
foreach ( var parameter in methodDeclaration.ParameterList.Parameters ) | ||
{ | ||
if ( parameter.Type is null ) | ||
continue; | ||
|
||
if ( parameter.Type.IsServerCommandSupported( context.SemanticModel ) ) | ||
continue; | ||
|
||
var diagnostic = Diagnostic.Create( Diagnostics.ServerCmd.NetworkableRule, | ||
parameter.Type.GetLocation(), | ||
parameter.Type.ToNameString( true, context.SemanticModel ) ); | ||
context.ReportDiagnostic( diagnostic ); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.