-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add initial analyzer that detects Moq #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Source/Mockolate.Migration.Analyzers.CodeFixers/AssertionCodeFixProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System.Collections.Immutable; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace Mockolate.Migration.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Base class for code fix provider that migrates assertions to aweXpect. | ||
| /// </summary> | ||
| public abstract class AssertionCodeFixProvider(DiagnosticDescriptor rule) : CodeFixProvider | ||
| { | ||
| /// <inheritdoc /> | ||
| public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = [rule.Id,]; | ||
|
|
||
| /// <inheritdoc /> | ||
| public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| /// <inheritdoc /> | ||
| public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| foreach (Diagnostic? diagnostic in context.Diagnostics) | ||
| { | ||
| TextSpan diagnosticSpan = diagnostic.Location.SourceSpan; | ||
|
|
||
| SyntaxNode? root = | ||
| await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
|
|
||
| if (root?.FindNode(diagnosticSpan) is ExpressionSyntax expressionSyntax | ||
| and (InvocationExpressionSyntax or ConditionalAccessExpressionSyntax or LambdaExpressionSyntax)) | ||
| { | ||
| context.RegisterCodeFix( | ||
| CodeAction.Create( | ||
| rule.Title.ToString(), | ||
| c => ConvertAssertionAsync(context, expressionSyntax, c), | ||
| rule.Title.ToString()), | ||
| diagnostic); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts the assertion. | ||
| /// </summary> | ||
| protected abstract Task<Document> ConvertAssertionAsync(CodeFixContext context, | ||
| ExpressionSyntax expressionSyntax, CancellationToken cancellationToken); | ||
| } |
32 changes: 32 additions & 0 deletions
32
.../Mockolate.Migration.Analyzers.CodeFixers/Mockolate.Migration.Analyzers.CodeFixers.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <Description>Code fix providers for Mockolate to migrate from other mocking libraries.</Description> | ||
| <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
| <IsRoslynComponent>true</IsRoslynComponent> | ||
| <DevelopmentDependency>false</DevelopmentDependency> | ||
| <NoPackageAnalysis>true</NoPackageAnalysis> | ||
| <NoWarn>RS2003</NoWarn> | ||
| <EnableTrimAnalyzer>false</EnableTrimAnalyzer> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <RootNamespace>Mockolate.Migration.Analyzers</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Analyzers"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Common"/> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp"/> | ||
| <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common"/> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Mockolate.Migration.Analyzers\Mockolate.Migration.Analyzers.csproj"/> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
35 changes: 35 additions & 0 deletions
35
Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System.Composition; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| #pragma warning disable S1192 // String literals should not be duplicated | ||
| namespace Mockolate.Migration.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// A code fix provider that migrates Moq to Mockolate. | ||
| /// </summary> | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MoqCodeFixProvider))] | ||
| [Shared] | ||
| public class MoqCodeFixProvider() : AssertionCodeFixProvider(Rules.MoqRule) | ||
| { | ||
| /// <inheritdoc /> | ||
| protected override async Task<Document> ConvertAssertionAsync(CodeFixContext context, | ||
| ExpressionSyntax expressionSyntax, CancellationToken cancellationToken) | ||
| { | ||
| Document? document = context.Document; | ||
|
vbreuss marked this conversation as resolved.
|
||
|
|
||
| SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| if (root is not CompilationUnitSyntax compilationUnit) | ||
|
Check warning on line 26 in Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs
|
||
| { | ||
| return document; | ||
| } | ||
|
|
||
| return document; | ||
| } | ||
| } | ||
|
|
||
| #pragma warning restore S1192 | ||
7 changes: 7 additions & 0 deletions
7
Source/Mockolate.Migration.Analyzers/AnalyzerReleases.Shipped.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Release 1.0 | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------------|----------|----------|------------------------------------------- | ||
| MockolateM001 | Usage | Warning | Moq should be migrated. |
4 changes: 4 additions & 0 deletions
4
Source/Mockolate.Migration.Analyzers/AnalyzerReleases.Unshipped.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| ---------|----------|----------|------- |
36 changes: 36 additions & 0 deletions
36
Source/Mockolate.Migration.Analyzers/Common/TypeExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Mockolate.Migration.Analyzers.Common; | ||
|
|
||
| internal static class TypeExtensions | ||
| { | ||
| private static readonly SymbolDisplayFormat FullyQualifiedNonGenericWithGlobalPrefix = new( | ||
| SymbolDisplayGlobalNamespaceStyle.Included, | ||
| SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, | ||
| SymbolDisplayGenericsOptions.None, | ||
| SymbolDisplayMemberOptions.IncludeContainingType, | ||
| SymbolDisplayDelegateStyle.NameAndSignature, | ||
| SymbolDisplayExtensionMethodStyle.Default, | ||
| SymbolDisplayParameterOptions.IncludeType, | ||
| SymbolDisplayPropertyStyle.NameOnly, | ||
| SymbolDisplayLocalOptions.IncludeType | ||
| ); | ||
|
|
||
| private static readonly SymbolDisplayFormat? FullyQualifiedGenericWithGlobalPrefix = new( | ||
| SymbolDisplayGlobalNamespaceStyle.Included, | ||
| SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, | ||
| SymbolDisplayGenericsOptions.None, | ||
| SymbolDisplayMemberOptions.IncludeContainingType, | ||
| SymbolDisplayDelegateStyle.NameAndSignature, | ||
| SymbolDisplayExtensionMethodStyle.Default, | ||
| SymbolDisplayParameterOptions.IncludeType, | ||
| SymbolDisplayPropertyStyle.NameOnly, | ||
| SymbolDisplayLocalOptions.IncludeType | ||
| ); | ||
|
|
||
| public static string GloballyQualified(this ISymbol typeSymbol) => | ||
| typeSymbol.ToDisplayString(FullyQualifiedGenericWithGlobalPrefix); | ||
|
|
||
| public static string GloballyQualifiedNonGeneric(this ISymbol typeSymbol) => | ||
| typeSymbol.ToDisplayString(FullyQualifiedNonGenericWithGlobalPrefix); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.