JavaScript: Teach Unused{Variable,Parameter} to ignore variables with leading underscore.

This commit is contained in:
Max Schaefer
2018-12-11 08:49:17 +00:00
parent a4b3b1e8c8
commit 4d186e0edc
7 changed files with 26 additions and 5 deletions

View File

@@ -9,7 +9,7 @@
*/
import javascript
import UnusedParameter // local library
import UnusedParameter
from Parameter p
where isAnAccidentallyUnusedParameter(p)

View File

@@ -46,7 +46,9 @@ predicate isUnused(Function f, Parameter p, Variable pv, int i) {
// functions without a body cannot use their parameters
f.hasBody() and
// field parameters are used to initialize a field
not p instanceof FieldParameter
not p instanceof FieldParameter and
// common convention: parameters with leading underscore are intentionally unused
pv.getName().charAt(0) != "_"
}
/**

View File

@@ -22,7 +22,9 @@ class UnusedLocal extends LocalVariable {
not exists(FunctionExpr fe | this = fe.getVariable()) and
not exists(ClassExpr ce | this = ce.getVariable()) and
not exists(ExportDeclaration ed | ed.exportsAs(this, _)) and
not exists(LocalVarTypeAccess type | type.getVariable() = this)
not exists(LocalVarTypeAccess type | type.getVariable() = this) and
// common convention: variables with leading underscore are intentionally unused
getName().charAt(0) != "_"
}
}