Ruby: handle private module methods

`private` can be used in both classes and modules.
This commit is contained in:
Harry Maclean
2021-12-09 15:47:58 +13:00
parent b49ca6a24c
commit e811ba1150
3 changed files with 33 additions and 6 deletions

View File

@@ -49,7 +49,7 @@ private class Private extends MethodCall {
* method named `name`.
*/
pragma[noinline]
predicate isRef(ClassDeclaration c, string name) {
predicate isRef(Namespace c, string name) {
this = c.getAStmt() and
name = this.getMethod().(SymbolLiteral).getValueText()
}
@@ -59,7 +59,7 @@ private class Private extends MethodCall {
* and the call has no arguments.
*/
pragma[noinline]
predicate hasNoArg(ClassDeclaration c, int i) {
predicate hasNoArg(Namespace c, int i) {
this = c.getStmt(i) and
not exists(this.getMethod())
}
@@ -91,7 +91,7 @@ class Method extends MethodBase, TMethod {
final predicate isSetter() { g.getName() instanceof Ruby::Setter }
pragma[noinline]
private predicate isDeclaredIn(ClassDeclaration c, string name) {
private predicate isDeclaredIn(Namespace c, string name) {
this = c.getAStmt() and
name = this.getName()
}
@@ -125,12 +125,12 @@ class Method extends MethodBase, TMethod {
predicate isPrivate() {
this = any(Private p).getMethod()
or
exists(ClassDeclaration c, Private p, string name |
exists(Namespace c, Private p, string name |
this.isDeclaredIn(c, name) and
p.isRef(c, name)
)
or
exists(ClassDeclaration c, Private p, int i, int j |
exists(Namespace c, Private p, int i, int j |
p.hasNoArg(c, i) and
this = c.getStmt(j) and
j > i

View File

@@ -85,6 +85,9 @@ getTarget
| private.rb:28:1:28:5 | call to new | calls.rb:99:5:99:16 | new |
| private.rb:28:1:28:12 | call to public | private.rb:5:3:6:5 | public |
| private.rb:30:1:30:15 | call to private_on_main | private.rb:21:1:22:3 | private_on_main |
| private.rb:33:3:34:5 | call to private | calls.rb:94:5:94:20 | private |
| private.rb:41:3:41:19 | call to private | calls.rb:94:5:94:20 | private |
| private.rb:43:3:43:9 | call to private | calls.rb:94:5:94:20 | private |
unresolvedCall
| calls.rb:19:5:19:14 | call to instance_m |
| calls.rb:20:5:20:19 | call to instance_m |
@@ -127,3 +130,7 @@ privateMethod
| private.rb:14:3:15:5 | private3 |
| private.rb:17:3:18:5 | private4 |
| private.rb:21:1:22:3 | private_on_main |
| private.rb:33:11:34:5 | private1 |
| private.rb:39:3:40:5 | private2 |
| private.rb:45:3:46:5 | private3 |
| private.rb:48:3:49:5 | private4 |

View File

@@ -27,4 +27,24 @@ C.new.private3
C.new.private4
C.new.public
private_on_main
private_on_main
module D
private def private1
end
def public
end
def private2
end
private :private2
private
def private3
end
def private4
end
end