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

Fix MA0051.skip_local_functions when counting statements #580

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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ internal static int CountStatements(SyntaxNodeAnalysisContext context, SyntaxNod

bool ShouldDescendIntoChildren(SyntaxNode node)
{
if (!skipLocalFunctions && node is LocalFunctionStatementSyntax)
if (skipLocalFunctions && node.IsKind(SyntaxKind.LocalFunctionStatement))
return false;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class Test
{
void [||]Method()
{
var a = 0;var b = 0;var c = 0;
var a = 0;var b = 0;
void A(){var c = 0;}
}
}";
await CreateProjectBuilder()
Expand Down Expand Up @@ -135,6 +136,34 @@ await CreateProjectBuilder()
.AddAnalyzerConfiguration("MA0051.skip_local_functions", "true")
.ValidateAsync();
}

[Fact]
public async Task ValidMethod_Statements_SkipLocalFunction()
{
const string SourceCode = """
public class Test
{
void Method()
{
var a = 0;
var b = 0;

void A()
{
_ = "";

void B() { }
}
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.AddAnalyzerConfiguration("MA0051.maximum_lines_per_method", "-1")
.AddAnalyzerConfiguration("MA0051.maximum_statements_per_method", "2")
.AddAnalyzerConfiguration("MA0051.skip_local_functions", "true")
.ValidateAsync();
}

[Fact]
public void CountStatement_ForLoop()
Expand Down Expand Up @@ -162,7 +191,7 @@ void B()
";

var count = CountStatements(SourceCode);
Assert.Equal(1, count);
Assert.Equal(2, count);
}

[Fact]
Expand Down