Skip to content

Commit 5736897

Browse files
authored
SONARPY-2074: Fix count of line of codes for single line notebooks (#1934)
1 parent 4c259cc commit 5736897

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

python-frontend/src/main/java/org/sonar/plugins/python/api/tree/Token.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public interface Token extends Tree {
3434

3535
TokenType type();
3636

37-
int physicalLine();
37+
int pythonLine();
3838

39-
int physicalColumn();
39+
int pythonColumn();
4040

4141
int includedEscapeChars();
4242

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static Set<Integer> tokenLineNumbers(Token token) {
148148
if (!token.type().equals(PythonTokenType.DEDENT) && !token.type().equals(PythonTokenType.INDENT) && !token.type().equals(PythonTokenType.NEWLINE)) {
149149
// Handle all the lines of the token
150150
String[] tokenLines = token.value().split("\n", -1);
151-
int tokenLine = token.line();
151+
int tokenLine = token.pythonLine();
152152
for (int line = tokenLine; line < tokenLine + tokenLines.length; line++) {
153153
lines.add(line);
154154
}

python-frontend/src/main/java/org/sonar/python/tree/TokenImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ public String value() {
5959

6060
@Override
6161
public int line() {
62-
return line != null ? line : physicalLine();
62+
return line != null ? line : pythonLine();
6363
}
6464

6565
@Override
6666
public int column() {
67-
return column != null ? column : physicalColumn();
67+
return column != null ? column : pythonColumn();
6868
}
6969

7070
@Override
71-
public int physicalLine() {
71+
public int pythonLine() {
7272
return token.getLine();
7373
}
7474

@@ -78,7 +78,7 @@ public int includedEscapeChars() {
7878
}
7979

8080
@Override
81-
public int physicalColumn() {
81+
public int pythonColumn() {
8282
return token.getColumn();
8383
}
8484

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.python;
2121

2222
import java.io.File;
23+
import java.util.Map;
2324
import org.junit.jupiter.api.Test;
2425
import org.sonar.python.metrics.FileLinesVisitor;
2526

@@ -67,6 +68,23 @@ void empty_file() {
6768
assertThat(visitor.getExecutableLines()).isEmpty();
6869
}
6970

71+
@Test
72+
void notebook_locs_single_line_file() {
73+
FileLinesVisitor visitor = new FileLinesVisitor(true);
74+
String content = """
75+
a = 2
76+
def foo():
77+
return 3
78+
""";
79+
var locations = Map.of(1, new IPythonLocation(1, 383, Map.of(-1, 0)),
80+
2, new IPythonLocation(1, 390, Map.of(-1, 0)),
81+
3, new IPythonLocation(1, 402, Map.of(-1, 0)),
82+
4, new IPythonLocation(1, 402, Map.of(-1, 0)));
83+
TestPythonVisitorRunner.scanNotebookFile(new File(BASE_DIR, "notebook_locs_single_line.ipynb"), locations, content, visitor);
84+
assertThat(visitor.getExecutableLines()).isEmpty();
85+
assertThat(visitor.getLinesOfCode()).hasSize(3);
86+
}
87+
7088
@Test
7189
void notebook_locs() {
7290
FileLinesVisitor visitor = new FileLinesVisitor(true);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-18T13:57:08.798932Z","iopub.status.busy":"2022-04-18T13:57:08.798479Z","iopub.status.idle":"2022-04-18T13:57:10.082993Z","shell.execute_reply":"2022-04-18T13:57:10.081937Z","shell.execute_reply.started":"2022-04-18T13:57:08.798842Z"},"trusted":true},"outputs":[],"source":"a = 2\ndef foo():\n return 3"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.4"}},"nbformat":4,"nbformat_minor":4}
2+

0 commit comments

Comments
 (0)