-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DescriptorFactory and DiagnosticAnalyzers to namespaces (#9294)
- Loading branch information
1 parent
c225c8e
commit ae9b48b
Showing
21 changed files
with
353 additions
and
357 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
83 changes: 83 additions & 0 deletions
83
analyzers/src/SonarAnalyzer.Common/Analyzers/DiagnosticDescriptorFactory.cs
This file contains 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,83 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.Analyzers; | ||
|
||
public static class DiagnosticDescriptorFactory | ||
{ | ||
public static readonly string SonarWayTag = "SonarWay"; | ||
public static readonly string UtilityTag = "Utility"; | ||
public static readonly string MainSourceScopeTag = "MainSourceScope"; | ||
public static readonly string TestSourceScopeTag = "TestSourceScope"; | ||
|
||
public static DiagnosticDescriptor CreateUtility(string diagnosticId, string title) => | ||
new(diagnosticId, | ||
title, | ||
string.Empty, | ||
string.Empty, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
customTags: BuildUtilityTags()); | ||
|
||
public static DiagnosticDescriptor Create(AnalyzerLanguage language, RuleDescriptor rule, string messageFormat, bool? isEnabledByDefault, bool fadeOutCode) => | ||
new(rule.Id, | ||
rule.Title, | ||
messageFormat, | ||
rule.Category, | ||
fadeOutCode ? DiagnosticSeverity.Info : DiagnosticSeverity.Warning, | ||
rule.IsHotspot || (isEnabledByDefault ?? rule.SonarWay), | ||
rule.Description, | ||
language.HelpLink(rule.Id), | ||
BuildTags(language, rule, fadeOutCode)); | ||
|
||
private static string[] BuildTags(AnalyzerLanguage language, RuleDescriptor rule, bool fadeOutCode) | ||
{ | ||
var tags = new List<string> { language.LanguageName }; | ||
tags.AddRange(rule.Scope.ToTags()); | ||
Add(rule.SonarWay, SonarWayTag); | ||
Add(fadeOutCode, WellKnownDiagnosticTags.Unnecessary); | ||
return tags.ToArray(); | ||
|
||
void Add(bool condition, string tag) | ||
{ | ||
if (condition) | ||
{ | ||
tags.Add(tag); | ||
} | ||
} | ||
} | ||
|
||
private static IEnumerable<string> ToTags(this SourceScope sourceScope) => | ||
sourceScope switch | ||
{ | ||
SourceScope.Main => new[] { MainSourceScopeTag }, | ||
SourceScope.Tests => new[] { TestSourceScopeTag }, | ||
SourceScope.All => new[] { MainSourceScopeTag, TestSourceScopeTag }, | ||
_ => throw new NotSupportedException($"{sourceScope} is not supported 'SourceScope' value."), | ||
}; | ||
|
||
private static string[] BuildUtilityTags() => | ||
SourceScope.All.ToTags().Concat(new[] { UtilityTag }) | ||
#if !DEBUG | ||
// Allow to configure the analyzers in debug mode only. This allows to run test selectively (for example to test only one rule) | ||
.Union(new[] { WellKnownDiagnosticTags.NotConfigurable }) | ||
#endif | ||
.ToArray(); | ||
} |
This file contains 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 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
61 changes: 61 additions & 0 deletions
61
analyzers/src/SonarAnalyzer.Common/Analyzers/SonarDiagnosticAnalyzer.cs
This file contains 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,61 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using RoslynAnalysisContext = Microsoft.CodeAnalysis.Diagnostics.AnalysisContext; | ||
|
||
namespace SonarAnalyzer.Analyzers; | ||
|
||
public abstract class SonarDiagnosticAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public static readonly string EnableConcurrentExecutionVariable = "SONAR_DOTNET_ENABLE_CONCURRENT_EXECUTION"; | ||
|
||
protected abstract void Initialize(SonarAnalysisContext context); | ||
|
||
protected virtual bool EnableConcurrentExecution => IsConcurrentExecutionEnabled(); | ||
|
||
public sealed override void Initialize(RoslynAnalysisContext context) | ||
{ | ||
// The default values are Analyze | ReportDiagnostics. We do this call to make sure it will be still enabled even if the default values changed. (Needed for the razor analysis) | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
if (EnableConcurrentExecution) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
} | ||
Initialize(new SonarAnalysisContext(context, SupportedDiagnostics)); | ||
} | ||
|
||
protected static bool IsConcurrentExecutionEnabled() | ||
{ | ||
var value = Environment.GetEnvironmentVariable(EnableConcurrentExecutionVariable); | ||
return value is null || !bool.TryParse(value, out var result) || result; | ||
} | ||
} | ||
|
||
public abstract class SonarDiagnosticAnalyzer<TSyntaxKind> : SonarDiagnosticAnalyzer | ||
where TSyntaxKind : struct | ||
{ | ||
protected abstract string MessageFormat { get; } | ||
protected abstract ILanguageFacade<TSyntaxKind> Language { get; } | ||
protected DiagnosticDescriptor Rule { get; } | ||
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
protected SonarDiagnosticAnalyzer(string diagnosticId) => | ||
Rule = Language.CreateDescriptor(diagnosticId, MessageFormat); | ||
} |
37 changes: 37 additions & 0 deletions
37
analyzers/src/SonarAnalyzer.Common/Analyzers/TrackerHotspotDiagnosticAnalyzer.cs
This file contains 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,37 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
namespace SonarAnalyzer.Analyzers; | ||
|
||
public abstract class TrackerHotspotDiagnosticAnalyzer<TSyntaxKind> : HotspotDiagnosticAnalyzer where TSyntaxKind : struct | ||
{ | ||
protected abstract ILanguageFacade<TSyntaxKind> Language { get; } | ||
protected abstract void Initialize(TrackerInput input); | ||
|
||
protected DiagnosticDescriptor Rule { get; } | ||
|
||
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
protected TrackerHotspotDiagnosticAnalyzer(IAnalyzerConfiguration configuration, string diagnosticId, string messageFormat) : base(configuration) => | ||
Rule = Language.CreateDescriptor(diagnosticId, messageFormat); | ||
|
||
protected override void Initialize(SonarAnalysisContext context) => | ||
Initialize(new TrackerInput(context, Configuration, Rule)); | ||
} |
84 changes: 0 additions & 84 deletions
84
analyzers/src/SonarAnalyzer.Common/DiagnosticAnalyzer/DiagnosticDescriptorFactory.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.