Skip to content

Commit

Permalink
Clean up edit properties
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Dec 20, 2024
1 parent 858be62 commit 2af5b47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
18 changes: 9 additions & 9 deletions src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol
return;
}

ImmutableDictionary<string, string?> properties = new Dictionary<string, string?>(StringComparer.Ordinal)
ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties
{
{ DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Insert.ToString() },
{ DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() },
TypeOfEdit = DiagnosticEditProperties.EditType.Insert,
EditPosition = parameterMatch.Ordinal,
}.ToImmutableDictionary();

// Using a method that doesn't accept a MockBehavior parameter, however there's an overload that does
Expand All @@ -123,10 +123,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol
return;
}

ImmutableDictionary<string, string?> properties = new Dictionary<string, string?>(StringComparer.Ordinal)
ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties
{
{ DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Insert.ToString() },
{ DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() },
TypeOfEdit = DiagnosticEditProperties.EditType.Insert,
EditPosition = parameterMatch.Ordinal,
}.ToImmutableDictionary();

context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties));
Expand All @@ -143,10 +143,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol
return;
}

ImmutableDictionary<string, string?> properties = new Dictionary<string, string?>(StringComparer.Ordinal)
ImmutableDictionary<string, string?> properties = new DiagnosticEditProperties
{
{ DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Replace.ToString() },
{ DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() },
TypeOfEdit = DiagnosticEditProperties.EditType.Replace,
EditPosition = parameterMatch.Ordinal,
}.ToImmutableDictionary();

context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties));
Expand Down
30 changes: 1 addition & 29 deletions src/CodeFixes/CodeFixContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,6 @@ public static bool TryGetEditProperties(this CodeFixContext context, [NotNullWhe
{
ImmutableDictionary<string, string?> properties = context.Diagnostics[0].Properties;

// Try parsing; if anything fails return false
editProperties = null;
if (!properties.TryGetValue(DiagnosticEditProperties.EditTypeKey, out string? editTypeString))
{
return false;
}

if (!properties.TryGetValue(DiagnosticEditProperties.EditPositionKey, out string? editPositionString))
{
return false;
}

if (!Enum.TryParse(editTypeString, out DiagnosticEditProperties.EditType editType))
{
return false;
}

if (!int.TryParse(editPositionString, out int editPosition))
{
return false;
}

editProperties = new DiagnosticEditProperties
{
TypeOfEdit = editType,
EditPosition = editPosition,
};

return true;
return DiagnosticEditProperties.TryGetFromImmutableDictionary(properties, out editProperties);
}
}
56 changes: 55 additions & 1 deletion src/Common/DiagnosticEditProperties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Moq.Analyzers.Common;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace Moq.Analyzers.Common;

internal record class DiagnosticEditProperties
{
Expand Down Expand Up @@ -30,4 +33,55 @@ internal enum EditType
/// Gets the zero-based position where the edit should be applied.
/// </summary>
public int EditPosition { get; init; }

/// <summary>
/// Returns the current object as an <see cref="ImmutableDictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>The current objbect as an immutable dictionary.</returns>
public ImmutableDictionary<string, string?> ToImmutableDictionary()
{
return new Dictionary<string, string?>(StringComparer.Ordinal)
{
{ EditTypeKey, TypeOfEdit.ToString() },
{ EditPositionKey, EditPosition.ToString(CultureInfo.InvariantCulture) },
}.ToImmutableDictionary();
}

/// <summary>
/// Tries to convert an immuatble dictionary to a <see cref="DiagnosticEditProperties"/>.
/// </summary>
/// <param name="dictionary">The dictionary to try to convert.</param>
/// <param name="editProperties">The output edit properties if parsing suceeded, otherwise <c>null</c>.</param>
/// <returns><c>true</c> if parsing succeeded; <c>false</c> otherwise.</returns>
public static bool TryGetFromImmutableDictionary(ImmutableDictionary<string, string?> dictionary, [NotNullWhen(true)] out DiagnosticEditProperties? editProperties)
{
editProperties = null;
if (!dictionary.TryGetValue(EditTypeKey, out string? editTypeString))
{
return false;
}

if (!dictionary.TryGetValue(EditPositionKey, out string? editPositionString))
{
return false;
}

if (!Enum.TryParse(editTypeString, out EditType editType))
{
return false;
}

if (!int.TryParse(editPositionString, NumberStyles.Integer, CultureInfo.InvariantCulture, out int editPosition))
{
return false;
}

editProperties = new DiagnosticEditProperties
{
TypeOfEdit = editType,
EditPosition = editPosition,
};

return true;
}
}

0 comments on commit 2af5b47

Please sign in to comment.