Skip to content

Commit ada011d

Browse files
committed
SONARPY-2051 Single quotes should not count as escaped characters in TokenEnricher or IPynbNotebookParser (#1905)
1 parent 657a505 commit ada011d

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

python-frontend/src/main/java/org/sonar/python/tree/TokenEnricher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class TokenEnricher {
3030

31-
private static final Set<Character> ESCAPED_CHARS = Set.of('"', '\'', '\\', '\b', '\f', '\n', '\r', '\t');
31+
private static final Set<Character> ESCAPED_CHARS = Set.of('"', '\\', '\b', '\f', '\n', '\r', '\t');
3232

3333
private TokenEnricher() {
3434
}

python-frontend/src/test/java/org/sonar/python/tree/TokenEnricherTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,18 @@ void shouldComputeColCorrectlyForTriviaOnDifferentLine() {
157157
assertThat(trivias.get(0).token().column()).isEqualTo(300);
158158
assertThat(trivias.get(0).token().includedEscapeChars()).isZero();
159159
}
160+
161+
@Test
162+
void shouldComputeCorrectlyForSingleQuote() {
163+
var code = "a = '1'";
164+
var expectedTokens = lexer.lex(code);
165+
var escapedChars = new LinkedHashMap<Integer, Integer>();
166+
escapedChars.put(4, 305);
167+
escapedChars.put(6, 308);
168+
var tokens = TokenEnricher.enrichTokens(expectedTokens, Map.of(1, new IPythonLocation(100, 300, escapedChars)));
169+
var stringToken = tokens.get(2);
170+
assertThat(stringToken.line()).isEqualTo(100);
171+
assertThat(stringToken.column()).isEqualTo(304);
172+
assertThat(stringToken.includedEscapeChars()).isZero();
173+
}
160174
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static Map<Integer, Integer> countEscapeCharacters(String sourceLine, Ma
205205
for (int i = 1; i < sourceLine.length(); ++i) {
206206
char c = arr[i];
207207
switch (c) {
208-
case '"', '\'', '\\':
208+
case '"', '\\':
209209
numberOfExtraChars++;
210210
colMap.put(i, i + colOffSet + count + numberOfExtraChars);
211211
break;

0 commit comments

Comments
 (0)