Skip to content

Commit 520b71a

Browse files
committed
SONARPY-2027 IPynbParser doesn't create new lines when parsing multiline strings code cells (#1878)
1 parent 5102602 commit 520b71a

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
public class IpynbNotebookParser {
3232

33-
public static final String SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER = "\n#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER\n";
33+
public static final String SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER = "#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER\n";
3434

3535
public static GeneratedIPythonFile parseNotebook(PythonInputFile inputFile) {
3636
try {
@@ -96,6 +96,7 @@ private boolean parseSourceArray(JsonParser jParser, JsonToken jsonToken) throws
9696
var countEscapedChar = countEscapeCharacters(sourceLine, new LinkedHashMap<>(), tokenLocation.getColumnNr());
9797
addLineToSource(sourceLine, tokenLocation, countEscapedChar);
9898
}
99+
aggregatedSource.append("\n");
99100
// Account for the last cell delimiter
100101
addDelimiterToSource();
101102
return true;
@@ -115,6 +116,7 @@ private boolean parseSourceMultilineString(JsonParser jParser, JsonToken jsonTok
115116
var currentCount = countEscapedChar.get(-1);
116117
addLineToSource(line, new IPythonLocation(tokenLocation.getLineNr(),
117118
tokenLocation.getColumnNr() + previousLen + previousExtraChars + 1, countEscapedChar));
119+
aggregatedSource.append("\n");
118120
previousLen = line.length() + 2;
119121
previousExtraChars = currentCount;
120122
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void testParseNotebook() throws IOException {
4040
var result = IpynbNotebookParser.parseNotebook(inputFile);
4141

4242
assertThat(result.locationMap().keySet()).hasSize(20);
43+
assertThat(result.contents()).hasLineCount(27);
4344
assertThat(StringUtils.countMatches(result.contents(), IpynbNotebookParser.SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER))
4445
.isEqualTo(7);
4546
assertThat(result.locationMap()).extracting(map -> map.get(17)).isEqualTo(new IpynbNotebookParser.IPythonLocation(64, 27, Map.of(6, 21, 20, 37, -1, 3)));

0 commit comments

Comments
 (0)