Add `BizHawk.Analyzer` project

This commit is contained in:
YoshiRulz 2022-07-14 00:56:54 +10:00
parent 6f372820a7
commit 395aa0755b
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 106 additions and 0 deletions

View File

@ -1,5 +1,18 @@
<?xml version="1.0"?>
<RuleSet Name="BizHawk Rules" Description="Applies to all projects in the solution -- or, it will eventually." ToolsVersion="14.0">
<Rules AnalyzerId="BizHawk.Analyzer" RuleNamespace="BizHawk.Analyzer">
<!-- Do not use anonymous delegates -->
<Rule Id="BHI1001" Action="Hidden" />
<!-- Do not use anonymous types (classes) -->
<Rule Id="BHI1002" Action="Hidden" />
<!-- Do not use query expression syntax -->
<Rule Id="BHI1003" Action="Error" />
<!-- Verbatim interpolated strings should begin $@, not @$ -->
<Rule Id="BHI1004" Action="Error" />
</Rules>
<Rules AnalyzerId="DocumentationAnalyzers" RuleNamespace="DocumentationAnalyzers.StyleRules">
<!-- Place text in paragraphs -->
<Rule Id="DOC100" Action="Hidden" />

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<Import Project="../../Common.props" />
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(OutputPath)BizHawk.Analyzer.dll" DestinationFolder="$(ProjectDir)../../References/" />
</Target>
</Project>

View File

@ -0,0 +1,79 @@
namespace BizHawk.Analyzers;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class HawkSourceAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor DiagInterpStringIsDollarAt = new(
id: "BHI1004",
title: "Verbatim interpolated strings should begin $@, not @$",
messageFormat: "Swap @ and $ on interpolated string",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor DiagNoAnonClasses = new(
id: "BHI1002",
title: "Do not use anonymous types (classes)",
messageFormat: "Replace anonymous class with tuple or explicit type",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor DiagNoAnonDelegates = new(
id: "BHI1001",
title: "Do not use anonymous delegates",
messageFormat: "Replace anonymous delegate with lambda or local method",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor DiagNoQueryExpression = new(
id: "BHI1003",
title: "Do not use query expression syntax",
messageFormat: "Use method chain for LINQ instead of query expression syntax",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
DiagInterpStringIsDollarAt,
DiagNoAnonClasses,
DiagNoAnonDelegates,
DiagNoQueryExpression);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(
static snac =>
{
switch (snac.Node)
{
case AnonymousMethodExpressionSyntax:
snac.ReportDiagnostic(Diagnostic.Create(DiagNoAnonDelegates, snac.Node.GetLocation()));
break;
case AnonymousObjectCreationExpressionSyntax:
snac.ReportDiagnostic(Diagnostic.Create(DiagNoAnonClasses, snac.Node.GetLocation()));
break;
case InterpolatedStringExpressionSyntax ises:
if (ises.StringStartToken.Text[0] is '@') snac.ReportDiagnostic(Diagnostic.Create(DiagInterpStringIsDollarAt, ises.GetLocation()));
break;
case QueryExpressionSyntax:
snac.ReportDiagnostic(Diagnostic.Create(DiagNoQueryExpression, snac.Node.GetLocation()));
break;
}
},
SyntaxKind.AnonymousObjectCreationExpression,
SyntaxKind.AnonymousMethodExpression,
SyntaxKind.InterpolatedStringExpression,
SyntaxKind.QueryExpression);
}
}

Binary file not shown.

View File

@ -12,6 +12,7 @@
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="$(ProjectDir)../../.stylecop.json" />
<Analyzer Include="$(ProjectDir)../../References/BizHawk.Analyzer.dll" />
<Analyzer Include="$(ProjectDir)../../References/BizHawk.SrcGen.ReflectionCache.dll" />
</ItemGroup>
</Project>