-
Notifications
You must be signed in to change notification settings - Fork 4
Remove Regex from analyzers #81
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
MattKotsenas
merged 7 commits into
rjmurillo:main
from
MattKotsenas:refactor/remove-regex
Jun 12, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
63b6960
Refactor MoqMethodDescriptor in prep for moving away from Regex
MattKotsenas a3e40dd
Fix Regex bug in As method descriptor
MattKotsenas 5c5682b
Refactor Setup method descriptor
MattKotsenas 1e75c47
Simplify AsShouldBeUsedOnlyForInterfaceAnalyzer
MattKotsenas 23481a4
Plumb CancellationToken through `IsMoqSetupMethod`
MattKotsenas a4bb21e
Add xmldocs
MattKotsenas e9df911
Remove unused `IsFastMatch` method
MattKotsenas 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace Moq.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// A class that, given a <see cref="SemanticModel"/> and a <see cref="MemberAccessExpressionSyntax"/>, determines if | ||
| /// it is a call to the Moq `Mock.As()` method. | ||
| /// </summary> | ||
| internal class MoqAsMethodDescriptor : MoqMethodDescriptorBase | ||
| { | ||
| private const string MethodName = "As"; | ||
|
|
||
| public override bool IsMatch(SemanticModel semanticModel, MemberAccessExpressionSyntax memberAccessSyntax, CancellationToken cancellationToken) | ||
| { | ||
| if (!IsFastMatch(memberAccessSyntax, MethodName.AsSpan())) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| ISymbol? symbol = semanticModel.GetSymbolInfo(memberAccessSyntax, cancellationToken).Symbol; | ||
|
|
||
| if (symbol is not IMethodSymbol methodSymbol) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!IsContainedInMockType(methodSymbol)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return methodSymbol.Name.AsSpan().SequenceEqual(MethodName.AsSpan()) && methodSymbol.IsGenericMethod; | ||
|
MattKotsenas marked this conversation as resolved.
|
||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| namespace Moq.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// A base that that provides common functionality for identifying if a given <see cref="SyntaxNode"/> | ||
| /// is a specific Moq method. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Currently the <see cref="IsMatch(SemanticModel, MemberAccessExpressionSyntax, CancellationToken)"/> abstract method | ||
| /// is specific to <see cref="MemberAccessExpressionSyntax"/> because that's the only type of syntax in use. I expect we'll need | ||
| /// to loosen this restriction if we start using other types of syntax. | ||
| /// </remarks> | ||
| internal abstract class MoqMethodDescriptorBase | ||
| { | ||
| private const string ContainingNamespace = "Moq"; | ||
| private const string ContainingType = "Mock"; | ||
|
|
||
| public abstract bool IsMatch(SemanticModel semanticModel, MemberAccessExpressionSyntax memberAccessSyntax, CancellationToken cancellationToken); | ||
|
|
||
| protected static bool IsFastMatch(MemberAccessExpressionSyntax memberAccessSyntax, ReadOnlySpan<char> methodName) | ||
| { | ||
| return memberAccessSyntax.Name.Identifier.Text.AsSpan().SequenceEqual(methodName); | ||
| } | ||
|
|
||
| protected static bool IsContainedInMockType(IMethodSymbol methodSymbol) | ||
| { | ||
| return IsInMoqNamespace(methodSymbol) && IsInMockType(methodSymbol); | ||
| } | ||
|
|
||
| private static bool IsInMoqNamespace(ISymbol symbol) | ||
| { | ||
| return symbol.ContainingNamespace.Name.AsSpan().SequenceEqual(ContainingNamespace.AsSpan()); | ||
| } | ||
|
|
||
| private static bool IsInMockType(ISymbol symbol) | ||
| { | ||
| return symbol.ContainingType.Name.AsSpan().SequenceEqual(ContainingType.AsSpan()); | ||
| } | ||
| } |
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 @@ | ||
| namespace Moq.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// A class that, given a <see cref="SemanticModel"/> and a <see cref="MemberAccessExpressionSyntax"/>, determines if | ||
| /// it is a call to the Moq `Mock.Setup()` method. | ||
| /// </summary> | ||
| internal class MoqSetupMethodDescriptor : MoqMethodDescriptorBase | ||
| { | ||
| private const string MethodName = "Setup"; | ||
|
|
||
| public override bool IsMatch(SemanticModel semanticModel, MemberAccessExpressionSyntax memberAccessSyntax, CancellationToken cancellationToken) | ||
| { | ||
| if (!IsFastMatch(memberAccessSyntax, MethodName.AsSpan())) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| ISymbol? symbol = semanticModel.GetSymbolInfo(memberAccessSyntax, cancellationToken).Symbol; | ||
|
|
||
| if (symbol is not IMethodSymbol methodSymbol) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!IsContainedInMockType(methodSymbol)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return methodSymbol.Name.AsSpan().SequenceEqual(MethodName.AsSpan()) && methodSymbol.IsGenericMethod; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Moq.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Extensions methods for <see cref="SyntaxNode"/>s. | ||
| /// </summary> | ||
| internal static class SyntaxExtensions | ||
| { | ||
| /// <summary> | ||
| /// Tries to get the generic arguments of a given <see cref="NameSyntax"/>. | ||
| /// </summary> | ||
| /// <param name="syntax">The syntax to inspect.</param> | ||
| /// <param name="typeArguments">The collection of <see cref="TypeSyntax"/> elements on the <paramref name="syntax"/>.</param> | ||
| /// <returns><see langword="true"/> if <paramref name="syntax"/> has generic / type parameters; <see langword="false"/> otherwise.</returns> | ||
| /// <example> | ||
| /// x.As<ISampleInterface>() returns <see langword="true"/> and <paramref name="typeArguments"/> will contain <c>ISampleInterface</c>. | ||
| /// </example> | ||
| /// <example> | ||
| /// x.As() returns <see langword="false"/> and <paramref name="typeArguments"/> will be empty. | ||
| /// </example> | ||
| public static bool TryGetGenericArguments(this NameSyntax syntax, [NotNullWhen(true)] out SeparatedSyntaxList<TypeSyntax> typeArguments) | ||
|
MattKotsenas marked this conversation as resolved.
|
||
| { | ||
| if (syntax is GenericNameSyntax genericName) | ||
| { | ||
| typeArguments = genericName.TypeArgumentList.Arguments; | ||
| return true; | ||
| } | ||
|
|
||
| typeArguments = default; | ||
| return false; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Project> | ||
| <ItemGroup> | ||
| <PackageVersion Include="PolySharp" Version="1.14.1" /> | ||
| </ItemGroup> | ||
| </Project> |
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.