Skip to content

Commit adf1bf9

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2042: Feed IPythonTreeMaker with GeneratedIPythonFile data (#1892)
1 parent 556b576 commit adf1bf9

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class IPythonTreeMaker extends PythonTreeMaker {
4848
private Map<Integer, IPythonLocation> offsetMap;
4949

5050
public IPythonTreeMaker() {
51-
this(null);
51+
this(Map.of());
5252
}
53-
public IPythonTreeMaker(@Nullable Map<Integer, IPythonLocation> offsetMap) {
53+
public IPythonTreeMaker(Map<Integer, IPythonLocation> offsetMap) {
5454
this.offsetMap = offsetMap;
5555
}
5656

@@ -59,7 +59,7 @@ protected Token toPyToken(@Nullable com.sonar.sslr.api.Token token) {
5959
if (token == null) {
6060
return null;
6161
}
62-
if (this.offsetMap != null) {
62+
if (this.offsetMap != null && !this.offsetMap.isEmpty()) {
6363
return TokenEnricher.enrichToken(token, offsetMap);
6464
}
6565
return new TokenImpl(token);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ 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
}
305+
306306
@Test
307307
void toPyTokenShouldReturnNull(){
308308
//when the token is null

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.sonar.plugins.python.api.tree.FileInput;
6262
import org.sonar.plugins.python.cpd.PythonCpdAnalyzer;
6363
import org.sonar.plugins.python.indexer.PythonIndexer;
64+
import org.sonar.python.IPythonLocation;
6465
import org.sonar.python.SubscriptionVisitor;
6566
import org.sonar.python.metrics.FileLinesVisitor;
6667
import org.sonar.python.metrics.FileMetrics;
@@ -150,7 +151,14 @@ protected void scanFile(PythonInputFile inputFile) throws IOException {
150151
}
151152

152153
private static PythonTreeMaker getTreeMaker(PythonInputFile inputFile) {
153-
return Python.KEY.equals(inputFile.wrappedFile().language()) ? new PythonTreeMaker() : new IPythonTreeMaker();
154+
return Python.KEY.equals(inputFile.wrappedFile().language()) ? new PythonTreeMaker() : new IPythonTreeMaker(getOffsetLocations(inputFile));
155+
}
156+
157+
private static Map<Integer, IPythonLocation> getOffsetLocations(PythonInputFile inputFile) {
158+
if(inputFile.kind() == PythonInputFile.Kind.IPYTHON){
159+
return ((GeneratedIPythonFile) inputFile).locationMap();
160+
}
161+
return Map.of();
154162
}
155163

156164
@Override

0 commit comments

Comments
 (0)