Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics analyzer for Razor: Lines of code are outside the range of the file #9312

Merged
merged 10 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions analyzers/src/SonarAnalyzer.Common/Metrics/MetricsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public abstract class MetricsBase
public ISet<int> CodeLines =>
tree.GetRoot()
.DescendantTokens()
.Where(x => !IsEndOfFile(x))
.Select(x => x.GetLocation().GetMappedLineSpan())
.Where(IsInSameFile)
.Select(GetMappedLineSpan)
.Where(x => x is not null)
.SelectMany(
x =>
{
var start = x.StartLinePosition.GetLineNumberToReport();
var end = x.EndLinePosition.GetLineNumberToReport();
var start = x.Value.StartLinePosition.GetLineNumberToReport();
var end = x.Value.EndLinePosition.GetLineNumberToReport();
return Enumerable.Range(start, end - start + 1);
})
.ToHashSet();
Expand Down Expand Up @@ -119,6 +118,14 @@ protected bool IsInSameFile(FileLinePositionSpan span) =>
private IEnumerable<SyntaxNode> FunctionNodes =>
tree.GetRoot().DescendantNodes().Where(IsFunction);

private FileLinePositionSpan? GetMappedLineSpan(SyntaxToken token) =>
!IsEndOfFile(token)
&& token.GetLocation().GetMappedLineSpan() is { IsValid: true } mappedLineSpan
&& (!GeneratedCodeRecognizer.IsRazor(token.SyntaxTree) || mappedLineSpan.HasMappedPath)
&& IsInSameFile(mappedLineSpan)
? mappedLineSpan
: null;

private static void CategorizeLines(string line, int lineNumber, ISet<int> noSonar, ISet<int> nonBlank)
{
if (line.Contains("NOSONAR"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,14 @@ public void GetLineNumbers_Razor_FieldReference() =>
@page "/razor"
@using TestCases

<p>Current count: @currentCount</p> <!-- +1 -->
<p>Current count: @currentCount</p> <!-- Not counted -->

@currentCount <!-- +1 -->
@currentCount <!-- Not counted -->

@code {
private int currentCount = 0;
}
""", RazorFile, 4, 6);
""", RazorFile);

[TestMethod]
public void GetLineNumbers_Razor_MethodReferenceAndCall() =>
Expand All @@ -927,13 +927,13 @@ private string ShowAmount()
[TestMethod]
public void GetLineNumbers_Razor_PropertyReference() =>
AssertLineNumbersOfExecutableLinesRazor("""
@IncrementAmount <!-- +1 -->
@IncrementAmount <!-- Not counted -->

@code {
[Parameter]
public int IncrementAmount { get; set; } = 1;
}
""", RazorFile, 1);
""", RazorFile);

[TestMethod]
public void GetLineNumbers_Razor_Html() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MetricsAnalyzerTest
private const string RazorFileName = "Razor.razor";
private const string CsHtmlFileName = "Razor.cshtml";
private const string CSharp12FileName = "Metrics.CSharp12.cs";
private const string ImportsRazorFileName = "_Imports.razor";

public TestContext TestContext { get; set; }

Expand Down Expand Up @@ -69,14 +70,35 @@ public void VerifyMetrics_Razor() =>
var metrics = messages.Single(x => x.FilePath.EndsWith(RazorFileName));

metrics.ClassCount.Should().Be(1);
metrics.CodeLine.Should().BeEquivalentTo(new[] { 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 });
metrics.CodeLine.Should().BeEquivalentTo(new[] { 1, 2, 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 });
metrics.CognitiveComplexity.Should().Be(3);
metrics.Complexity.Should().Be(4);
metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 3, 5, 13, 15, 17, 24, 28, 29, 32, 36, 39, 43 });
metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 13, 15, 28, 29, 32, 36 });
metrics.FunctionCount.Should().Be(1);
metrics.NoSonarComment.Should().BeEmpty();
metrics.NonBlankComment.Should().BeEquivalentTo(new[] { 7, 8, 10, 15, 21, 22, 23, 28, 29, 32, 33, 36, 37, 38 });
metrics.StatementCount.Should().Be(13);
metrics.StatementCount.Should().Be(6);
});

[TestMethod]
public void VerifyMetrics_Razor_Usings() =>
CreateBuilder(false, ImportsRazorFileName)
.VerifyUtilityAnalyzer<MetricsInfo>(messages =>
{
var orderedMessages = messages.OrderBy(x => x.FilePath, StringComparer.InvariantCulture).ToArray();
orderedMessages.Select(x => Path.GetFileName(x.FilePath)).Should().BeEquivalentTo(ImportsRazorFileName);

var metrics = messages.Single(x => x.FilePath.EndsWith(ImportsRazorFileName));

metrics.ClassCount.Should().Be(0);
metrics.CodeLine.Should().BeEquivalentTo(new[] { 1, 2, 4 }); // FN: this file has only 3 lines. See: https://github.com/SonarSource/sonar-dotnet/issues/9288
metrics.CognitiveComplexity.Should().Be(0);
metrics.Complexity.Should().Be(1);
metrics.ExecutableLines.Should().BeEquivalentTo(Array.Empty<int>());
metrics.FunctionCount.Should().Be(0);
metrics.NoSonarComment.Should().BeEmpty();
metrics.NonBlankComment.Should().BeEquivalentTo(Array.Empty<int>());
metrics.StatementCount.Should().Be(0);
});

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ public void Verify_Razor() =>

orderedSymbols[1].FilePath.Should().EndWith("RazorComponent.razor"); // RazorComponent.razor
// https://github.com/SonarSource/sonar-dotnet/issues/8417
// Net8 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping
// before dotnet 8.0.5 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping
// Declaration of TSomeVeryLongName is placed starting at index 0 (ignoring "@typeparam ")
// Reference "where TSomeVeryLongName" is placed starting at index 6 (ignoring "@typeparam TSomeVeryLongName ")
VerifyReferences(orderedSymbols[1].Reference, 1, 1, 1);
orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 6, EndOffset = 23 });
orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 35, EndOffset = 52 });
});

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@page "/razor" // FN: line of code, comment (location mapping issue)
@namespace RazorNamespace // FN: line of code, comment (location mapping issue)
<p>Current count: @currentCount</p> <!-- code line, FN: comment -->
@page "/razor"
@namespace RazorNamespace
<p>Current count: @currentCount</p> <!-- FN: comment -->

@currentCount <!-- code line, FN: comment -->
@currentCount <!-- FN: comment -->

@code { // FN: line of code (location mapping issue)
private int currentCount = 0; // code line
Expand All @@ -14,14 +14,14 @@

@for (var i = 0; i < 5; i++) // code line
{ <!-- code line, FN: comment -->
<text>Value: @i</text> <!-- code line, FN: comment -->
<text>Value: @i</text> <!-- FN: comment -->
<text>test</text>
} <!-- code line, FN: comment -->

@{ // FN: code line (location mapping issue)
void RenderName(string name) // code line
{ // code line
<p>Name: <strong>@name</strong></p> <!-- code line, FN: comment -->
<p>Name: <strong>@name</strong></p> <!-- FN: comment -->
<p>Name: <strong>Hardcoded</strong></p>
} <!-- code line, FN: comment -->

Expand All @@ -40,7 +40,7 @@
} <!-- code line, FN: comment -->

<Component>
@currentCount <!-- code line, FN: comment -->
@currentCount <!-- FN: comment -->
</Component>

<!-- FN: comment -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@using System
@using System.Collections.Generic
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,40 @@ namespace SonarAnalyzer.TestFramework.Test.Common;
[TestClass]
public class SourceGeneratorProviderTest
{
private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.dll"));
[TestMethod]
public void SourceGenerators_ContainsRazorSourceGenerator() =>
SourceGeneratorProvider.SourceGenerators.Should().ContainSingle().Which.FullPath.Should().EndWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
SourceGeneratorProvider.SourceGenerators.Should()
.Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll")));

[TestMethod]
public void RazorSourceGenerator_HasCorrectPath() =>
RazorSourceGenerator.FullPath
.Should()
.Be(Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
public void RazorSourceGenerator_ExistsLocally() =>
File.Exists(RazorSourceGenerator.FullPath).Should().BeTrue();

[TestMethod]
public void RazorSourceGenerator_LoadsCorrectAssembly() =>
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators");
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler");

private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
[TestMethod]
public void LatestSdkFolder_ReturnsAssemblyMajor()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue($"'{latestSdkFolder}' cannot be parsed to a version number");
latestSdkVersion.Major.Should().Be(typeof(object).Assembly.GetName().Version.Major);
}

[TestMethod]
public void LatestSdkFolder_ReturnLatest()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue($"'{latestSdkFolder}' cannot be parsed to a version number");
var parentDirectory = Directory.GetParent(latestSdkFolder);
parentDirectory.Name.Should().Be("sdk", "Parent directory of the latest SDK should be 'sdk'");
Directory.GetDirectories(parentDirectory.FullName, $"{typeof(object).Assembly.GetName().Version.Major}.*")
.Should().NotContain(x => IsHigherVersion(x, latestSdkVersion), "There should be no SDK folders with a higher version number than the latest SDK folder");
}

private static bool IsHigherVersion(string directory, Version referenceVersion) =>
Version.TryParse(new DirectoryInfo(directory).Name, out var version) && version > referenceVersion;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<p>The solution to all problems is: @(RaiseHere(21))</p> @* wrong location *@
@* ^^^^^^^^^ *@
@* ^^ Secondary@-1 *@
<p>The solution to all problems is: @(RaiseHere(21))</p>
@* ^^^^^^^^^ *@
@* ^^ Secondary@-1 *@

@code
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p>Explicit expression: @(RaiseHere())</p> @* Noncompliant, wrong location*@
@* ^^^^^^^^^ *@
<p>Explicit expression: @(RaiseHere())</p> @* Noncompliant *@
@* ^^^^^^^^^ *@

<p>Implicit expression: @RaiseHere()</p> @* Noncompliant, wrong location *@
@* ^^^^^^^^^ *@
<p>Implicit expression: @RaiseHere()</p> @* Noncompliant *@
@* ^^^^^^^^^ *@

<p>Multi-statement block: @{ var result = RaiseHere(); }</p>
@* ^^^^^^^^^ *@
Expand All @@ -16,16 +16,16 @@
<p>Code blocks: @{ RaiseHere(); }</p>
@* ^^^^^^^^^ *@

<p>Lambda expression: @((Func<int>)(() => RaiseHere()))()</p> @* Noncompliant, wrong location *@
@* ^^^^^^^^^ *@
<p>Lambda expression: @((Func<int>)(() => RaiseHere()))()</p> @* Noncompliant *@
@* ^^^^^^^^^ *@

<p>Nested multi-statement block: @{ var result2 = RaiseHere(); var result3 = RaiseHere(); }</p>
@* ^^^^^^^^^ *@
@* ^^^^^^^^^@-1 *@

<p>Nested control structures: @if(RaiseHere() == 42) { <text>@(RaiseHere() + 1)</text> }</p> @* second issue has wrong location *@
<p>Nested control structures: @if(RaiseHere() == 42) { <text>@(RaiseHere() + 1)</text> }</p>
@* ^^^^^^^^^ *@
@* ^^^^^^^^^@-1 *@
@* ^^^^^^^^^@-1 *@

@code
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ namespace SonarAnalyzer.TestFramework.Common;
public static class SourceGeneratorProvider
{
private static readonly string RazorSourceGeneratorPath =
Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll");

public static AnalyzerFileReference[] SourceGenerators { get; } =
[
new(RazorSourceGeneratorPath, new AssemblyLoader())
];

public static string LatestSdkFolder()
{
var objectAssembly = typeof(object).Assembly;
var objectAssemblyDirectory = Directory.GetParent(objectAssembly.Location); // C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.4
var dotnetDirectory = objectAssemblyDirectory.Parent.Parent.Parent; // C:\Program Files\dotnet
var sdkDirectory = Path.Combine(dotnetDirectory.FullName, "sdk"); // C:\Program Files\dotnet\sdk

if (!Directory.Exists(sdkDirectory))
{
throw new NotSupportedException($"The directory '{sdkDirectory}' does not exist. " +
$"This may be because you are not using .NET Core. " +
$"Please note that Razor analysis is only supported when using .NET Core.");
}
return Directory.GetDirectories(sdkDirectory, $"{objectAssembly.GetName().Version.Major}.*")
.OrderBy(x => Version.Parse(new DirectoryInfo(x).Name))
.Last();
}

private sealed class AssemblyLoader : IAnalyzerAssemblyLoader
{
public void AddDependencyLocation(string fullPath) { }
Expand Down
Binary file not shown.
Loading
Loading