• Stars
    star
    1,443
  • Rank 31,332 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 12 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Prettier Ruby Plugin
Prettier Ruby

Prettier for Ruby

Gitter GitHub Actions NPM Version code style: prettier Follow+Prettier+on+Twitter

@prettier/plugin-ruby is a prettier plugin for the Ruby programming language and its ecosystem. 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

The @prettier/plugin-ruby plugin for prettier is a small wrapper around the Syntax Tree gem that provides a Ruby formatter for prettier. It does this by keeping a Ruby server running in that background that prettier can communicate with when it needs to format a Ruby file. This means that in order to function, you will need to have both the requisite node and ruby dependencies installed. Because of this configuration, there are a couple of ways that you can get setup to use this plugin.

  • If you're already using prettier in your project to format other files in your project and want to install this as a plugin, you can install it using npm.
  • If you're not using prettier yet in your project, then we recommend using the Syntax Tree gem directly instead of using this plugin.
  • Note that this plugin also ships a gem named prettier which is a wrapper around the prettier CLI and includes this plugin by default, but we no longer recommend its use. If you're using that gem, you should migrate to using Syntax Tree instead.

To run prettier with the Ruby plugin as an npm package, you're going to need ruby (version 2.7 or newer) and node (version 16 or newer).

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

You'll also need to add the necessary Ruby dependencies. You can do this by running:

gem install bundler prettier_print syntax_tree syntax_tree-haml syntax_tree-rbs

The prettier executable is now installed and ready for use:

./node_modules/.bin/prettier --plugin=@prettier/plugin-ruby --write '**/*'

Using Prettier >= 3.0

You need to tell Prettier to use the plugin, add the following to your existing prettier configuration file.

{
  "plugins": ["@prettier/plugin-ruby"]
}

Configuration

Below are the options (from src/plugin.js) that @prettier/plugin-ruby currently supports:

API Option CLI Option Default Description
printWidth --print-width 80 Same as in Prettier (see prettier docs).
requirePragma --require-pragma false Same as in Prettier (see prettier docs).
rubyExecutablePath "ruby" Allows you to configure your Ruby executable path.
rubyPlugins --ruby-plugins "" The comma-separated list of plugins to require. See Syntax Tree.
rubySingleQuote --ruby-single-quote false Whether or not to default to single quotes for Ruby code. See Syntax Tree.
tabWidth --tab-width 2 Same as in Prettier (see prettier docs).
trailingComma --trailing-comma es5 Almost same as in Prettier (see prettier docs). Will be on for any value except none.

Any of these can be added to your existing prettier configuration file. For example:

{
  "tabWidth": 4
}

Or, they can be passed to prettier as arguments:

bundle exec rbprettier --tab-width 4 --write '**/*'

Ignoring code

Sometimes you want to leave your formatting in place and have prettier not format it, but continue to format the rest of the file. prettier has the ability to do this with prettier-ignore comments, but because the underlying formatter for this plugin is Syntax Tree, you instead would use a stree-ignore comment.

Usage with RuboCop

RuboCop and Prettier for Ruby serve different purposes, but there is overlap with some of RuboCop's functionality. Prettier provides a RuboCop configuration file to disable the rules which would clash. To enable this file, add the following configuration at the top of your project's .rubocop.yml:

inherit_from:
  - node_modules/@prettier/plugin-ruby/rubocop.yml

Usage with an editor

For supported editor integrations, you should follow the instructions for installing the integration, then install the npm version of this plugin as a development dependency of your project. For most integrations, that should be sufficient. For convenience, the instructions for integrating with VSCode are used as an example below:

  • Install the Prettier - Code Formatter extension.
  • Add the npm @prettier/plugin-ruby package to your project as described above.
  • Configure in your settings.json (formatOnSave is optional):
{
  "[ruby]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true
  }
}

Refer to this issue if you're having difficulties.

Contributing

Thanks so much for your interest in contributing! You can contribute in many ways, including:

  • Contributing code to fix any bugs on GitHub.
  • Reporting issues on GitHub.
  • Supporting prettier/plugin-ruby on OpenCollective. Your organization's logo will show up here with a link to your website.

License

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

More Repositories

1

prettier

Prettier is an opinionated code formatter.
JavaScript
48,276
star
2

eslint-config-prettier

Turns off all rules that are unnecessary or might conflict with Prettier.
JavaScript
5,171
star
3

prettier-vscode

Visual Studio Code extension for Prettier
TypeScript
5,003
star
4

prettier-eslint

Code ➑️ prettier ➑️ eslint --fix ➑️ Formatted Code ✨
JavaScript
3,933
star
5

eslint-plugin-prettier

ESLint plugin for Prettier formatting
JavaScript
3,140
star
6

pretty-quick

⚑ Get Pretty Quick
TypeScript
2,173
star
7

vim-prettier

A Vim plugin for Prettier
Vim Script
1,751
star
8

plugin-php

Prettier PHP Plugin
PHP
1,704
star
9

tslint-config-prettier

Use TSLint with Prettier without any conflict
TypeScript
1,233
star
10

prettier-atom

An atom package for the prettier formatter.
JavaScript
756
star
11

prettier-eslint-cli

CLI for prettier-eslint
JavaScript
531
star
12

plugin-python

Prettier Python Plugin
JavaScript
516
star
13

stylelint-config-prettier

Turns off all rules that are unnecessary or might conflict with prettier.
JavaScript
373
star
14

prettier-emacs

Minor mode to format JS code on file save
Emacs Lisp
368
star
15

stylelint-prettier

Stylelint plugin for Prettier formatting
JavaScript
328
star
16

plugin-swift

[DEPRECATED] Prettier Swift Plugin - WARNING: The AST parser is not stable yet
JavaScript
276
star
17

tslint-plugin-prettier

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues
TypeScript
235
star
18

plugin-xml

Prettier XML plugin
JavaScript
218
star
19

prettier-browser-extension

Prettier Browser Extension
JavaScript
198
star
20

plugin-pug

Prettier Pug Plugin
TypeScript
193
star
21

plugin-lua

Prettier Lua Plugin (WIP)
Lua
79
star
22

prettier-logo

The Prettier logo.
JavaScript
38
star
23

prettier-cli

TypeScript
24
star
24

prettier-printer

Library for building and pretty printing text documents
JavaScript
23
star
25

yaml-unist-parser

A YAML parser that produces output compatible with unist
TypeScript
22
star
26

stylelint-config-prettier-scss

Turns off all CSS and SCSS rules that are unnecessary or might conflict with prettier.
JavaScript
19
star
27

prettier-linter-helpers

Helper functions for exposing prettier changes within linting tools
JavaScript
17
star
28

angular-estree-parser

A parser that converts Angular source code into an ESTree-compatible form
TypeScript
16
star
29

prettier-synchronized

Synchronous version of Prettier
JavaScript
14
star
30

prettier-rpc

Single-file build of prettier with JSON-RPC communication
JavaScript
13
star
31

prettier-regression-testing

Automates the prettier/prettier regression checks with GitHub Actions.
TypeScript
11
star
32

parse-srcset

A spec-conformant JavaScript parser for the HTML5 srcset attribute
JavaScript
8
star
33

plugin-haml

Plugin for the HAML template language
JavaScript
5
star
34

pre-commit

Mirror of Prettier package for pre-commit.
JavaScript
5
star
35

is-es5-identifier-name

Check if provided string is an `IdentifierName` as specified in ECMA262 edition 5.1 section 7.6.
JavaScript
2
star
36

core-test-fixtures

Test fixtures for Prettier core.
JavaScript
2
star
37

eslint-plugin-prettier-internal-rules

Wrapper for Prettier internal rules
JavaScript
1
star