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 supports skip_local_functions when counting lines #562

Merged
merged 1 commit into from
Jul 19, 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
12 changes: 12 additions & 0 deletions src/Meziantou.Analyzer/Rules/MethodShouldNotBeTooLongAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context, SyntaxNode? n
var location = node.GetLocation();
var lineSpan = location.GetLineSpan();
var lines = lineSpan.EndLinePosition.Line - lineSpan.StartLinePosition.Line;

if (GetSkipLocalFunctions(context))
{
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;
}
}


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 @@ -88,6 +88,54 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task TooLong_SkipLocalFunction()
{
const string SourceCode = @"
public class Test
{
void [||]Method()
{
var a = 0;
var b = 0;
var c = 0;
void A()
{
void B() { }
}
}
}";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.AddAnalyzerConfiguration("MA0051.maximum_lines_per_method", "5")
.AddAnalyzerConfiguration("MA0051.skip_local_functions", "false")
.ValidateAsync();
}

[Fact]
public async Task ValidMethod_SkipLocalFunction()
{
const string SourceCode = @"
public class Test
{
void Method()
{
var a = 0;
var b = 0;
var c = 0;
void A()
{
void B() { }
}
}
}";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.AddAnalyzerConfiguration("MA0051.maximum_lines_per_method", "5")
.AddAnalyzerConfiguration("MA0051.skip_local_functions", "true")
.ValidateAsync();
}

[Fact]
public void CountStatement_ForLoop()
{
Expand Down