Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSense Knows Your Code

RSense Knows Your Code

Rsense brings the kind of static analysis tooling to Ruby that programmers in other languages take for granted. The kind of tooling I kept hearing was impossible to implement for Ruby. Accurate type inference, code completion, definition finding, and eventually so much more. How about a real refactoring tool, or the ability to find whole classes of bugs without executing the code? This is where Rsense is headed.

Learn how Rsense does its magic, hear the story of how one determined noob brought it back to life, and find out how you can use it to improve your daily coding experience.

Talk given at JRubyConfEU/eurucamp 2014 (Berlin, Germany)

http://rsense.github.io/
https://github.com/rsense/rsense

edubkendo

August 01, 2014
Tweet

Other Decks in Programming

Transcript

  1. RSense is a static-analysis, type- inference tool for Ruby code.

    ie. It reads in source code and provides information about it.
  2. This means we can build tools with this information: —

    code completion — find-definition — bug-detection — refactoring (Can your ctags do all that?)
  3. Google Summer of Code GSoC pays students a stipend to

    work on an open- source project. In 2013, JRuby was selected to be a participating organization. (As they had the year prior.)
  4. Why wasn't rsense more widely adopted when it originally debuted?

    — People had a hard time installing it. — Written in Java.
  5. Rsense gem uses Spoon (based on posix_spawn) to launch a

    jruby process (via the jruby-jars gem).
  6. Cartesian Product Algorithm CPA was first implemented for the language

    Self, by Ole Agesen. There is a paper describing a project called Ecstatic that appears to have been a major influence on Rsense. (Search ecstatic ruby type inference to find it.)
  7. Source code is parsed into an AST so that compilers

    and interpreters have a structure more suitable for processing.
  8. jruby-parser Library originally extracted from JRuby. Parser with special methods

    for use by IDEs and tools. Especially tracks source code position.
  9. Rsense walks the AST, building up a graph of the

    nodes in the AST via an Abstract Interpreter.
  10. Abstract Interpreter A Virtual Ruby. It interprets the code in

    order to make sense of branches, conditionals, etc without executing any code.
  11. Rsense relies on stubbed out copies of the Ruby core

    libs and std_lib to fill in missing information.
  12. This would be almost enough, however, Ruby's dynamic metaprogramming feature

    does mean that a small subset of valid Ruby expressions cannot be type- inferred.
  13. Annotation Resolver The annotation resolver uses a special type annotation

    syntax in the comments to allow these rare edge cases to be resolved. Otherwise the failed inferences would bleed into the results and taint them or prevent types from propagating altogether.
  14. This is mostly only necessary in the built-in stubs, because

    outside of the std_lib these techniques are less common.
  15. Still, I'd like to move Rsense off of the unconventional

    annotation syntax and towards using something many Rubyists already use (YARD).
  16. TODO: — Vim & Emacs plugins — Caching the type

    graph (fix Rails) — Switch to YARD for annotations — Expose find-definition, where, type-inference — Build bug detection, refactoring tools on top