|
| 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