Skip to content

Commit 6e0631e

Browse files
committed
SONARPY-2049 Remove extra line break when the last line of a source array ends with \n (#1901)
1 parent 005ac5e commit 6e0631e

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,18 @@ private boolean parseSourceArray(JsonParser jParser, JsonToken jsonToken) throws
135135
}
136136
appendNewLineAfterPreviousCellDelimiter();
137137
JsonLocation tokenLocation = jParser.currentTokenLocation();
138+
// In case of an empty cell, we don't add an extra line
139+
var lastSourceLine = "\n";
138140
while (jParser.nextToken() != JsonToken.END_ARRAY) {
139141
String sourceLine = jParser.getValueAsString();
140142
tokenLocation = jParser.currentTokenLocation();
141143
var countEscapedChar = countEscapeCharacters(sourceLine, new LinkedHashMap<>(), tokenLocation.getColumnNr());
142144
addLineToSource(sourceLine, tokenLocation, countEscapedChar);
145+
lastSourceLine = sourceLine;
146+
}
147+
if (!lastSourceLine.endsWith("\n")) {
148+
aggregatedSource.append("\n");
143149
}
144-
aggregatedSource.append("\n");
145150
// Account for the last cell delimiter
146151
addDelimiterToSource(tokenLocation);
147152
lastPythonLine = aggregatedSourceLine;

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
@@ -78,7 +78,7 @@ void testParseNotebookWithEmptyLines() throws IOException {
7878
var result = resultOptional.get();
7979

8080
assertThat(result.locationMap().keySet()).hasSize(4);
81-
assertThat(result.contents()).hasLineCount(5);
81+
assertThat(result.contents()).hasLineCount(4);
8282
assertThat(StringUtils.countMatches(result.contents(), IpynbNotebookParser.SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER))
8383
.isEqualTo(1);
8484
assertThat(result.locationMap()).extracting(map -> map.get(3)).isEqualTo(new IPythonLocation(11, 5, Map.of(-1, 0)));
@@ -114,4 +114,17 @@ void testParseNotebookWithNoLanguage() {
114114
assertThat(resultOptional).isPresent();
115115
}
116116

117+
@Test
118+
void testParseNotebookWithExtraLineEndInArray() throws IOException {
119+
var inputFile = createInputFile(baseDir, "notebook_extra_line.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
120+
121+
var resultOptional = IpynbNotebookParser.parseNotebook(inputFile);
122+
123+
assertThat(resultOptional).isPresent();
124+
125+
var result = resultOptional.get();
126+
assertThat(result.locationMap()).hasSize(3);
127+
assertThat(result.contents()).hasLineCount(3);
128+
}
129+
117130
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Files already downloaded and verified\n",
13+
"Files already downloaded and verified\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"#correct += (predicted == y).sum().item()\n",
19+
"#print(f'Epoch {epoch}: {correct / total}')\n"
20+
],
21+
"id": "6cbd92ef6319ca9a"
22+
}
23+
],
24+
"metadata": {
25+
"kernelspec": {
26+
"display_name": "Python 3",
27+
"language": "python",
28+
"name": "python3"
29+
},
30+
"language_info": {
31+
"codemirror_mode": {
32+
"name": "ipython",
33+
"version": 2
34+
},
35+
"file_extension": ".py",
36+
"mimetype": "text/x-python",
37+
"name": "python",
38+
"nbconvert_exporter": "python",
39+
"pygments_lexer": "ipython2",
40+
"version": "2.7.6"
41+
}
42+
},
43+
"nbformat": 4,
44+
"nbformat_minor": 5
45+
}

0 commit comments

Comments
 (0)