Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKotsenas committed Dec 20, 2024
1 parent 64001ab commit a407efd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
6 changes: 1 addition & 5 deletions src/CodeFixes/CodeFixContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using System.Diagnostics.CodeAnalysis;

Check failure on line 1 in src/CodeFixes/CodeFixContextExtensions.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/CodeFixes/CodeFixContextExtensions.cs#L1

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Simplification;

namespace Moq.CodeFixes;

Expand Down
13 changes: 8 additions & 5 deletions src/CodeFixes/SetExplicitMockBehaviorFixer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Composition;

Check failure on line 1 in src/CodeFixes/SetExplicitMockBehaviorFixer.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/CodeFixes/SetExplicitMockBehaviorFixer.cs#L1

Provide an 'AssemblyVersion' attribute for assembly 'srcassembly.dll'.
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
Expand All @@ -8,7 +7,7 @@
namespace Moq.CodeFixes;

/// <summary>
/// Fixes for SetExplicitMockBehaviorAnalyzer (Moq1400).
/// Fixes for <see cref="DiagnosticIds.SetExplicitMockBehavior"/>.
/// </summary>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SetExplicitMockBehaviorFixer))]
[Shared]
Expand Down Expand Up @@ -71,12 +70,16 @@ public SetExplicitMockBehaviorCodeAction(string title, Document document, Syntax
protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)

Check failure on line 70 in src/CodeFixes/SetExplicitMockBehaviorFixer.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/CodeFixes/SetExplicitMockBehaviorFixer.cs#L70

The Cyclomatic Complexity of this method is 13 which is greater than 10 authorized.
{
DocumentEditor editor = await DocumentEditor.CreateAsync(_document, cancellationToken).ConfigureAwait(false);
SemanticModel? model = await _document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
IOperation? operation = model?.GetOperation(_nodeToFix, cancellationToken);

MoqKnownSymbols knownSymbols = new(editor.SemanticModel.Compilation);

if (knownSymbols.MockBehavior is null

Check failure on line 78 in src/CodeFixes/SetExplicitMockBehaviorFixer.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/CodeFixes/SetExplicitMockBehaviorFixer.cs#L78

Reduce the number of conditional operators (4) used in the expression (maximum allowed 3).
|| knownSymbols.MockBehaviorDefault is null
|| knownSymbols.MockBehaviorLoose is null
|| knownSymbols.MockBehaviorStrict is null)
|| knownSymbols.MockBehaviorStrict is null
|| operation is null)
{
return _document;
}
Expand All @@ -92,8 +95,8 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke

SyntaxNode newNode = _editType switch
{
DiagnosticEditProperties.EditType.Insert => editor.Generator.InsertArguments(_nodeToFix, _position, argument),
DiagnosticEditProperties.EditType.Replace => editor.Generator.ReplaceArgument(_nodeToFix, _position, argument),
DiagnosticEditProperties.EditType.Insert => editor.Generator.InsertArguments(operation, _position, argument),
DiagnosticEditProperties.EditType.Replace => editor.Generator.ReplaceArgument(operation, _position, argument),
_ => throw new InvalidOperationException(),
};

Expand Down
38 changes: 16 additions & 22 deletions src/CodeFixes/SyntaxGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public static SyntaxNode MemberAccessExpression(this SyntaxGenerator generator,
return generator.MemberAccessExpression(generator.TypeExpression(fieldSymbol.Type), generator.IdentifierName(fieldSymbol.Name));
}

public static SyntaxNode InsertArguments(this SyntaxGenerator generator, IOperation operation, int index, params SyntaxNode[] items)
{
// Ideally we could modify argument lists only using the IOperation APIs, but I haven't figured out a way to do that yet.
return generator.InsertArguments(operation.Syntax, index, items);
}

public static SyntaxNode InsertArguments(this SyntaxGenerator generator, SyntaxNode syntax, int index, params SyntaxNode[] items)
{
if (Array.Exists(items, item => item is not ArgumentSyntax))
Expand All @@ -19,29 +25,26 @@ public static SyntaxNode InsertArguments(this SyntaxGenerator generator, SyntaxN
if (syntax is InvocationExpressionSyntax invocation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = invocation.ArgumentList.Arguments;

arguments = arguments.InsertRange(index, items.OfType<ArgumentSyntax>());

syntax = syntax.ReplaceNode(invocation.ArgumentList, invocation.ArgumentList.WithArguments(arguments));

return syntax;
return invocation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

if (syntax is ObjectCreationExpressionSyntax creation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = creation.ArgumentList?.Arguments ?? [];

arguments = arguments.InsertRange(index, items.OfType<ArgumentSyntax>());
ArgumentListSyntax argumentList = SyntaxFactory.ArgumentList(arguments);

syntax = syntax.ReplaceNode(creation, creation.WithArgumentList(argumentList));

return syntax;
return creation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(ObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
}

public static SyntaxNode ReplaceArgument(this SyntaxGenerator generator, IOperation operation, int index, SyntaxNode item)
{
// Ideally we could modify argument lists only using the IOperation APIs, but I haven't figured out a way to do that yet.
return generator.ReplaceArgument(operation.Syntax, index, item);
}

public static SyntaxNode ReplaceArgument(this SyntaxGenerator generator, SyntaxNode syntax, int index, SyntaxNode item)
{
if (item is not ArgumentSyntax argument)
Expand All @@ -52,24 +55,15 @@ public static SyntaxNode ReplaceArgument(this SyntaxGenerator generator, SyntaxN
if (syntax is InvocationExpressionSyntax invocation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = invocation.ArgumentList.Arguments;

arguments = arguments.RemoveAt(index).Insert(index, argument);

syntax = syntax.ReplaceNode(invocation.ArgumentList, invocation.ArgumentList.WithArguments(arguments));

return syntax;
return invocation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

if (syntax is ObjectCreationExpressionSyntax creation)
{
SeparatedSyntaxList<ArgumentSyntax> arguments = creation.ArgumentList?.Arguments ?? [];

arguments = arguments.RemoveAt(index).Insert(index, argument);
ArgumentListSyntax argumentList = SyntaxFactory.ArgumentList(arguments);

syntax = syntax.ReplaceNode(creation, creation.WithArgumentList(argumentList));

return syntax;
return creation.WithArgumentList(SyntaxFactory.ArgumentList(arguments));
}

throw new ArgumentException($"Must be of type {nameof(InvocationExpressionSyntax)} or {nameof(ObjectCreationExpressionSyntax)} but is of type {syntax.GetType().Name}", nameof(syntax));
Expand Down

0 comments on commit a407efd

Please sign in to comment.