|
5 | 5 | * @id py/alert-suppression
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import python |
| 8 | +private import codeql.suppression.AlertSuppression as AS |
| 9 | +private import semmle.python.Comment as P |
9 | 10 |
|
10 |
| -/** |
11 |
| - * An alert suppression comment. |
12 |
| - */ |
13 |
| -abstract class SuppressionComment extends Comment { |
14 |
| - /** Gets the scope of this suppression. */ |
15 |
| - abstract SuppressionScope getScope(); |
16 |
| - |
17 |
| - /** Gets the suppression annotation in this comment. */ |
18 |
| - abstract string getAnnotation(); |
19 |
| - |
20 |
| - /** |
21 |
| - * Holds if this comment applies to the range from column `startcolumn` of line `startline` |
22 |
| - * to column `endcolumn` of line `endline` in file `filepath`. |
23 |
| - */ |
24 |
| - abstract predicate covers( |
25 |
| - string filepath, int startline, int startcolumn, int endline, int endcolumn |
26 |
| - ); |
27 |
| -} |
28 |
| - |
29 |
| -/** |
30 |
| - * An alert comment that applies to a single line |
31 |
| - */ |
32 |
| -abstract class LineSuppressionComment extends SuppressionComment { |
33 |
| - LineSuppressionComment() { |
34 |
| - exists(string filepath, int l | |
35 |
| - this.getLocation().hasLocationInfo(filepath, l, _, _, _) and |
36 |
| - any(AstNode a).getLocation().hasLocationInfo(filepath, l, _, _, _) |
37 |
| - ) |
38 |
| - } |
39 |
| - |
40 |
| - /** Gets the scope of this suppression. */ |
41 |
| - override SuppressionScope getScope() { result = this } |
42 |
| - |
43 |
| - override predicate covers( |
| 11 | +class SingleLineComment instanceof P::Comment { |
| 12 | + predicate hasLocationInfo( |
44 | 13 | string filepath, int startline, int startcolumn, int endline, int endcolumn
|
45 | 14 | ) {
|
46 |
| - this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and |
47 |
| - startcolumn = 1 |
| 15 | + super.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
48 | 16 | }
|
49 |
| -} |
50 |
| - |
51 |
| -/** |
52 |
| - * An lgtm suppression comment. |
53 |
| - */ |
54 |
| -class LgtmSuppressionComment extends LineSuppressionComment { |
55 |
| - string annotation; |
56 | 17 |
|
57 |
| - LgtmSuppressionComment() { |
58 |
| - exists(string all | all = this.getContents() | |
59 |
| - // match `lgtm[...]` anywhere in the comment |
60 |
| - annotation = all.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _) |
61 |
| - or |
62 |
| - // match `lgtm` at the start of the comment and after semicolon |
63 |
| - annotation = all.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim() |
64 |
| - ) |
65 |
| - } |
| 18 | + string getText() { result = super.getContents() } |
66 | 19 |
|
67 |
| - /** Gets the suppression annotation in this comment. */ |
68 |
| - override string getAnnotation() { result = annotation } |
| 20 | + string toString() { result = super.toString() } |
69 | 21 | }
|
70 | 22 |
|
| 23 | +import AS::Make<SingleLineComment> |
| 24 | + |
71 | 25 | /**
|
72 | 26 | * A noqa suppression comment. Both pylint and pyflakes respect this, so lgtm ought to too.
|
73 | 27 | */
|
74 |
| -class NoqaSuppressionComment extends LineSuppressionComment { |
75 |
| - NoqaSuppressionComment() { this.getContents().toLowerCase().regexpMatch("\\s*noqa\\s*([^:].*)?") } |
| 28 | +class NoqaSuppressionComment extends SuppressionComment instanceof SingleLineComment { |
| 29 | + NoqaSuppressionComment() { |
| 30 | + SingleLineComment.super.getText().regexpMatch("(?i)\\s*noqa\\s*([^:].*)?") |
| 31 | + } |
76 | 32 |
|
77 | 33 | override string getAnnotation() { result = "lgtm" }
|
78 |
| -} |
79 | 34 |
|
80 |
| -/** |
81 |
| - * The scope of an alert suppression comment. |
82 |
| - */ |
83 |
| -class SuppressionScope extends @py_comment instanceof SuppressionComment { |
84 |
| - /** |
85 |
| - * Holds if this element is at the specified location. |
86 |
| - * The location spans column `startcolumn` of line `startline` to |
87 |
| - * column `endcolumn` of line `endline` in file `filepath`. |
88 |
| - * For more information, see |
89 |
| - * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
90 |
| - */ |
91 |
| - predicate hasLocationInfo( |
| 35 | + override predicate covers( |
92 | 36 | string filepath, int startline, int startcolumn, int endline, int endcolumn
|
93 | 37 | ) {
|
94 |
| - super.covers(filepath, startline, startcolumn, endline, endcolumn) |
| 38 | + this.hasLocationInfo(filepath, startline, _, endline, endcolumn) and |
| 39 | + startcolumn = 1 |
95 | 40 | }
|
96 |
| - |
97 |
| - /** Gets a textual representation of this element. */ |
98 |
| - string toString() { result = "suppression range" } |
99 | 41 | }
|
100 |
| - |
101 |
| -from SuppressionComment c |
102 |
| -select c, // suppression comment |
103 |
| - c.getContents(), // text of suppression comment (excluding delimiters) |
104 |
| - c.getAnnotation(), // text of suppression annotation |
105 |
| - c.getScope() // scope of suppression |
0 commit comments