mirror of
https://github.com/github/codeql.git
synced 2026-05-05 05:35:13 +02:00
QL: add no-uppercase-variables query
This commit is contained in:
@@ -29,6 +29,24 @@ string getName(AstNode node, string kind) {
|
||||
result = node.(Module).getName() and kind = "module"
|
||||
}
|
||||
|
||||
string prettyPluralKind(string kind) {
|
||||
kind = "class" and result = "classes"
|
||||
or
|
||||
kind = "classlessPredicate" and result = "predicates"
|
||||
or
|
||||
kind = "classPredicate" and result = "class predicates"
|
||||
or
|
||||
kind = "newtypeBranch" and result = "newtype branches"
|
||||
or
|
||||
kind = "newtype" and result = "newtypes"
|
||||
or
|
||||
kind = "variable" and result = "variables"
|
||||
or
|
||||
kind = "field" and result = "fields"
|
||||
or
|
||||
kind = "module" and result = "modules"
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds if `name` seems to contain an upper-cased acronym that could be pascal-cased.
|
||||
* `name` is the name of `node`, and `kind` describes what kind of definition `node` is.
|
||||
|
||||
42
ql/ql/src/queries/style/NoUppercaseVariables.ql
Normal file
42
ql/ql/src/queries/style/NoUppercaseVariables.ql
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @name No upper case variables
|
||||
* @description Variables/fields/predicates should be lower-case, classes/modules should be upper-case
|
||||
* @kind problem
|
||||
* @problem.severity error
|
||||
* @id ql/no-upper-case-variables
|
||||
* @tags correctness
|
||||
* @precision very-high
|
||||
*/
|
||||
|
||||
import ql
|
||||
import codeql_ql.style.AcronymsShouldBeCamelCaseQuery as AcronymsQuery
|
||||
|
||||
predicate shouldBeUpperCase(AstNode node, string name, string kind) {
|
||||
name = AcronymsQuery::getName(node, kind) and
|
||||
kind = ["class", "newtypeBranch", "newtype", "module"]
|
||||
}
|
||||
|
||||
predicate shouldBeLowerCase(AstNode node, string name, string kind) {
|
||||
name = AcronymsQuery::getName(node, kind) and
|
||||
not shouldBeUpperCase(node, name, kind)
|
||||
}
|
||||
|
||||
string prettyKind(string kind) {
|
||||
exists(string prettyLower | prettyLower = AcronymsQuery::prettyPluralKind(kind) |
|
||||
result = prettyLower.prefix(1).toUpperCase() + prettyLower.suffix(1)
|
||||
)
|
||||
}
|
||||
|
||||
from string name, AstNode node, string message, string kind
|
||||
where
|
||||
(
|
||||
shouldBeLowerCase(node, name, kind) and
|
||||
name.regexpMatch("[A-Z].*") and
|
||||
message = "lowercase"
|
||||
or
|
||||
shouldBeUpperCase(node, name, kind) and
|
||||
name.regexpMatch("[a-z].*") and
|
||||
message = "uppercase"
|
||||
) and
|
||||
not node.hasAnnotation("deprecated")
|
||||
select node, prettyKind(kind) + " should start with an " + message + " letter."
|
||||
Reference in New Issue
Block a user