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

MA0051 exclude local functions's trivia when skipping local functions #581

Merged
merged 1 commit into from
Jul 27, 2023
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
31 changes: 28 additions & 3 deletions src/Meziantou.Analyzer/Rules/MethodShouldNotBeTooLongAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,37 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context, SyntaxNode? n
var localFunctions = node.DescendantNodes(node => !node.IsKind(SyntaxKind.LocalFunctionStatement)).Where(node => node.IsKind(SyntaxKind.LocalFunctionStatement));
foreach (var localFunction in localFunctions)
{
var functionLocation = localFunction.GetLocation().GetLineSpan();
lines -= functionLocation.EndLinePosition.Line - functionLocation.StartLinePosition.Line;
var firstLine = -1;
var lastLine = -1;

if (localFunction.HasLeadingTrivia)
{
firstLine = localFunction.GetLeadingTrivia()[0].GetLocation().GetLineSpan().StartLinePosition.Line;
}

if (localFunction.HasTrailingTrivia)
{
lastLine = localFunction.GetTrailingTrivia()[^1].GetLocation().GetLineSpan().EndLinePosition.Line;
}

if (firstLine < 0 || lastLine < 0)
{
var functionLocation = localFunction.GetLocation().GetLineSpan();
if (firstLine < 0)
{
firstLine = functionLocation.StartLinePosition.Line;
}

if (lastLine < 0)
{
lastLine = functionLocation.EndLinePosition.Line;
}
}

lines -= lastLine - firstLine;
}
}


if (lines > maximumLines)
{
context.ReportDiagnostic(s_rule, reportNode, $"{lines} lines; maximum allowed: {maximumLines}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ await CreateProjectBuilder()
}

[Fact]
public async Task TooLong_SkipLocalFunction()
public async Task TooLong_SkipLocalFunction_False()
{
const string SourceCode = @"
public class Test
Expand All @@ -100,6 +100,7 @@ public class Test
var a = 0;
var b = 0;
var c = 0;

void A()
{
void B() { }
Expand All @@ -114,7 +115,7 @@ await CreateProjectBuilder()
}

[Fact]
public async Task ValidMethod_SkipLocalFunction()
public async Task ValidMethod_SkipLocalFunction_True()
{
const string SourceCode = @"
public class Test
Expand All @@ -124,6 +125,7 @@ void Method()
var a = 0;
var b = 0;
var c = 0;

void A()
{
void B() { }
Expand All @@ -136,7 +138,7 @@ await CreateProjectBuilder()
.AddAnalyzerConfiguration("MA0051.skip_local_functions", "true")
.ValidateAsync();
}

[Fact]
public async Task ValidMethod_Statements_SkipLocalFunction()
{
Expand Down