Skip to content

Commit 621a108

Browse files
committed
Ruby: use shared AlertSuppression.qll
1 parent 453045e commit 621a108

File tree

2 files changed

+10
-62
lines changed

2 files changed

+10
-62
lines changed

ruby/ql/src/AlertSuppression.ql

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,23 @@
55
* @id rb/alert-suppression
66
*/
77

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
1010

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() {
1813
// 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()
4415
}
4516

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-
*/
6617
predicate hasLocationInfo(
6718
string filepath, int startline, int startcolumn, int endline, int endcolumn
6819
) {
69-
super.covers(filepath, startline, startcolumn, endline, endcolumn)
20+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
7021
}
7122

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) }
7425
}
7526

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>

ruby/ql/src/qlpack.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ defaultSuiteFile: codeql-suites/ruby-code-scanning.qls
88
dependencies:
99
codeql/ruby-all: ${workspace}
1010
codeql/suite-helpers: ${workspace}
11+
codeql/util: ${workspace}

0 commit comments

Comments
 (0)