From fc8f5919f31497dbf3b83c04c44638ff4b62d804 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 27 Jul 2021 15:29:17 +0200 Subject: [PATCH] Remove Ruby specific parts from FileSystem.qll --- ql/src/codeql/files/FileSystem.qll | 23 -------------------- ql/src/codeql_ruby/AST.qll | 25 ++++++++++++++++++++++ ql/src/queries/metrics/FLines.ql | 2 +- ql/src/queries/metrics/FLinesOfCode.ql | 2 +- ql/src/queries/metrics/FLinesOfComments.ql | 2 +- ql/src/queries/summary/LinesOfCode.ql | 2 +- ql/src/queries/summary/LinesOfUserCode.ql | 2 +- 7 files changed, 30 insertions(+), 28 deletions(-) diff --git a/ql/src/codeql/files/FileSystem.qll b/ql/src/codeql/files/FileSystem.qll index d9da4d01cac..0bcb4ee1372 100644 --- a/ql/src/codeql/files/FileSystem.qll +++ b/ql/src/codeql/files/FileSystem.qll @@ -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) } } diff --git a/ql/src/codeql_ruby/AST.qll b/ql/src/codeql_ruby/AST.qll index 2afd80a0a71..6eb8f7efef7 100644 --- a/ql/src/codeql_ruby/AST.qll +++ b/ql/src/codeql_ruby/AST.qll @@ -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)) } +} diff --git a/ql/src/queries/metrics/FLines.ql b/ql/src/queries/metrics/FLines.ql index 1decdb88024..97c319fbf73 100644 --- a/ql/src/queries/metrics/FLines.ql +++ b/ql/src/queries/metrics/FLines.ql @@ -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 diff --git a/ql/src/queries/metrics/FLinesOfCode.ql b/ql/src/queries/metrics/FLinesOfCode.ql index 2558abd92cc..0c1d15960cc 100644 --- a/ql/src/queries/metrics/FLinesOfCode.ql +++ b/ql/src/queries/metrics/FLinesOfCode.ql @@ -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 diff --git a/ql/src/queries/metrics/FLinesOfComments.ql b/ql/src/queries/metrics/FLinesOfComments.ql index d737a91b27b..8af882f13d1 100644 --- a/ql/src/queries/metrics/FLinesOfComments.ql +++ b/ql/src/queries/metrics/FLinesOfComments.ql @@ -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 diff --git a/ql/src/queries/summary/LinesOfCode.ql b/ql/src/queries/summary/LinesOfCode.ql index 32590b861af..f727cf504d9 100644 --- a/ql/src/queries/summary/LinesOfCode.ql +++ b/ql/src/queries/summary/LinesOfCode.ql @@ -12,4 +12,4 @@ import ruby -select sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode()) +select sum(RubyFile f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode()) diff --git a/ql/src/queries/summary/LinesOfUserCode.ql b/ql/src/queries/summary/LinesOfUserCode.ql index 7f12e3e5a5b..19f4f46fb8d 100644 --- a/ql/src/queries/summary/LinesOfUserCode.ql +++ b/ql/src/queries/summary/LinesOfUserCode.ql @@ -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/%")