Skip to content

Commit 3df7793

Browse files
committed
add more test cases, fix bug by adding getFullName() predicate
1 parent 8e46eeb commit 3df7793

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

ruby/ql/lib/codeql/ruby/ast/Constant.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class ConstantAccess extends Expr, TConstantAccess {
4040
*/
4141
predicate hasGlobalScope() { none() }
4242

43+
// gets the full name
44+
string getFullName() {
45+
exists(ConstantAccess ca | this.getScopeExpr() = ca |
46+
result = ca.getFullName() + "::" + this.getName())
47+
or
48+
// TODO if the getScopeExpr is not a constant, try to figure out which constants it could be?
49+
not exists(ConstantAccess ca | this.getScopeExpr() = ca) and
50+
result = this.getName()
51+
}
52+
4353
override string toString() { result = this.getName() }
4454

4555
override AstNode getAChild(string pred) {
@@ -188,12 +198,12 @@ class ConstantWriteAccess extends ConstantAccess {
188198
string getQualifiedName() {
189199
/* get the qualified name for the parent module, then append w */
190200
exists(ConstantWriteAccess parent | parent = this.getEnclosingModule() |
191-
result = parent.getQualifiedName() + "::" + this.getName()
201+
result = parent.getQualifiedName() + "::" + this.getFullName()
192202
)
193203
or
194204
/* base case - there's no parent module */
195205
not exists(ConstantWriteAccess parent | parent = this.getEnclosingModule()) and
196-
result = this.getName()
206+
result = this.getFullName()
197207
}
198208
}
199209

ruby/ql/test/library-tests/ast/constants/constants.expected

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ constantAccess
3434
| constants.rb:39:6:39:31 | MAX_SIZE | read | MAX_SIZE | ConstantReadAccess |
3535
| constants.rb:41:6:41:13 | GREETING | read | GREETING | ConstantReadAccess |
3636
| constants.rb:42:6:42:15 | GREETING | read | GREETING | ConstantReadAccess |
37+
| constants.rb:44:1:47:3 | ModuleB | write | ModuleB | ModuleDeclaration |
38+
| constants.rb:44:8:44:14 | ModuleA | read | ModuleA | ConstantReadAccess |
39+
| constants.rb:45:3:46:5 | ClassB | write | ClassB | ClassDeclaration |
40+
| constants.rb:45:18:45:21 | Base | read | Base | ConstantReadAccess |
41+
| constants.rb:49:1:52:3 | ModuleA | write | ModuleA | ModuleDeclaration |
42+
| constants.rb:50:3:51:5 | ClassB | write | ClassB | ClassDeclaration |
43+
| constants.rb:50:9:50:15 | ModuleB | read | ModuleB | ConstantReadAccess |
44+
| constants.rb:50:27:50:30 | Base | read | Base | ConstantReadAccess |
3745
getConst
3846
| constants.rb:1:1:15:3 | ModuleA | CONST_B | constants.rb:6:15:6:23 | "const_b" |
3947
| constants.rb:2:5:4:7 | ModuleA::ClassA | CONST_A | constants.rb:3:19:3:27 | "const_a" |
@@ -67,4 +75,8 @@ constantWriteAccessQualifiedName
6775
| constants.rb:20:5:20:9 | Names | Names |
6876
| constants.rb:31:1:32:3 | ClassD | ModuleA::ClassD |
6977
| constants.rb:34:1:35:3 | ModuleC | ModuleA::ModuleC |
70-
| constants.rb:37:1:37:26 | MAX_SIZE | MAX_SIZE |
78+
| constants.rb:37:1:37:26 | MAX_SIZE | ModuleA::ModuleB::MAX_SIZE |
79+
| constants.rb:44:1:47:3 | ModuleB | ModuleA::ModuleB |
80+
| constants.rb:45:3:46:5 | ClassB | ModuleA::ModuleB::ClassB |
81+
| constants.rb:49:1:52:3 | ModuleA | ModuleA |
82+
| constants.rb:50:3:51:5 | ClassB | ModuleA::ModuleB::ClassB |

ruby/ql/test/library-tests/ast/constants/constants.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ module ModuleA::ModuleC
4040

4141
puts GREETING
4242
puts ::GREETING
43+
44+
module ModuleA::ModuleB
45+
class ClassB < Base
46+
end
47+
end
48+
49+
module ModuleA
50+
class ModuleB::ClassB < Base
51+
end
52+
end

0 commit comments

Comments
 (0)