Skip to content

Commit cb3a0fb

Browse files
committed
make a Comment superclass
1 parent f20c186 commit cb3a0fb

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,38 @@ class TopLevel extends TTopLevel, AstNode {
156156
override QLDoc getQLDoc() { result = this.getMember(0) }
157157
}
158158

159-
class QLDoc extends TQLDoc, AstNode {
159+
abstract class Comment extends AstNode, TComment {
160+
abstract string getContents();
161+
}
162+
163+
class QLDoc extends TQLDoc, Comment {
160164
QL::Qldoc qldoc;
161165

162166
QLDoc() { this = TQLDoc(qldoc) }
163167

164-
string getContents() { result = qldoc.getValue() }
168+
override string getContents() { result = qldoc.getValue() }
165169

166170
override string getAPrimaryQlClass() { result = "QLDoc" }
167171

168172
override AstNode getParent() { result.getQLDoc() = this }
169173
}
170174

171-
class BlockComment extends TBlockComment, AstNode {
175+
class BlockComment extends TBlockComment, Comment {
172176
QL::BlockComment comment;
173177

174178
BlockComment() { this = TBlockComment(comment) }
175179

176-
string getContents() { result = comment.getValue() }
180+
override string getContents() { result = comment.getValue() }
177181

178182
override string getAPrimaryQlClass() { result = "BlockComment" }
179183
}
180184

181-
class LineComment extends TLineComment, AstNode {
185+
class LineComment extends TLineComment, Comment {
182186
QL::LineComment comment;
183187

184188
LineComment() { this = TLineComment(comment) }
185189

186-
string getContents() { result = comment.getValue() }
190+
override string getContents() { result = comment.getValue() }
187191

188192
override string getAPrimaryQlClass() { result = "LineComment" }
189193
}

ql/ql/src/codeql_ql/ast/internal/AstNodes.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class TYamlNode = TYamlCommemt or TYamlEntry or TYamlKey or TYamlListitem or TYa
9090

9191
class TSignatureExpr = TPredicateExpr or TType;
9292

93+
class TComment = TQLDoc or TBlockComment or TLineComment;
94+
9395
/** DEPRECATED: Alias for TYamlNode */
9496
deprecated class TYAMLNode = TYamlNode;
9597

ql/ql/src/queries/style/RepeatedWord.ql

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@
99

1010
import ql
1111

12-
string getComment(AstNode node) {
13-
result = node.(QLDoc).getContents()
14-
or
15-
result = node.(BlockComment).getContents()
16-
or
17-
result = node.(LineComment).getContents()
18-
}
19-
2012
/** Gets a word used in `node` */
21-
string getWord(AstNode node) { result = getComment(node).regexpFind("\\b[A-Za-z]+\\b", _, _) }
13+
string getWord(Comment node) { result = node.getContents().regexpFind("\\b[A-Za-z]+\\b", _, _) }
2214

23-
AstNode hasRepeatedWord(string word) {
15+
Comment hasRepeatedWord(string word) {
2416
word = getWord(result) and
25-
getComment(result).regexpMatch(".*\\b" + word + "\\s+" + word + "\\b.*")
17+
result.getContents().regexpMatch(".*\\b" + word + "\\s+" + word + "\\b.*")
2618
}
2719

28-
from AstNode comment, string word
20+
from Comment comment, string word
2921
where comment = hasRepeatedWord(word)
3022
select comment, "The comment repeats " + word + "."

0 commit comments

Comments
 (0)