Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby: query to automatically extract type definitions from library code #13750

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexrford
Copy link
Contributor

Currently very limited and WIP - this automatically extracts some type definitions from a library codebase in a MaD amenable format.

The first query looks at returned values from methods in the codebase and determines if they return an instance of some class that we can find. For instance, in the case of some code like:

class Foo
  def get_bar
    Bar.new
  end

  def get_bar_indirectly
    get_bar
  end
end

class Bar
end

We can determine that the return value from get_bar or get_bar_indirectly may be a Bar instance, giving us typeModel rows of:

type1 type2 path
Bar Foo Method[get_bar].ReturnValue
Bar Foo Method[get_bar_indirectly].ReturnValue

The second query looks at cases where we call some method and pass it an instance of some type. e.g. in

class Foo
  def execute_with_database(db, action)
    ...
  end
end
...

db = Some::Database.new
foo = Foo.new

foo.execute_with_database(db, "SELECT name FROM users")

we get:

type1 type2 path
Some::Database Foo Method[execute_with_database].Parameter[0]

This is heavily WIP at the moment - many of the limitations are TODOs in the code, but as a quick list of things that are not yet supported or need additional work:

  • singleton methods
  • non-positional parameters
  • parameters to block arguments
  • cases where there are multiple potential access paths to a value
  • heuristic ways to determine types outside of calls to SomeClass.new
  • construction of access paths in general is rough - possibly API graphs can help here

@alexrford alexrford added WIP This is a work-in-progress, do not merge yet! Ruby labels Jul 14, 2023
concat(Ast::Expr ancestor, int depth |
ancestor = getAncestor(e, depth) and depth = [0 .. 10]
|
ancestor.toString(), "::" order by depth desc

Check warning

Code scanning / CodeQL

Using 'toString' in query logic Warning

Query logic depends on implementation of 'toString'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ruby WIP This is a work-in-progress, do not merge yet!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant