Skip to content

Commit

Permalink
Add server command analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-r-g committed May 15, 2023
1 parent 1dd9434 commit fb1ce9b
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SboxAnalyzers/SboxAnalyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
<Compile Update="AccessListResources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="AccessListResources.resx" />
<Compile Update="MethodArgumentsResources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="MethodArgumentsResources.resx" />
<Compile Update="NetPropertyResources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="NetPropertyResources.resx" />
<Compile Update="ServerCmd.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="ServerCmd.resx" />
<EmbeddedResource Update="AccessListResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="AccessListResources.Designer.cs" />
<EmbeddedResource Update="MethodArgumentsResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="MethodArgumentsResources.Designer.cs" />
<EmbeddedResource Update="NetPropertyResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="NetPropertyResources.Designer.cs" />
<EmbeddedResource Update="ServerCmd.resx" Generator="ResXFileCodeGenerator" LastGenOutput="ServerCmd.Designer.cs" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions SboxAnalyzers/ServerCmdAnalyzer.cs
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 );
}
}
}
252 changes: 252 additions & 0 deletions SboxAnalyzers/ServerCmdResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fb1ce9b

Please sign in to comment.