Python: Fix bad join order in py/unused-import

This commit is contained in:
Taus Brock-Nannestad
2019-11-06 15:14:48 +01:00
parent 2bcd418c23
commit b6f16dee81

View File

@@ -41,41 +41,58 @@ predicate all_not_understood(Module m) {
}
predicate imported_module_used_in_doctest(Import imp) {
exists(string modname |
exists(string modname, string docstring |
imp.getAName().getAsname().(Name).getId() = modname and
// Look for doctests containing the patterns:
// >>> …name…
// ... …name…
exists(StrConst doc |
doc.getEnclosingModule() = imp.getScope() and
doc.isDocString() and
doc.getText().regexpMatch("[\\s\\S]*(>>>|\\.\\.\\.).*" + modname + "[\\s\\S]*")
)
docstring = doctest_in_scope(imp.getScope()) and
docstring.regexpMatch("[\\s\\S]*(>>>|\\.\\.\\.).*" + modname + "[\\s\\S]*")
)
}
pragma[noinline]
private string doctest_in_scope(Scope scope) {
exists(StrConst doc |
doc.getEnclosingModule() = scope and
doc.isDocString() and
result = doc.getText() and
result.regexpMatch("[\\s\\S]*(>>>|\\.\\.\\.)[\\s\\S]*")
)
}
pragma[noinline]
private string typehint_annotation_in_file(File file) {
exists(StrConst annotation |
annotation = any(Arguments a).getAnAnnotation()
or
annotation = any(AnnAssign a).getAnnotation()
|
annotation.pointsTo(Value::forString(result)) and
file = annotation.getLocation().getFile()
)
}
pragma[noinline]
private string typehint_comment_in_file(File file) {
exists(Comment typehint |
file = typehint.getLocation().getFile() and
result = typehint.getText() and
result.matches("# type:%")
)
}
predicate imported_module_used_in_typehint(Import imp) {
exists(string modname, Location loc |
exists(string modname, File file |
imp.getAName().getAsname().(Name).getId() = modname and
loc.getFile() = imp.getScope().(Module).getFile()
file = imp.getScope().(Module).getFile()
|
// Look for type hints containing the patterns:
// # type: …name…
exists(Comment typehint |
loc = typehint.getLocation() and
typehint.getText().regexpMatch("# type:.*" + modname + ".*")
)
typehint_comment_in_file(file).regexpMatch("# type:.*" + modname + ".*")
or
// Type hint is inside a string annotation, as needed for forward references
exists(string typehint, Expr annotation |
annotation = any(Arguments a).getAnAnnotation()
or
annotation = any(AnnAssign a).getAnnotation()
|
annotation.pointsTo(Value::forString(typehint)) and
loc = annotation.getLocation() and
typehint.regexpMatch(".*\\b" + modname + "\\b.*")
)
typehint_annotation_in_file(file).regexpMatch(".*\\b" + modname + "\\b.*")
)
}