Skip to content

Commit 005ac5e

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2046: Fix missing enrichment of tokens (#1898)
1 parent ce6ff92 commit 005ac5e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ protected Token toPyToken(@Nullable com.sonar.sslr.api.Token token) {
135135
return new TokenImpl(token);
136136
}
137137

138-
private static List<Token> toPyToken(List<com.sonar.sslr.api.Token> tokens) {
139-
return tokens.stream().map(TokenImpl::new).collect(Collectors.toList());
138+
protected List<Token> toPyToken(List<com.sonar.sslr.api.Token> tokens) {
139+
return tokens.stream().map(this::toPyToken).toList();
140140
}
141141

142142
public void setParents(Tree root) {
@@ -1319,7 +1319,7 @@ private Expression repr(AstNode astNode) {
13191319
return new ReprExpressionImpl(openingBacktick, expressionListTree, closingBacktick);
13201320
}
13211321

1322-
private static List<Token> punctuators(AstNode astNode, PythonPunctuator punctuator) {
1322+
private List<Token> punctuators(AstNode astNode, PythonPunctuator punctuator) {
13231323
return toPyToken(astNode.getChildren(punctuator).stream().map(AstNode::getToken).toList());
13241324
}
13251325

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ void assignmentRhs() {
291291

292292
@Test
293293
void enrichTokens() {
294-
var offsetMap = Map.of(1, new IPythonLocation(7, 10, Map.of(4, 15, 8, 20)));
295-
var statementList = parseIPython(
294+
var offsetMap = Map.of(1, new IPythonLocation(7, 10, Map.of(4, 15, 8, 20, -1, 2)));
295+
var statementList = parseIPython(
296296
"a = \"123\"", new IPythonTreeMaker(offsetMap)::fileInput).statements();
297297
assertThat(statementList).isNotNull();
298298

@@ -301,13 +301,16 @@ void enrichTokens() {
301301
assertThat(stringLiteral).hasSize(1);
302302
assertThat(stringLiteral.get(0).firstToken().line()).isEqualTo(7);
303303
assertThat(stringLiteral.get(0).firstToken().column()).isEqualTo(14);
304-
}
305304

306-
@Test
307-
void toPyTokenShouldReturnNull(){
308-
//when the token is null
309-
var token = treeMaker.toPyToken(null);
310-
assertThat(token).isNull();
305+
offsetMap = Map.of(1, new IPythonLocation(7, 10, Map.of(-1,0)), 2, new IPythonLocation(8, 10, Map.of(-1, 0)));
306+
statementList = parseIPython(
307+
"def foo(): # comment \n pass", new IPythonTreeMaker(offsetMap)::fileInput).statements();
308+
assertThat(statementList).isNotNull();
309+
var passStatement = findChildrenWithKind(statementList, Tree.Kind.PASS_STMT)
310+
.stream().map(PassStatementImpl.class::cast).toList();
311+
assertThat(passStatement).hasSize(1);
312+
assertThat(passStatement.get(0).firstToken().line()).isEqualTo(8);
313+
assertThat(passStatement.get(0).firstToken().column()).isEqualTo(14);
311314
}
312315

313316
private static void assertLineMagicStatement(Statement statement) {

0 commit comments

Comments
 (0)