Skip to content

Commit

Permalink
SONARPY-2074: Fix count of line of codes for single line notebooks (S…
Browse files Browse the repository at this point in the history
  • Loading branch information
joke1196 authored Aug 16, 2024
1 parent 4c259cc commit 5736897
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public interface Token extends Tree {

TokenType type();

int physicalLine();
int pythonLine();

int physicalColumn();
int pythonColumn();

int includedEscapeChars();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static Set<Integer> tokenLineNumbers(Token token) {
if (!token.type().equals(PythonTokenType.DEDENT) && !token.type().equals(PythonTokenType.INDENT) && !token.type().equals(PythonTokenType.NEWLINE)) {
// Handle all the lines of the token
String[] tokenLines = token.value().split("\n", -1);
int tokenLine = token.line();
int tokenLine = token.pythonLine();
for (int line = tokenLine; line < tokenLine + tokenLines.length; line++) {
lines.add(line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public String value() {

@Override
public int line() {
return line != null ? line : physicalLine();
return line != null ? line : pythonLine();
}

@Override
public int column() {
return column != null ? column : physicalColumn();
return column != null ? column : pythonColumn();
}

@Override
public int physicalLine() {
public int pythonLine() {
return token.getLine();
}

Expand All @@ -78,7 +78,7 @@ public int includedEscapeChars() {
}

@Override
public int physicalColumn() {
public int pythonColumn() {
return token.getColumn();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.python;

import java.io.File;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.sonar.python.metrics.FileLinesVisitor;

Expand Down Expand Up @@ -67,6 +68,23 @@ void empty_file() {
assertThat(visitor.getExecutableLines()).isEmpty();
}

@Test
void notebook_locs_single_line_file() {
FileLinesVisitor visitor = new FileLinesVisitor(true);
String content = """
a = 2
def foo():
return 3
""";
var locations = Map.of(1, new IPythonLocation(1, 383, Map.of(-1, 0)),
2, new IPythonLocation(1, 390, Map.of(-1, 0)),
3, new IPythonLocation(1, 402, Map.of(-1, 0)),
4, new IPythonLocation(1, 402, Map.of(-1, 0)));
TestPythonVisitorRunner.scanNotebookFile(new File(BASE_DIR, "notebook_locs_single_line.ipynb"), locations, content, visitor);
assertThat(visitor.getExecutableLines()).isEmpty();
assertThat(visitor.getLinesOfCode()).hasSize(3);
}

@Test
void notebook_locs() {
FileLinesVisitor visitor = new FileLinesVisitor(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"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}

0 comments on commit 5736897

Please sign in to comment.