standard 1.27.0 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,295 +1,247 @@
1
- ## Standard - Ruby style guide, linter, and formatter
1
+ <img src="https://user-images.githubusercontent.com/79303/233717126-9fd13e6d-9a66-4f1c-b40c-fe875cb1d1b4.png" style="width: 100%"/>
2
2
 
3
- [![Tests](https://github.com/testdouble/standard/workflows/Tests/badge.svg)](https://github.com/testdouble/standard/actions?query=workflow%3ATests)
4
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
3
+ [![Tests](https://github.com/standardrb/standard/actions/workflows/test.yml/badge.svg)](https://github.com/standardrb/standard/actions/workflows/test.yml)
4
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/standardrb/standard)
5
5
  [![Gem Version](https://badge.fury.io/rb/standard.svg)](https://rubygems.org/gems/standard)
6
6
 
7
- This gem is a spiritual port of [StandardJS](https://standardjs.com) and aims
8
- to save you (and others!) time in the same three ways:
7
+ The `standard` gem brings the ethos of [StandardJS](https://standardjs.com) to
8
+ Ruby. It's a linter & formatter built on
9
+ [RuboCop](https://github.com/rubocop/rubocop) and provides an **unconfigurable
10
+ configuration** to all of RuboCop's built-in rules as well as those included in
11
+ [rubocop-performance](https://github.com/rubocop/rubocop-performance). It also
12
+ supports plugins built with
13
+ [lint_roller](https://github.com/standardrb/lint_roller), like
14
+ [standard-rails](https://github.com/standardrb/standard-rails).
15
+
16
+ Standard Ruby was created and is maintained by the team at [Test
17
+ Double](https://testdouble.com), because we appreciate the importance of
18
+ balancing predictably consistent code with preserving developers' autonomy. 💚
19
+
20
+ Topics covered in this README:
21
+
22
+ * [Wait, what?!](#wait-did-you-say-unconfigurable-configuration)
23
+ * [Basic usage](#usage)
24
+ * [Editor & CI integrations](#integrating-standard-into-your-workflow)
25
+ * [Ignoring errors](#ignoring-errors)
26
+ * [Configuration options](#configuring-standard)
27
+ * [Plugins and extensions](#extending-standard)
28
+ * [Community](#who-uses-standard-ruby)
29
+
30
+ ## Wait, did you say unconfigurable configuration?
31
+
32
+ Yes, Standard is unconfigurable. See, pretty much every developer can agree that
33
+ automatically identifying and fixing slow, insecure, and error-prone code is a
34
+ good idea. People also agree it's easier to work in codebases that exhibit a
35
+ consistent style and format. So, what's the problem? **No two developers will
36
+ ever agree on what all the rules and format should be.**
37
+
38
+ This has resulted in innumerable teams arguing how to configure their linters
39
+ and formatters over literal decades. Some teams routinely divert time and energy
40
+ from whatever they're building to reach concensus on where commas should go.
41
+ Other teams have an overzealous tech lead who sets up everything _his favorite
42
+ way_ and then imposes his will on others. It's not uncommon to witness
43
+ passive-aggressive programmers change a contentious rule back-and-forth,
44
+ resulting in both acrimony and git churn (and acrimony _about_ git churn).
45
+ Still, most developers give way to whoever cares more, often resulting in an
46
+ inconsistent configuration that nobody understands and isn't kept up-to-date
47
+ with changes to their linter and its target language. Whatever the approach,
48
+ time spent
49
+ [bikeshedding](https://blog.testdouble.com/posts/2019-09-17-bike-shed-history/)
50
+ is time wasted.
51
+
52
+ **But you can't configure Standard Ruby.** You can take it or leave it. If this
53
+ seems presumptive, constraining, and brusque: you're right, it usually does feel
54
+ that way at first. But [since
55
+ 2018](https://www.youtube.com/watch?v=uLyV5hOqGQ8), the Ruby community has
56
+ debated Standard Ruby's ruleset, ultimately landing us on a
57
+ [sensible](/config/base.yml)
58
+ [set](https://github.com/standardrb/standard-performance/blob/main/config/base.yml)
59
+ of
60
+ [defaults](https://github.com/standardrb/standard-custom/blob/main/config/base.yml)
61
+ that—_while no one person's favorite way to format Ruby_—seems to be good enough
62
+ for most of the ways people use Ruby, most of the time.
63
+
64
+ If you adopt Standard Ruby, what's in it for you? Time saved that would've been
65
+ spent arguing over how to set things up. Also, seamless upgrades: we stay on top
66
+ of each new RuboCop rule ship an updated configuration on a monthly release
67
+ cadence. But the best part of adopting Standard as your linter and formatter?
68
+ You'll spend a whole lot less time thinking about linters and formatters.
69
+
70
+ So please, give Standard Ruby a try. If you're like [these
71
+ folks](#who-uses-standard-ruby), you'll soon realize that the value of a linter
72
+ is in using one at all and not in the particulars of how it's configured.
9
73
 
10
- * **No configuration.** The easiest way to enforce consistent style in your
11
- project. Just drop it in.
12
- * **Automatically format code.** Just run `standardrb --fix` and say goodbye to
13
- messy or inconsistent code.
14
- * **Catch style issues & programmer errors early.** Save precious code review
15
- time by eliminating back-and-forth between reviewer & contributor.
74
+ ## Usage
16
75
 
17
- No decisions to make. It just works. Here's a [⚡ lightning talk
18
- ⚡](https://www.youtube.com/watch?v=uLyV5hOqGQ8) about it.
76
+ ### Install
19
77
 
20
- Install Standard by adding it to your Gemfile and running `bundle install`:
78
+ Getting started is as easy as `gem install standard` or throwing it in your
79
+ project's Gemfile:
21
80
 
22
81
  ```ruby
23
82
  gem "standard", group: [:development, :test]
24
83
  ```
25
84
 
26
- You can then run Standard from the command line with:
27
-
28
- ```ruby
29
- $ bundle exec standardrb
30
- ```
85
+ ### Running Standard
31
86
 
32
- And if you'd like, Standard can autocorrect your code by tacking on a `--fix`
33
- flag.
87
+ Once installed, you can either run it as a CLI or as a Rake task.
34
88
 
35
- If your project uses Rake, adding `require "standard/rake"` adds two tasks:
36
- `standard` and `standard:fix`. In most new projects, we tend to add the fixer
37
- variant to our default `rake` task after our test command, similar to this:
89
+ The CLI is called `standardrb` to distinguish it from
90
+ [StandardJS](https://github.com/standard/standard):
38
91
 
39
- ```ruby
40
- task default: [:test, "standard:fix"]
41
92
  ```
42
-
43
- ## StandardRB — The Rules
44
-
45
- - **2 spaces** – for indentation
46
- - **Double quotes for string literals** - because pre-committing to whether
47
- you'll need interpolation in a string slows people down
48
- - **1.9 hash syntax** - When all the keys in a hash literal are symbols,
49
- Standard enforces Ruby 1.9's `{hash: syntax}`
50
- - **Braces for single-line blocks** - Require `{`/`}` for one-line blocks, but
51
- allow either braces or `do`/`end` for multiline blocks. Like using `do`/`end`
52
- for multiline blocks? Prefer `{`/`}` when chaining? A fan of expressing intent
53
- with Jim Weirich's [semantic
54
- block](http://www.virtuouscode.com/2011/07/26/the-procedurefunction-block-convention-in-ruby/)
55
- approach? Standard lets you do you!
56
- - **Leading dots on multi-line method chains** - chosen for
57
- [these](https://github.com/testdouble/standard/issues/75) reasons.
58
- - **Spaces inside blocks, but not hash literals** - In Ruby, the `{` and `}`
59
- characters do a lot of heavy lifting. To visually distinguish hash literals
60
- from blocks, Standard enforces that (like arrays), no leading or trailing
61
- spaces be added to pad hashes
62
- - **And a good deal more**
63
-
64
- If you're familiar with [RuboCop](https://github.com/rubocop-hq/rubocop), you
65
- can look at Standard's current base configuration in
66
- [config/base.yml](/config/base.yml). In lieu of a separate changelog file,
67
- significant changes to the configuration will be documented as [GitHub release
68
- notes](https://github.com/testdouble/standard/releases).
69
-
70
- ## Usage
71
-
72
- Once you've installed Standard, you should be able to use the `standardrb`
73
- program. The simplest use case would be checking the style of all Ruby
74
- files in the current working directory:
75
-
76
- ```bash
77
- $ bundle exec standardrb
78
- standard: Use Ruby Standard Style (https://github.com/testdouble/standard)
79
- standard: Run `standardrb --fix` to potentially fix one problem.
80
- /Users/jill/code/cli.rb:31:23: Style/Semicolon: Do not use semicolons to terminate expressions.
93
+ $ standardrb
81
94
  ```
82
95
 
83
- You can optionally pass in a directory (or directories) using a glob pattern.
84
- Be sure to quote paths containing glob patterns so that they are expanded by
85
- `standardrb` instead of your shell:
96
+ And the Rake task can be run if your Rakefile includes `require
97
+ "standard/rake"`. This will load the `standard` task, allowing you to run:
86
98
 
87
- ```bash
88
- $ bundle exec standardrb "lib/**/*.rb" test
89
99
  ```
90
-
91
- **Note:** by default, StandardRB will look for all `*.rb` files (and some other
92
- files typically associated with Ruby like `*.gemspec` and `Gemfile`)
93
-
94
- If you want to add Standard to an existing project, but don't want to stop all
95
- development until you've fixed every violation in every file, you can create a
96
- backlog of to-be-converted files by generating a TODO file:
97
-
98
- ```bash
99
- $ bundle exec standardrb --generate-todo
100
+ $ rake standard
100
101
  ```
101
102
 
102
- This will create a `.standard_todo.yml` that lists all the files that contain
103
- errors. When you run Standard in the future, it will ignore these files as if
104
- they were listed under the `ignore` section in the `.standard.yml` file.
103
+ Either way, Standard will inspect any Ruby files found in the current directory
104
+ tree. If any errors are found, it'll report them. If your code is
105
+ standard-compliant, it will get out of your way by quietly exiting code 0.
105
106
 
106
- As you refactor your existing project you can remove files from the list. You
107
- can also regenerate the TODO file at any time by re-running the above command.
107
+ ### Fixing errors
108
108
 
109
- ### Using with Rake
109
+ A majority of Standard's rules can be safely fixed automatically.
110
110
 
111
- Standard also ships with Rake tasks. If you're using Rails, these should
112
- autoload and be available after installing Standard. Otherwise, just require the
113
- tasks in your `Rakefile`:
111
+ ```
112
+ # CLI
113
+ $ standard --fix
114
114
 
115
- ```ruby
116
- require "standard/rake"
115
+ # Rake
116
+ $ rake standard:fix
117
117
  ```
118
118
 
119
- Here are the tasks bundled with Standard:
119
+ Because we're confident Standard's fixes won't change the behavior of our code,
120
+ we typically run with fix enabled _all the time_ because it saves us from having
121
+ to look at and think about problems the computer can solve for us.
120
122
 
121
- ```
122
- $ rake standard # equivalent to running `standardrb`
123
- $ rake standard:fix # equivalent to running `standardrb --fix`
124
- $ rake standard:fix_unsafely # equivalent to running `standardrb --fix-unsafely`
125
- ```
123
+ ### Applying unsafe fixes
126
124
 
127
- You may also pass command line options to Standard's Rake tasks by embedding
128
- them in a `STANDARDOPTS` environment variable (similar to how the Minitest Rake
129
- task accepts CLI options in `TESTOPTS`).
125
+ There are a number of other rules that can be automatically remedied, but not
126
+ without the risk of changing your code's behavior. While we wouldn't recommend
127
+ running it all the time, if the CLI suggests that additional errors can be fixed
128
+ _unsafely_, here's how to do that:
130
129
 
131
130
  ```
132
- # equivalent to `standardrb --format progress`:
133
- $ rake standard STANDARDOPTS="--format progress"
131
+ # CLI
132
+ $ standardrb --fix-unsafely
134
133
 
135
- # equivalent to `standardrb lib "app/**/*"`, to lint just certain paths:
136
- $ rake standard STANDARDOPTS="lib \"app/**/*\""
134
+ # Rake
135
+ $ rake standard:fix_unsafely
137
136
  ```
138
137
 
139
- ## What you might do if you're clever
138
+ So long as your code is checked into source control, there's no mortal harm in
139
+ running with unsafe fixes enabled. If the changes look good to you and your
140
+ tests pass, then it's probably less error prone than manually editing everything
141
+ by hand.
140
142
 
141
- If you want or need to configure Standard, there are a _handful_ of options
142
- available by creating a `.standard.yml` file in the root of your project.
143
+ ## Integrating Standard into your workflow
143
144
 
144
- Here's an example yaml file with every option set:
145
+ Because code formatting is so integral to programming productivity and linter
146
+ violations risk becoming bugs if released into production, tools like
147
+ Standard Ruby are only as useful as their integration into code editors and
148
+ continuous integration systems.
145
149
 
146
- ```yaml
147
- fix: true # default: false
148
- parallel: true # default: false
149
- format: progress # default: Standard::Formatter
150
- ruby_version: 2.3 # default: RUBY_VERSION
151
- default_ignores: false # default: true
150
+ ### Editor support
152
151
 
153
- ignore: # default: []
154
- - 'db/schema.rb'
155
- - 'vendor/**/*'
156
- - 'test/**/*':
157
- - Layout/AlignHash
158
- ```
159
-
160
- Note: If you're running Standard in a context where your `.standard.yml` file
161
- cannot be found by ascending the current working directory (i.e., against a
162
- temporary file buffer in your editor), you can specify the config location with
163
- `--config path/to/.standard.yml`. (Similarly, for the `.standard_todo.yml` file,
164
- you can specify `--todo path/to/.standard_todo.yml`.)
152
+ We've added a number of editing guides for getting started:
165
153
 
166
- ## What you might do if you're REALLY clever
167
-
168
- Because StandardRB is essentially a wrapper on top of
169
- [RuboCop](https://github.com/rubocop-hq/rubocop), it will actually forward the
170
- vast majority of CLI and ENV arguments to RuboCop.
154
+ - [VS Code](https://github.com/standardrb/vscode-standard-ruby)
155
+ - [vim](https://github.com/standardrb/standard/wiki/IDE:-vim)
156
+ - [neovim](https://github.com/standardrb/standard/wiki/IDE:-neovim)
157
+ - [RubyMine](https://www.jetbrains.com/help/ruby/rubocop.html#disable_rubocop)
158
+ - [emacs](https://github.com/julianrubisch/flycheck-standardrb)
159
+ - [Atom](https://github.com/standardrb/standard/wiki/IDE:-Atom)
171
160
 
172
- You can see a list of
173
- [RuboCop](https://docs.rubocop.org/rubocop/usage/basic_usage.html#command-line-flags)'s
174
- CLI flags here.
161
+ If you'd like to help by creating a guide, please draft one [in an
162
+ issue](https://github.com/standardrb/standard/issues/new) and we'll get it
163
+ added!
175
164
 
176
- ## Why should I use Ruby Standard Style?
165
+ #### Language Server Protocol support
177
166
 
178
- (This section will [look
179
- familiar](https://github.com/standard/standard#why-should-i-use-javascript-standard-style)
180
- if you've used StandardJS.)
167
+ If you don't see your preferred editor above, Standard Ruby also ships with a
168
+ built-in [language
169
+ server](https://microsoft.github.io/language-server-protocol/) that many modern
170
+ editors can support natively, even without a Standard-specific plugin.
181
171
 
182
- The beauty of Ruby Standard Style is that it's simple. No one wants to
183
- maintain multiple hundred-line style configuration files for every module/project
184
- they work on. Enough of this madness!
172
+ You can run the server by supplying the `--lsp` flag:
185
173
 
186
- This gem saves you (and others!) time in four ways:
174
+ ```
175
+ standard --lsp
176
+ ```
187
177
 
188
- - **No configuration.** The easiest way to enforce consistent style in your
189
- project. Just drop it in.
190
- - **Automatically format code.** Just run `standardrb --fix` and say goodbye to
191
- messy or inconsistent code.
192
- - **Catch style issues & programmer errors early.** Save precious code review
193
- time by eliminating back-and-forth between reviewer & contributor.
194
- - **Deliberate pace.** We strive to take the hassle of upgrading Rubocop out of each
195
- individual team's hands and shoulder it ourselves. We enable about ~20% of new
196
- cops and generally choose conservative configurations for them.
178
+ If your editor offers LSP support, it probably has a place to configure the
179
+ above command and will subsequently manage the server process for you.
197
180
 
198
- Adopting Standard style means ranking the importance of code clarity and
199
- community conventions higher than personal style. This might not make sense for
200
- 100% of projects and development cultures, however open source can be a hostile
201
- place for newbies. Setting up clear, automated contributor expectations makes a
202
- project healthier.
181
+ ### CI support
203
182
 
204
- ## Who uses Ruby Standard Style?
183
+ Various continuous integration and quality-checking tools have been made to
184
+ support Standard Ruby, as well.
205
185
 
206
- Here are a few examples of Ruby Standard-compliant teams & projects:
186
+ * [Github Actions](https://github.com/andrewmcodes/standardrb-action)
187
+ * [Code Climate](https://github.com/standardrb/standard/wiki/CI:-Code-Climate)
188
+ * [Pronto](https://github.com/julianrubisch/pronto-standardrb)
189
+ * [Danger](https://github.com/ashfurrow/danger-rubocop/)
207
190
 
208
- * [Test Double](https://testdouble.com/agency)
209
- * [Amazon Web Services](https://aws.amazon.com/)
210
- * [Arrows](https://arrows.to/)
211
- * [Avo Admin](https://avohq.io/)
212
- * [Babylist](https://www.babylist.com/)
213
- * [Brand New Box](https://brandnewbox.com)
214
- * [Brave Software](https://github.com/brave-intl/publishers)
215
- * [Collective Idea](https://collectiveidea.com/)
216
- * [Culture Foundry](https://www.culturefoundry.com/)
217
- * [Elevate Labs](https://elevatelabs.com)
218
- * [Envoy](https://www.envoy.com)
219
- * [Evil Martians](https://evilmartians.com)
220
- * [Firstline](https://firstline.org/)
221
- * [Hashrocket](https://hashrocket.com)
222
- * [Honeybadger](https://www.honeybadger.io)
223
- * [JetThoughts](https://www.jetthoughts.com/)
224
- * [Level UP Solutions](https://levups.com)
225
- * [Monterail](https://www.monterail.com)
226
- * [myRent](https://www.myrent.co.nz)
227
- * [OBLSK](https://oblsk.com/)
228
- * [Oyster](https://www.oysterhr.com/)
229
- * [Planet Argon](https://www.planetargon.com/)
230
- * [PLT4M](https://plt4m.com/)
231
- * [Podia](https://www.podia.com/)
232
- * [Rebase Interactive](https://www.rebaseinteractive.com/)
233
- * [Renuo](https://www.renuo.ch/)
234
- * [RubyCI](https://ruby.ci)
235
- * [Spinal](https://spinalcms.com/)
236
- * [Teamtailor](https://www.teamtailor.com/)
237
- * [thoughtbot](https://thoughtbot.com/)
191
+ Of course, no special plugin is necessary to run Standard Ruby in a CI
192
+ environment, as `standardrb` and `rake standard` work just fine on their own!
238
193
 
239
- Does your team use Standard? [Add your name to the list](https://github.com/testdouble/standard/edit/main/README.md)!
194
+ ### Other tools
240
195
 
241
- ## Is there a readme badge?
196
+ These tool integrations are also available:
242
197
 
243
- Yes! If you use Standard in your project, you can include one of these
244
- badges in your readme to let people know that your code is using the StandardRB
245
- style.
198
+ * [Guard](https://github.com/JodyVanden/guard-standardrb)
199
+ * [Spring](https://github.com/lakim/spring-commands-standard)
246
200
 
247
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
201
+ ## Ignoring errors
248
202
 
249
- ```md
250
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
251
- ```
203
+ While Standard is very strict in how each formatting and linting rule is configured,
204
+ it's mercifully flexible when you need to ignore a violation to focus on a higher
205
+ priority (like, say, keeping the build running). There are a number of ways to
206
+ ignore any errors, with the right answer depending on the situation.
252
207
 
253
- ## I disagree with rule X, can you change it?
208
+ ### Ignoring a line with a comment
254
209
 
255
- No. The whole point of Standard is to save you time by avoiding
256
- [bikeshedding](https://www.freebsd.org/doc/en/books/faq/misc.html#bikeshed-painting)
257
- about code style. There are lots of debates online about tabs vs. spaces, etc.
258
- that will never be resolved. These debates just distract from getting stuff
259
- done. At the end of the day you have to 'just pick something', and that's the
260
- whole philosophy of Standard -- it's a bunch of sensible 'just pick something'
261
- opinions. Hopefully, users see the value in that over defending their own
262
- opinions.
210
+ Individual lines can be ignored with a comment directive at the end of the line.
211
+ As an example, the line `text = 'hi'` violates two rules,
212
+ [Lint/UselessAssignment](https://docs.rubocop.org/rubocop/cops_lint.html#lintuselessassignment)
213
+ and
214
+ [Style/StringLiterals](https://docs.rubocop.org/rubocop/cops_style.html#stylestringliterals).
263
215
 
264
- Pro tip: Just use Standard and move on. There are actual real problems that
265
- you could spend your time solving! :P
216
+ You could ignore one, both, or all errors with the following comments:
266
217
 
267
- ## Is there an automatic formatter?
218
+ ```ruby
219
+ # Disable one error but keep Lint/UselessAssignment
220
+ text = 'hi' # standard:disable Style/StringLiterals
268
221
 
269
- Yes! You can use `standardrb --fix` to fix most issues automatically.
222
+ # Disable both errors explicitly
223
+ text = 'hi' # standard:disable Style/StringLiterals, Lint/UselessAssignment
270
224
 
271
- `standardrb --fix` is built into `standardrb` for maximum convenience. Most
272
- problems are fixable, but some errors must be fixed manually.
225
+ # Disable all errors on the line with "all"
226
+ text = "hi" # standard:disable all
227
+ ```
273
228
 
274
- If you're feeling sinister, you can also use `standardrb --fix-unsafely`, which
275
- will also perform fixes, even if they run the risk of altering program behavior.
276
- If you read your git diffs closely and have good tests, this is often okay, but
277
- YMMV… it's called unsafe for a reason!
229
+ ### Ignoring multiple lines with comments
278
230
 
279
- ## Can I override the `fix: true` config setting?
231
+ Similarly to individual lines, you can also disable multiple lines by wrapping
232
+ them in comments that disable and re-enable them:
280
233
 
281
- Also yes! You can use `standardrb --no-fix`. Not `fix`ing is the default
282
- behavior, but this flag will override the `fix: true` setting in your
283
- [`.standard.yml` config](#what-you-might-do-if-youre-clever).
284
- This is especially useful for checking your project's compliance with
285
- `standardrb` in CI environments while keeping the `fix: true` option enabled
286
- locally.
234
+ ```ruby
235
+ # standard:disable Style/StringLiterals, Lint/UselessAssignment
236
+ text = "hi"
237
+ puts 'bye'
238
+ # standard:enable Style/StringLiterals, Lint/UselessAssignment
239
+ ```
287
240
 
288
- ## How do I ignore files?
241
+ ### Ignoring entire files and globs
289
242
 
290
- Sometimes you need to ignore additional folders or specific minified files. To
291
- do that, add a `.standard.yml` file to the root of your project and specify a
292
- list of files and globs that should be excluded:
243
+ You can ignore entire files and file patterns by adding them to `ignore:` in
244
+ your project's `.standard.yml` file:
293
245
 
294
246
  ```yaml
295
247
  ignore:
@@ -297,159 +249,173 @@ ignore:
297
249
  - 'a/whole/directory/**/*'
298
250
  ```
299
251
 
300
- You can see the files Standard ignores by default
301
- [here](https://github.com/testdouble/standard/blob/main/lib/standard/creates_config_store/configures_ignored_paths.rb#L3-L13)
252
+ ### Ignoring specific rules in files and globs
302
253
 
303
- ## How do I hide a certain warning?
304
-
305
- In rare cases, you'll need to break a rule and hide the warning generated by
306
- Standard.
307
-
308
- Ruby Standard Style uses [RuboCop](https://github.com/rubocop-hq/rubocop)
309
- under-the-hood and you can hide warnings as you normally would if you used
310
- RuboCop directly.
311
-
312
- To ignore only certain rules from certain globs (not recommended, but maybe your
313
- test suite uses a non-standardable DSL, you can specify an array of RuboCop
314
- rules to ignore for a particular glob:
254
+ For a given file or glob, you can even ignore only specific rules by nesting an
255
+ array of the rules you'd like to ignore:
315
256
 
316
257
  ```yaml
317
258
  ignore:
318
259
  - 'test/**/*':
319
- - Layout/EndAlignment
260
+ - Layout/AlignHash
320
261
  ```
321
262
 
322
- ## How do I disable a warning within my source code?
323
-
324
- You can also use special comments to disable all or certain rules within your
325
- source code.
263
+ ### Ignoring every violation and converting them into a todo list
326
264
 
327
- Given this source listing `foo.rb`:
265
+ If you're adopting Standard in a large codebase and you don't want to convert
266
+ everything all at once, you can work incrementally by generating a "todo" file
267
+ which will cause Standard to ignore every violation present in each file of the
268
+ codebase.
328
269
 
329
- ```ruby
330
- baz = 42
331
- ```
270
+ This way, you can gradually work through the todo list, removing ignore
271
+ directives and fixing any associated errors, while also being alerted to any
272
+ regressions if they're introduced into the project.
332
273
 
333
- Running `standard foo.rb` would fail:
274
+ Here are the commands you might run to get started:
334
275
 
335
276
  ```
336
- foo.rb:1:1: Lint/UselessAssignment: Useless assignment to variable - `baz`.
277
+ # Start by clearing any auto-fixable errors:
278
+ $ standardrb --fix
279
+
280
+ # Generate a `.standard_todo.yml' to work from
281
+ $ standardrb --generate-todo
337
282
  ```
338
283
 
339
- If we wanted to make an exception, we could add the following comment:
284
+ ## Configuring Standard
340
285
 
341
- ```ruby
342
- baz = 42 # standard:disable Lint/UselessAssignment
343
- ```
286
+ While the rules aren't configurable, Standard offers a number of options that
287
+ can be configured as CLI flags and YAML properties.
344
288
 
345
- The comment directives (both `standard:disable` and `rubocop:disable`) will
346
- suppress the error and Standard would succeed.
289
+ ### CLI flags
347
290
 
348
- If, however, you needed to disable standard for multiple lines, you could use
349
- open and closing directives like this:
291
+ The easiest way to summarize the available CLI flags is to invoke `standardrb -h`:
350
292
 
351
- ```ruby
352
- # standard:disable Layout/IndentationWidth
353
- def foo
354
- 123
355
- end
356
- # standard:enable Layout/IndentationWidth
357
293
  ```
294
+ Usage: standardrb [--fix] [--lsp] [-vh] [--format <name>] [--] [FILE]...
358
295
 
359
- And if you don't know or care which rule is being violated, you can also
360
- substitute its name for "all". This line actually triggers three different
361
- violations, so we can suppress them like this:
296
+ Options:
362
297
 
363
- ```ruby
364
- baz = ['a'].each do end # standard:disable all
365
- ```
298
+ --fix Apply automatic fixes that we're confident won't break your code
299
+ --fix-unsafely Apply even more fixes, including some that may change code behavior
300
+ --no-fix Do not automatically fix failures
301
+ --format <name> Format output with any RuboCop formatter (e.g. "json")
302
+ --generate-todo Create a .standard_todo.yml that lists all the files that contain errors
303
+ --lsp Start a LSP server listening on STDIN
304
+ -v, --version Print the version of Standard
305
+ -V, --verbose-version Print the version of Standard and its dependencies.
306
+ -h, --help Print this message
307
+ FILE Files to lint [default: ./]
366
308
 
367
- ## How do I specify a Ruby version? What is supported?
309
+ Standard also forwards most CLI arguments to RuboCop. To see them, run:
368
310
 
369
- Because Standard wraps RuboCop, they share the same [runtime
370
- requirements](https://github.com/rubocop-hq/rubocop#compatibility)—currently,
371
- that's MRI 2.3 and newer. While Standard can't avoid this runtime requirement,
372
- it does allow you to lint codebases that target Ruby versions older than 2.3 by
373
- narrowing the ruleset somewhat.
311
+ $ rubocop --help
312
+ ```
374
313
 
375
- Standard will default to telling RuboCop to target the currently running version
376
- of Ruby (by inspecting `RUBY_VERSION` at runtime. But if you want to lock it
377
- down, you can specify `ruby_version` in `.standard.yml`.
314
+ If you run Standard via Rake, you can specify your CLI flags in an environment
315
+ variable named `STANDARDOPTS` like so:
378
316
 
379
317
  ```
380
- ruby_version: 1.8
318
+ $ rake standard STANDARDOPTS="--format progress"
381
319
  ```
382
320
 
383
- See
384
- [testdouble/suture](https://github.com/testdouble/suture/blob/main/.standard.yml)
385
- for an example.
321
+ ### YAML options
386
322
 
387
- It's a little confusing to consider, but the targeted Ruby version for linting
388
- may or may not match the version of the runtime (suppose you're on Ruby 2.5.1,
389
- but your library supports Ruby 2.3.0). In this case, specify `ruby_version` and
390
- you should be okay. However, note that if you target a _newer_ Ruby version than
391
- the runtime, RuboCop may behave in surprising or inconsistent ways.
323
+ In addition to CLI flags, Standard will search for a `.standard.yml` file
324
+ (ascending to parent directories if the current working directory doesn't
325
+ contain one). If you find yourself repeatedly running Standard with the same
326
+ CLI options, it probably makes more sense to set it once in a YAML file:
392
327
 
393
- If you are targeting a Ruby older than 2.3 and run into an issue, check out
394
- Standard's [version-specific RuboCop
395
- configurations](https://github.com/testdouble/standard/tree/main/config) and
396
- consider helping out by submitting a pull request if you find a rule that won't
397
- work for older Rubies.
328
+ ```yaml
329
+ fix: true # default: false
330
+ parallel: true # default: false
331
+ format: progress # default: Standard::Formatter
332
+ ruby_version: 3.0 # default: RUBY_VERSION
333
+ default_ignores: false # default: true
398
334
 
399
- ## How do I use Standard with RuboCop extensions or custom rules?
335
+ ignore: # default: []
336
+ - 'vendor/**/*'
400
337
 
401
- If you want to use Standard in conjunction with RuboCop extensions or custom
402
- cops, you can specify them in your own [RuboCop configuration
403
- YAML](https://docs.rubocop.org/rubocop/configuration.html) files and
404
- `.standard.yml` using the "extend_config` setting.
338
+ plugins: # default: []
339
+ - standard-rails
405
340
 
406
- For a simple example, you could include [rubocop-rails](https://github.com/rubocop/rubocop-rails)
407
- when Standard runs by first specifying a file in `.standard.yml`:
341
+ extend_config: # default: []
342
+ - .standard_ext.yml
343
+ ```
408
344
 
409
- ```yaml
410
- # .standard.yml
345
+ #### Configuring ruby_version
411
346
 
412
- extend_config:
413
- - .standard_rubocop_extensions.yml
414
- ```
347
+ One notable YAML setting is `ruby_version`, which allows you to set the **oldest
348
+ version of Ruby the project needs to support** [RuboCop's `TargetRubyVersion`
349
+ setting](https://docs.rubocop.org/rubocop/configuration.html#setting-the-target-ruby-version)
350
+ explicitly. Because this value is inferred from any `.ruby-version`,
351
+ `.tool-versions`, `Gemfile.lock`, and `*.gemspec` files that might be present,
352
+ most applications won't need to set this.
415
353
 
416
- And a minimal RuboCop configuration file:
354
+ However, gems and libraries that support older versions of Ruby will want
355
+ to lock the `ruby-version:` explicitly in their `.standard.yml` file to ensure
356
+ new rules don't break old rubies:
417
357
 
418
358
  ```yaml
419
- # .standard_rubocop_extensions.yml
420
-
421
- require:
422
- - rubocop-rails
359
+ ruby_version: 2.7
423
360
  ```
424
361
 
425
- That's it! Now, in addition to all of Standard's built-in rules, `standardrb`
426
- and `rake standard` will also execute the default configuration of the
427
- `rubocop-rails` gem without needing to invoke `rubocop` separately.
362
+ ## Extending Standard
428
363
 
429
- For a slightly more complex example, we could add the
430
- [betterlint](https://github.com/Betterment/betterlint) gem from our friends at
431
- [Betterment](https://www.betterment.com), first by telling Standard where our
432
- configuration file is:
364
+ Standard includes two extension mechanisms: plugins and configuration
365
+ extensions. While neither can _change_ the rules configured out-of-the-box by
366
+ Standard, they can define, require, and configure _additional_ RuboCop rules.
433
367
 
434
- ```yml
435
- # .standard.yml
368
+ Both are "first-in-wins", meaning once a rule is configured by a plugin or
369
+ extension, it can't be changed or reconfigured by a later plugin or extension.
370
+ This way, each Standard plugin you add becomes a de facto "standard" of its
371
+ own. Plugins have precedence over extensions as they are loaded first.
436
372
 
437
- extend_config:
438
- - .betterlint.yml
373
+ ### Plugins
374
+
375
+ Adding a plugin to your project is as easy as adding it to your Gemfile and
376
+ specifying it in `.standard.yml` in the root of your project. For example, after
377
+ installing [standard-rails](https://github.com/standardrb/standard-rails), you
378
+ can configure it by adding it to `plugins`:
379
+
380
+ ```yaml
381
+ plugins:
382
+ - standard-rails
439
383
  ```
440
384
 
441
- But if we only wanted to enable a particular rule, we could configure it more
442
- narrowly, like this:
385
+ Each plugin can be passed configuration options by specifying them in a nested
386
+ hash. For example, `standard-rails` allows you to configure its rules for
387
+ a particular version of Rails (though this will usually be detected for you
388
+ automatically):
443
389
 
444
- ```yml
445
- # .betterlint.yml
390
+ ```yaml
391
+ plugins:
392
+ - standard-rails:
393
+ target_rails_version: 7.0
394
+ ```
395
+
396
+ You can develop your own plugins, too! Check out the
397
+ [lint_roller](https://github.com/standardrb/lint_roller) gem to learn how. For a
398
+ simple example, you can look at
399
+ [standard-custom](https://github.com/standardrb/standard-custom), which is one
400
+ of the default plugins included by Standard.
446
401
 
402
+ ### Extended config files
403
+
404
+ Of course, you may want to add more rules without going to the trouble
405
+ of packaging them in a plugin gem. For cases like this, you can define one or
406
+ more [RuboCop configuration
407
+ files](https://docs.rubocop.org/rubocop/configuration.html) and then list them
408
+ in your `.standard.yml` file under `extend_config`.
409
+
410
+ For example, after installing the
411
+ [betterlint](https://github.com/Betterment/betterlint) gem from our friends at
412
+ [Betterment](https://www.betterment.com), we could create a RuboCop config
413
+ file named `.betterlint.yml`:
414
+
415
+ ```yaml
447
416
  require:
448
417
  - rubocop/cop/betterment.rb
449
418
 
450
- AllCops:
451
- DisabledByDefault: true
452
-
453
419
  Betterment/UnscopedFind:
454
420
  Enabled: true
455
421
 
@@ -457,157 +423,79 @@ Betterment/UnscopedFind:
457
423
  - SystemConfiguration
458
424
  ```
459
425
 
460
- This same approach works for more than just gems! Just require a Ruby file that
461
- defines or loads your [custom RuboCop
462
- implementation](https://docs.rubocop.org/rubocop/development.html) and configure
463
- it using `extend_config`.
464
-
465
- When Standard encounters an `extend_config` property, it will merge your
466
- configuration files with Standard's base ruleset. To prevent Standard's built-in
467
- rules from being modified, any configuration of rules includued in the `rubocop`
468
- or `rubocop-performance` gems will be ignored. Most settings under `AllCops:`
469
- can be configured, however, unless they'd conflict with a setting used by
470
- Standard (like `TargetRubyVersion`) or prevent Standard's own rules from running
471
- (like `StyleGuideCopsOnly`). If you specify multiple YAML files under
472
- `extend_config`, note that their resulting RuboCop configurations will be merged
473
- in order (i.e. last-in-wins).
474
-
475
- ### Usage via RuboCop
476
-
477
- If you find that Standard's `extend_config` feature doesn't meet your needs or
478
- if you only want to use Standard's rules while continuing to use RuboCop's CLI
479
- (e.g., to continue using your favorite IDE/tooling/workflow with RuboCop
480
- support) Evil Martians also maintains [a regularly updated
426
+ And then reference it in our `.standard.yml`:
427
+
428
+ ```yml
429
+ extend_config:
430
+ - .betterlint.yml
431
+ ```
432
+
433
+ ### Running Standard's rules via RuboCop
434
+
435
+ If you find that neither plugins or configuration extensions meet your needs or
436
+ if you have some other reason to run Standard's rules with RuboCop's CLI (e.g.,
437
+ to continue using your favorite IDE/tooling/workflow with RuboCop support) Evil
438
+ Martians also maintains [a regularly updated
481
439
  guide](https://evilmartians.com/chronicles/rubocoping-with-legacy-bring-your-ruby-code-up-to-standard)
482
440
  on how to configure RuboCop to load and execute Standard's ruleset.
483
441
 
484
- In short, you can configure this in your `.rubocop.yml` to load Standard's
485
- ruleset like any other gem:
442
+ In short, you can configure this in your `.rubocop.yml` to load Standard's three
443
+ default rulesets, just as you would any other gem:
486
444
 
487
445
  ```yaml
488
- require: standard
446
+ require:
447
+ - standard
448
+ - rubocop-performance
489
449
 
490
450
  inherit_gem:
491
451
  standard: config/base.yml
452
+ standard-performance: config/base.yml
453
+ standard-custom: config/base.yml
492
454
  ```
493
455
 
494
- ## How do I change the output?
456
+ ## Who uses Standard Ruby?
495
457
 
496
- Standard's built-in formatter is intentionally minimal, printing only unfixed
497
- failures or (when successful) printing nothing at all. If you'd like to use a
498
- different formatter, you can specify any of RuboCop's built-in formatters or
499
- write your own.
500
-
501
- For example, if you'd like to see colorful progress dots, you can either run
502
- Standard with:
503
-
504
- ```
505
- $ bundle exec standardrb --format progress
506
- Inspecting 15 files
507
- ...............
508
-
509
- 15 files inspected, no offenses detected
510
- ```
511
-
512
- Or, in your project's `.standard.yml` file, specify:
513
-
514
- ```yaml
515
- format: progress
516
- ```
517
-
518
- Refer to RuboCop's [documentation on
519
- formatters](https://rubocop.readthedocs.io/en/latest/formatters/) for more
520
- information.
521
-
522
- ## How do I run Standard in my editor?
523
-
524
- It can be very handy to know about failures while editing to shorten the
525
- feedback loop.
526
-
527
- ### Language Server Protocol support
528
-
529
- To provide immediate feedback of Standard violations and support autofixing
530
- of your code while avoiding the performance cost of starting and stopping the
531
- `standardrb` binary repeatedly, Standard Ruby ships with a built-in [Language
532
- Server Protocol](https://microsoft.github.io/language-server-protocol/) server,
533
- which is powered by the [language_server-protocol
534
- gem](https://github.com/mtsmfm/language_server-protocol-ruby) and can be
535
- activated from the command line with the `--lsp` flag.
536
-
537
- Most likely, you'd instantiate this server indirectly in your editor's
538
- configuration, as can be demonstrated easily with
539
- [neovim](https://github.com/testdouble/standard/wiki/IDE:-neovim).
540
- Theoretically, this feature could be leveraged by a purpose-built editor plugin
541
- to performantly format and fix your code. (If you're looking for a project, we'd
542
- love to see one created for VS Code!)
458
+ Here are a few examples of Ruby Standard-compliant teams & projects:
543
459
 
544
- ### Editor-specific guides
460
+ * [Test Double](https://testdouble.com/agency)
461
+ * [Amazon Web Services](https://aws.amazon.com/)
462
+ * [Arrows](https://arrows.to/)
463
+ * [Avo Admin](https://avohq.io/)
464
+ * [Babylist](https://www.babylist.com/)
465
+ * [Brand New Box](https://brandnewbox.com)
466
+ * [Brave Software](https://github.com/brave-intl/publishers)
467
+ * [Collective Idea](https://collectiveidea.com/)
468
+ * [Culture Foundry](https://www.culturefoundry.com/)
469
+ * [Elevate Labs](https://elevatelabs.com)
470
+ * [Envoy](https://www.envoy.com)
471
+ * [Evil Martians](https://evilmartians.com)
472
+ * [Firstline](https://firstline.org/)
473
+ * [Hashrocket](https://hashrocket.com)
474
+ * [Honeybadger](https://www.honeybadger.io)
475
+ * [JetThoughts](https://www.jetthoughts.com/)
476
+ * [Level UP Solutions](https://levups.com)
477
+ * [Monterail](https://www.monterail.com)
478
+ * [myRent](https://www.myrent.co.nz)
479
+ * [OBLSK](https://oblsk.com/)
480
+ * [Oyster](https://www.oysterhr.com/)
481
+ * [Planet Argon](https://www.planetargon.com/)
482
+ * [PLT4M](https://plt4m.com/)
483
+ * [Podia](https://www.podia.com/)
484
+ * [Rebase Interactive](https://www.rebaseinteractive.com/)
485
+ * [Renuo](https://www.renuo.ch/)
486
+ * [RubyCI](https://ruby.ci)
487
+ * [Spinal](https://spinalcms.com/)
488
+ * [Teamtailor](https://www.teamtailor.com/)
489
+ * [thoughtbot](https://thoughtbot.com/)
545
490
 
546
- - [Atom](https://github.com/testdouble/standard/wiki/IDE:-Atom)
547
- - [emacs (via flycheck)](https://github.com/julianrubisch/flycheck-standardrb)
548
- - [RubyMine](https://www.jetbrains.com/help/ruby/rubocop.html#disable_rubocop)
549
- - [vim (via ALE)](https://github.com/testdouble/standard/wiki/IDE:-vim)
550
- - [neovim (via LSP)](https://github.com/testdouble/standard/wiki/IDE:-neovim)
551
- - [VS Code](https://github.com/testdouble/standard/wiki/IDE:-vscode)
552
-
553
- ## Why aren't `frozen_string_literal: true` magic comments enforced?
554
-
555
- Standard does not take a stance on whether you should plaster a
556
- [frozen_string_literal magic
557
- comment](https://docs.ruby-lang.org/en/3.0/doc/syntax/comments_rdoc.html#label-Magic+Comments)
558
- directive at the top of every file. Enforcing use of the comment became popular
559
- when it was believed that string literals would be frozen by default in a future
560
- version of Ruby, but [according to
561
- Matz](https://bugs.ruby-lang.org/issues/11473#note-53) there are no (longer any)
562
- such plans.
563
-
564
- Aside from one's personal opinion on the degree to which the comment is an
565
- eyesore, the decision to include the magic comment at the top of every file
566
- listing ought to be made based on the performance characteristics of each
567
- project (e.g. whether it defines a significant number of string literals,
568
- whether the commensurate memory usage is a material constraint, whether the code
569
- is run as a one-off command or a long-lived server application). These tend to
570
- indicate whether the magic comment might lead to meaningful reductions in memory
571
- usage.
572
-
573
- Because Standard is intended to be used as a default for every kind of Ruby
574
- file—from shell scripts to Rails apps—it wouldn't be appropriate for Standard to
575
- either enforce or preclude the magic comment. Instead, you might consider
576
- either:
577
-
578
- * Measuring memory performance by enabling frozen string literals as
579
- the default at runtime (with `RUBYOPT=--enable-frozen-string-literal`)
580
- * Introducing the
581
- [magic_frozen_string_literal](https://github.com/Invoca/magic_frozen_string_literal)
582
- gem to your build, which will automatically ensure that the comment is
583
- prepended for every applicable file in your project
584
-
585
- ## How often is Standard updated?
586
-
587
- We aim to update Standard once a month, in the first week of the month. In between releases, we'll be considering RuboCop updates, RuboCop Performance updates, and community contributions.
588
-
589
- ## Does Standard work with [Insert other tool name here]?
590
-
591
- Maybe! Start by searching the repository to see if there's an existing issue
592
- open for the tool you're interested in. That aside, here are other known
593
- integrations aside from editor plugins:
594
-
595
- * [Code Climate](https://github.com/testdouble/standard/wiki/CI:-Code-Climate)
596
- * [Pronto](https://github.com/julianrubisch/pronto-standardrb)
597
- * [Spring](https://github.com/lakim/spring-commands-standard)
598
- * [Guard](https://github.com/JodyVanden/guard-standardrb)
599
- * [Danger](https://github.com/ashfurrow/danger-rubocop/)
491
+ Does your team use Standard? [Add your name to the list](https://github.com/standardrb/standard/edit/main/README.md)!
600
492
 
601
- ## Contributing
493
+ If you really want to show off, you can also add a badge to your project's README, like this one:
602
494
 
603
- Follow the steps below to setup standard locally:
495
+ [![Ruby Code Style](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/standardrb/standard)
604
496
 
605
- ```bash
606
- $ git clone https://github.com/testdouble/standard
607
- $ cd standard
608
- $ gem install bundler # if working with ruby version below 2.6.0
609
- $ bundle install
610
- $ bundle exec rake # to run test suite
497
+ ```md
498
+ [![Ruby Code Style](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/standardrb/standard)
611
499
  ```
612
500
 
613
501
  ## Code of Conduct