diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index dffabffa649..f01eba6c323 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -1,46 +1,8 @@ private import codeql_ruby.AST private import codeql_ruby.controlflow.ControlFlowGraph private import internal.AST -private import internal.Method private import internal.TreeSitter -/** - * A representation of a method. - */ -class Method extends TMethod { - /** Gets a declaration of this module, if any. */ - MethodBase getADeclaration() { result.getMethod() = this } - - /** Gets the name of this method */ - string getName() { this = TInstanceMethod(_, result) } - - /** Gets the module in which this method is defined */ - Module getModule() { this = TInstanceMethod(result, _) } - - /** Gets a textual representation of this method. */ - string toString() { - exists(Module m, string name | - this = TInstanceMethod(m, name) and result = m.toString() + "." + name - ) - } - - /** Gets the location of this method. */ - Location getLocation() { - result = - min(MethodBase decl, Module m, string name, Location loc, int weight | - this = TInstanceMethod(m, name) and - decl = methodDeclaration(m, name) and - loc = decl.getLocation() and - if exists(loc.getFile().getRelativePath()) then weight = 0 else weight = 1 - | - loc - order by - weight, count(decl.getAStmt()) desc, loc.getFile().getAbsolutePath(), loc.getStartLine(), - loc.getStartColumn() - ) - } -} - /** A callable. */ class Callable extends Expr, Scope, TCallable { /** Gets the number of parameters of this callable. */ @@ -62,9 +24,6 @@ class MethodBase extends Callable, BodyStmt, Scope, TMethodBase { /** Gets the name of this method. */ string getName() { none() } - /** Gets the method defined by this declaration. */ - Method getMethod() { none() } - override AstNode getAChild(string pred) { result = Callable.super.getAChild(pred) or @@ -85,12 +44,6 @@ class MethodDeclaration extends MethodBase, TMethodDeclaration { result = g.getName().(Generated::Setter).getName().getValue() + "=" } - final override Method getMethod() { - exists(Module owner, string name | - result = TInstanceMethod(owner, name) and this = methodDeclaration(owner, name) - ) - } - /** * Holds if this is a setter method, as in the following example: * ```rb diff --git a/ql/src/codeql_ruby/ast/Module.qll b/ql/src/codeql_ruby/ast/Module.qll index ae46b46d0c2..17661c281ba 100644 --- a/ql/src/codeql_ruby/ast/Module.qll +++ b/ql/src/codeql_ruby/ast/Module.qll @@ -14,12 +14,6 @@ class Module extends TModule { /** Gets the super class of this module, if any. */ Module getSuperClass() { result = getSuperClass(this) } - /** Gets a method defined in this module by name. */ - Method getMethod(string name) { result.getName() = name and result.getModule() = this } - - /** Look up a method in this module's ancestor chain. */ - Method lookupMethod(string name) { result = lookupMethod(this, name) } - /** Gets a `prepend`ed module. */ Module getAPrependedModule() { result = getAPrependedModule(this) } diff --git a/ql/src/codeql_ruby/ast/internal/Method.qll b/ql/src/codeql_ruby/ast/internal/Method.qll deleted file mode 100644 index 82e0f53d47f..00000000000 --- a/ql/src/codeql_ruby/ast/internal/Method.qll +++ /dev/null @@ -1,10 +0,0 @@ -private import codeql_ruby.ast.Method -private import codeql_ruby.ast.Module -private import codeql_ruby.ast.internal.Module - -newtype TMethod = - TInstanceMethod(TModule owner, string name) { exists(methodDeclaration(owner, name)) } - -MethodDeclaration methodDeclaration(TModule owner, string name) { - exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) -} diff --git a/ql/src/codeql_ruby/ast/internal/Module.qll b/ql/src/codeql_ruby/ast/internal/Module.qll index 554516f3c2f..90dd5500775 100644 --- a/ql/src/codeql_ruby/ast/internal/Module.qll +++ b/ql/src/codeql_ruby/ast/internal/Module.qll @@ -301,18 +301,22 @@ private Module getAncestors(Module m) { result = getAncestors(m.getAPrependedModule()) } -private Method lookupMethod0(Module m, string name) { +MethodDeclaration getMethod(TModule owner, string name) { + exists(ModuleBase m | m.getModule() = owner and result = m.getMethod(name)) +} + +private MethodDeclaration lookupMethod0(Module m, string name) { result = lookupMethod0(m.getAPrependedModule(), name) or - not exists(getAncestors(m.getAPrependedModule()).getMethod(name)) and + not exists(getMethod(getAncestors(m.getAPrependedModule()), name)) and ( - result = m.getMethod(name) + result = getMethod(m, name) or - not exists(m.getMethod(name)) and result = lookupMethod0(m.getAnIncludedModule(), name) + not exists(getMethod(m, name)) and result = lookupMethod0(m.getAnIncludedModule(), name) ) } -Method lookupMethod(Module m, string name) { +MethodDeclaration lookupMethod(Module m, string name) { result = lookupMethod0(m, name) or not exists(lookupMethod0(m, name)) and diff --git a/ql/test/library-tests/modules/methods.expected b/ql/test/library-tests/modules/methods.expected index 5f7d1a5a76a..1a745517cfd 100644 --- a/ql/test/library-tests/modules/methods.expected +++ b/ql/test/library-tests/modules/methods.expected @@ -1,70 +1,26 @@ -method -| hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:19:5:21:7 | HelloWorld.message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:41:3:42:5 | Bar.method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -getADeclaration -| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:2:5:4:7 | hello | -| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:5:5:7:7 | world | -| hello.rb:13:5:15:7 | Greeting.message | hello.rb:13:5:15:7 | message | -| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:19:5:21:7 | message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:16:3:17:5 | method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:38:3:39:5 | method_a | -| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:41:3:42:5 | method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | -getName -| hello.rb:2:5:4:7 | EnglishWords.hello | hello | -| hello.rb:5:5:7:7 | EnglishWords.world | world | -| hello.rb:13:5:15:7 | Greeting.message | message | -| hello.rb:19:5:21:7 | HelloWorld.message | message | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | method_in_foo_bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | method_in_foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | method_in_another_definition_of_foo | -| modules.rb:38:3:39:5 | Bar.method_a | method_a | -| modules.rb:41:3:42:5 | Bar.method_b | method_b | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | method_in_another_definition_of_foo_bar | -getModule -| hello.rb:2:5:4:7 | EnglishWords.hello | hello.rb:1:1:8:3 | EnglishWords | -| hello.rb:5:5:7:7 | EnglishWords.world | hello.rb:1:1:8:3 | EnglishWords | -| hello.rb:13:5:15:7 | Greeting.message | hello.rb:11:1:16:3 | Greeting | -| hello.rb:19:5:21:7 | HelloWorld.message | hello.rb:18:1:22:3 | HelloWorld | -| modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | -| modules.rb:16:3:17:5 | Foo.method_in_foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | modules.rb:4:1:24:3 | Foo | -| modules.rb:38:3:39:5 | Bar.method_a | modules.rb:37:1:46:3 | Bar | -| modules.rb:41:3:42:5 | Bar.method_b | modules.rb:37:1:46:3 | Bar | -| modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | modules.rb:5:3:14:5 | Foo::Bar | getMethod -| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | -| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | world | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | message | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | message | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | method_b | lookupMethod -| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:11:1:16:3 | Greeting | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | Greeting.message | -| hello.rb:11:1:16:3 | Greeting | world | hello.rb:5:5:7:7 | EnglishWords.world | -| hello.rb:18:1:22:3 | HelloWorld | hello | hello.rb:2:5:4:7 | EnglishWords.hello | -| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | HelloWorld.message | -| hello.rb:18:1:22:3 | HelloWorld | world | hello.rb:5:5:7:7 | EnglishWords.world | -| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | Foo.method_in_another_definition_of_foo | -| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | Foo.method_in_foo | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | Foo::Bar.method_in_another_definition_of_foo_bar | -| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | Foo::Bar.method_in_foo_bar | -| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | Bar.method_a | -| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | Bar.method_b | +| hello.rb:1:1:8:3 | EnglishWords | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:1:1:8:3 | EnglishWords | world | hello.rb:5:5:7:7 | world | +| hello.rb:11:1:16:3 | Greeting | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:11:1:16:3 | Greeting | message | hello.rb:13:5:15:7 | message | +| hello.rb:11:1:16:3 | Greeting | world | hello.rb:5:5:7:7 | world | +| hello.rb:18:1:22:3 | HelloWorld | hello | hello.rb:2:5:4:7 | hello | +| hello.rb:18:1:22:3 | HelloWorld | message | hello.rb:19:5:21:7 | message | +| hello.rb:18:1:22:3 | HelloWorld | world | hello.rb:5:5:7:7 | world | +| modules.rb:4:1:24:3 | Foo | method_in_another_definition_of_foo | modules.rb:27:3:28:5 | method_in_another_definition_of_foo | +| modules.rb:4:1:24:3 | Foo | method_in_foo | modules.rb:16:3:17:5 | method_in_foo | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_another_definition_of_foo_bar | modules.rb:52:3:53:5 | method_in_another_definition_of_foo_bar | +| modules.rb:5:3:14:5 | Foo::Bar | method_in_foo_bar | modules.rb:9:5:10:7 | method_in_foo_bar | +| modules.rb:37:1:46:3 | Bar | method_a | modules.rb:38:3:39:5 | method_a | +| modules.rb:37:1:46:3 | Bar | method_b | modules.rb:41:3:42:5 | method_b | diff --git a/ql/test/library-tests/modules/methods.ql b/ql/test/library-tests/modules/methods.ql index 1bb7296026d..323a4c61476 100644 --- a/ql/test/library-tests/modules/methods.ql +++ b/ql/test/library-tests/modules/methods.ql @@ -1,13 +1,6 @@ import ruby +import codeql_ruby.ast.internal.Module as M -query Method method() { any() } +query MethodBase getMethod(Module m, string name) { result = M::getMethod(m, name) } -query MethodBase getADeclaration(Method m) { result = m.getADeclaration() } - -query string getName(Method m) { result = m.getName() } - -query Module getModule(Method m) { result = m.getModule() } - -query Method getMethod(Module m, string name) { result = m.getMethod(name) } - -query Method lookupMethod(Module m, string name) { result = m.lookupMethod(name) } +query MethodBase lookupMethod(Module m, string name) { result = M::lookupMethod(m, name) }