Skip to content

Commit bc6aec7

Browse files
authored
Merge pull request #167 from github/alexrford/numlines
Implement FLines metrics queries
2 parents abb37e2 + 240f0ab commit bc6aec7

12 files changed

Lines changed: 108 additions & 0 deletions

File tree

ql/src/codeql/files/FileSystem.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/** Provides classes for working with files and folders. */
22

3+
private import codeql_ruby.ast.internal.TreeSitter
4+
private import codeql.Locations
5+
36
/** A file or folder. */
47
abstract class Container extends @container {
58
/** Gets a file or sub-folder in this container. */
@@ -165,4 +168,26 @@ class File extends Container, @file {
165168

166169
/** Gets the URL of this file. */
167170
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
171+
172+
/** Gets a token in this file. */
173+
private Generated::Token getAToken() { result.getLocation().getFile() = this }
174+
175+
/** Holds if `line` contains a token. */
176+
private predicate line(int line, boolean comment) {
177+
exists(Generated::Token token, Location l |
178+
token = this.getAToken() and
179+
l = token.getLocation() and
180+
line in [l.getStartLine() .. l.getEndLine()] and
181+
if token instanceof @token_comment then comment = true else comment = false
182+
)
183+
}
184+
185+
/** Gets the number of lines in this file. */
186+
int getNumberOfLines() { result = max([0, this.getAToken().getLocation().getEndLine()]) }
187+
188+
/** Gets the number of lines of code in this file. */
189+
int getNumberOfLinesOfCode() { result = count(int line | this.line(line, false)) }
190+
191+
/** Gets the number of lines of comments in this file. */
192+
int getNumberOfLinesOfComments() { result = count(int line | this.line(line, true)) }
168193
}

ql/src/queries/metrics/FLines.ql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Number of lines
3+
* @kind metric
4+
* @description The number of lines in each file.
5+
* @metricType file
6+
* @id rb/lines-per-file
7+
*/
8+
9+
import ruby
10+
11+
from File f, int n
12+
where n = f.getNumberOfLines()
13+
select f, n order by n desc
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Lines of code in files
3+
* @kind metric
4+
* @description Measures the number of lines of code in each file, ignoring lines that
5+
* contain only comments or whitespace.
6+
* @metricType file
7+
* @id rb/lines-of-code-in-files
8+
*/
9+
10+
import ruby
11+
12+
from File f, int n
13+
where n = f.getNumberOfLinesOfCode()
14+
select f, n order by n desc
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Lines of comments in files
3+
* @kind metric
4+
* @description Measures the number of lines of comments in each file.
5+
* @metricType file
6+
* @id rb/lines-of-comments-in-files
7+
*/
8+
9+
import ruby
10+
11+
from File f, int n
12+
where n = f.getNumberOfLinesOfComments()
13+
select f, n order by n desc

ql/test/query-tests/metrics/FLines/Empty.rb

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| FLines.rb:0:0:0:0 | FLines.rb | 34 |
2+
| Empty.rb:0:0:0:0 | Empty.rb | 0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/metrics/FLines.ql
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
=begin
4+
some preprocessing here
5+
and here
6+
=end
7+
8+
class FLinesTest
9+
10+
def foo(bar)
11+
# This is a comment
12+
# and another
13+
14+
some_string = <<-ESCAPE
15+
hello world
16+
multiple
17+
lines
18+
19+
how many lines of code in this heredoc?
20+
# 9 lines total
21+
22+
ESCAPE
23+
24+
some_other_string = "line 1
25+
line" + bar
26+
27+
p some_string
28+
p some_other_string
29+
30+
some_string + some_other_string
31+
end
32+
33+
34+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| FLines.rb:0:0:0:0 | FLines.rb | 18 |
2+
| Empty.rb:0:0:0:0 | Empty.rb | 0 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/metrics/FLinesOfCode.ql

0 commit comments

Comments
 (0)