Skip to content

Commit 7d6d5bf

Browse files
committed
Unified: add test for comments
1 parent f83adb5 commit 7d6d5bf

5 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
private import unified
2+
3+
/**
4+
* A comment appearing in the source code.
5+
*/
6+
class Comment extends TriviaToken {
7+
// At the moment, comments are the only type trivia token we extract
8+
string getCommentText() {
9+
result = this.getValue().regexpCapture("//(.*)", 1)
10+
or
11+
result = this.getValue().regexpCapture("(?s)/\\*(.*)\\*/", 1)
12+
}
13+
}

unified/ql/lib/unified.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import codeql.Locations
2+
import codeql.files.FileSystem
3+
import codeql.unified.Ast::Unified
4+
import codeql.unified.Comments
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| comments.swift:1:1:1:22 | // Hello this is swift | Hello this is swift |
2+
| comments.swift:3:1:6:3 | /*\n * This is a multi-line comment\n * It should be ignored by the parser\n */ | \n * This is a multi-line comment\n * It should be ignored by the parser\n |
3+
| comments.swift:9:5:9:36 | // This is a single-line comment | This is a single-line comment |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unified
2+
3+
query predicate comments(Comment c, string text) { text = c.getCommentText() }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Hello this is swift
2+
3+
/*
4+
* This is a multi-line comment
5+
* It should be ignored by the parser
6+
*/
7+
8+
func hello() {
9+
// This is a single-line comment
10+
print("Hello, world!")
11+
}

0 commit comments

Comments
 (0)