Skip to content

Commit 858e9bf

Browse files
committed
QL: add query detecting upper-case acronyms
1 parent 3fc2f2f commit 858e9bf

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import ql
2+
3+
/**
4+
* Gets the name for a `node` that defines something in a QL program.
5+
* E.g. a predicate, class, or module definition.
6+
*/
7+
string getName(AstNode node, string kind) {
8+
result = node.(Class).getName() and kind = "class"
9+
or
10+
// not including CharPreds or db relations. The remaining are: classlessPredicate, classPredicate, newTypeBranch.
11+
result = node.(ClasslessPredicate).getName() and
12+
kind = "classlessPredicate"
13+
or
14+
result = node.(ClassPredicate).getName() and
15+
kind = "classPredicate"
16+
or
17+
result = node.(NewTypeBranch).getName() and
18+
kind = "newtypeBranch"
19+
or
20+
result = node.(VarDecl).getName() and
21+
kind = "variable" and
22+
not node = any(FieldDecl f).getVarDecl()
23+
or
24+
result = node.(FieldDecl).getName() and kind = "field"
25+
or
26+
result = node.(Module).getName() and kind = "module"
27+
}
28+
29+
/**
30+
* Holds if `name` seems to contain an upper-cased acronym that could be pascal-cased.
31+
* `name` is the name of `node`, and `kind` describes what kind of definition `node` is.
32+
*/
33+
predicate shouldBePascalCased(string name, AstNode node, string kind) {
34+
name = getName(node, kind) and
35+
name.regexpMatch(".*[A-Z]{4,}.+") and
36+
not node.hasAnnotation("deprecated") and
37+
// allowed upper-case acronyms.
38+
not name.regexpMatch(".*(PEP|AES|DES|EOF).*")
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Acronyms should be PascalCase/camelCase.
3+
* @description Acronyms should be PascalCase/camelCase instead of upper-casing all the letters.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/acronyms-should-be-pascal-case
7+
* @tags correctness
8+
* maintainability
9+
* @precision high
10+
*/
11+
12+
import ql
13+
import codeql_ql.style.AcronymsShouldBeCamelCaseQuery
14+
15+
from string name, AstNode node
16+
where shouldBePascalCased(name, node, _)
17+
select node, "Acronyms should be PascalCase/camelCase"

0 commit comments

Comments
 (0)