Skip to content

Commit c33f3bd

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2035: Update TokenLocation to make use of the correct location for reporting (#1886)
1 parent 4b09ae6 commit c33f3bd

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

python-frontend/src/main/java/org/sonar/plugins/python/api/tree/Token.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public interface Token extends Tree {
4040

4141
int includedEscapeChars();
4242

43+
int valueLength();
44+
4345
}

python-frontend/src/main/java/org/sonar/python/TokenLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public TokenLocation(Token token) {
4141

4242
} else {
4343
this.endLine = this.startLine;
44-
this.endLineOffset = this.startLineOffset + token.value().length();
44+
this.endLineOffset = this.startLineOffset + token.valueLength();
4545
}
4646
}
4747

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,9 @@ public Token firstToken() {
112112
public Token lastToken() {
113113
return this;
114114
}
115+
116+
@Override
117+
public int valueLength() {
118+
return value().length() + includedEscapeChars();
119+
}
115120
}

python-frontend/src/test/java/org/sonar/python/TokenLocationTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,40 @@ void test_comment() {
6565
assertOffsets(commentLocation, 1, 0, 1, 8);
6666
}
6767

68+
69+
@Test
70+
void test_escaped_chars_ipython_lexer() {
71+
var token = new TokenImpl(iPythonLex("\"1\\n3\"").get(0), 3, 10, 3);
72+
TokenLocation tokenLocation = new TokenLocation(token);
73+
assertOffsets(tokenLocation, 3, 10, 3, 19);
74+
75+
token = new TokenImpl(iPythonLex("foo").get(0), 10 , 20, 0);
76+
tokenLocation = new TokenLocation(token);
77+
assertOffsets(tokenLocation, 10, 20, 10, 23);
78+
}
79+
80+
@Test
81+
void test_multiline_ipython_lexer() {
82+
var tokens = iPythonLex("'''first line\nsecond\\t'''");
83+
var token = new TokenImpl(tokens.get(0), 3, 10, 1);
84+
TokenLocation tokenLocation = new TokenLocation(token);
85+
assertOffsets(tokenLocation, 3, 10, 4, 11);
86+
}
87+
6888
private static void assertOffsets(TokenLocation tokenLocation, int startLine, int startLineOffset, int endLine, int endLineOffset) {
6989
assertThat(tokenLocation.startLine()).as("start line").isEqualTo(startLine);
7090
assertThat(tokenLocation.startLineOffset()).as("start line offset").isEqualTo(startLineOffset);
7191
assertThat(tokenLocation.endLine()).as("end line").isEqualTo(endLine);
7292
assertThat(tokenLocation.endLineOffset()).as("end line offset").isEqualTo(endLineOffset);
7393
}
7494

95+
private List<com.sonar.sslr.api.Token> iPythonLex(String toLex) {
96+
LexerState lexerState = new LexerState();
97+
lexerState.reset();
98+
Lexer lexer = PythonLexer.create(lexerState);
99+
return lexer.lex(toLex);
100+
}
101+
75102
private List<Token> lex(String toLex) {
76103
LexerState lexerState = new LexerState();
77104
lexerState.reset();

0 commit comments

Comments
 (0)