|
1 | 1 | /** Provides classes for working with files and folders. */ |
2 | 2 |
|
| 3 | +private import codeql_ruby.ast.internal.TreeSitter |
| 4 | +private import codeql.Locations |
| 5 | + |
3 | 6 | /** A file or folder. */ |
4 | 7 | abstract class Container extends @container { |
5 | 8 | /** Gets a file or sub-folder in this container. */ |
@@ -165,4 +168,26 @@ class File extends Container, @file { |
165 | 168 |
|
166 | 169 | /** Gets the URL of this file. */ |
167 | 170 | 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)) } |
168 | 193 | } |
0 commit comments