dgsuarez / reruby

Refactor Ruby
MIT License
35 stars 2 forks source link
ruby

Reruby CircleCI

Automatic refactorings for Ruby.

A note on safety

While we try to make Reruby as safe as possible to use, we still consider it to be alpha. Please, commit your code before running any Reruby command.

Rationale

Ruby is notoriously hard to refactor automatically, it's not just a dynamic language, but one where metaprogramming is not only allowed but even encouraged.

Reruby uses the excellent parser gem to transform Ruby code. This means that we mostly rely on syntactic information to perform the refactorings. Although not ideal, we've found that it works remarkably well for the operations that Reruby provides, and while it's true that some use cases are necessarily left out we firmly believe that a refactoring tool that works 90% of the time is better than no tool at all.

Installation

We are waiting for 0.1.0 in order to release on Rubygems. Meanwhile you can install by:

git clone https://github.com/dgsuarez/reruby.git
cd reruby
rake install

Available refactorings

Note: Some of the refactorings will produce code that is not properly indented and that may not follow your own styleguide. Reruby can optionally run rubocop autofix on the changed/created files after those operations by specifying the --rubocop-autofix flag or adding it to your configuration file (see below for more on this).

Rename Const

Rename classes or modules:

reruby rename_const 'Some::Const' 'NewConstName'

This will:

Right now it won't, but should...

Extract method

Extract method refactoring:

reruby extract_method -l some/path.rb:2:10:4:9 my_new_method

This will:

Right now it won't, but should...

Explode Namespace

Extract classes or modules defined in the same file as their parent to a file for each.

reruby explode_namespace MyClass

This will:

Right now it won't, but should...

Instances to readers

Turn any instance variable read to a call to the reader method, adding the attr_reader declaration as well.

reruby instances_to_readers MyClass

This will:

Right now it won't, but should...

Configuration

You can have per project configuration files in .reruby.yml, or a global one in ~/.reruby.yml.

Here is a sample full configuration file:

paths:
  exclude: # Regexes for paths to ignore when looking for code to transform
    - ^vendor/
    - ^coverage/
    - ^log/
ruby_extensions: # Extensions for ruby files in your project
  - .rb
  - .rake
  - .gemspec
  - Rakefile
rubocop_autofix: true # Run rubocop -a over the files that have changed
extract_method:
  keyword_arguments: true # Use keyword arguments when using extract method

All options can also be specified using the command line, run reruby --help for more information.

Editor integration

Reruby reaches it's full potential when it's integrated in your text editor, here's a list of available plugins. Please let us know which integrations you would like to see, or if you know of an integration not available here.

If you'd like to create a new integration, our guidelines may help you get started.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To run the executable while in development you can use bundle exec exe/reruby ….

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dgsuarez/reruby.

License

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