Skip to content

Commit 3c72b74

Browse files
authored
SONARPY-817 Empty files should have no executable line (SonarSource#885)
1 parent 9df4170 commit 3c72b74

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

python-frontend/src/main/java/org/sonar/python/metrics/FileLinesVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ private void visitFile() {
8686
private void visitNode(SubscriptionContext ctx) {
8787
Tree tree = ctx.syntaxNode();
8888
if (tree.is(Tree.Kind.FILE_INPUT)) {
89-
statements--;
9089
handleDocString(((FileInput) tree).docstring());
90+
} else {
91+
statements++;
92+
executableLines.add(tree.firstToken().line());
9193
}
9294
if (tree.is(Tree.Kind.CLASSDEF)) {
9395
classDefs++;
@@ -96,8 +98,6 @@ private void visitNode(SubscriptionContext ctx) {
9698
if (tree.is(Tree.Kind.FUNCDEF)) {
9799
handleDocString(((FunctionDef) tree).docstring());
98100
}
99-
statements++;
100-
executableLines.add(tree.firstToken().line());
101101
}
102102

103103
private void handleDocString(@Nullable StringLiteral docstring) {

python-frontend/src/test/java/org/sonar/python/FileLinesVisitorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ public void executable_lines() {
6060
assertThat(visitor.getExecutableLines()).containsOnly(1, 2, 4, 7, 11, 13, 14, 15, 16, 18, 20, 21, 22, 23, 25, 27, 28, 29);
6161
}
6262

63+
@Test
64+
public void empty_file() {
65+
FileLinesVisitor visitor = new FileLinesVisitor();
66+
TestPythonVisitorRunner.scanFile(new File(BASE_DIR, "empty.py"), visitor);
67+
assertThat(visitor.getExecutableLines()).isEmpty();
68+
}
6369
}

python-frontend/src/test/resources/metrics/empty.py

Whitespace-only changes.

0 commit comments

Comments
 (0)