Skip to content

Commit 1a534ab

Browse files
committed
JS: Add support for YAML comments.
1 parent cb8cf8a commit 1a534ab

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

actions/ql/lib/codeql/Locations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ newtype TLocation =
2323
e.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
2424
)
2525
or
26+
exists(File file |
27+
yamlCommentHasLocationInfo(_, file, startline, startcolumn, endline, endcolumn) and
28+
file.getAbsolutePath() = filepath
29+
)
30+
or
2631
filepath = "" and startline = 0 and startcolumn = 0 and endline = 0 and endcolumn = 0
2732
}
2833

actions/ql/lib/codeql/actions/ast/internal/Ast.qll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,3 +1920,39 @@ private YamlMappingLikeNode resolveMatrixAccessPath(
19201920
else result = resolveMatrixAccessPath(newRoot, rest)
19211921
)
19221922
}
1923+
1924+
/** Holds if `c` has location information `(file, startline, startcolumn, endline, endcolumn)`. */
1925+
predicate yamlCommentHasLocationInfo(
1926+
@yaml_comment c, File file, int startline, int startcolumn, int endline, int endcolumn
1927+
) {
1928+
exists(@location l |
1929+
hasLocation(c, l) and
1930+
locations_default(l, file, startline, startcolumn, endline, endcolumn)
1931+
)
1932+
}
1933+
1934+
/**
1935+
* A comment.
1936+
*
1937+
* Example:
1938+
*
1939+
* ```
1940+
* # here is a comment
1941+
* ```
1942+
*/
1943+
class Comment extends @yaml_comment {
1944+
/** Gets the text of this comment, not including delimiters. */
1945+
string getText() { comments(this, _, _, result, _) }
1946+
1947+
/** Gets the location of this comment. */
1948+
Location getLocation() {
1949+
exists(File file, string filepath, int startline, int startcolumn, int endline, int endcolumn |
1950+
yamlCommentHasLocationInfo(this, file, startline, startcolumn, endline, endcolumn) and
1951+
filepath = file.getAbsolutePath() and
1952+
result.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
1953+
)
1954+
}
1955+
1956+
/** Gets a string representation of this comment. */
1957+
string toString() { comments(this, _, _, _, result) }
1958+
}

javascript/ql/lib/semmle/javascript/Comments.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module;
55
import javascript
66

77
/**
8-
* A JavaScript source-code comment.
8+
* A JavaScript or YAML source-code comment.
99
*
1010
* Examples:
1111
*
@@ -14,6 +14,7 @@ import javascript
1414
* /* a block
1515
* comment *&#47
1616
* <!-- an HTML line comment
17+
* # a YAML line comment
1718
* </pre>
1819
*/
1920
class Comment extends @comment, Locatable {
@@ -44,13 +45,14 @@ class Comment extends @comment, Locatable {
4445
}
4546

4647
/**
47-
* A line comment, that is, either an HTML comment or a `//` comment.
48+
* A line comment, that is, an HTML comment, a `//` comment, or a YAML comment.
4849
*
4950
* Examples:
5051
*
5152
* <pre>
5253
* // a line comment
5354
* &lt;!-- an HTML line comment
55+
* # a YAML line comment
5456
* </pre>
5557
*/
5658
class LineComment extends @line_comment, Comment { }
@@ -100,6 +102,17 @@ class HtmlCommentEnd extends @htmlcommentend, HtmlLineComment { }
100102
*/
101103
class SlashSlashComment extends @slashslash_comment, LineComment { }
102104

105+
/**
106+
* A YAML line comment.
107+
*
108+
* Example:
109+
*
110+
* ```
111+
* # a line comment
112+
* ```
113+
*/
114+
class YamlComment extends @yaml_comment, LineComment { }
115+
103116
/**
104117
* A block comment (which may be a JSDoc comment).
105118
*

javascript/ql/lib/semmlecode.javascript.dbscheme

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,11 @@ case @comment.kind of
800800
| 1 = @slashstar_comment
801801
| 2 = @doc_comment
802802
| 3 = @html_comment_start
803-
| 4 = @htmlcommentend;
803+
| 4 = @htmlcommentend
804+
| 5 = @yaml_comment;
804805

805806
@html_comment = @html_comment_start | @htmlcommentend;
806-
@line_comment = @slashslash_comment | @html_comment;
807+
@line_comment = @slashslash_comment | @html_comment | @yaml_comment;
807808
@block_comment = @slashstar_comment | @doc_comment;
808809

809810
// source lines

0 commit comments

Comments
 (0)