Skip to content

Commit

Permalink
Fix and clean-up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel Nobel committed Apr 19, 2023
1 parent 133ff1e commit f63801f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using SonarAnalyzer.Helpers;

namespace SonarAnalyzer.Rules.CSharp;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TestsShouldNotUseThreadSleep : TestsShouldNotUseThreadSleepBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance;

protected override bool IsWithinTest(SyntaxNode node, SemanticModel model) =>
protected override bool IsInTestMethod(SyntaxNode node, SemanticModel model) =>
node.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault() is { } method
&& model.GetDeclaredSymbol(method) is IMethodSymbol symbol
&& symbol.IsTestMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using SonarAnalyzer.Helpers;

namespace SonarAnalyzer.Rules;

public abstract class TestsShouldNotUseThreadSleepBase<TSyntaxKind> : SonarDiagnosticAnalyzer<TSyntaxKind>
Expand All @@ -37,12 +32,12 @@ public abstract class TestsShouldNotUseThreadSleepBase<TSyntaxKind> : SonarDiagn
protected TestsShouldNotUseThreadSleepBase() : base(DiagnosticId) { }

protected sealed override void Initialize(SonarAnalysisContext context) =>
context.RegisterSyntaxNodeActionInNonGenerated(Language.GeneratedCodeRecognizer, c =>
context.RegisterNodeAction(Language.GeneratedCodeRecognizer, c =>
{
if (IsThreadSleepMethod(Language.Syntax.NodeIdentifier(c.Node)?.Text)
&& c.SemanticModel.GetSymbolInfo(c.Node).Symbol is IMethodSymbol method
&& method.Is(KnownType.System_Threading_Thread, nameof(Thread.Sleep))
&& IsWithinTest(c.Node, c.SemanticModel))
&& IsInTestMethod(c.Node, c.SemanticModel))
{
c.ReportIssue(Diagnostic.Create(Rule, c.Node.Parent.Parent.GetLocation()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using SonarAnalyzer.Helpers;

namespace SonarAnalyzer.Rules.VisualBasic;

[DiagnosticAnalyzer(LanguageNames.VisualBasic)]
public sealed class TestsShouldNotUseThreadSleep : TestsShouldNotUseThreadSleepBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;

protected override bool IsWithinTest(SyntaxNode node, SemanticModel model) =>
protected override bool IsInTestMethod(SyntaxNode node, SemanticModel model) =>
node.Ancestors().OfType<MethodBlockSyntax>().FirstOrDefault() is { } block
&& model.GetDeclaredSymbol(block.SubOrFunctionStatement) is IMethodSymbol symbol
&& symbol.IsTestMethod();
Expand Down

0 comments on commit f63801f

Please sign in to comment.