Skip to content

Commit c655375

Browse files
authored
SONARPY-2196 fix off-by-one error (#2061)
1 parent 4656dbc commit c655375

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,15 @@ private static NotebookParsingData parseSourceMultilineString(int startLine, Jso
202202
JsonLocation tokenLocation = jParser.currentTokenLocation();
203203
var previousLen = 0;
204204
var previousExtraChars = 0;
205-
var isFirstLine = true;
206205

207206
for (String line : sourceLine.lines().toList()) {
208207
var countEscapedChar = countEscapeCharacters(line);
209208
var currentCount = countEscapedChar.stream().mapToInt(EscapeCharPositionInfo::numberOfExtraChars).sum();
210209
cellData.addLineToSource(line, new IPythonLocation(tokenLocation.getLineNr(),
211210
tokenLocation.getColumnNr() + previousLen + previousExtraChars, countEscapedChar, true));
212211
cellData.appendToSource("\n");
213-
previousLen = previousLen + line.length() + 2;
214-
previousExtraChars = previousExtraChars + currentCount;
215-
if (isFirstLine) {
216-
isFirstLine = false;
217-
previousLen += 1;
218-
}
212+
previousLen += line.length() + 2;
213+
previousExtraChars += currentCount;
219214
}
220215
// Account for the last cell delimiter
221216
cellData.addDelimiterToSource(SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER + "\n", tokenLocation.getLineNr(), tokenLocation.getColumnNr());

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ void testParseNotebook() throws IOException {
5656
mapToColumnMappingList(Map.of(10, 1, 19, 1))));
5757
//"source": "#Some code\nprint(\"hello world\\n\")",
5858
assertThat(result.locationMap()).extracting(map -> map.get(16)).isEqualTo(new IPythonLocation(64, 14, List.of(), true));
59-
assertThat(result.locationMap()).extracting(map -> map.get(17)).isEqualTo(new IPythonLocation(64, 27, mapToColumnMappingList(Map.of(6
59+
assertThat(result.locationMap()).extracting(map -> map.get(17)).isEqualTo(new IPythonLocation(64, 26, mapToColumnMappingList(Map.of(6
6060
, 1, 18, 1, 20, 1)), true));
6161
//"source": "print(\"My\\ntext\")\nprint(\"Something else\\n\")"
6262
assertThat(result.locationMap()).extracting(map -> map.get(22)).isEqualTo(new IPythonLocation(83, 14, mapToColumnMappingList(Map.of(6
6363
, 1, 9, 1, 15, 1)), true));
64-
assertThat(result.locationMap()).extracting(map -> map.get(23)).isEqualTo(new IPythonLocation(83, 37, mapToColumnMappingList(Map.of(6
64+
assertThat(result.locationMap()).extracting(map -> map.get(23)).isEqualTo(new IPythonLocation(83, 36, mapToColumnMappingList(Map.of(6
6565
, 1, 21, 1, 23, 1)), true));
6666

6767
//"source": "a = \"A bunch of characters \\n \\f \\r \\ \"\nb = None"
6868
assertThat(result.locationMap()).extracting(map -> map.get(25))
6969
.isEqualTo(new IPythonLocation(90, 14, mapToColumnMappingList(Map.of(4, 1, 27, 1, 30, 1, 33, 1, 36, 1, 39, 1)), true));
70-
assertThat(result.locationMap()).extracting(map -> map.get(26)).isEqualTo(new IPythonLocation(90, 63, List.of(), true));
70+
assertThat(result.locationMap()).extracting(map -> map.get(26)).isEqualTo(new IPythonLocation(90, 62, List.of(), true));
7171
// last line with the cell delimiter which contains the EOF token
7272
assertThat(result.locationMap()).extracting(map -> map.get(27)).isEqualTo(new IPythonLocation(90, 14, List.of()));
7373
}
@@ -173,15 +173,15 @@ void testParseNotebookSingleLine() throws IOException {
173173
assertThat(result.locationMap()).hasSize(9);
174174
assertThat(result.contents()).hasLineCount(9);
175175
// position of variable t
176-
assertThat(result.locationMap().get(4).column()).isEqualTo(452);
176+
assertThat(result.locationMap().get(4).column()).isEqualTo(451);
177177

178178
// First and second line
179179
assertThat(result.locationMap()).containsEntry(1, new IPythonLocation(1, 382, List.of(), true));
180-
assertThat(result.locationMap()).containsEntry(2, new IPythonLocation(1, 429, List.of(), true));
180+
assertThat(result.locationMap()).containsEntry(2, new IPythonLocation(1, 428, List.of(), true));
181181

182182
assertThat(result.locationMap()).containsEntry(6, new IPythonLocation(1, 559, mapToColumnMappingList(Map.of(0, 1, 1, 1, 2, 1)), true));
183-
assertThat(result.locationMap()).containsEntry(7, new IPythonLocation(1, 610, List.of(), true));
184-
assertThat(result.locationMap()).containsEntry(8, new IPythonLocation(1, 637, mapToColumnMappingList(Map.of(1, 1, 2, 1, 0, 1)), true));
183+
assertThat(result.locationMap()).containsEntry(7, new IPythonLocation(1, 609, List.of(), true));
184+
assertThat(result.locationMap()).containsEntry(8, new IPythonLocation(1, 636, mapToColumnMappingList(Map.of(1, 1, 2, 1, 0, 1)), true));
185185
}
186186

187187
@Test

0 commit comments

Comments
 (0)