Skip to content

Commit 856f2f5

Browse files
pavel-mikula-sonarsourcesonartech
authored andcommitted
NET-1042 Extract semantic Helper extensions from SonarAnalyzer.Core
1 parent 412b9f8 commit 856f2f5

20 files changed

Lines changed: 314 additions & 478 deletions

analyzers/src/SonarAnalyzer.CSharp.Core/Syntax/Extensions/BaseMethodDeclarationSyntaxExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static bool ContainsMethodInvocation(this BaseMethodDeclarationSyntax met
4848
return childNodes
4949
.OfType<InvocationExpressionSyntax>()
5050
.Where(syntaxPredicate)
51-
.Select(x => x.Expression.SyntaxTree.GetSemanticModelOrDefault(semanticModel)?.GetSymbolInfo(x.Expression).Symbol)
51+
.Select(x => x.Expression.SyntaxTree.SemanticModelOrDefault(semanticModel)?.GetSymbolInfo(x.Expression).Symbol)
5252
.OfType<IMethodSymbol>()
5353
.Any(symbolPredicate);
5454
}

analyzers/src/SonarAnalyzer.CSharp/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,8 @@ private sealed class CatchFinallyAssertion : SafeCSharpSyntaxWalker
4848
{
4949
public bool HasAssertion { get; set; }
5050

51-
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
52-
{
53-
if (HasAssertion)
54-
{
55-
return;
56-
}
57-
58-
HasAssertion = node.Expression
59-
.ToString()
60-
.SplitCamelCaseToWords()
61-
.Intersect(UnitTestHelper.KnownAssertionMethodParts)
62-
.Any();
63-
}
51+
public override void VisitInvocationExpression(InvocationExpressionSyntax node) =>
52+
HasAssertion = HasAssertion || node.Expression.ToString().SplitCamelCaseToWords().Intersect(KnownMethods.AssertionMethodParts).Any();
6453
}
6554
}
6655
}

analyzers/src/SonarAnalyzer.CSharp/Rules/ImplementIDisposableCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private void VerifyDispose(MethodDeclarationSyntax disposeMethod, bool isSealedC
198198

199199
// Because of partial classes we cannot always rely on the current semantic model.
200200
// See issue: https://github.com/SonarSource/sonar-dotnet/issues/690
201-
var disposeMethodSymbol = disposeMethod.SyntaxTree.GetSemanticModelOrDefault(semanticModel)?.GetDeclaredSymbol(disposeMethod);
201+
var disposeMethodSymbol = disposeMethod.SyntaxTree.SemanticModelOrDefault(semanticModel)?.GetDeclaredSymbol(disposeMethod);
202202
if (disposeMethodSymbol == null)
203203
{
204204
return;

analyzers/src/SonarAnalyzer.CSharp/Rules/TestMethodShouldContainAssertion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static bool IsTestMethod(IMethodSymbol symbol, bool isLocalFunction) =>
7979
isLocalFunction ? IsXunitTestMethod(symbol) : symbol.IsTestMethod();
8080

8181
private static bool IsXunitTestMethod(IMethodSymbol methodSymbol) =>
82-
methodSymbol.AnyAttributeDerivesFromAny(UnitTestHelper.KnownTestMethodAttributesOfxUnit);
82+
methodSymbol.AnyAttributeDerivesFromAny(KnownType.TestMethodAttributesOfxUnit);
8383

8484
private static bool ContainsAssertion(SyntaxNode methodDeclaration, SemanticModel model, ISet<IMethodSymbol> visitedSymbols, int level)
8585
{
@@ -139,7 +139,7 @@ private static bool IsAssertion(InvocationExpressionSyntax invocation) =>
139139
invocation.Expression
140140
.ToString()
141141
.SplitCamelCaseToWords()
142-
.Intersect(UnitTestHelper.KnownAssertionMethodParts)
142+
.Intersect(KnownMethods.AssertionMethodParts)
143143
.Any();
144144

145145
private static bool IsKnownAssertion(ISymbol methodSymbol) =>

analyzers/src/SonarAnalyzer.CSharp/Rules/TestMethodShouldNotBeIgnored.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ public sealed class TestMethodShouldNotBeIgnored : SonarDiagnosticAnalyzer
2323
private const string MessageFormat = "Either remove this 'Ignore' attribute or add an explanation about why this test is ignored.";
2424

2525
private static readonly DiagnosticDescriptor Rule = DescriptorFactory.Create(DiagnosticId, MessageFormat);
26-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
2726

2827
private static readonly ImmutableArray<KnownType> TrackedTestIdentifierAttributes =
29-
// xUnit has it's own "ignore" mechanism (by providing a (Skip = "reason") string in
30-
// the attribute, so there is always an explanation for the test being skipped).
31-
UnitTestHelper.KnownTestMethodAttributesOfMSTest
32-
.Concat(new[] { KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_TestClassAttribute })
33-
.Concat(UnitTestHelper.KnownTestMethodAttributesOfNUnit)
28+
// xUnit has it's own "ignore" mechanism (by providing a (Skip = "reason") string in the attribute, so there is always an explanation for the test being skipped).
29+
KnownType.TestMethodAttributesOfMSTest
30+
.Concat(KnownType.TestMethodAttributesOfNUnit)
31+
.Append(KnownType.Microsoft_VisualStudio_TestTools_UnitTesting_TestClassAttribute)
3432
.ToImmutableArray();
3533

34+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
35+
3636
protected override void Initialize(SonarAnalysisContext context) =>
3737
context.RegisterNodeAction(
3838
c =>
@@ -78,7 +78,7 @@ private static bool IsKnownIgnoreAttribute(AttributeSyntax attributeSyntax, Sema
7878

7979
var attributeConstructor = symbolInfo.Symbol ?? symbolInfo.CandidateSymbols.FirstOrDefault();
8080

81-
return attributeConstructor != null && attributeConstructor.ContainingType.DerivesFromAny(UnitTestHelper.KnownIgnoreAttributes);
81+
return attributeConstructor is not null && attributeConstructor.ContainingType.DerivesFromAny(KnownType.IgnoreAttributes);
8282
}
8383

8484
private static bool IsTestOrTestClassAttribute(AttributeData a) =>

analyzers/src/SonarAnalyzer.Core/Extensions/CompilationExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ public static bool IsMemberAvailable<TMemberType>(this Compilation compilation,
5353
? memberSymbols.Any()
5454
: memberSymbols.Any(memberCheck);
5555
}
56+
57+
public static bool ReferencesNetCoreControllers(this Compilation compilation) =>
58+
compilation.GetTypeByMetadataName(KnownType.Microsoft_AspNetCore_Mvc_Controller) is not null
59+
|| compilation.GetTypeByMetadataName(KnownType.Microsoft_AspNetCore_Mvc_ControllerBase) is not null;
60+
61+
public static bool ReferencesNetFrameworkControllers(this Compilation compilation) =>
62+
compilation.GetTypeByMetadataName(KnownType.System_Web_Mvc_Controller) is not null;
5663
}

analyzers/src/SonarAnalyzer.Core/Helpers/AspNetMvcHelper.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

analyzers/src/SonarAnalyzer.Core/Helpers/SemanticModelHelper.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

analyzers/src/SonarAnalyzer.Core/Helpers/UnitTestHelper.cs

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)