Skip to content

Commit df58518

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2058: Fix offset computation on single line JSON files (#1907)
1 parent 244dd45 commit df58518

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

sonar-python-plugin/src/main/java/org/sonar/plugins/python/IpynbNotebookParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ private boolean parseSourceMultilineString(JsonParser jParser, JsonToken jsonTok
169169
addLineToSource(line, new IPythonLocation(tokenLocation.getLineNr(),
170170
tokenLocation.getColumnNr() + previousLen + previousExtraChars, countEscapedChar));
171171
aggregatedSource.append("\n");
172-
previousLen = line.length() + 2;
173-
previousExtraChars = currentCount;
172+
previousLen = previousLen + line.length() + 2;
173+
previousExtraChars = previousExtraChars + currentCount;
174174
}
175175
// Account for the last cell delimiter
176176
addDelimiterToSource(tokenLocation);

sonar-python-plugin/src/test/java/org/sonar/plugins/python/IpynbNotebookParserTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void testParseNotebook() throws IOException {
5454
//"source": "#Some code\nprint(\"hello world\\n\")",
5555
assertThat(result.locationMap()).extracting(map -> map.get(16)).isEqualTo(new IPythonLocation(64, 14, Map.of(-1, 0)));
5656
assertThat(result.locationMap()).extracting(map -> map.get(17)).isEqualTo(new IPythonLocation(64, 26, Map.of(6, 33, 18, 46, 20, 49, -1, 3)));
57-
5857
//"source": "print(\"My\\ntext\")\nprint(\"Something else\\n\")"
5958
assertThat(result.locationMap()).extracting(map -> map.get(22)).isEqualTo(new IPythonLocation(83, 14, Map.of(6, 21, 9, 25, 15, 32, -1, 3)));
6059
assertThat(result.locationMap()).extracting(map -> map.get(23)).isEqualTo(new IPythonLocation(83, 36, Map.of(6, 43, 21, 59, 23, 62, -1, 3)));
@@ -127,4 +126,18 @@ void testParseNotebookWithExtraLineEndInArray() throws IOException {
127126
assertThat(result.contents()).hasLineCount(3);
128127
}
129128

129+
@Test
130+
void testParseNotebookSingleLine() throws IOException {
131+
var inputFile = createInputFile(baseDir, "notebook_single_line.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
132+
133+
var resultOptional = IpynbNotebookParser.parseNotebook(inputFile);
134+
135+
assertThat(resultOptional).isPresent();
136+
137+
var result = resultOptional.get();
138+
assertThat(result.locationMap()).hasSize(5);
139+
assertThat(result.contents()).hasLineCount(5);
140+
// position of variable t
141+
assertThat(result.locationMap().get(4).column()).isEqualTo(451);
142+
}
130143
}
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":"# Code you have previously used to load data\nimport pandas as pd\n\nt = '../input/home-data-for-ml-course/train.csv'"}],"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)