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