standard 1.26.0 → 1.28.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,293 +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
140
-
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
-
144
- Here's an example yaml file with every option set:
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.
145
142
 
146
- ```yaml
147
- fix: true # default: false
148
- parallel: true # default: false
149
- format: progress # default: Standard::Formatter
150
- ruby_version: 2.3.3 # default: RUBY_VERSION
151
- default_ignores: false # default: true
143
+ ## Integrating Standard into your workflow
152
144
 
153
- ignore: # default: []
154
- - 'db/schema.rb'
155
- - 'vendor/**/*'
156
- - 'test/**/*':
157
- - Layout/AlignHash
158
- ```
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.
159
149
 
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`.)
150
+ ### Editor support
165
151
 
166
- ## What you might do if you're REALLY clever
152
+ We've added a number of editing guides for getting started:
167
153
 
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
- * [Oyster](https://www.oysterhr.com/)
228
- * [Planet Argon](https://www.planetargon.com/)
229
- * [Podia](https://www.podia.com/)
230
- * [Rebase Interactive](https://www.rebaseinteractive.com/)
231
- * [Renuo](https://www.renuo.ch/)
232
- * [RubyCI](https://ruby.ci)
233
- * [Spinal](https://spinalcms.com/)
234
- * [Teamtailor](https://www.teamtailor.com/)
235
- * [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!
236
193
 
237
- Does your team use Standard? [Add your name to the list](https://github.com/testdouble/standard/edit/main/README.md)!
194
+ ### Other tools
238
195
 
239
- ## Is there a readme badge?
196
+ These tool integrations are also available:
240
197
 
241
- Yes! If you use Standard in your project, you can include one of these
242
- badges in your readme to let people know that your code is using the StandardRB
243
- style.
198
+ * [Guard](https://github.com/JodyVanden/guard-standardrb)
199
+ * [Spring](https://github.com/lakim/spring-commands-standard)
244
200
 
245
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
201
+ ## Ignoring errors
246
202
 
247
- ```md
248
- [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
249
- ```
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.
250
207
 
251
- ## I disagree with rule X, can you change it?
208
+ ### Ignoring a line with a comment
252
209
 
253
- No. The whole point of Standard is to save you time by avoiding
254
- [bikeshedding](https://www.freebsd.org/doc/en/books/faq/misc.html#bikeshed-painting)
255
- about code style. There are lots of debates online about tabs vs. spaces, etc.
256
- that will never be resolved. These debates just distract from getting stuff
257
- done. At the end of the day you have to 'just pick something', and that's the
258
- whole philosophy of Standard -- it's a bunch of sensible 'just pick something'
259
- opinions. Hopefully, users see the value in that over defending their own
260
- 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).
261
215
 
262
- Pro tip: Just use Standard and move on. There are actual real problems that
263
- you could spend your time solving! :P
216
+ You could ignore one, both, or all errors with the following comments:
264
217
 
265
- ## Is there an automatic formatter?
218
+ ```ruby
219
+ # Disable one error but keep Lint/UselessAssignment
220
+ text = 'hi' # standard:disable Style/StringLiterals
266
221
 
267
- 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
268
224
 
269
- `standardrb --fix` is built into `standardrb` for maximum convenience. Most
270
- 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
+ ```
271
228
 
272
- If you're feeling sinister, you can also use `standardrb --fix-unsafely`, which
273
- will also perform fixes, even if they run the risk of altering program behavior.
274
- If you read your git diffs closely and have good tests, this is often okay, but
275
- YMMV… it's called unsafe for a reason!
229
+ ### Ignoring multiple lines with comments
276
230
 
277
- ## 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:
278
233
 
279
- Also yes! You can use `standardrb --no-fix`. Not `fix`ing is the default
280
- behavior, but this flag will override the `fix: true` setting in your
281
- [`.standard.yml` config](#what-you-might-do-if-youre-clever).
282
- This is especially useful for checking your project's compliance with
283
- `standardrb` in CI environments while keeping the `fix: true` option enabled
284
- locally.
234
+ ```ruby
235
+ # standard:disable Style/StringLiterals, Lint/UselessAssignment
236
+ text = "hi"
237
+ puts 'bye'
238
+ # standard:enable Style/StringLiterals, Lint/UselessAssignment
239
+ ```
285
240
 
286
- ## How do I ignore files?
241
+ ### Ignoring entire files and globs
287
242
 
288
- Sometimes you need to ignore additional folders or specific minified files. To
289
- do that, add a `.standard.yml` file to the root of your project and specify a
290
- 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:
291
245
 
292
246
  ```yaml
293
247
  ignore:
@@ -295,159 +249,173 @@ ignore:
295
249
  - 'a/whole/directory/**/*'
296
250
  ```
297
251
 
298
- You can see the files Standard ignores by default
299
- [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
300
253
 
301
- ## How do I hide a certain warning?
302
-
303
- In rare cases, you'll need to break a rule and hide the warning generated by
304
- Standard.
305
-
306
- Ruby Standard Style uses [RuboCop](https://github.com/rubocop-hq/rubocop)
307
- under-the-hood and you can hide warnings as you normally would if you used
308
- RuboCop directly.
309
-
310
- To ignore only certain rules from certain globs (not recommended, but maybe your
311
- test suite uses a non-standardable DSL, you can specify an array of RuboCop
312
- 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:
313
256
 
314
257
  ```yaml
315
258
  ignore:
316
259
  - 'test/**/*':
317
- - Layout/EndAlignment
260
+ - Layout/AlignHash
318
261
  ```
319
262
 
320
- ## How do I disable a warning within my source code?
321
-
322
- You can also use special comments to disable all or certain rules within your
323
- source code.
263
+ ### Ignoring every violation and converting them into a todo list
324
264
 
325
- 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.
326
269
 
327
- ```ruby
328
- baz = 42
329
- ```
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.
330
273
 
331
- Running `standard foo.rb` would fail:
274
+ Here are the commands you might run to get started:
332
275
 
333
276
  ```
334
- 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
335
282
  ```
336
283
 
337
- If we wanted to make an exception, we could add the following comment:
284
+ ## Configuring Standard
338
285
 
339
- ```ruby
340
- baz = 42 # standard:disable Lint/UselessAssignment
341
- ```
286
+ While the rules aren't configurable, Standard offers a number of options that
287
+ can be configured as CLI flags and YAML properties.
342
288
 
343
- The comment directives (both `standard:disable` and `rubocop:disable`) will
344
- suppress the error and Standard would succeed.
289
+ ### CLI flags
345
290
 
346
- If, however, you needed to disable standard for multiple lines, you could use
347
- open and closing directives like this:
291
+ The easiest way to summarize the available CLI flags is to invoke `standardrb -h`:
348
292
 
349
- ```ruby
350
- # standard:disable Layout/IndentationWidth
351
- def foo
352
- 123
353
- end
354
- # standard:enable Layout/IndentationWidth
355
293
  ```
294
+ Usage: standardrb [--fix] [--lsp] [-vh] [--format <name>] [--] [FILE]...
356
295
 
357
- And if you don't know or care which rule is being violated, you can also
358
- substitute its name for "all". This line actually triggers three different
359
- violations, so we can suppress them like this:
296
+ Options:
360
297
 
361
- ```ruby
362
- baz = ['a'].each do end # standard:disable all
363
- ```
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: ./]
364
308
 
365
- ## How do I specify a Ruby version? What is supported?
309
+ Standard also forwards most CLI arguments to RuboCop. To see them, run:
366
310
 
367
- Because Standard wraps RuboCop, they share the same [runtime
368
- requirements](https://github.com/rubocop-hq/rubocop#compatibility)—currently,
369
- that's MRI 2.3 and newer. While Standard can't avoid this runtime requirement,
370
- it does allow you to lint codebases that target Ruby versions older than 2.3 by
371
- narrowing the ruleset somewhat.
311
+ $ rubocop --help
312
+ ```
372
313
 
373
- Standard will default to telling RuboCop to target the currently running version
374
- of Ruby (by inspecting `RUBY_VERSION` at runtime. But if you want to lock it
375
- 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:
376
316
 
377
317
  ```
378
- ruby_version: 1.8.7
318
+ $ rake standard STANDARDOPTS="--format progress"
379
319
  ```
380
320
 
381
- See
382
- [testdouble/suture](https://github.com/testdouble/suture/blob/main/.standard.yml)
383
- for an example.
321
+ ### YAML options
384
322
 
385
- It's a little confusing to consider, but the targeted Ruby version for linting
386
- may or may not match the version of the runtime (suppose you're on Ruby 2.5.1,
387
- but your library supports Ruby 2.3.0). In this case, specify `ruby_version` and
388
- you should be okay. However, note that if you target a _newer_ Ruby version than
389
- 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:
390
327
 
391
- If you are targeting a Ruby older than 2.3 and run into an issue, check out
392
- Standard's [version-specific RuboCop
393
- configurations](https://github.com/testdouble/standard/tree/main/config) and
394
- consider helping out by submitting a pull request if you find a rule that won't
395
- 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
396
334
 
397
- ## How do I use Standard with RuboCop extensions or custom rules?
335
+ ignore: # default: []
336
+ - 'vendor/**/*'
398
337
 
399
- If you want to use Standard in conjunction with RuboCop extensions or custom
400
- cops, you can specify them in your own [RuboCop configuration
401
- YAML](https://docs.rubocop.org/rubocop/configuration.html) files and
402
- `.standard.yml` using the "extend_config` setting.
338
+ plugins: # default: []
339
+ - standard-rails
403
340
 
404
- For a simple example, you could include [rubocop-rails](https://github.com/rubocop/rubocop-rails)
405
- when Standard runs by first specifying a file in `.standard.yml`:
341
+ extend_config: # default: []
342
+ - .standard_ext.yml
343
+ ```
406
344
 
407
- ```yaml
408
- # .standard.yml
345
+ #### Configuring ruby_version
409
346
 
410
- extend_config:
411
- - .standard_rubocop_extensions.yml
412
- ```
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.
413
353
 
414
- 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:
415
357
 
416
358
  ```yaml
417
- # .standard_rubocop_extensions.yml
418
-
419
- require:
420
- - rubocop-rails
359
+ ruby_version: 2.7
421
360
  ```
422
361
 
423
- That's it! Now, in addition to all of Standard's built-in rules, `standardrb`
424
- and `rake standard` will also execute the default configuration of the
425
- `rubocop-rails` gem without needing to invoke `rubocop` separately.
362
+ ## Extending Standard
426
363
 
427
- For a slightly more complex example, we could add the
428
- [betterlint](https://github.com/Betterment/betterlint) gem from our friends at
429
- [Betterment](https://www.betterment.com), first by telling Standard where our
430
- 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.
431
367
 
432
- ```yml
433
- # .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.
434
372
 
435
- extend_config:
436
- - .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
437
383
  ```
438
384
 
439
- But if we only wanted to enable a particular rule, we could configure it more
440
- 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):
441
389
 
442
- ```yml
443
- # .betterlint.yml
390
+ ```yaml
391
+ plugins:
392
+ - standard-rails:
393
+ target_rails_version: 7.0
394
+ ```
444
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.
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
445
416
  require:
446
417
  - rubocop/cop/betterment.rb
447
418
 
448
- AllCops:
449
- DisabledByDefault: true
450
-
451
419
  Betterment/UnscopedFind:
452
420
  Enabled: true
453
421
 
@@ -455,157 +423,79 @@ Betterment/UnscopedFind:
455
423
  - SystemConfiguration
456
424
  ```
457
425
 
458
- This same approach works for more than just gems! Just require a Ruby file that
459
- defines or loads your [custom RuboCop
460
- implementation](https://docs.rubocop.org/rubocop/development.html) and configure
461
- it using `extend_config`.
462
-
463
- When Standard encounters an `extend_config` property, it will merge your
464
- configuration files with Standard's base ruleset. To prevent Standard's built-in
465
- rules from being modified, any configuration of rules includued in the `rubocop`
466
- or `rubocop-performance` gems will be ignored. Most settings under `AllCops:`
467
- can be configured, however, unless they'd conflict with a setting used by
468
- Standard (like `TargetRubyVersion`) or prevent Standard's own rules from running
469
- (like `StyleGuideCopsOnly`). If you specify multiple YAML files under
470
- `extend_config`, note that their resulting RuboCop configurations will be merged
471
- in order (i.e. last-in-wins).
472
-
473
- ### Usage via RuboCop
474
-
475
- If you find that Standard's `extend_config` feature doesn't meet your needs or
476
- if you only want to use Standard's rules while continuing to use RuboCop's CLI
477
- (e.g., to continue using your favorite IDE/tooling/workflow with RuboCop
478
- 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
479
439
  guide](https://evilmartians.com/chronicles/rubocoping-with-legacy-bring-your-ruby-code-up-to-standard)
480
440
  on how to configure RuboCop to load and execute Standard's ruleset.
481
441
 
482
- In short, you can configure this in your `.rubocop.yml` to load Standard's
483
- 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:
484
444
 
485
445
  ```yaml
486
- require: standard
446
+ require:
447
+ - standard
448
+ - rubocop-performance
487
449
 
488
450
  inherit_gem:
489
451
  standard: config/base.yml
452
+ standard-performance: config/base.yml
453
+ standard-custom: config/base.yml
490
454
  ```
491
455
 
492
- ## How do I change the output?
493
-
494
- Standard's built-in formatter is intentionally minimal, printing only unfixed
495
- failures or (when successful) printing nothing at all. If you'd like to use a
496
- different formatter, you can specify any of RuboCop's built-in formatters or
497
- write your own.
498
-
499
- For example, if you'd like to see colorful progress dots, you can either run
500
- Standard with:
501
-
502
- ```
503
- $ bundle exec standardrb --format progress
504
- Inspecting 15 files
505
- ...............
506
-
507
- 15 files inspected, no offenses detected
508
- ```
509
-
510
- Or, in your project's `.standard.yml` file, specify:
456
+ ## Who uses Standard Ruby?
511
457
 
512
- ```yaml
513
- format: progress
514
- ```
515
-
516
- Refer to RuboCop's [documentation on
517
- formatters](https://rubocop.readthedocs.io/en/latest/formatters/) for more
518
- information.
519
-
520
- ## How do I run Standard in my editor?
521
-
522
- It can be very handy to know about failures while editing to shorten the
523
- feedback loop.
524
-
525
- ### Language Server Protocol support
526
-
527
- To provide immediate feedback of Standard violations and support autofixing
528
- of your code while avoiding the performance cost of starting and stopping the
529
- `standardrb` binary repeatedly, Standard Ruby ships with a built-in [Language
530
- Server Protocol](https://microsoft.github.io/language-server-protocol/) server,
531
- which is powered by the [language_server-protocol
532
- gem](https://github.com/mtsmfm/language_server-protocol-ruby) and can be
533
- activated from the command line with the `--lsp` flag.
534
-
535
- Most likely, you'd instantiate this server indirectly in your editor's
536
- configuration, as can be demonstrated easily with
537
- [neovim](https://github.com/testdouble/standard/wiki/IDE:-neovim).
538
- Theoretically, this feature could be leveraged by a purpose-built editor plugin
539
- to performantly format and fix your code. (If you're looking for a project, we'd
540
- love to see one created for VS Code!)
458
+ Here are a few examples of Ruby Standard-compliant teams & projects:
541
459
 
542
- ### 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/)
543
490
 
544
- - [Atom](https://github.com/testdouble/standard/wiki/IDE:-Atom)
545
- - [emacs (via flycheck)](https://github.com/julianrubisch/flycheck-standardrb)
546
- - [RubyMine](https://www.jetbrains.com/help/ruby/rubocop.html#disable_rubocop)
547
- - [vim (via ALE)](https://github.com/testdouble/standard/wiki/IDE:-vim)
548
- - [neovim (via LSP)](https://github.com/testdouble/standard/wiki/IDE:-neovim)
549
- - [VS Code](https://github.com/testdouble/standard/wiki/IDE:-vscode)
550
-
551
- ## Why aren't `frozen_string_literal: true` magic comments enforced?
552
-
553
- Standard does not take a stance on whether you should plaster a
554
- [frozen_string_literal magic
555
- comment](https://docs.ruby-lang.org/en/3.0/doc/syntax/comments_rdoc.html#label-Magic+Comments)
556
- directive at the top of every file. Enforcing use of the comment became popular
557
- when it was believed that string literals would be frozen by default in a future
558
- version of Ruby, but [according to
559
- Matz](https://bugs.ruby-lang.org/issues/11473#note-53) there are no (longer any)
560
- such plans.
561
-
562
- Aside from one's personal opinion on the degree to which the comment is an
563
- eyesore, the decision to include the magic comment at the top of every file
564
- listing ought to be made based on the performance characteristics of each
565
- project (e.g. whether it defines a significant number of string literals,
566
- whether the commensurate memory usage is a material constraint, whether the code
567
- is run as a one-off command or a long-lived server application). These tend to
568
- indicate whether the magic comment might lead to meaningful reductions in memory
569
- usage.
570
-
571
- Because Standard is intended to be used as a default for every kind of Ruby
572
- file—from shell scripts to Rails apps—it wouldn't be appropriate for Standard to
573
- either enforce or preclude the magic comment. Instead, you might consider
574
- either:
575
-
576
- * Measuring memory performance by enabling frozen string literals as
577
- the default at runtime (with `RUBYOPT=--enable-frozen-string-literal`)
578
- * Introducing the
579
- [magic_frozen_string_literal](https://github.com/Invoca/magic_frozen_string_literal)
580
- gem to your build, which will automatically ensure that the comment is
581
- prepended for every applicable file in your project
582
-
583
- ## How often is Standard updated?
584
-
585
- 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.
586
-
587
- ## Does Standard work with [Insert other tool name here]?
588
-
589
- Maybe! Start by searching the repository to see if there's an existing issue
590
- open for the tool you're interested in. That aside, here are other known
591
- integrations aside from editor plugins:
592
-
593
- * [Code Climate](https://github.com/testdouble/standard/wiki/CI:-Code-Climate)
594
- * [Pronto](https://github.com/julianrubisch/pronto-standardrb)
595
- * [Spring](https://github.com/lakim/spring-commands-standard)
596
- * [Guard](https://github.com/JodyVanden/guard-standardrb)
597
- * [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)!
598
492
 
599
- ## Contributing
493
+ If you really want to show off, you can also add a badge to your project's README, like this one:
600
494
 
601
- 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)
602
496
 
603
- ```bash
604
- $ git clone https://github.com/testdouble/standard
605
- $ cd standard
606
- $ gem install bundler # if working with ruby version below 2.6.0
607
- $ bundle install
608
- $ 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)
609
499
  ```
610
500
 
611
501
  ## Code of Conduct