Skip to content

Commit

Permalink
Address review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini committed Apr 26, 2024
1 parent 2716b35 commit 704ab23
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ public async Task SonarCompilationStartAnalysisContext_RegisterSymbolStartAction
new VerifierBuilder()
.AddAnalyzer(() => new DummyAnalyzerWithLocation(ruleId, DiagnosticDescriptorFactory.MainSourceScopeTag))
.AddSnippet(Snippet(extension), $"SomeFile.{extension}")
.WithOptions(ParseOptionsHelper.FromCSharp9)
.VerifyNoIssueReported();

[DataTestMethod]
Expand All @@ -440,7 +439,6 @@ public async Task SonarCompilationStartAnalysisContext_RegisterSymbolStartAction
.AddAnalyzer(() => new DummyAnalyzerWithLocation("DummyId", DiagnosticDescriptorFactory.TestSourceScopeTag))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Test))
.AddSnippet(Snippet(extension), $"SomeFile.{extension}")
.WithOptions(ParseOptionsHelper.FromCSharp9)
.VerifyNoIssueReported();

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ public class AvoidLambdaExpressionInLoopsInBlazorTest

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_Blazor() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.razor")
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.razor").Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_BlazorLoopsWithNoBody() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.LoopsWithNoBody.razor")
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.LoopsWithNoBody.razor").Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_UsingRenderFragment() =>
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.RenderFragment.razor", "AvoidLambdaExpressionInLoopsInBlazor.RenderFragmentConsumer.razor")
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();
builder.AddPaths("AvoidLambdaExpressionInLoopsInBlazor.RenderFragment.razor", "AvoidLambdaExpressionInLoopsInBlazor.RenderFragmentConsumer.razor").Verify();

[TestMethod]
public void AvoidLambdaExpressionInLoopsInBlazor_CS() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public class CollectionPropertiesShouldBeReadOnlyTest

[TestMethod]
public void CollectionPropertiesShouldBeReadOnly_Razor() =>
builder.AddPaths("CollectionPropertiesShouldBeReadOnly.razor", "CollectionPropertiesShouldBeReadOnly.razor.cs")
.WithOptions(ParseOptionsHelper.FromCSharp11)
.Verify();
builder.AddPaths("CollectionPropertiesShouldBeReadOnly.razor", "CollectionPropertiesShouldBeReadOnly.razor.cs").Verify();

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ public class UnnecessaryUsingsTest
[DataRow("_imports.razor")]
[DataRow("_iMpoRts.razor")]
public void UnnecessaryUsings_RazorImportsRazorFile_NoIssueReported(string fileName) =>
builder.AddSnippet(@"@using System.Text.Json;", fileName).VerifyNoIssueReported(); // ToDo TAKE A LOOK
builder.AddSnippet(@"@using System.Text.Json;", fileName).VerifyNoIssueReported();

[DataTestMethod]
[DataRow("RandomFile_ViewImports.cshtml")]
[DataRow("RandomFile_Imports.cshtml")]
[DataRow("_Imports.cshtml")]
public void UnnecessaryUsings_RazorViewImportsSimilarCshtmlFile_IssuesReported(string fileName) =>
builder.AddSnippet("@using System.Linq;", "_ViewImports.cshtml")
builder.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
.AddSnippet("@using System.Linq;", "_ViewImports.cshtml")
.AddSnippet(@"@using System.Text.Json; @* Noncompliant *@", fileName)
.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
.Verify();

[DataTestMethod]
[DataRow("RandomFile_ViewImports.razor")]
[DataRow("RandomFile_Imports.razor")]
[DataRow("_ViewImports.razor")]
public void UnnecessaryUsings_RazorViewImportsSimilarRazorFile_IssuesReported(string fileName) =>
builder.AddSnippet("@using System.Linq;", "_Imports.razor")
builder.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
.AddSnippet("@using System.Linq;", "_Imports.razor")
.AddSnippet(@"@using System.Text.Json; @* Noncompliant *@", fileName)
.AddReferences(NuGetMetadataReference.SystemTextJson("7.0.4"))
.Verify();

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@
}

@code {
private List<Button> Buttons { get; } = new();
private List<Button> Buttons { get; } = new List<Button>();

private void DoAction(MouseEventArgs e, Button button) { }

private class Button
{
public string? Id { get; } = Guid.NewGuid().ToString();
public string Id { get; } = Guid.NewGuid().ToString();
public Action<MouseEventArgs> Action { get; set; } = e => { };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
</AvoidLambdaExpressionInLoopsInBlazor_RenderFragment>

@code {
private List<Button> Buttons { get; } = new();
private List<Button> Buttons { get; } = new List<Button>();

private void DoAction(MouseEventArgs e, Button button) { }

private class Button
{
public string? Id { get; } = Guid.NewGuid().ToString();
public string Id { get; } = Guid.NewGuid().ToString();
public Action<MouseEventArgs> Action { get; set; } = e => { };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
}

@code {
private List<Button> Buttons { get; } = new();
private List<Button> Buttons { get; } = new List<Button>();

private void DoAction(MouseEventArgs e, Button button) { }

private class Button
{
public string? Id { get; } = Guid.NewGuid().ToString();
public string Id { get; } = Guid.NewGuid().ToString();
public Action<MouseEventArgs> Action { get; set; } = e => { };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

@code {
[Parameter]
public Dictionary<string, object> Items { get; set; } = new (); // Compliant
public Dictionary<string, object> Items { get; set; } = new Dictionary<string, object>(); // Compliant
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;

namespace TestSamples;

public partial class Sample
namespace TestSamples
{
[Parameter]
public Dictionary<string, object> Attributes { get; set; } = new (); // Compliant
public partial class Sample
{
[Parameter]
public Dictionary<string, object> Attributes { get; set; } = new Dictionary<string, object>(); // Compliant

public Dictionary<string, object> Items { get; set; } = new (); // Noncompliant
public Dictionary<string, object> Items { get; set; } = new Dictionary<string, object>(); // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,17 @@ public class Sample
[DataRow("Dummy.razor")]
[DataRow("Dummy.cshtml")]
public void Verify_RazorAnalysisIsDisabled_DoesNotRaise(string path) =>
DummyWithLocation.AddPaths(path).VerifyNoIssueReported();
DummyWithLocation.AddPaths(path)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarLintXml(TestContext, analyzeRazorCode: false))
.VerifyNoIssueReported();

[DataTestMethod]
[DataRow("Dummy.razor")]
[DataRow("Dummy.cshtml")]
public void Verify_RazorAnalysisInSLAndNugetContext_DoesNotRaise(string path) =>
DummyWithLocation.AddPaths(path).VerifyNoIssueReported();
DummyWithLocation.AddPaths(path)
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Unknown))
.VerifyNoIssueReported();

[TestMethod]
public void Compile_Razor_DefaultFramework()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private ProjectBuilder CreateProject(bool concurrentAnalysis)
var contentFilePaths = paths.Where(IsRazorOrCshtml).ToArray();
var sourceFilePaths = paths.Except(contentFilePaths).ToArray();
var contentSnippets = builder.Snippets.Where(x => IsRazorOrCshtml(x.FileName)).ToArray();
var sourceSnippets = builder.Snippets.Where(x => !contentSnippets.Contains(x)).ToArray(); // Cannot use Except because it Distincts the elements
var sourceSnippets = builder.Snippets.Where(x => !IsRazorOrCshtml(x.FileName)).ToArray(); // Cannot use Except because it Distincts the elements

contentSnippets = contentSnippets.Select(x =>
{
Expand All @@ -177,16 +177,16 @@ private ProjectBuilder CreateProject(bool concurrentAnalysis)
var concurrentContentFiles = concurrentAnalysis && builder.AutogenerateConcurrentFiles ? CreateConcurrencyTest(contentFilePaths) : [];
return SolutionBuilder.Create()
.AddProject(language, builder.OutputKind)
.AddSnippets(sourceSnippets)
.AddDocuments(sourceFilePaths)
.AddAdditionalDocuments(contentFilePaths)
.AddDocuments(concurrentAnalysis && builder.AutogenerateConcurrentFiles ? CreateConcurrencyTest(sourceFilePaths) : [])
.AddAdditionalDocuments(contentFilePaths)
.AddAdditionalDocuments(concurrentContentFiles)
.AddAnalyzerReferences(hasContentDocuments ? SourceGeneratorProvider.SourceGenerators : [])
.AddSnippets(sourceSnippets)
.AddAdditionalDocuments(contentSnippets.Length > 0 ? contentSnippets.Select(x => x.FileName) : [])
.AddReferences(builder.References)
.AddReferences(hasContentDocuments ? NuGetMetadataReference.MicrosoftAspNetCoreAppRef("7.0.17") : [])
.AddReferences(hasContentDocuments ? NuGetMetadataReference.SystemTextEncodingsWeb("7.0.0") : [])
.AddAnalyzerReferences(hasContentDocuments ? SourceGeneratorProvider.SourceGenerators : [])
.AddAnalyzerConfigDocument(
Path.Combine(Directory.GetCurrentDirectory(), ".editorconfig"),
editorConfigGenerator.Generate(concurrentContentFiles.Concat(contentFilePaths).Concat(contentSnippets.Select(x => x.FileName))));
Expand Down

0 comments on commit 704ab23

Please sign in to comment.