Skip to content

Commit d7b4278

Browse files
authored
SONARPY-2014 SONARPY-2377 Repair rules reading PythonVisitorContext.pythonFile.contents() on Jupyter notebooks (#2192)
1 parent a988726 commit d7b4278

File tree

22 files changed

+331
-252
lines changed

22 files changed

+331
-252
lines changed
Lines changed: 4 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -1,247 +1,22 @@
11
{
22
"project:Keras-Tutorials-master/01_linear_regression.ipynb": [
3-
36,
4-
337
5-
],
6-
"project:Keras-Tutorials-master/03_net.ipynb": [
7-
324
8-
],
9-
"project:Keras-Tutorials-master/04_modern_net.ipynb": [
10-
340
11-
],
12-
"project:Keras-Tutorials-master/05_convolutional_net.ipynb": [
13-
168,
14-
425
15-
],
16-
"project:Keras-Tutorials-master/06_autoencoder.ipynb": [
17-
390
18-
],
19-
"project:Keras-Tutorials-master/10_save_restore_net.ipynb": [
20-
121,
21-
123,
22-
125,
23-
127,
24-
129,
25-
131,
26-
133,
27-
135,
28-
137,
29-
139,
30-
146
3+
36
314
],
325
"project:pecos/tutorials/kdd22/Session 2 Extreme Multi-label Classification with PECOS.ipynb": [
33-
10,
34-
16,
35-
23,
36-
86,
37-
88,
38-
102,
39-
105,
40-
107,
41-
112,
42-
157,
43-
231,
44-
234,
45-
235,
46-
280,
47-
286,
48-
288,
49-
309,
50-
323,
51-
325,
52-
358,
53-
379,
54-
411,
55-
444,
56-
466,
57-
470,
58-
474,
59-
475,
60-
476,
61-
477,
62-
478,
63-
479,
64-
486,
65-
496,
66-
498,
67-
531,
68-
533,
69-
537,
70-
570,
71-
583,
72-
584,
73-
585,
74-
586,
75-
599,
76-
602,
77-
612,
78-
614,
79-
617,
80-
620,
81-
675,
82-
690,
83-
691,
84-
692,
85-
693,
86-
732,
87-
799,
88-
810,
89-
824,
90-
855,
91-
961,
92-
991,
93-
1025,
94-
1057,
95-
1061,
96-
1067,
97-
1069,
98-
1163,
99-
1213,
100-
1300,
101-
1302
6+
855
1027
],
1038
"project:pecos/tutorials/kdd22/Session 3 Approximate Nearest Neighbor Search in PECOS.ipynb": [
104-
21,
105-
23,
106-
28,
107-
29,
108-
40,
109-
58,
110-
59,
111-
72,
112-
77,
113-
145,
114-
147,
115-
148,
116-
149,
117-
150,
118-
153,
119-
155,
120-
216,
121-
219,
122-
228,
123-
257,
124-
258,
125-
348,
126-
443,
127-
474,
128-
481,
129-
495,
130-
701,
131-
751,
132-
755,
133-
766
9+
443
13410
],
13511
"project:pecos/tutorials/kdd22/Session 4 Utilities in PECOS.ipynb": [
136-
10,
137-
25,
138-
35,
139-
183,
140-
261,
141-
263,
142-
273,
143-
274,
144-
409,
145-
437,
146-
462,
147-
531,
14812
581,
14913
582,
150-
592,
151-
724,
152-
791,
153-
798,
154-
801,
155-
803,
156-
808,
157-
818,
158-
895,
159-
1003,
160-
1014
14+
1003
16115
],
16216
"project:pecos/tutorials/kdd22/Session 5 eXtreme Multi-label Classification with XR-Transformer.ipynb": [
163-
12,
164-
31,
165-
103,
166-
113,
167-
117,
168-
178,
16917
195,
170-
276,
171-
277,
172-
278,
173-
320,
174-
322,
175-
323,
17618
352,
177-
428,
17819
450,
179-
472,
180-
482,
181-
483,
182-
484,
183-
485,
184-
486,
185-
576,
186-
577,
187-
579,
188-
594,
189-
595,
190-
596,
191-
597,
192-
598,
193-
599,
194-
600,
195-
601,
196-
602,
197-
647,
198-
650,
199-
671,
200-
672,
201-
673,
202-
674,
203-
675,
204-
676,
205-
706,
206-
719,
207-
770,
208-
786,
209-
787,
210-
788,
211-
789,
212-
829,
213-
892,
214-
893,
215-
894,
216-
895,
217-
967,
21820
974
219-
],
220-
"project:tensorflow/python/autograph/g3doc/pyct_tutorial.ipynb": [
221-
16,
222-
18,
223-
45,
224-
55,
225-
131,
226-
158,
227-
221,
228-
290,
229-
325,
230-
370,
231-
372,
232-
413,
233-
444
234-
],
235-
"project:tensorflow/python/autograph/g3doc/reference/_control_flow_tutorial.ipynb": [
236-
10,
237-
73,
238-
75,
239-
131,
240-
198,
241-
268,
242-
299,
243-
377,
244-
524,
245-
704
24621
]
24722
}

its/ruling/src/test/resources/expected/ipython-S113.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,35 @@
1313
],
1414
"project:Keras-Tutorials-master/05_convolutional_net.ipynb": [
1515
0
16+
],
17+
"project:Keras-Tutorials-master/06_autoencoder.ipynb": [
18+
0
19+
],
20+
"project:Keras-Tutorials-master/07_lstm.ipynb": [
21+
0
22+
],
23+
"project:Keras-Tutorials-master/09_tensorboard.ipynb": [
24+
0
25+
],
26+
"project:Keras-Tutorials-master/10_save_restore_net.ipynb": [
27+
0
28+
],
29+
"project:pecos/tutorials/kdd22/Session 2 Extreme Multi-label Classification with PECOS.ipynb": [
30+
0
31+
],
32+
"project:pecos/tutorials/kdd22/Session 3 Approximate Nearest Neighbor Search in PECOS.ipynb": [
33+
0
34+
],
35+
"project:pecos/tutorials/kdd22/Session 4 Utilities in PECOS.ipynb": [
36+
0
37+
],
38+
"project:pecos/tutorials/kdd22/Session 5 eXtreme Multi-label Classification with XR-Transformer.ipynb": [
39+
0
40+
],
41+
"project:tensorflow/python/autograph/g3doc/pyct_tutorial.ipynb": [
42+
0
43+
],
44+
"project:tensorflow/python/autograph/g3doc/reference/_control_flow_tutorial.ipynb": [
45+
0
1646
]
1747
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"project:Keras-Tutorials-master/06_autoencoder.ipynb": [
3+
403
4+
],
5+
"project:pecos/tutorials/kdd22/Session 2 Extreme Multi-label Classification with PECOS.ipynb": [
6+
75,
7+
644,
8+
665,
9+
774,
10+
847,
11+
851,
12+
852,
13+
853,
14+
856,
15+
866,
16+
867,
17+
868,
18+
873,
19+
877,
20+
879,
21+
882,
22+
1316,
23+
1354
24+
],
25+
"project:pecos/tutorials/kdd22/Session 3 Approximate Nearest Neighbor Search in PECOS.ipynb": [
26+
342,
27+
346,
28+
350
29+
],
30+
"project:pecos/tutorials/kdd22/Session 4 Utilities in PECOS.ipynb": [
31+
108,
32+
116,
33+
122,
34+
375,
35+
628,
36+
635,
37+
672,
38+
712,
39+
1074
40+
],
41+
"project:pecos/tutorials/kdd22/Session 5 eXtreme Multi-label Classification with XR-Transformer.ipynb": [
42+
70,
43+
197
44+
],
45+
"project:tensorflow/python/autograph/g3doc/reference/_control_flow_tutorial.ipynb": [
46+
677
47+
]
48+
}

python-checks/src/main/java/org/sonar/python/checks/ParsingErrorCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ParsingErrorCheck implements PythonCheck {
2929
public void scanFile(PythonVisitorContext context) {
3030
RecognitionException parsingException = context.parsingException();
3131
if (parsingException != null) {
32-
context.addIssue(new PreciseIssue(this, IssueLocation.atLineLevel(parsingException.getMessage(), parsingException.getLine())));
32+
context.addIssue(new PreciseIssue(this, IssueLocation.atLineLevel(parsingException.getMessage(), parsingException.getLine(), context.pythonFile())));
3333
}
3434
}
3535

python-checks/src/main/java/org/sonar/python/checks/TrailingCommentCheck.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void initialize(Context context) {
6161
String comment = commentToken.value();
6262
if (!pattern.matcher(comment).matches()) {
6363
var issue = ctx.addIssue(commentToken, MESSAGE);
64-
String line = getLines(ctx).get(commentToken.line() - 1);
64+
String line = getLines(ctx).get(commentToken.pythonLine() - 1);
6565
addQuickFix(issue, commentToken, line);
6666
}
6767
}
@@ -72,10 +72,10 @@ public void initialize(Context context) {
7272

7373
private static void addQuickFix(PreciseIssue issue, Token commentToken, String line) {
7474
String indent = calculateIndent(line);
75-
PythonTextEdit insertComment = TextEditUtils.insertAtPosition(commentToken.line(), 0, indent + commentToken.value() + "\n");
75+
PythonTextEdit insertComment = TextEditUtils.insertAtPosition(commentToken.pythonLine(), 0, indent + commentToken.value() + "\n");
7676

7777
int startColumnRemove = calculateStartColumnToRemove(commentToken, line);
78-
PythonTextEdit removeTrailingComment = TextEditUtils.removeRange(commentToken.line(), startColumnRemove, commentToken.line(), line.length());
78+
PythonTextEdit removeTrailingComment = TextEditUtils.removeRange(commentToken.pythonLine(), startColumnRemove, commentToken.pythonLine(), line.length());
7979

8080
PythonQuickFix fix = PythonQuickFix.newQuickFix(MESSAGE, removeTrailingComment, insertComment);
8181
issue.addQuickFix(fix);
@@ -95,7 +95,7 @@ private List<String> getLines(SubscriptionContext ctx) {
9595
}
9696

9797
private static int calculateStartColumnToRemove(Token commentToken, String line) {
98-
return line.substring(0, commentToken.column()).stripTrailing().length();
98+
return line.substring(0, commentToken.pythonColumn()).stripTrailing().length();
9999
}
100100
}
101101

python-checks/src/main/java/org/sonar/python/checks/TrailingWhitespaceCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public void scanFile(PythonVisitorContext ctx) {
3838
Matcher matcher = TRAILING_WS.matcher(lines[i]);
3939
if (matcher.find()) {
4040
int lineNumber = i + 1;
41-
PreciseIssue issue = new PreciseIssue(this, IssueLocation.atLineLevel(MESSAGE, lineNumber));
41+
PreciseIssue issue = new PreciseIssue(this, IssueLocation.atLineLevel(MESSAGE, lineNumber, ctx.pythonFile()));
4242

4343
issue.addQuickFix(PythonQuickFix.newQuickFix("Remove trailing whitespaces")
44-
.addTextEdit(TextEditUtils.removeRange(lineNumber, matcher.start(), lineNumber, matcher.end()))
44+
.addTextEdit(TextEditUtils.removeRange(lineNumber, matcher.start(), lineNumber, matcher.end()))
4545
.build());
4646

4747
ctx.addIssue(issue);

0 commit comments

Comments
 (0)