Skip to content

Commit 11bbd87

Browse files
committed
add ql-for-ql query for detecting bad predicate qldoc
1 parent 9196b64 commit 11bbd87

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @name Predicate QLDoc style.
3+
* @description The QLDoc for a predicate should start with "Gets" or "Holds".
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/pred-doc-style
7+
* @tags maintainability
8+
* @precision very-high
9+
*/
10+
11+
import ql
12+
13+
string docLines(Predicate pred) {
14+
result =
15+
pred.getQLDoc().getContents().replaceAll("/**", "").replaceAll("*", "").splitAt("\n").trim()
16+
}
17+
18+
from Predicate pred, string message
19+
where
20+
// only considering qldocs that look like a class-doc, to avoid reporting way too much.
21+
docLines(pred).matches(["A", "An", "The"] + " %") and // looks like a class doc.
22+
not pred instanceof NewTypeBranch and // <- these are actually kinda class-like.
23+
(
24+
exists(pred.getReturnTypeExpr()) and
25+
not docLines(pred)
26+
.matches([
27+
["Gets", "Get", "Holds", "Create", "Creates"] + " %", "INTERNAL%", "DEPRECATED%",
28+
"EXPERIMENTAL%"
29+
]) and // <- predicates without a result can sometimes still use 'Holds'.
30+
message = "The QLDoc for a predicate with a result should start with 'Gets'."
31+
or
32+
not exists(pred.getReturnTypeExpr()) and
33+
not docLines(pred).matches(["Holds" + " %", "INTERNAL%", "DEPRECATED%", "EXPERIMENTAL%"]) and
34+
message = "The QLDoc for a predicate without a result should start with 'Holds'."
35+
)
36+
select pred.getQLDoc(), message

0 commit comments

Comments
 (0)