Skip to content

Commit

Permalink
Refactor ShouldAnalyzeTree
Browse files Browse the repository at this point in the history
refactor shouldAnalyzeTree method
  • Loading branch information
mary-georgiou-sonarsource committed Mar 14, 2023
1 parent 262b3b7 commit bb2a4ec
Showing 1 changed file with 7 additions and 4 deletions.
Expand Up @@ -57,9 +57,9 @@ protected SonarAnalysisContextBase(SonarAnalysisContext analysisContext, TContex
/// <param name="tree">Tree to decide on. Can be null for Symbol-based and Compilation-based scenarios. And we want to analyze those too.</param>
/// <param name="generatedCodeRecognizer">When set, generated trees are analyzed only when language-specific 'analyzeGeneratedCode' configuration property is also set.</param>
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));

/// <summary>
/// Reads configuration from SonarProjectConfig.xml file and caches the result for scope of this analysis.
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit bb2a4ec

Please sign in to comment.