mirror of
https://github.com/github/codeql.git
synced 2026-04-24 08:15:14 +02:00
Merge pull request #167 from github/alexrford/numlines
Implement FLines metrics queries
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
/** Provides classes for working with files and folders. */
|
||||
|
||||
private import codeql_ruby.ast.internal.TreeSitter
|
||||
private import codeql.Locations
|
||||
|
||||
/** A file or folder. */
|
||||
abstract class Container extends @container {
|
||||
/** Gets a file or sub-folder in this container. */
|
||||
@@ -165,4 +168,26 @@ class File extends Container, @file {
|
||||
|
||||
/** Gets the URL of this file. */
|
||||
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
|
||||
|
||||
/** Gets a token in this file. */
|
||||
private Generated::Token getAToken() { result.getLocation().getFile() = this }
|
||||
|
||||
/** Holds if `line` contains a token. */
|
||||
private predicate line(int line, boolean comment) {
|
||||
exists(Generated::Token token, Location l |
|
||||
token = this.getAToken() and
|
||||
l = token.getLocation() and
|
||||
line in [l.getStartLine() .. l.getEndLine()] and
|
||||
if token instanceof @token_comment then comment = true else comment = false
|
||||
)
|
||||
}
|
||||
|
||||
/** Gets the number of lines in this file. */
|
||||
int getNumberOfLines() { result = max([0, this.getAToken().getLocation().getEndLine()]) }
|
||||
|
||||
/** Gets the number of lines of code in this file. */
|
||||
int getNumberOfLinesOfCode() { result = count(int line | this.line(line, false)) }
|
||||
|
||||
/** Gets the number of lines of comments in this file. */
|
||||
int getNumberOfLinesOfComments() { result = count(int line | this.line(line, true)) }
|
||||
}
|
||||
|
||||
13
ql/src/queries/metrics/FLines.ql
Normal file
13
ql/src/queries/metrics/FLines.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Number of lines
|
||||
* @kind metric
|
||||
* @description The number of lines in each file.
|
||||
* @metricType file
|
||||
* @id rb/lines-per-file
|
||||
*/
|
||||
|
||||
import ruby
|
||||
|
||||
from File f, int n
|
||||
where n = f.getNumberOfLines()
|
||||
select f, n order by n desc
|
||||
14
ql/src/queries/metrics/FLinesOfCode.ql
Normal file
14
ql/src/queries/metrics/FLinesOfCode.ql
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name Lines of code in files
|
||||
* @kind metric
|
||||
* @description Measures the number of lines of code in each file, ignoring lines that
|
||||
* contain only comments or whitespace.
|
||||
* @metricType file
|
||||
* @id rb/lines-of-code-in-files
|
||||
*/
|
||||
|
||||
import ruby
|
||||
|
||||
from File f, int n
|
||||
where n = f.getNumberOfLinesOfCode()
|
||||
select f, n order by n desc
|
||||
13
ql/src/queries/metrics/FLinesOfComments.ql
Normal file
13
ql/src/queries/metrics/FLinesOfComments.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Lines of comments in files
|
||||
* @kind metric
|
||||
* @description Measures the number of lines of comments in each file.
|
||||
* @metricType file
|
||||
* @id rb/lines-of-comments-in-files
|
||||
*/
|
||||
|
||||
import ruby
|
||||
|
||||
from File f, int n
|
||||
where n = f.getNumberOfLinesOfComments()
|
||||
select f, n order by n desc
|
||||
0
ql/test/query-tests/metrics/FLines/Empty.rb
Normal file
0
ql/test/query-tests/metrics/FLines/Empty.rb
Normal file
2
ql/test/query-tests/metrics/FLines/FLines.expected
Normal file
2
ql/test/query-tests/metrics/FLines/FLines.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
| FLines.rb:0:0:0:0 | FLines.rb | 34 |
|
||||
| Empty.rb:0:0:0:0 | Empty.rb | 0 |
|
||||
1
ql/test/query-tests/metrics/FLines/FLines.qlref
Normal file
1
ql/test/query-tests/metrics/FLines/FLines.qlref
Normal file
@@ -0,0 +1 @@
|
||||
queries/metrics/FLines.ql
|
||||
34
ql/test/query-tests/metrics/FLines/FLines.rb
Normal file
34
ql/test/query-tests/metrics/FLines/FLines.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
=begin
|
||||
some preprocessing here
|
||||
and here
|
||||
=end
|
||||
|
||||
class FLinesTest
|
||||
|
||||
def foo(bar)
|
||||
# This is a comment
|
||||
# and another
|
||||
|
||||
some_string = <<-ESCAPE
|
||||
hello world
|
||||
multiple
|
||||
lines
|
||||
|
||||
how many lines of code in this heredoc?
|
||||
# 9 lines total
|
||||
|
||||
ESCAPE
|
||||
|
||||
some_other_string = "line 1
|
||||
line" + bar
|
||||
|
||||
p some_string
|
||||
p some_other_string
|
||||
|
||||
some_string + some_other_string
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
2
ql/test/query-tests/metrics/FLines/FLinesOfCode.expected
Normal file
2
ql/test/query-tests/metrics/FLines/FLinesOfCode.expected
Normal file
@@ -0,0 +1,2 @@
|
||||
| FLines.rb:0:0:0:0 | FLines.rb | 18 |
|
||||
| Empty.rb:0:0:0:0 | Empty.rb | 0 |
|
||||
1
ql/test/query-tests/metrics/FLines/FLinesOfCode.qlref
Normal file
1
ql/test/query-tests/metrics/FLines/FLinesOfCode.qlref
Normal file
@@ -0,0 +1 @@
|
||||
queries/metrics/FLinesOfCode.ql
|
||||
@@ -0,0 +1,2 @@
|
||||
| FLines.rb:0:0:0:0 | FLines.rb | 7 |
|
||||
| Empty.rb:0:0:0:0 | Empty.rb | 0 |
|
||||
@@ -0,0 +1 @@
|
||||
queries/metrics/FLinesOfComments.ql
|
||||
Reference in New Issue
Block a user