|
5 | 5 | * @id go/alert-suppression |
6 | 6 | */ |
7 | 7 |
|
8 | | -import go |
| 8 | +private import codeql.suppression.AlertSuppression as AS |
| 9 | +private import semmle.go.Comments as G |
9 | 10 |
|
10 | | -/** |
11 | | - * An alert suppression comment. |
12 | | - */ |
13 | | -class SuppressionComment extends Locatable { |
14 | | - string text; |
15 | | - string annotation; |
16 | | - |
17 | | - SuppressionComment() { |
18 | | - text = this.(Comment).getText() and |
| 11 | +class SingleLineComment extends G::Comment { |
| 12 | + SingleLineComment() { |
19 | 13 | // suppression comments must be single-line |
20 | | - not text.matches("%\n%") and |
21 | | - ( |
22 | | - // match `lgtm[...]` anywhere in the comment |
23 | | - annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _) |
24 | | - or |
25 | | - // match `lgtm` at the start of the comment and after semicolon |
26 | | - annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim() |
27 | | - ) |
28 | | - } |
29 | | - |
30 | | - /** Gets the text of this suppression comment, not including delimiters. */ |
31 | | - string getText() { result = text } |
32 | | - |
33 | | - /** Gets the suppression annotation in this comment. */ |
34 | | - string getAnnotation() { result = annotation } |
35 | | - |
36 | | - /** |
37 | | - * Holds if this comment applies to the range from column `startcolumn` of line `startline` |
38 | | - * to column `endcolumn` of line `endline` in file `filepath`. |
39 | | - */ |
40 | | - predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) { |
41 | | - this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and |
42 | | - startcolumn = 1 |
| 14 | + not this.getText().matches("%\n%") |
43 | 15 | } |
44 | | - |
45 | | - /** Gets the scope of this suppression. */ |
46 | | - SuppressionScope getScope() { this = result.getSuppressionComment() } |
47 | | -} |
48 | | - |
49 | | -/** |
50 | | - * The scope of an alert suppression comment. |
51 | | - */ |
52 | | -class SuppressionScope extends @locatable instanceof SuppressionComment { |
53 | | - /** Gets a suppression comment with this scope. */ |
54 | | - SuppressionComment getSuppressionComment() { result = this } |
55 | | - |
56 | | - /** |
57 | | - * Holds if this element is at the specified location. |
58 | | - * The location spans column `startcolumn` of line `startline` to |
59 | | - * column `endcolumn` of line `endline` in file `filepath`. |
60 | | - * For more information, see |
61 | | - * [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). |
62 | | - */ |
63 | | - predicate hasLocationInfo( |
64 | | - string filepath, int startline, int startcolumn, int endline, int endcolumn |
65 | | - ) { |
66 | | - super.covers(filepath, startline, startcolumn, endline, endcolumn) |
67 | | - } |
68 | | - |
69 | | - /** Gets a textual representation of this element. */ |
70 | | - string toString() { result = "suppression range" } |
71 | 16 | } |
72 | 17 |
|
73 | | -from SuppressionComment c |
74 | | -select c, // suppression comment |
75 | | - c.getText(), // text of suppression comment (excluding delimiters) |
76 | | - c.getAnnotation(), // text of suppression annotation |
77 | | - c.getScope() // scope of suppression |
| 18 | +import AS::Make<SingleLineComment> |
0 commit comments