Skip to content

Commit

Permalink
Analyzer - make ParamAnalyzer class static
Browse files Browse the repository at this point in the history
  • Loading branch information
psxvoid committed Jun 28, 2021
1 parent 1e62744 commit 97e9017
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ public override void Initialize(AnalysisContext context)
return;
}
var paramAnalyzer = new ParameterAnalyzer(jsonConstructorAttributeNamedSymbol);
context.RegisterSymbolStartAction((context) =>
{
var constructors = ((INamedTypeSymbol)context.Symbol).InstanceConstructors;
foreach (var ctor in constructors)
{
if (paramAnalyzer.ShouldAnalyzeMethod(ctor))
if (ParameterAnalyzer.ShouldAnalyzeMethod(ctor, jsonConstructorAttributeNamedSymbol))
{
context.RegisterOperationAction(
context => ParameterAnalyzer.AnalyzeOperationAndReport(context),
Expand All @@ -109,15 +107,8 @@ internal enum ParameterDiagnosticReason
UnreferencedParameter,
}

private sealed class ParameterAnalyzer
private static class ParameterAnalyzer
{
private readonly INamedTypeSymbol _jsonConstructorAttributeInfoType;

public ParameterAnalyzer(INamedTypeSymbol jsonConstructorAttributeInfoType)
{
_jsonConstructorAttributeInfoType = jsonConstructorAttributeInfoType;
}

public static void AnalyzeOperationAndReport(OperationAnalysisContext context)
{
var operation = (IParameterReferenceOperation)context.Operation;
Expand Down Expand Up @@ -159,7 +150,7 @@ public static void AnalyzeOperationAndReport(OperationAnalysisContext context)
}
}

public bool ShouldAnalyzeMethod(IMethodSymbol method)
public static bool ShouldAnalyzeMethod(IMethodSymbol method, INamedTypeSymbol jsonConstructorAttribute)
{
// We only care about constructors with parameters.
if (method.Parameters.IsEmpty)
Expand All @@ -168,7 +159,7 @@ public bool ShouldAnalyzeMethod(IMethodSymbol method)
}

// We only care about constructors that are marked with JsonConstructor attribute.
return this.IsJsonConstructor(method);
return ParameterAnalyzer.IsJsonConstructor(method, jsonConstructorAttribute);
}

private static bool IsParamMatchesReferencedMemberName(IParameterSymbol param, ISymbol referencedMember)
Expand All @@ -184,9 +175,11 @@ private static bool IsParamMatchesReferencedMemberName(IParameterSymbol param, I
return paramWords.SequenceEqual(memberWords, StringComparer.OrdinalIgnoreCase);
}

private bool IsJsonConstructor([NotNullWhen(returnValue: true)] IMethodSymbol? method)
private static bool IsJsonConstructor(
[NotNullWhen(returnValue: true)] IMethodSymbol? method,
INamedTypeSymbol jsonConstructorAttribute)
=> method.IsConstructor() &&
method.HasAttribute(this._jsonConstructorAttributeInfoType);
method.HasAttribute(jsonConstructorAttribute);

private static IMemberReferenceOperation? TryGetMemberReferenceOperation(IParameterReferenceOperation paramOperation)
{
Expand Down

0 comments on commit 97e9017

Please sign in to comment.