From bb2a4ec48864d92beba25c30c0593ccb6ba0f0f1 Mon Sep 17 00:00:00 2001 From: mary-georgiou-sonarsource Date: Mon, 13 Mar 2023 14:46:15 +0100 Subject: [PATCH] Refactor ShouldAnalyzeTree refactor shouldAnalyzeTree method --- .../AnalysisContext/SonarAnalysisContextBase.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.Common/AnalysisContext/SonarAnalysisContextBase.cs b/analyzers/src/SonarAnalyzer.Common/AnalysisContext/SonarAnalysisContextBase.cs index 70fd96b25ee..431e96c8643 100644 --- a/analyzers/src/SonarAnalyzer.Common/AnalysisContext/SonarAnalysisContextBase.cs +++ b/analyzers/src/SonarAnalyzer.Common/AnalysisContext/SonarAnalysisContextBase.cs @@ -57,9 +57,9 @@ protected SonarAnalysisContextBase(SonarAnalysisContext analysisContext, TContex /// Tree to decide on. Can be null for Symbol-based and Compilation-based scenarios. And we want to analyze those too. /// When set, generated trees are analyzed only when language-specific 'analyzeGeneratedCode' configuration property is also set. public bool ShouldAnalyzeTree(SyntaxTree tree, GeneratedCodeRecognizer generatedCodeRecognizer) => - SonarLintFile() is var sonarLintReader - && (generatedCodeRecognizer is null || sonarLintReader.AnalyzeGeneratedCode || !tree.IsGenerated(generatedCodeRecognizer, Compilation)) - && (tree is null || (!IsUnchanged(tree) && ShouldAnalyzeFile(sonarLintReader, tree.FilePath))); + ReadSonarLintXml() is var sonarLint + && (generatedCodeRecognizer is null || sonarLint.AnalyzeGeneratedCode || !tree.IsGenerated(generatedCodeRecognizer, Compilation)) + && (tree is null || ShouldAnalyzeFile(sonarLint, tree)); /// /// Reads configuration from SonarProjectConfig.xml file and caches the result for scope of this analysis. @@ -125,7 +125,10 @@ public bool HasMatchingScope(DiagnosticDescriptor descriptor) descriptor.CustomTags.Contains(tag); } - private bool ShouldAnalyzeFile(SonarLintXmlReader sonarLintXml, string filePath) => + private bool ShouldAnalyzeFile(SonarLintXmlReader sonarLintXml, SyntaxTree tree) => + !IsUnchanged(tree) && IsInSonarLintContextAndIncluded(sonarLintXml, tree.FilePath); + + private bool IsInSonarLintContextAndIncluded(SonarLintXmlReader sonarLintXml, string filePath) => ProjectConfiguration().ProjectType != ProjectType.Unknown // Not SonarLint context, NuGet or Scanner <= 5.0 || (FileInclusionCache.GetValue(Compilation, _ => new()) is var cache && cache.GetOrAdd(filePath, _ => IsFileIncluded(sonarLintXml, filePath)));