Remove Ruby specific parts from FileSystem.qll

This commit is contained in:
Arthur Baars
2021-07-27 15:29:17 +02:00
parent 3790611ca1
commit fc8f5919f3
7 changed files with 30 additions and 28 deletions

View File

@@ -1,6 +1,5 @@
/** Provides classes for working with files and folders. */
private import codeql_ruby.ast.internal.TreeSitter
private import codeql.Locations
/** A file or folder. */
@@ -169,28 +168,6 @@ 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)) }
/** Holds if this file was extracted from ordinary source code. */
predicate fromSource() { files(this, _, _, _, 1) }
}

View File

@@ -15,6 +15,7 @@ import ast.Variable
private import ast.internal.AST
private import ast.internal.Scope
private import ast.internal.Synthesis
private import ast.internal.TreeSitter
/**
* A node in the abstract syntax tree. This class is the base class for all Ruby
@@ -104,3 +105,27 @@ class AstNode extends TAstNode {
*/
final AstNode getDesugared() { result = getSynthChild(this, -1) }
}
class RubyFile extends File {
/** Gets a token in this file. */
private Ruby::Token getAToken() { result.getLocation().getFile() = this }
/** Holds if `line` contains a token. */
private predicate line(int line, boolean comment) {
exists(Ruby::Token token, Location l |
token = this.getAToken() and
l = token.getLocation() and
line in [l.getStartLine() .. l.getEndLine()] and
if token instanceof @ruby_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

@@ -8,6 +8,6 @@
import ruby
from File f, int n
from RubyFile f, int n
where n = f.getNumberOfLines()
select f, n order by n desc

View File

@@ -9,6 +9,6 @@
import ruby
from File f, int n
from RubyFile f, int n
where n = f.getNumberOfLinesOfCode()
select f, n order by n desc

View File

@@ -8,6 +8,6 @@
import ruby
from File f, int n
from RubyFile f, int n
where n = f.getNumberOfLinesOfComments()
select f, n order by n desc

View File

@@ -12,4 +12,4 @@
import ruby
select sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode())
select sum(RubyFile f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode())

View File

@@ -10,7 +10,7 @@
import ruby
select sum(File f |
select sum(RubyFile f |
f.fromSource() and
exists(f.getRelativePath()) and
not f.getAbsolutePath().matches("%/vendor/%")