Giter Club home page Giter Club logo

gelauto's Introduction

gelauto Build Status

Automatically annotate your code with Sorbet type definitions.

What is This Thing?

The wonderful folks at Stripe recently released a static type checker for Ruby called Sorbet. It works by examining type signatures placed at the beginning of each method. For example:

# typed: true

class Car
  extend T::Sig

  sig { params(num_wheels: Integer) }
  def initialize(num_wheels)
    @num_wheels = num_wheels
  end

  sig { params(speed: Float).returns(T::Boolean) }
  def drive(speed)
    true
  end
end

Adding these definitions means you get cool stuff like auto-complete and type checking in your editor. Pretty freaking rad.

Ok, so what is Gelauto?

Gelauto is an auto-matic way (get it?! lol) of adding Sorbet type signatures to your methods. It works by running your code (for example, your test suite). As your code runs, Gelauto keeps track of the actual types of objects that were passed to your methods as well as the types of objects they return. After gathering the info, Gelauto then (optionally) inserts type signatures into your Ruby files.

Installation

gem install gelauto

Usage

You can run Gelauto either via the command line or by adding it to your bundle.

Command Line Usage

First, install the gem by running gem install gelauto. That will make the gelauto executable available on your system.

Gelauto's only subcommand is run, which accepts a list of Ruby files to scan for methods and a command to run that will exercise your code.

In this example, we're going to be running an RSpec test suite. Like most RSpec test suites, let's assume ours is stored in the spec/ directory (that's the RSpec default too). To run the test suite in spec/ and add type definitions to our ruby files, we might run the following command:

gelauto run --annotate $(find . -name '*.rb') -- bundle exec rspec spec/

You can also choose to run Gelauto with the --rbi flag, which will cause Gelauto to print results to standard output or to the given file in RBI format:

# print RBI output to STDOUT
gelauto run --annotate --rbi - $(find . -name '*.rb') -- bundle exec rspec spec/

# write RBI output to a file
gelauto run --annotate --rbi ./rbi/mylib.rbi $(find . -name '*.rb') -- bundle exec rspec spec/

In this second example, we're going to be running a minitest test suite. Like most minitest suites, let's assume ours is stored in the test/ directory (that's the Rails default too). To run the test suite in test/, we might run the following command:

gelauto run --annotate $(find . -name '*.rb') -- bundle exec rake test/

Gelauto in your Bundle

If you would rather run Gelauto as part of your bundle, add it to your Gemfile like so:

gem 'gelauto'

Gelauto can be invoked from within your code in one of several ways.

Gelauto.discover

Wrap code you'd like to run with Gelauto in Gelauto.discover:

require 'gelauto'

Gelauto.paths << 'path/to/file/i/want/to/annotate.rb'

Gelauto.discover do
  call_some_method(with, some, params)
end

# loop over files and annotate them
Gelauto.each_absolute_path do |path|
  Gelauto.annotate_file(path)
end

# you can also grab a reference to the method cache Gelauto
# has populated with all the type information it's been able
# to gather:
Gelauto.method_index

Setup and Teardown

Gelauto.discover is just syntactic sugar around two methods that start and stop Gelauto's method tracing functionality:

Gelauto.setup

begin
  call_some_method(with, some, params)
ensure
  Gelauto.teardown
end

RSpec Helper

Gelauto comes with a handy RSpec helper that can do most of this for you. Simply add

require 'gelauto/rspec'

to your spec_helper.rb, Rakefile, or wherever RSpec is configured. You'll also need to set the GELAUTO_FILES environment variable when running your test suite. For example:

GELAUTO_FILES=$(find . -name *.rb) bundle exec rspec

Files can be separated by spaces, newlines, or commas. If you want Gelauto to annotate them, set GELAUTO_ANNOTATE to true, eg:

GELAUTO_FILES=$(find . -name *.rb) GELAUTO_ANNOTATE=true bundle exec rspec

Finally, set GELAUTO_RBI=/path/to/output.rbi to have Gelauto emit an RBI file when the test suite finishes.

How does it Work?

Gelauto makes use of Ruby's TracePoint API. TracePoint effectively allows Gelauto to receive a notification whenever a Ruby method is called and whenever a method returns. That info combined with method location information gathered from parsing your Ruby files ahead of time allows Gelauto to know a) where methods are located, 2) what arguments they take, 3) the types of those arguments, and 4) the type of the return value.

"Doesn't that potentially make my code run slower?" is a question you might ask. Yes. Gelauto adds overhead to literally every Ruby method call, so your code will probably run quite a bit slower. For that reason you probably won't want to enable Gelauto in, for example, a production web application.

Known Limitations

  • Half-baked support for singleton (i.e. static) methods.
  • Gelauto does not annotate Ruby files with # typed: true comments or extend T::Sig.

Running Tests

bundle exec rspec should do the trick :)

Contributing

Please fork this repo and submit a pull request.

License

Licensed under the MIT license. See LICENSE for details.

Authors

gelauto's People

Contributors

camertron avatar jasnow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gelauto's Issues

Detect whether method has signature and skip gelauto on it

Gelauto ignores existing type signatures and will simply add another one right above the method.

I think this is a big usability issue because it's difficult to use gelauto on a file that already has some methods with signatures. It is possible to detect whether there is a sorbet signature around a method or not and skip sig generation for the method

See example: https://github.com/chanzuckerberg/sorbet-rails/blob/master/lib/sorbet-rails/sorbet_utils.rb#L11

Getting: uninitialized constant Gelauto::VERSION (NameError)

When I run "gelauto run --annotate $(find . -name '*.rb') -- bundle rspec spec",
I get this:

Traceback (most recent call last):
	4: from /Users/USER/.rvm/gems/[email protected]/bin/gelauto:23:in `<main>'
	3: from /Users/USER/.rvm/gems/[email protected]/bin/gelauto:23:in `load'
	2: from /Users/USER/.rvm/gems/[email protected]/gems/gelauto-1.0.0/bin/gelauto:6:in `<top (required)>'
	1: from /Users/USER/.rvm/gems/[email protected]/gems/gelauto-1.0.0/bin/gelauto:7:in `<module:Gelauto>'
/Users/USER/.rvm/gems/[email protected]/gems/gelauto-1.0.0/bin/gelauto:12:in `<module:CLI>': uninitialized constant Gelauto::VERSION (NameError)

Previously I ran "gem install gelauto".

Running on a Mac OS 10.12.6 on Mid 2014 MacBook Pro.

--rbi missing from "--help" output and unknown option

just released v1.1.0 that logs to STDERR instead of STDOUT. Also added a global --silent flag and --rbi option for the run subcommand.

Do not see the --rbi in "--help" output.

When I tried "--rbi", I get: error: Unknown option --rbi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.