Project

rbi

0.43
A long-lived project that still receives updates
RBI generation framework
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 4.0.1
~> 1.0
 Project Readme

RBI generation framework

RBI provides a Ruby API to build, parse, print, format, rewrite, and merge Ruby Interface files consumed by Sorbet.

Installation

rbi requires Ruby 3.3 or newer.

Add this line to your application's Gemfile:

gem "rbi"

And then execute:

bundle install

Or install it yourself as:

gem install rbi

Usage

Generating RBI

require "rbi"

rbi = RBI::File.new(strictness: "true") do |file|
  file << RBI::Module.new("Foo") do |mod|
    mod << RBI::Method.new("foo")
  end
end

puts rbi.string

will produce:

# typed: true

module Foo
  def foo; end
end

Parsing RBI

require "rbi"

tree = RBI::Parser.parse_string(<<~RBI)
  class Foo
    def bar; end
  end
RBI

puts tree.nodes.first.fully_qualified_name # => ::Foo

Formatting and rewriting RBI

require "rbi"

tree = RBI::Parser.parse_string(<<~RBI)
  class Foo
    def bar; end
  end
RBI

formatter = RBI::Formatter.new(add_sig_templates: true, group_nodes: true, sort_nodes: true)
formatter.format_tree(tree)

puts tree.string

will produce:

class Foo
  # TODO: fill in signature with appropriate type information
  sig { returns(::T.untyped) }
  def bar; end
end

Merging RBI trees

require "rbi"

left = RBI::Parser.parse_string(<<~RBI)
  class Foo
    def a; end
  end
RBI

right = RBI::Parser.parse_string(<<~RBI)
  class Foo
    def b; end
  end
RBI

puts left.merge(right).string

will produce:

class Foo
  def a; end
  def b; end
end

Working with Sorbet types

require "rbi"

type = RBI::Type.parse_string("T.nilable(String)")

puts type # => ::T.nilable(String)
puts type.rbs_string # => String?

Translating RBS comments to Sorbet signatures

require "rbi"

tree = RBI::Parser.parse_string(<<~RBI)
  #: (String) -> Integer
  def foo(name); end
RBI

tree.translate_rbs_sigs!

puts tree.string

will produce:

sig { params(name: String).returns(Integer) }
def foo(name); end

Printing RBS

require "rbi"

file = RBI::File.new do |f|
  f << RBI::Class.new("User") do |klass|
    klass << RBI::Method.new("name", sigs: [RBI::Sig.new(return_type: "String")])
  end
end

puts file.rbs_string

will produce:

class User
  def name: -> String
end

Features

  • RBI generation API
  • RBI parsing with Prism
  • RBI printing and formatting
  • RBI tree merging
  • RBI normalization rewrites
  • Sorbet type parsing and modeling
  • RBS comments to Sorbet signature translation
  • RBS output via RBI::RBSPrinter

Development

After checking out the repo, run bin/setup to install dependencies.

Useful commands:

  • bin/test runs the test suite
  • bin/typecheck runs Sorbet
  • bin/style runs RuboCop
  • bin/console starts an interactive prompt

Releasing

Bump the gem version

  • Locally, update the version number in version.rb
  • Run bundle install to update the version number in Gemfile.lock
  • Commit this change with the message Bump version to vx.y.z
  • Push this change directly to main or open a PR

Create a new tag

  • Locally, create a new tag with the new version number: git tag vx.y.z
  • Push this tag up to the remote git push origin vx.y.z

Release workflow will run automatically

We have a release workflow that will publish your new gem version to rubygems.org via Trusted Publishing. This workflow must be approved by a member of the Ruby and Rails Infrastructure team at Shopify before it will run. Once it is approved, it will automatically publish a new gem version to rubygems.org and create a new GitHub release.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/rbi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in this project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.