mirror of
https://github.com/github/codeql.git
synced 2026-04-11 10:04:02 +02:00
Merge pull request #297 from github/aibaars/alert-suppression
Alert suppression and file classifier query
This commit is contained in:
82
ql/src/AlertSuppression.ql
Normal file
82
ql/src/AlertSuppression.ql
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @name Alert suppression
|
||||
* @description Generates information about alert suppressions.
|
||||
* @kind alert-suppression
|
||||
* @id rb/alert-suppression
|
||||
*/
|
||||
|
||||
import ruby
|
||||
import codeql.ruby.ast.internal.TreeSitter
|
||||
|
||||
/**
|
||||
* An alert suppression comment.
|
||||
*/
|
||||
class SuppressionComment extends Ruby::Comment {
|
||||
string annotation;
|
||||
|
||||
SuppressionComment() {
|
||||
// suppression comments must be single-line
|
||||
this.getLocation().getStartLine() = this.getLocation().getEndLine() and
|
||||
exists(string text | text = commentText(this) |
|
||||
// match `lgtm[...]` anywhere in the comment
|
||||
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
|
||||
or
|
||||
// match `lgtm` at the start of the comment and after semicolon
|
||||
annotation = text.regexpFind("(?i)(?<=^|;)\\s*lgtm(?!\\B|\\s*\\[)", _, _).trim()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the text of this suppression comment.
|
||||
*/
|
||||
string getText() { result = commentText(this) }
|
||||
|
||||
/** Gets the suppression annotation in this comment. */
|
||||
string getAnnotation() { result = annotation }
|
||||
|
||||
/**
|
||||
* Holds if this comment applies to the range from column `startcolumn` of line `startline`
|
||||
* to column `endcolumn` of line `endline` in file `filepath`.
|
||||
*/
|
||||
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
|
||||
this.getLocation().hasLocationInfo(filepath, startline, _, endline, endcolumn) and
|
||||
startcolumn = 1
|
||||
}
|
||||
|
||||
/** Gets the scope of this suppression. */
|
||||
SuppressionScope getScope() { this = result.getSuppressionComment() }
|
||||
}
|
||||
|
||||
private string commentText(Ruby::Comment comment) { result = comment.getValue().suffix(1) }
|
||||
|
||||
/**
|
||||
* The scope of an alert suppression comment.
|
||||
*/
|
||||
class SuppressionScope extends @ruby_token_comment {
|
||||
SuppressionScope() { this instanceof SuppressionComment }
|
||||
|
||||
/** Gets a suppression comment with this scope. */
|
||||
SuppressionComment getSuppressionComment() { result = this }
|
||||
|
||||
/**
|
||||
* Holds if this element is at the specified location.
|
||||
* The location spans column `startcolumn` of line `startline` to
|
||||
* column `endcolumn` of line `endline` in file `filepath`.
|
||||
* For more information, see
|
||||
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
|
||||
*/
|
||||
predicate hasLocationInfo(
|
||||
string filepath, int startline, int startcolumn, int endline, int endcolumn
|
||||
) {
|
||||
this.(SuppressionComment).covers(filepath, startline, startcolumn, endline, endcolumn)
|
||||
}
|
||||
|
||||
/** Gets a textual representation of this element. */
|
||||
string toString() { result = "suppression range" }
|
||||
}
|
||||
|
||||
from SuppressionComment c
|
||||
select c, // suppression comment
|
||||
c.getText(), // text of suppression comment (excluding delimiters)
|
||||
c.getAnnotation(), // text of suppression annotation
|
||||
c.getScope() // scope of suppression
|
||||
20
ql/src/filters/ClassifyFiles.ql
Normal file
20
ql/src/filters/ClassifyFiles.ql
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @name Classify files
|
||||
* @description This query produces a list of all files in a database
|
||||
* that are classified as generated code or test code.
|
||||
*
|
||||
* Used by LGTM.
|
||||
* @kind file-classifier
|
||||
* @id rb/file-classifier
|
||||
*/
|
||||
|
||||
import ruby
|
||||
import codeql.ruby.filters.GeneratedCode
|
||||
|
||||
predicate classify(File f, string category) {
|
||||
f instanceof GeneratedCodeFile and category = "generated"
|
||||
}
|
||||
|
||||
from File f, string category
|
||||
where classify(f, category)
|
||||
select f, category
|
||||
Reference in New Issue
Block a user