Merge pull request #167 from github/alexrford/numlines

Implement FLines metrics queries
This commit is contained in:
Arthur Baars
2021-04-21 14:42:18 +02:00
committed by GitHub
12 changed files with 108 additions and 0 deletions

View File

@@ -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)) }
}

View 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

View 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

View 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

View File

@@ -0,0 +1,2 @@
| FLines.rb:0:0:0:0 | FLines.rb | 34 |
| Empty.rb:0:0:0:0 | Empty.rb | 0 |

View File

@@ -0,0 +1 @@
queries/metrics/FLines.ql

View 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

View File

@@ -0,0 +1,2 @@
| FLines.rb:0:0:0:0 | FLines.rb | 18 |
| Empty.rb:0:0:0:0 | Empty.rb | 0 |

View File

@@ -0,0 +1 @@
queries/metrics/FLinesOfCode.ql

View File

@@ -0,0 +1,2 @@
| FLines.rb:0:0:0:0 | FLines.rb | 7 |
| Empty.rb:0:0:0:0 | Empty.rb | 0 |

View File

@@ -0,0 +1 @@
queries/metrics/FLinesOfComments.ql