Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions Harmonize.Analyzers/Analyzers/UnspecifiedPatchTypeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -40,14 +42,39 @@ private void AnalyzeMethod(SyntaxNodeAnalysisContext context)
}

PatchType type = method.GetPatchType();
if (type == PatchType.Unknown)
if (type != PatchType.Unknown)
{
context.ReportDiagnostic(
Diagnostic.Create(
Diagnostics.UnspecifiedPatchType,
Location.Create(context.FilterTree, syntax.Identifier.Span)
)
return;
}

// see if this method is used as a helper from with the patch class.
// usage from outside the patch class cannot be detected because SymbolFinder
// requires the workspace API which is not available to CLI builds.
IEnumerable<InvocationExpressionSyntax> invocations =
syntax
.FirstAncestorOrSelf<ClassDeclarationSyntax>()
?.DescendantNodes()
.OfType<InvocationExpressionSyntax>() ?? [];
foreach (InvocationExpressionSyntax invocation in invocations)
{
SymbolInfo calledSymbol = context.SemanticModel.GetSymbolInfo(
invocation,
context.CancellationToken
);
if (
calledSymbol.Symbol != null
&& calledSymbol.Symbol.Equals(method, SymbolEqualityComparer.Default)
)
{
return;
}
}

context.ReportDiagnostic(
Diagnostic.Create(
Diagnostics.UnspecifiedPatchType,
Location.Create(context.FilterTree, syntax.Identifier.Span)
)
);
}
}
2 changes: 1 addition & 1 deletion Harmonize.Package/Harmonize.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>Harmonize</PackageId>
<PackageVersion>1.0.4</PackageVersion>
<PackageVersion>1.0.5</PackageVersion>
<Authors>BadMagic100</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
21 changes: 21 additions & 0 deletions Harmonize.Test/Fixes/TestUnspecifiedPatchTypeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ namespace Harmonize.Test.Fixes;

public class TestUnspecifiedPatchTypeFixer
{
[Fact(DisplayName = "should not emit diagnostic for helper methods")]
public async Task TestHelperMethodNoDiagnostic()
{
string source = /*lang=c#-test*/
"""
using HarmonyLib;

[HarmonyPatch(typeof(string), nameof(string.Copy))]
class Patches
{
private static void Prefix()
{
Helper();
}

private static void Helper() { }
}
""";
await VerifyCS.VerifyAnalyzerOnlyAsync(source, [], TestContext.Current.CancellationToken);
}

[Fact(DisplayName = "should provide diagnostic without fix for multiple patch types")]
public async Task TestMultiplePatchTypes()
{
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ Harmonize provides the following diagnostics, most of which also come with quick
silently ignore the patch.
- `HARMONIZE004` - Warns when the type (Prefix, Postfix, Transpiler) of a patch method cannot be determined. If
the method is not named or annotated appropriately, this will cause PatchAll to silently ignore the patch. If
there are multiple patch types among the name/annotations this is undefined behavior.
there are multiple patch types among the name/annotations this is undefined behavior. Static helper methods
called from within the patch class are exempted from this rule.

### Completions

> [!NOTE]
> Completions do not work in Rider and have not been verified in VSCode
> [!WARNING]
> Completions are not supported in Rider due to limitations of the JetBrains platform.

Harmonize provides the following completion providers:

Expand Down
Loading