Skip to content

Commit 4f0d4ec

Browse files
committed
QL: add no-uppercase-variables query
1 parent bbb2847 commit 4f0d4ec

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

ql/ql/src/codeql_ql/style/AcronymsShouldBeCamelCaseQuery.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ string getName(AstNode node, string kind) {
2929
result = node.(Module).getName() and kind = "module"
3030
}
3131

32+
string prettyPluralKind(string kind) {
33+
kind = "class" and result = "classes"
34+
or
35+
kind = "classlessPredicate" and result = "predicates"
36+
or
37+
kind = "classPredicate" and result = "class predicates"
38+
or
39+
kind = "newtypeBranch" and result = "newtype branches"
40+
or
41+
kind = "newtype" and result = "newtypes"
42+
or
43+
kind = "variable" and result = "variables"
44+
or
45+
kind = "field" and result = "fields"
46+
or
47+
kind = "module" and result = "modules"
48+
}
49+
3250
/**
3351
* Holds if `name` seems to contain an upper-cased acronym that could be pascal-cased.
3452
* `name` is the name of `node`, and `kind` describes what kind of definition `node` is.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @name No upper case variables
3+
* @description Variables/fields/predicates should be lower-case, classes/modules should be upper-case
4+
* @kind problem
5+
* @problem.severity error
6+
* @id ql/no-upper-case-variables
7+
* @tags correctness
8+
* @precision very-high
9+
*/
10+
11+
import ql
12+
import codeql_ql.style.AcronymsShouldBeCamelCaseQuery as AcronymsQuery
13+
14+
predicate shouldBeUpperCase(AstNode node, string name, string kind) {
15+
name = AcronymsQuery::getName(node, kind) and
16+
kind = ["class", "newtypeBranch", "newtype", "module"]
17+
}
18+
19+
predicate shouldBeLowerCase(AstNode node, string name, string kind) {
20+
name = AcronymsQuery::getName(node, kind) and
21+
not shouldBeUpperCase(node, name, kind)
22+
}
23+
24+
string prettyKind(string kind) {
25+
exists(string prettyLower | prettyLower = AcronymsQuery::prettyPluralKind(kind) |
26+
result = prettyLower.prefix(1).toUpperCase() + prettyLower.suffix(1)
27+
)
28+
}
29+
30+
from string name, AstNode node, string message, string kind
31+
where
32+
(
33+
shouldBeLowerCase(node, name, kind) and
34+
name.regexpMatch("[A-Z].*") and
35+
message = "lowercase"
36+
or
37+
shouldBeUpperCase(node, name, kind) and
38+
name.regexpMatch("[a-z].*") and
39+
message = "uppercase"
40+
) and
41+
not node.hasAnnotation("deprecated")
42+
select node, prettyKind(kind) + " should start with an " + message + " letter."

0 commit comments

Comments
 (0)