What is @prettier/plugin-ruby?
@prettier/plugin-ruby is a plugin for Prettier that adds support for formatting Ruby code. It ensures that Ruby code adheres to a consistent style, making it easier to read and maintain.
What are @prettier/plugin-ruby's main functionalities?
Code Formatting
This feature automatically formats Ruby code to follow a consistent style. For example, it ensures proper indentation and spacing.
def hello_world
puts 'Hello, world!'
end
Consistent Style
This feature enforces a consistent coding style across Ruby files, such as consistent use of spaces, line breaks, and other stylistic elements.
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
Integration with Prettier
This feature allows the plugin to be easily integrated with Prettier, enabling seamless formatting of Ruby code alongside other supported languages.
{
"plugins": ["@prettier/plugin-ruby"]
}
Other packages similar to @prettier/plugin-ruby
standard
Standard is a Ruby style guide, linter, and formatter based on RuboCop. It enforces a standard style and can automatically fix issues, similar to @prettier/plugin-ruby, but with a focus on simplicity and minimal configuration.
Prettier for Ruby
@prettier/plugin-ruby
is a prettier plugin for the Ruby programming language (versions 2.5
and above). prettier
is an opinionated code formatter that supports multiple languages and integrates with most editors. The idea is to eliminate discussions of style in code review and allow developers to get back to thinking about code design instead.
For example, the below code segment:
d=[30644250780,9003106878,
30636278846,66641217692,4501790980,
671_24_603036,131_61973916,66_606629_920,
30642677916,30643069058];a,s=[],$*[0]
s.each_byte{|b|a<<("%036b"%d[b.
chr.to_i]).scan(/\d{6}/)}
a.transpose.each{ |a|
a.join.each_byte{\
|i|print i==49?\
($*[1]||"#")\
:32.chr}
puts
}
when run through @prettier/plugin-ruby
will generate:
d = [
30_644_250_780,
9_003_106_878,
30_636_278_846,
66_641_217_692,
4_501_790_980,
671_24_603036,
131_61973916,
66_606629_920,
30_642_677_916,
30_643_069_058
]
a, s = [], $*[0]
s.each_byte { |b| a << ('%036b' % d[b.chr.to_i]).scan(/\d{6}/) }
a.transpose.each do |a|
a.join.each_byte { |i| print i == 49 ? ($*[1] || '#') : 32.chr }
puts
end
Getting started
To run prettier
with the Ruby plugin, you're going to need ruby
(version 2.5
or newer) and node
(version 8.3
or newer). If you're integrating with a project that is not already using prettier
, you should use the ruby gem. Otherwise you can use the npm
package directly.
Ruby gem
Add this line to your application's Gemfile:
gem 'prettier'
And then execute:
bundle
Or install it yourself as:
gem install prettier
The rbprettier
executable is now installed and ready for use:
bundle exec rbprettier --write '**/*.rb'
npm
package
If you're using the npm
CLI, then add the plugin by:
npm install --save-dev prettier @prettier/plugin-ruby
Or if you're using yarn
, then add the plugin by:
yarn add --dev prettier @prettier/plugin-ruby
The prettier
executable is now installed and ready for use:
./node_modules/.bin/prettier --write '**/*.rb'
Configuration
Below are the options (from src/ruby.js
) that @prettier/plugin-ruby
currently supports:
Name | Default | Description |
---|
printWidth | 80 | Same as in Prettier (see prettier docs). |
requirePragma | false | Same as in Prettier (see prettier docs). |
tabWidth | 2 | Same as in Prettier (see prettier docs). |
addTrailingCommas | false | Adds a trailing comma to array literals, hash literals, and method calls. |
inlineConditionals | true | When it fits on one line, allows if and unless statements to use the modifier form. |
inlineLoops | true | When it fits on one line, allows while and until statements to use the modifier form. |
preferHashLabels | true | When possible, uses the shortened hash key syntax, as opposed to hash rockets. |
preferSingleQuotes | true | When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals. |
Any of these can be added to your existing prettier configuration
file. For example:
{
"preferSingleQuotes": false
}
Or, they can be passed to prettier
as arguments:
prettier --prefer-single-quotes false --write '**/*.rb'
Contributing
Check out our contributing guide. Bug reports and pull requests are welcome on GitHub at https://github.com/prettier/plugin-ruby.
License
The package is available as open source under the terms of the MIT License.
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
[0.15.1] - 2019-11-05
Changed
- AlanFoster - Add
bin/lex
for viewing the tokenized result of Ripper on Ruby code. - jakeprime, kddnewton - When predicates from within an
if
, unless
, while
, or until
loop break the line, they should be aligned together. For example,
<!-- prettier-ignore -->
if foooooo || barrrrrr
baz
end
If the line was set to very short, the binary node should be aligned to 3 spaces from the left of the file (which aligns with the if
, it would be more for unless
). So it would look like:
<!-- prettier-ignore -->
if foooooo ||
barrrrrr
baz
end
- jamescostian], [@AlanFoster - Empty
if
, and unless
conditionals are now handled gracefully:
<!-- prettier-ignore -->
if foo?
end
- mmainz, kddnewton - Hash keys are not converted to keyword syntax if they would make invalid symbols. For example,
<!-- prettier-ignore -->
{ :[] => nil }
cannot be translated into []:
as that is an invalid symbol. Instead, it stays with the hash rocket syntax.
- cldevs, kddnewton - Do not attempt to format the insides of xstring literals (string that get sent to the command line surrounded by backticks or
%x
). - cldevs, kddnewton - When predicates for
if
, unless
, while
, or until
nodes contain an assignment, we can't know for sure that it doesn't modify the body. In this case we need to always break and form a multi-line block. - MarcManiez, kddnewton - When the return value of
if
, unless
, while
, or until
nodes are assigned to anything other than a local variable, we need to wrap them in parentheses if we're changing to the modifier form. This is because the following expressions have different semantic meaning:
<!-- prettier-ignore -->
hash[:key] = break :value while false
hash[:key] = while false do break :value end
The first one will not result in an empty hash, whereas the second one will result in { key: nil }
. In this case what we need to do for the first expression to align is wrap it in parens, as in:
<!-- prettier-ignore -->
hash[:key] = (break :value while false)
That will guarantee that the expressions are equivalent.
- AlanFoster - Fix crashes that were happening with
ignored_nl
nodes.