From f12e6ea8ea3e671a360992538ea27d414807882d Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Tue, 30 Mar 2021 16:14:21 +0200 Subject: [PATCH] Avoid 'Object::' prefixes --- ql/src/codeql_ruby/ast/internal/Module.qll | 11 +++++-- .../ast/modules/modules.expected | 32 +++++++++++++++++++ ql/test/library-tests/ast/modules/modules.ql | 2 ++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index d5e85d84258..3d9d2e3203f 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -51,6 +51,11 @@ ModuleBase enclosing(ModuleBase m, int level) { result = enclosing(m.getOuterScope().getEnclosingModule(), level - 1) } +bindingset[qualifier, name] +private string scopeAppend(string qualifier, string name) { + if qualifier = "Object" then result = name else result = qualifier + "::" + name +} + private string resolveRelativeToEnclosing(ConstantAccess n, int i) { not isToplevel(n) and not exists(n.getScopeExpr()) and @@ -58,7 +63,7 @@ private string resolveRelativeToEnclosing(ConstantAccess n, int i) { n = s.getADescendant() and enclosing = enclosing(s.getEnclosingModule(), i) and ( - result = constantDefinition0(enclosing) + "::" + n.getName() + result = scopeAppend(constantDefinition0(enclosing), n.getName()) or enclosing instanceof Toplevel and result = n.getName() ) @@ -71,13 +76,13 @@ private string qualifiedNameForConstant0(ConstantAccess n) { or result = resolveRelativeToEnclosing(n, 0) or - result = resolveScopeExpr0(n.getScopeExpr()) + "::" + n.getName() + result = scopeAppend(resolveScopeExpr0(n.getScopeExpr()), n.getName()) } string constantDefinition(ConstantWriteAccess n) { result = constantDefinition0(n) or - result = resolveScopeExpr(n.getScopeExpr()) + "::" + n.getName() + result = scopeAppend(resolveScopeExpr(n.getScopeExpr()), n.getName()) } private string resolveScopeExpr(ConstantReadAccess n) { diff --git a/ql/test/library-tests/ast/modules/modules.expected b/ql/test/library-tests/ast/modules/modules.expected index 5bad601a779..7c877d40c2e 100644 --- a/ql/test/library-tests/ast/modules/modules.expected +++ b/ql/test/library-tests/ast/modules/modules.expected @@ -68,3 +68,35 @@ modulesInModules | modules.rb:63:1:81:3 | Test | modules.rb:70:3:74:5 | Foo2 | Foo2 | | modules.rb:63:1:81:3 | Test | modules.rb:76:3:80:5 | Foo3 | Foo3 | | modules.rb:70:3:74:5 | Foo2 | modules.rb:71:5:71:19 | Foo2 | Foo2 | +moduleTypes +| classes.rb:2:1:56:3 | classes.rb | file://:0:0:0:0 | Object | +| classes.rb:3:1:4:3 | Foo | modules.rb:4:1:24:3 | Foo | +| classes.rb:7:1:8:3 | Bar | modules.rb:37:1:46:3 | Bar | +| classes.rb:11:1:12:3 | Baz | classes.rb:11:1:12:3 | Baz | +| classes.rb:15:1:15:20 | MyModule | classes.rb:15:1:15:20 | MyModule | +| classes.rb:16:1:17:3 | MyClass | classes.rb:16:1:17:3 | MyModule::MyClass | +| classes.rb:20:1:37:3 | Wibble | classes.rb:20:1:37:3 | Wibble | +| classes.rb:32:3:33:5 | ClassInWibble | classes.rb:32:3:33:5 | Wibble::ClassInWibble | +| classes.rb:35:3:36:5 | ModuleInWibble | classes.rb:35:3:36:5 | Wibble::ModuleInWibble | +| classes.rb:55:1:56:3 | MyClassInGlobalScope | classes.rb:55:1:56:3 | MyClassInGlobalScope | +| modules.rb:1:1:2:3 | Empty | modules.rb:1:1:2:3 | Empty | +| modules.rb:1:1:82:1 | modules.rb | file://:0:0:0:0 | Object | +| modules.rb:4:1:24:3 | Foo | modules.rb:4:1:24:3 | Foo | +| modules.rb:5:3:14:5 | Bar | modules.rb:5:3:14:5 | Foo::Bar | +| modules.rb:6:5:7:7 | ClassInFooBar | modules.rb:6:5:7:7 | Foo::Bar::ClassInFooBar | +| modules.rb:19:3:20:5 | ClassInFoo | modules.rb:19:3:20:5 | Foo::ClassInFoo | +| modules.rb:26:1:35:3 | Foo | modules.rb:4:1:24:3 | Foo | +| modules.rb:30:3:31:5 | ClassInAnotherDefinitionOfFoo | modules.rb:30:3:31:5 | Foo::ClassInAnotherDefinitionOfFoo | +| modules.rb:37:1:46:3 | Bar | modules.rb:37:1:46:3 | Bar | +| modules.rb:48:1:57:3 | Bar | modules.rb:5:3:14:5 | Foo::Bar | +| modules.rb:49:3:50:5 | ClassInAnotherDefinitionOfFooBar | modules.rb:49:3:50:5 | Foo::Bar::ClassInAnotherDefinitionOfFooBar | +| modules.rb:60:1:61:3 | MyModuleInGlobalScope | modules.rb:60:1:61:3 | MyModuleInGlobalScope | +| modules.rb:63:1:81:3 | Test | modules.rb:63:1:81:3 | Test | +| modules.rb:65:3:68:5 | Foo1 | modules.rb:65:3:68:5 | Test::Foo1 | +| modules.rb:66:5:67:7 | Bar | modules.rb:66:5:67:7 | Test::Foo1::Bar | +| modules.rb:70:3:74:5 | Foo2 | modules.rb:70:3:74:5 | Test::Foo2 | +| modules.rb:71:5:71:19 | Foo2 | modules.rb:71:5:71:19 | Test::Foo2::Foo2 | +| modules.rb:72:5:73:7 | Bar | modules.rb:72:5:73:7 | Test::Foo2::Foo2::Bar | +| modules.rb:76:3:80:5 | Foo3 | modules.rb:76:3:80:5 | Test::Foo3 | +| modules.rb:78:5:79:7 | Bar | modules.rb:37:1:46:3 | Bar | +| toplevel.rb:1:1:5:23 | toplevel.rb | file://:0:0:0:0 | Object | diff --git a/ql/test/library-tests/ast/modules/modules.ql b/ql/test/library-tests/ast/modules/modules.ql index d0942303fb5..ba42d6365bf 100644 --- a/ql/test/library-tests/ast/modules/modules.ql +++ b/ql/test/library-tests/ast/modules/modules.ql @@ -23,3 +23,5 @@ query predicate classesInModules(ModuleDefinition mod, ClassDefinition klass, st query predicate modulesInModules(ModuleDefinition mod, ModuleDefinition child, string name) { child = mod.getModule(name) } + +query predicate moduleTypes(ModuleBase def, Module type) { type = def.getModule() }