Skip to content

Commit

Permalink
rename parameter to parameters and small re-arrange of class members
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed May 17, 2023
1 parent 6894ca5 commit d9589fd
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public abstract class CopyPasteTokenAnalyzerBase<TSyntaxKind> : UtilityAnalyzerB
protected abstract string GetCpdValue(SyntaxToken token);
protected abstract bool IsUsingDirective(SyntaxNode node);

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { AnalyzeTestProjects = false };

protected sealed override bool AnalyzeUnchangedFiles => true;
protected sealed override string FileName => "token-cpd.pb";

protected CopyPasteTokenAnalyzerBase() : base(DiagnosticId, Title) { }

protected sealed override CopyPasteTokenInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel)
protected sealed override CopyPasteTokenInfo CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel)
{
var cpdTokenInfo = new CopyPasteTokenInfo { FilePath = syntaxTree.FilePath };
foreach (var token in syntaxTree.GetRoot().DescendantTokens(n => !IsUsingDirective(n)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public abstract class FileMetadataAnalyzerBase<TSyntaxKind> : UtilityAnalyzerBas
private const string Title = "File metadata generator";

protected sealed override string FileName => "file-metadata.pb";
protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { AnalyzeGeneratedCode = true };

protected FileMetadataAnalyzerBase() : base(DiagnosticId, Title) { }

protected sealed override FileMetadataInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
new FileMetadataInfo
protected sealed override FileMetadataInfo CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
new()
{
FilePath = syntaxTree.FilePath,
IsGenerated = Language.GeneratedCodeRecognizer.IsGenerated(syntaxTree),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class LogAnalyzerBase<TSyntaxKind> : UtilityAnalyzerBase<TSyntax
protected abstract string LanguageVersion(Compilation compilation);

protected sealed override string FileName => "log.pb";
protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { AnalyzeGeneratedCode = true };

protected LogAnalyzerBase() : base(DiagnosticId, Title) { }
Expand All @@ -44,7 +44,7 @@ protected sealed override IEnumerable<LogInfo> CreateAnalysisMessages(SonarCompi
new LogInfo { Severity = LogSeverity.Info, Text = "Concurrent execution: " + (IsConcurrentExecutionEnabled() ? "enabled" : "disabled") }
};

protected sealed override LogInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
protected sealed override LogInfo CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
syntaxTree.IsGenerated(Language.GeneratedCodeRecognizer, semanticModel.Compilation)
? new LogInfo { Severity = LogSeverity.Debug, Text = $"File '{syntaxTree.FilePath}' was recognized as generated" }
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public abstract class MetricsAnalyzerBase<TSyntaxKind> : UtilityAnalyzerBase<TSy
protected abstract MetricsBase GetMetrics(SyntaxTree syntaxTree, SemanticModel semanticModel);

protected sealed override string FileName => "metrics.pb";
protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { AnalyzeTestProjects = false };

protected MetricsAnalyzerBase() : base(DiagnosticId, Title) { }

protected sealed override MetricsInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel)
protected sealed override MetricsInfo CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel)
{
var metrics = GetMetrics(syntaxTree, semanticModel);
var complexity = metrics.Complexity;
Expand All @@ -50,7 +50,7 @@ protected sealed override MetricsInfo CreateMessage(UtilityAnalyzerParameter par
CognitiveComplexity = metrics.CognitiveComplexity,
};

var comments = metrics.GetComments(parameter.IgnoreHeaderComments);
var comments = metrics.GetComments(parameters.IgnoreHeaderComments);
metricsInfo.NoSonarComment.AddRange(comments.NoSonar);
metricsInfo.NonBlankComment.AddRange(comments.NonBlank);
metricsInfo.CodeLine.AddRange(metrics.CodeLines);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class SymbolReferenceAnalyzerBase<TSyntaxKind> : UtilityAnalyzer

protected SymbolReferenceAnalyzerBase() : base(DiagnosticId, Title) { }

protected sealed override SymbolReferenceInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel)
protected sealed override SymbolReferenceInfo CreateMessage(UtilityAnalyzerParameters parameter, SyntaxTree syntaxTree, SemanticModel semanticModel)
{
var symbolReferenceInfo = new SymbolReferenceInfo { FilePath = syntaxTree.FilePath };
var references = GetReferences(syntaxTree.GetRoot(), semanticModel);
Expand All @@ -56,8 +56,8 @@ protected sealed override SymbolReferenceInfo CreateMessage(UtilityAnalyzerParam
return symbolReferenceInfo;
}

protected override bool ShouldGenerateMetrics(UtilityAnalyzerParameter parameter, SyntaxTree tree) =>
base.ShouldGenerateMetrics(parameter, tree)
protected override bool ShouldGenerateMetrics(UtilityAnalyzerParameters parameters, SyntaxTree tree) =>
base.ShouldGenerateMetrics(parameters, tree)
&& !HasTooManyTokens(tree);

private Dictionary<ISymbol, List<ReferenceInfo>> GetReferences(SyntaxNode root, SemanticModel model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected TokenTypeAnalyzerBase() : base(DiagnosticId, Title) { }
protected abstract TokenClassifierBase GetTokenClassifier(SemanticModel semanticModel, bool skipIdentifierTokens);
protected abstract TriviaClassifierBase GetTriviaClassifier();

protected sealed override TokenTypeInfo CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel)
protected sealed override TokenTypeInfo CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel)
{
var tokens = syntaxTree.GetRoot().DescendantTokens();
var identifierTokenKind = Language.SyntaxKind.IdentifierToken; // Performance optimization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@

namespace SonarAnalyzer.Rules
{
public readonly record struct UtilityAnalyzerParameter(bool IsAnalyzerEnabled, bool IgnoreHeaderComments, bool AnalyzeGeneratedCode, bool AnalyzeTestProjects, string OutPath, bool IsTestProject)
public readonly record struct UtilityAnalyzerParameters(bool IsAnalyzerEnabled, bool IgnoreHeaderComments, bool AnalyzeGeneratedCode, bool AnalyzeTestProjects, string OutPath, bool IsTestProject)
{
public static readonly UtilityAnalyzerParameter Default =
public static readonly UtilityAnalyzerParameters Default =
new(IsAnalyzerEnabled: false, IgnoreHeaderComments: false, AnalyzeGeneratedCode: false, AnalyzeTestProjects: true, OutPath: null, IsTestProject: false);
}

public abstract class UtilityAnalyzerBase : SonarDiagnosticAnalyzer
{
protected static readonly ISet<string> FileExtensionWhitelist = new HashSet<string> { ".cs", ".csx", ".vb" };
private readonly DiagnosticDescriptor rule;

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule);
protected override bool EnableConcurrentExecution => false;

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(rule);

protected UtilityAnalyzerBase(string diagnosticId, string title) =>
rule = DiagnosticDescriptorFactory.CreateUtility(diagnosticId, title);

Expand All @@ -50,7 +51,7 @@ internal static TextRange GetTextRange(FileLinePositionSpan lineSpan) =>
EndOffset = lineSpan.EndLinePosition.Character
};

protected virtual UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context)
protected virtual UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context)
{
var outPath = context.ProjectConfiguration().OutPath;
// For backward compatibility with S4MSB <= 5.0
Expand All @@ -62,15 +63,15 @@ protected virtual UtilityAnalyzerParameter ReadParameters(SonarCompilationStartA
{
var language = context.Compilation.Language;
var sonarLintXml = context.SonarLintXml();
return new UtilityAnalyzerParameter(
return new UtilityAnalyzerParameters(
IsAnalyzerEnabled: true,
IgnoreHeaderComments: sonarLintXml.IgnoreHeaderComments(language),
AnalyzeGeneratedCode: sonarLintXml.AnalyzeGeneratedCode(language),
AnalyzeTestProjects: true,
OutPath: Path.Combine(outPath, language == LanguageNames.CSharp ? "output-cs" : "output-vbnet"),
IsTestProject: context.IsTestProject());
}
return UtilityAnalyzerParameter.Default;
return UtilityAnalyzerParameters.Default;
}
}

Expand All @@ -82,29 +83,29 @@ public abstract class UtilityAnalyzerBase<TSyntaxKind, TMessage> : UtilityAnalyz

protected abstract ILanguageFacade<TSyntaxKind> Language { get; }
protected abstract string FileName { get; }
protected abstract TMessage CreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel);

protected virtual bool AnalyzeUnchangedFiles => false;

protected abstract TMessage CreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel);

protected virtual IEnumerable<TMessage> CreateAnalysisMessages(SonarCompilationReportingContext c) => Enumerable.Empty<TMessage>();

protected UtilityAnalyzerBase(string diagnosticId, string title) : base(diagnosticId, title) { }

protected sealed override void Initialize(SonarAnalysisContext context) =>
context.RegisterCompilationStartAction(startContext =>
{
var parameter = ReadParameters(startContext);
if (!parameter.IsAnalyzerEnabled)
var parameters = ReadParameters(startContext);
if (!parameters.IsAnalyzerEnabled)
{
return;
}

var treeMessages = new List<TMessage>();
startContext.RegisterSemanticModelAction(modelContext =>
{
if (ShouldGenerateMetrics(parameter, modelContext))
if (ShouldGenerateMetrics(parameters, modelContext))
{
treeMessages.Add(CreateMessage(parameter, modelContext.Tree, modelContext.SemanticModel));
treeMessages.Add(CreateMessage(parameters, modelContext.Tree, modelContext.SemanticModel));
}
});

Expand All @@ -116,8 +117,8 @@ protected sealed override void Initialize(SonarAnalysisContext context) =>
.ToArray();
lock (FileWriteLock)
{
Directory.CreateDirectory(parameter.OutPath);
using var stream = File.Create(Path.Combine(parameter.OutPath, FileName));
Directory.CreateDirectory(parameters.OutPath);
using var stream = File.Create(Path.Combine(parameters.OutPath, FileName));
foreach (var message in allMessages)
{
message.WriteDelimitedTo(stream);
Expand All @@ -126,14 +127,14 @@ protected sealed override void Initialize(SonarAnalysisContext context) =>
});
});

protected virtual bool ShouldGenerateMetrics(UtilityAnalyzerParameter parameter, SyntaxTree tree) =>
protected virtual bool ShouldGenerateMetrics(UtilityAnalyzerParameters parameters, SyntaxTree tree) =>
// The results of Metrics and CopyPasteToken analyzers are not needed for Test projects yet the plugin side expects the protobuf files, so we create empty ones.
(parameter.AnalyzeTestProjects || !parameter.IsTestProject)
(parameters.AnalyzeTestProjects || !parameters.IsTestProject)
&& FileExtensionWhitelist.Contains(Path.GetExtension(tree.FilePath))
&& (parameter.AnalyzeGeneratedCode || !Language.GeneratedCodeRecognizer.IsGenerated(tree));
&& (parameters.AnalyzeGeneratedCode || !Language.GeneratedCodeRecognizer.IsGenerated(tree));

private bool ShouldGenerateMetrics(UtilityAnalyzerParameter parameter, SonarSematicModelReportingContext context) =>
private bool ShouldGenerateMetrics(UtilityAnalyzerParameters parameters, SonarSematicModelReportingContext context) =>
(AnalyzeUnchangedFiles || !context.IsUnchanged(context.Tree))
&& ShouldGenerateMetrics(parameter, context.Tree);
&& ShouldGenerateMetrics(parameters, context.Tree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public TestAnalysisWarningAnalyzer_CS(bool isAnalyzerEnabled, int minimalSupport
this.outPath = outPath;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = isAnalyzerEnabled, OutPath = outPath };
}

Expand All @@ -117,7 +117,7 @@ public TestAnalysisWarningAnalyzer_VB(bool isAnalyzerEnabled, int minimalSupport
this.outPath = outPath;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = isAnalyzerEnabled, OutPath = outPath };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public TestCopyPasteTokenAnalyzer_CS(string outPath, bool isTestProject)
this.isTestProject = isTestProject;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = isTestProject };
}

Expand All @@ -166,7 +166,7 @@ public TestCopyPasteTokenAnalyzer_VB(string outPath, bool isTestProject)
this.isTestProject = isTestProject;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = isTestProject };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void CreateMessage_NoEncoding_SetsEmptyString(bool isTestProject)
tree.SetupGet(x => x.Encoding).Returns(() => null);
var sut = new TestFileMetadataAnalyzer(null, isTestProject);

sut.TestCreateMessage(UtilityAnalyzerParameter.Default, tree.Object, null).Encoding.Should().BeEmpty();
sut.TestCreateMessage(UtilityAnalyzerParameters.Default, tree.Object, null).Encoding.Should().BeEmpty();
}

[DataTestMethod]
Expand Down Expand Up @@ -142,11 +142,11 @@ public TestFileMetadataAnalyzer(string outPath, bool isTestProject)
this.isTestProject = isTestProject;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = isTestProject };

public FileMetadataInfo TestCreateMessage(UtilityAnalyzerParameter parameter, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
CreateMessage(parameter, syntaxTree, semanticModel);
public FileMetadataInfo TestCreateMessage(UtilityAnalyzerParameters parameters, SyntaxTree syntaxTree, SemanticModel semanticModel) =>
CreateMessage(parameters, syntaxTree, semanticModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public TestLogAnalyzer_CS(string outPath)
this.outPath = outPath;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = false };
}

Expand All @@ -169,7 +169,7 @@ public TestLogAnalyzer_VB(string outPath)
this.outPath = outPath;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = false };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public TestMetricsAnalyzer(string outPath, bool isTestProject)
this.isTestProject = isTestProject;
}

protected override UtilityAnalyzerParameter ReadParameters(SonarCompilationStartAnalysisContext context) =>
protected override UtilityAnalyzerParameters ReadParameters(SonarCompilationStartAnalysisContext context) =>
base.ReadParameters(context) with { IsAnalyzerEnabled = true, OutPath = outPath, IsTestProject = isTestProject };
}
}
Expand Down
Loading

0 comments on commit d9589fd

Please sign in to comment.