rubocop 0.40.0 → 0.41.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +7 -1014
- data/config/default.yml +61 -5
- data/config/disabled.yml +6 -0
- data/config/enabled.yml +63 -4
- data/lib/rubocop.rb +17 -1
- data/lib/rubocop/ast_node.rb +56 -42
- data/lib/rubocop/ast_node/traversal.rb +3 -3
- data/lib/rubocop/cli.rb +14 -9
- data/lib/rubocop/comment_config.rb +85 -32
- data/lib/rubocop/config.rb +29 -8
- data/lib/rubocop/config_loader.rb +1 -1
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/corrector.rb +13 -0
- data/lib/rubocop/cop/lint/block_alignment.rb +25 -11
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +5 -2
- data/lib/rubocop/cop/lint/inherit_exception.rb +69 -0
- data/lib/rubocop/cop/lint/percent_string_array.rb +60 -0
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +57 -0
- data/lib/rubocop/cop/lint/shadowed_exception.rb +95 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +28 -13
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +25 -19
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +16 -8
- data/lib/rubocop/cop/mixin/if_node.rb +1 -2
- data/lib/rubocop/cop/mixin/integer_node.rb +13 -0
- data/lib/rubocop/cop/mixin/match_range.rb +26 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +16 -7
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -1
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -4
- data/lib/rubocop/cop/mixin/percent_literal.rb +10 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +24 -6
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +20 -7
- data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +34 -20
- data/lib/rubocop/cop/performance/flat_map.rb +23 -10
- data/lib/rubocop/cop/performance/push_splat.rb +47 -0
- data/lib/rubocop/cop/performance/redundant_block_call.rb +24 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +3 -5
- data/lib/rubocop/cop/performance/sample.rb +15 -11
- data/lib/rubocop/cop/rails/exit.rb +62 -0
- data/lib/rubocop/cop/rails/output_safety.rb +45 -0
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +12 -4
- data/lib/rubocop/cop/rails/request_referer.rb +40 -0
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +63 -28
- data/lib/rubocop/cop/rails/validation.rb +37 -23
- data/lib/rubocop/cop/style/alias.rb +10 -6
- data/lib/rubocop/cop/style/bare_percent_literals.rb +18 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +15 -22
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +19 -8
- data/lib/rubocop/cop/style/comment_indentation.rb +13 -5
- data/lib/rubocop/cop/style/conditional_assignment.rb +111 -59
- data/lib/rubocop/cop/style/documentation.rb +7 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +43 -0
- data/lib/rubocop/cop/style/each_with_object.rb +25 -14
- data/lib/rubocop/cop/style/empty_else.rb +6 -10
- data/lib/rubocop/cop/style/extra_spacing.rb +20 -3
- data/lib/rubocop/cop/style/file_name.rb +16 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +9 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +20 -13
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +32 -0
- data/lib/rubocop/cop/style/infinite_loop.rb +42 -5
- data/lib/rubocop/cop/style/lambda.rb +22 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +12 -4
- data/lib/rubocop/cop/style/module_function.rb +28 -6
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +49 -12
- data/lib/rubocop/cop/style/mutable_constant.rb +8 -1
- data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
- data/lib/rubocop/cop/style/next.rb +43 -31
- data/lib/rubocop/cop/style/not.rb +33 -13
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +92 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +26 -8
- data/lib/rubocop/cop/style/{deprecated_hash_methods.rb → preferred_hash_methods.rb} +8 -8
- data/lib/rubocop/cop/style/redundant_parentheses.rb +29 -19
- data/lib/rubocop/cop/style/redundant_self.rb +13 -6
- data/lib/rubocop/cop/style/space_after_not.rb +7 -5
- data/lib/rubocop/cop/style/space_around_keyword.rb +6 -0
- data/lib/rubocop/cop/style/space_around_operators.rb +5 -1
- data/lib/rubocop/cop/style/space_before_first_arg.rb +21 -9
- data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +53 -0
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +2 -2
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +26 -6
- data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +64 -0
- data/lib/rubocop/cop/style/string_literals.rb +37 -8
- data/lib/rubocop/cop/style/symbol_array.rb +21 -12
- data/lib/rubocop/cop/style/symbol_proc.rb +26 -19
- data/lib/rubocop/cop/style/word_array.rb +1 -5
- data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -6
- data/lib/rubocop/cop/team.rb +40 -27
- data/lib/rubocop/cop/util.rb +13 -42
- data/lib/rubocop/formatter/disabled_config_formatter.rb +37 -14
- data/lib/rubocop/formatter/html_formatter.rb +3 -7
- data/lib/rubocop/result_cache.rb +18 -4
- data/{spec/support → lib/rubocop/rspec}/cop_helper.rb +3 -0
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +33 -0
- data/lib/rubocop/rspec/shared_contexts.rb +75 -0
- data/lib/rubocop/rspec/shared_examples.rb +101 -0
- data/lib/rubocop/rspec/support.rb +9 -0
- data/lib/rubocop/runner.rb +2 -2
- data/lib/rubocop/string_interpreter.rb +58 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +27 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0791b6d45f757c5aa7d753a3da95ad6edb4c3f81
|
4
|
+
data.tar.gz: 319febb8012ffb45dbec46c4007ad1eeb8da212d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adb956749fb18f0704476657494d7a65567946ae0dfa8eaf3d978f117203bbcc903e8a63c68eccbb4edfe79fbee9994306e38f35cd239495830f8845b3a9cd0e
|
7
|
+
data.tar.gz: c707072a7b5ab71dc4b753221ae456a8157dbf18d746740a857a8251ce2ca557183d39885ac7f88198b051f6498d82d8639e8dcd6e878f8a120d6c7cfa53000e
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
[![Coverage Status](https://img.shields.io/codeclimate/coverage/github/bbatsov/rubocop.svg)](https://codeclimate.com/github/bbatsov/rubocop)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/bbatsov/rubocop/badges/gpa.svg)](https://codeclimate.com/github/bbatsov/rubocop)
|
6
6
|
[![Inline docs](http://inch-ci.org/github/bbatsov/rubocop.svg)](http://inch-ci.org/github/bbatsov/rubocop)
|
7
|
+
[![Gratipay Team](https://img.shields.io/gratipay/team/rubocop.svg?maxAge=2592000)](https://gratipay.com/rubocop/)
|
7
8
|
|
8
9
|
<p align="center">
|
9
10
|
<img src="https://raw.githubusercontent.com/bbatsov/rubocop/master/logo/rubo-logo-horizontal.png" alt="RuboCop Logo"/>
|
@@ -30,74 +31,6 @@ You can support my work on RuboCop via
|
|
30
31
|
|
31
32
|
[![Support via Gratipay](https://cdn.rawgit.com/gratipay/gratipay-badge/2.1.3/dist/gratipay.png)](https://gratipay.com/rubocop/)
|
32
33
|
|
33
|
-
**This documentation tracks the `master` branch of RuboCop. Some of
|
34
|
-
the features and settings discussed here might not be available in
|
35
|
-
older releases (including the current stable release). Please, consult
|
36
|
-
the relevant git tag (e.g. v0.30.0) if you need documentation for a
|
37
|
-
specific RuboCop release.**
|
38
|
-
|
39
|
-
- [Installation](#installation)
|
40
|
-
- [Basic Usage](#basic-usage)
|
41
|
-
- [Cops](#cops)
|
42
|
-
- [Style](#style)
|
43
|
-
- [Lint](#lint)
|
44
|
-
- [Metrics](#metrics)
|
45
|
-
- [Performance](#performance)
|
46
|
-
- [Rails](#rails)
|
47
|
-
- [Configuration](#configuration)
|
48
|
-
- [Inheritance](#inheritance)
|
49
|
-
- [Defaults](#defaults)
|
50
|
-
- [Including/Excluding files](#includingexcluding-files)
|
51
|
-
- [Generic configuration parameters](#generic-configuration-parameters)
|
52
|
-
- [Setting the target Ruby version](#setting-the-target-ruby-version)
|
53
|
-
- [Automatically Generated Configuration](#automatically-generated-configuration)
|
54
|
-
- [Disabling Cops within Source Code](#disabling-cops-within-source-code)
|
55
|
-
- [Formatters](#formatters)
|
56
|
-
- [Progress Formatter (default)](#progress-formatter-default)
|
57
|
-
- [Clang Style Formatter](#clang-style-formatter)
|
58
|
-
- [Fuubar Style Formatter](#fuubar-style-formatter)
|
59
|
-
- [Emacs Style Formatter](#emacs-style-formatter)
|
60
|
-
- [Simple Formatter](#simple-formatter)
|
61
|
-
- [File List Formatter](#file-list-formatter)
|
62
|
-
- [JSON Formatter](#json-formatter)
|
63
|
-
- [Offense Count Formatter](#offense-count-formatter)
|
64
|
-
- [Worst Offenders Formatter](#worst-offenders-formatter)
|
65
|
-
- [HTML Formatter](#html-formatter)
|
66
|
-
- [Compatibility](#compatibility)
|
67
|
-
- [Editor integration](#editor-integration)
|
68
|
-
- [Emacs](#emacs)
|
69
|
-
- [Vim](#vim)
|
70
|
-
- [Sublime Text](#sublime-text)
|
71
|
-
- [Brackets](#brackets)
|
72
|
-
- [TextMate2](#textmate2)
|
73
|
-
- [Atom](#atom)
|
74
|
-
- [LightTable](#lighttable)
|
75
|
-
- [RubyMine](#rubymine)
|
76
|
-
- [Other Editors](#other-editors)
|
77
|
-
- [Git pre-commit hook integration](#git-pre-commit-hook-integration)
|
78
|
-
- [Guard integration](#guard-integration)
|
79
|
-
- [Rake integration](#rake-integration)
|
80
|
-
- [Exit codes](#exit-codes)
|
81
|
-
- [Caching](#caching)
|
82
|
-
- [Cache Validity](#cache-validity)
|
83
|
-
- [Enabling and Disabling the Cache](#enabling-and-disabling-the-cache)
|
84
|
-
- [Cache Path](#cache-path)
|
85
|
-
- [Cache Pruning](#cache-pruning)
|
86
|
-
- [Extensions](#extensions)
|
87
|
-
- [Loading Extensions](#loading-extensions)
|
88
|
-
- [Custom Cops](#custom-cops)
|
89
|
-
- [Known Custom Cops](#known-custom-cops)
|
90
|
-
- [Custom Formatters](#custom-formatters)
|
91
|
-
- [Creating Custom Formatter](#creating-custom-formatter)
|
92
|
-
- [Using Custom Formatter in Command Line](#using-custom-formatter-in-command-line)
|
93
|
-
- [Team](#team)
|
94
|
-
- [Logo](#logo)
|
95
|
-
- [Contributors](#contributors)
|
96
|
-
- [Mailing List](#mailing-list)
|
97
|
-
- [Freenode](#freenode)
|
98
|
-
- [Changelog](#changelog)
|
99
|
-
- [Copyright](#copyright)
|
100
|
-
|
101
34
|
## Installation
|
102
35
|
|
103
36
|
**RuboCop**'s installation is pretty standard:
|
@@ -118,720 +51,21 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
|
|
118
51
|
might want to use a conservative version locking in your `Gemfile`:
|
119
52
|
|
120
53
|
```rb
|
121
|
-
gem 'rubocop', '~> 0.
|
122
|
-
```
|
123
|
-
|
124
|
-
## Basic Usage
|
125
|
-
|
126
|
-
Running `rubocop` with no arguments will check all Ruby source files
|
127
|
-
in the current directory:
|
128
|
-
|
129
|
-
```sh
|
130
|
-
$ rubocop
|
131
|
-
```
|
132
|
-
|
133
|
-
Alternatively you can pass `rubocop` a list of files and directories to check:
|
134
|
-
|
135
|
-
```sh
|
136
|
-
$ rubocop app spec lib/something.rb
|
137
|
-
```
|
138
|
-
|
139
|
-
Here's RuboCop in action. Consider the following Ruby source code:
|
140
|
-
|
141
|
-
```ruby
|
142
|
-
def badName
|
143
|
-
if something
|
144
|
-
test
|
145
|
-
end
|
146
|
-
end
|
147
|
-
```
|
148
|
-
|
149
|
-
Running RuboCop on it (assuming it's in a file named `test.rb`) would produce the following report:
|
150
|
-
|
151
|
-
```
|
152
|
-
Inspecting 1 file
|
153
|
-
W
|
154
|
-
|
155
|
-
Offenses:
|
156
|
-
|
157
|
-
test.rb:1:5: C: Use snake_case for method names.
|
158
|
-
def badName
|
159
|
-
^^^^^^^
|
160
|
-
test.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression.
|
161
|
-
if something
|
162
|
-
^^
|
163
|
-
test.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
|
164
|
-
if something
|
165
|
-
^^
|
166
|
-
test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2
|
167
|
-
end
|
168
|
-
^^^
|
169
|
-
|
170
|
-
1 file inspected, 4 offenses detected
|
171
|
-
```
|
172
|
-
|
173
|
-
For more details check the available command-line options:
|
174
|
-
|
175
|
-
```sh
|
176
|
-
$ rubocop -h
|
177
|
-
```
|
178
|
-
|
179
|
-
Command flag | Description
|
180
|
-
--------------------------|------------------------------------------------------------
|
181
|
-
`-v/--version` | Displays the current version and exits.
|
182
|
-
`-V/--verbose-version` | Displays the current version plus the version of Parser and Ruby.
|
183
|
-
`-L/--list-target-files` | List all files RuboCop will inspect.
|
184
|
-
`-F/--fail-fast` | Inspects in modification time order and stops after first file with offenses.
|
185
|
-
`-C/--cache` | Store and reuse results for faster operation.
|
186
|
-
`-d/--debug` | Displays some extra debug output.
|
187
|
-
`-D/--display-cop-names` | Displays cop names in offense messages.
|
188
|
-
`-E/--extra-details` | Displays extra details in offense messages.
|
189
|
-
`-c/--config` | Run with specified config file.
|
190
|
-
`-f/--format` | Choose a formatter.
|
191
|
-
`-o/--out` | Write output to a file instead of STDOUT.
|
192
|
-
`-r/--require` | Require Ruby file (see [Loading Extensions](#loading-extensions)).
|
193
|
-
`-R/--rails` | Run extra Rails cops.
|
194
|
-
`-l/--lint` | Run only lint cops.
|
195
|
-
`-a/--auto-correct` | Auto-correct certain offenses. *Note:* Experimental - use with caution.
|
196
|
-
`--only` | Run only the specified cop(s) and/or cops in the specified departments.
|
197
|
-
`--except` | Run all cops enabled by configuration except the specified cop(s) and/or departments.
|
198
|
-
`--auto-gen-config` | Generate a configuration file acting as a TODO list.
|
199
|
-
`--no-offense-counts` | Don't show offense counts in config file generated by --auto-gen-config
|
200
|
-
`--exclude-limit` | Limit how many individual files `--auto-gen-config` can list in `Exclude` parameters, default is 15.
|
201
|
-
`--show-cops` | Shows available cops and their configuration.
|
202
|
-
`--fail-level` | Minimum [severity](#severity) for exit with error code. Full severity name or upper case initial can be given. Normally, auto-corrected offenses are ignored. Use `A` or `autocorrect` if you'd like them to trigger failure.
|
203
|
-
`-s/--stdin` | Pipe source from STDIN. This is useful for editor integration.
|
204
|
-
`--[no-]color` | Force color output on or off.
|
205
|
-
|
206
|
-
Default command-line options are loaded from `.rubocop` and `RUBOCOP_OPTS` and are combined with command-line options that are explicitly passed to `rubocop`.
|
207
|
-
Thus, the options have the following order of precedence (from highest to lowest):
|
208
|
-
|
209
|
-
1. Explicit command-line options
|
210
|
-
2. Options from `RUBOCOP_OPTS` environment variable
|
211
|
-
3. Options from `.rubocop` file.
|
212
|
-
|
213
|
-
### Cops
|
214
|
-
|
215
|
-
In RuboCop lingo the various checks performed on the code are called cops. There are several cop departments.
|
216
|
-
|
217
|
-
You can also load [custom cops](#custom-cops).
|
218
|
-
|
219
|
-
#### Style
|
220
|
-
|
221
|
-
Most of the cops in RuboCop are so called style cops that check for
|
222
|
-
stylistic problems in your code. Almost all of the them are based on
|
223
|
-
the Ruby Style Guide. Many of the style cops have configurations
|
224
|
-
options allowing them to support different popular coding
|
225
|
-
conventions.
|
226
|
-
|
227
|
-
#### Lint
|
228
|
-
|
229
|
-
Lint cops check for possible errors and very bad practices in your
|
230
|
-
code. RuboCop implements in a portable way all built-in MRI lint
|
231
|
-
checks (`ruby -wc`) and adds a lot of extra lint checks of its
|
232
|
-
own. You can run only the lint cops like this:
|
233
|
-
|
234
|
-
```sh
|
235
|
-
$ rubocop -l
|
54
|
+
gem 'rubocop', '~> 0.41.0', require: false
|
236
55
|
```
|
237
56
|
|
238
|
-
|
239
|
-
enabled lint cops plus a selection of other cops.
|
240
|
-
|
241
|
-
Disabling any of the lint cops is generally a bad idea.
|
242
|
-
|
243
|
-
#### Metrics
|
244
|
-
|
245
|
-
Metrics cops deal with properties of the source code that can be measured,
|
246
|
-
such as class length, method length, etc. Generally speaking, they have a
|
247
|
-
configuration parameter called `Max` and when running
|
248
|
-
`rubocop --auto-gen-config`, this parameter will be set to the highest value
|
249
|
-
found for the inspected code.
|
57
|
+
## Quickstart
|
250
58
|
|
251
|
-
|
59
|
+
Just type `rubocop` in a Ruby project's folder and watch the magic happen.
|
252
60
|
|
253
|
-
Performance cops catch Ruby idioms which are known to be slower than another
|
254
|
-
equivalent (and equally readable) idiom.
|
255
|
-
|
256
|
-
#### Rails
|
257
|
-
|
258
|
-
Rails cops are specific to the Ruby on Rails framework. Unlike style
|
259
|
-
and lint cops they are not used by default and you have to request them
|
260
|
-
specifically:
|
261
|
-
|
262
|
-
```sh
|
263
|
-
$ rubocop -R
|
264
61
|
```
|
265
|
-
|
266
|
-
or add the following directive to your `.rubocop.yml`:
|
267
|
-
|
268
|
-
```yaml
|
269
|
-
Rails:
|
270
|
-
Enabled: true
|
271
|
-
```
|
272
|
-
|
273
|
-
## Configuration
|
274
|
-
|
275
|
-
The behavior of RuboCop can be controlled via the
|
276
|
-
[.rubocop.yml](https://github.com/bbatsov/rubocop/blob/master/.rubocop.yml)
|
277
|
-
configuration file. It makes it possible to enable/disable certain cops
|
278
|
-
(checks) and to alter their behavior if they accept any parameters. The file
|
279
|
-
can be placed either in your home directory or in some project directory.
|
280
|
-
|
281
|
-
RuboCop will start looking for the configuration file in the directory
|
282
|
-
where the inspected file is and continue its way up to the root directory.
|
283
|
-
|
284
|
-
The file has the following format:
|
285
|
-
|
286
|
-
```yaml
|
287
|
-
inherit_from: ../.rubocop.yml
|
288
|
-
|
289
|
-
Style/Encoding:
|
290
|
-
Enabled: false
|
291
|
-
|
292
|
-
Metrics/LineLength:
|
293
|
-
Max: 99
|
294
|
-
```
|
295
|
-
|
296
|
-
**Note**: Qualifying cop name with its type, e.g., `Style`, is recommended,
|
297
|
-
but not necessary as long as the cop name is unique across all types.
|
298
|
-
|
299
|
-
### Inheritance
|
300
|
-
|
301
|
-
RuboCop supports inheriting configuration from one or more supplemental
|
302
|
-
configuration files at runtime. Settings in the file that inherits
|
303
|
-
override settings in the file that's inherited from. Configuration
|
304
|
-
parameter that are hashes, for example `PreferredMethods` in
|
305
|
-
`Style/CollectionMethods` are merged with the same parameter in the base
|
306
|
-
configuration, while other parameter, such as `AllCops` / `Include`, are
|
307
|
-
simply replaced by the local setting. If arrays were merged, there would
|
308
|
-
be no way to remove elements through overriding them in local
|
309
|
-
configuration.
|
310
|
-
|
311
|
-
#### Inheriting from another configuration file in the project
|
312
|
-
|
313
|
-
The optional `inherit_from` directive is used to include configuration
|
314
|
-
from one or more files. This makes it possible to have the common
|
315
|
-
project settings in the `.rubocop.yml` file at the project root, and
|
316
|
-
then only the deviations from those rules in the subdirectories. The
|
317
|
-
files can be given with absolute paths or paths relative to the file
|
318
|
-
where they are referenced. The settings after an `inherit_from`
|
319
|
-
directive override any settings in the file(s) inherited from. When
|
320
|
-
multiple files are included, the first file in the list has the lowest
|
321
|
-
precedence and the last one has the highest. The format for multiple
|
322
|
-
inheritance is:
|
323
|
-
|
324
|
-
```yaml
|
325
|
-
inherit_from:
|
326
|
-
- ../.rubocop.yml
|
327
|
-
- ../conf/.rubocop.yml
|
328
|
-
```
|
329
|
-
|
330
|
-
### Inheriting configuration from a remote URL
|
331
|
-
|
332
|
-
The optional `inherit_from` directive can contain a full url to a remote
|
333
|
-
file. This makes it possible to have common project settings stored on a http
|
334
|
-
server and shared between many projects.
|
335
|
-
|
336
|
-
The remote config file is cached locally and is only updated if:
|
337
|
-
|
338
|
-
- The file does not exist.
|
339
|
-
- The file has not been updated in the last 24 hours.
|
340
|
-
- The remote copy has a newer modification time than the local copy.
|
341
|
-
|
342
|
-
You can inherit from both remote and local files in the same config and the
|
343
|
-
same inheritance rules apply to remote URLs and inheriting from local
|
344
|
-
files where the first file in the list has the lowest precedence and the
|
345
|
-
last one has the highest. The format for multiple inheritance using URLs is:
|
346
|
-
|
347
|
-
```yaml
|
348
|
-
inherit_from:
|
349
|
-
- http://www.example.com/rubocop.yml
|
350
|
-
- ../.rubocop.yml
|
351
|
-
```
|
352
|
-
|
353
|
-
#### Inheriting configuration from a dependency gem
|
354
|
-
|
355
|
-
The optional `inherit_gem` directive is used to include configuration from
|
356
|
-
one or more gems external to the current project. This makes it possible to
|
357
|
-
inherit a shared dependency's RuboCop configuration that can be used from
|
358
|
-
multiple disparate projects.
|
359
|
-
|
360
|
-
Configurations inherited in this way will be essentially *prepended* to the
|
361
|
-
`inherit_from` directive, such that the `inherit_gem` configurations will be
|
362
|
-
loaded first, then the `inherit_from` relative file paths will be loaded
|
363
|
-
(overriding the configurations from the gems), and finally the remaining
|
364
|
-
directives in the configuration file will supersede any of the inherited
|
365
|
-
configurations. This means the configurations inherited from one or more gems
|
366
|
-
have the lowest precedence of inheritance.
|
367
|
-
|
368
|
-
The directive should be formatted as a YAML Hash using the gem name as the
|
369
|
-
key and the relative path within the gem as the value:
|
370
|
-
|
371
|
-
```yaml
|
372
|
-
inherit_gem:
|
373
|
-
my-shared-gem: .rubocop.yml
|
374
|
-
cucumber: conf/rubocop.yml
|
375
|
-
```
|
376
|
-
|
377
|
-
**Note**: If the shared dependency is declared using a [Bundler](http://bundler.io/)
|
378
|
-
Gemfile and the gem was installed using `bundle install`, it would be
|
379
|
-
necessary to also invoke RuboCop using Bundler in order to find the
|
380
|
-
dependency's installation path at runtime:
|
381
|
-
|
382
|
-
```
|
383
|
-
$ bundle exec rubocop <options...>
|
384
|
-
```
|
385
|
-
|
386
|
-
### Defaults
|
387
|
-
|
388
|
-
The file
|
389
|
-
[config/default.yml](https://github.com/bbatsov/rubocop/blob/master/config/default.yml)
|
390
|
-
under the RuboCop home directory contains the default settings that
|
391
|
-
all configurations inherit from. Project and personal `.rubocop.yml`
|
392
|
-
files need only make settings that are different from the default
|
393
|
-
ones. If there is no `.rubocop.yml` file in the project or home
|
394
|
-
directory, `config/default.yml` will be used.
|
395
|
-
|
396
|
-
### Including/Excluding files
|
397
|
-
|
398
|
-
RuboCop checks all files found by a recursive search starting from the
|
399
|
-
directory it is run in, or directories given as command line
|
400
|
-
arguments. However, it only recognizes files ending with `.rb` or
|
401
|
-
extensionless files with a `#!.*ruby` declaration as Ruby files.
|
402
|
-
Hidden directories (i.e., directories whose names start with a dot)
|
403
|
-
are not searched by default. If you'd like it to check files that are
|
404
|
-
not included by default, you'll need to pass them in on the command
|
405
|
-
line, or to add entries for them under `AllCops`/`Include`. Files and
|
406
|
-
directories can also be ignored through `AllCops`/`Exclude`.
|
407
|
-
|
408
|
-
Here is an example that might be used for a Rails project:
|
409
|
-
|
410
|
-
```yaml
|
411
|
-
AllCops:
|
412
|
-
Include:
|
413
|
-
- '**/Rakefile'
|
414
|
-
- '**/config.ru'
|
415
|
-
Exclude:
|
416
|
-
- 'db/**/*'
|
417
|
-
- 'config/**/*'
|
418
|
-
- 'script/**/*'
|
419
|
-
- !ruby/regexp /old_and_unused\.rb$/
|
420
|
-
|
421
|
-
# other configuration
|
422
|
-
# ...
|
423
|
-
```
|
424
|
-
|
425
|
-
Files and directories are specified relative to the `.rubocop.yml` file.
|
426
|
-
|
427
|
-
**Note**: Patterns that are just a file name, e.g. `Rakefile`, will match
|
428
|
-
that file name in any directory, but this pattern style is deprecated. The
|
429
|
-
correct way to match the file in any directory, including the current, is
|
430
|
-
`**/Rakefile`.
|
431
|
-
|
432
|
-
**Note**: The pattern `config/**` will match any file recursively under
|
433
|
-
`config`, but this pattern style is deprecated and should be replaced by
|
434
|
-
`config/**/*`.
|
435
|
-
|
436
|
-
**Note**: The `Include` and `Exclude` parameters are special. They are
|
437
|
-
valid for the directory tree starting where they are defined. They are not
|
438
|
-
shadowed by the setting of `Include` and `Exclude` in other `.rubocop.yml`
|
439
|
-
files in subdirectories. This is different from all other parameters, who
|
440
|
-
follow RuboCop's general principle that configuration for an inspected file
|
441
|
-
is taken from the nearest `.rubocop.yml`, searching upwards.
|
442
|
-
|
443
|
-
Cops can be run only on specific sets of files when that's needed (for
|
444
|
-
instance you might want to run some Rails model checks only on files whose
|
445
|
-
paths match `app/models/*.rb`). All cops support the
|
446
|
-
`Include` param.
|
447
|
-
|
448
|
-
```yaml
|
449
|
-
Rails/HasAndBelongsToMany:
|
450
|
-
Include:
|
451
|
-
- app/models/*.rb
|
452
|
-
```
|
453
|
-
|
454
|
-
Cops can also exclude only specific sets of files when that's needed (for
|
455
|
-
instance you might want to run some cop only on a specific file). All cops support the
|
456
|
-
`Exclude` param.
|
457
|
-
|
458
|
-
```yaml
|
459
|
-
Rails/HasAndBelongsToMany:
|
460
|
-
Exclude:
|
461
|
-
- app/models/problematic.rb
|
462
|
-
```
|
463
|
-
|
464
|
-
### Generic configuration parameters
|
465
|
-
|
466
|
-
In addition to `Include` and `Exclude`, the following parameters are available
|
467
|
-
for every cop.
|
468
|
-
|
469
|
-
#### Enabled
|
470
|
-
|
471
|
-
Specific cops can be disabled by setting `Enabled` to `false` for that specific cop.
|
472
|
-
|
473
|
-
```yaml
|
474
|
-
Metrics/LineLength:
|
475
|
-
Enabled: false
|
476
|
-
```
|
477
|
-
|
478
|
-
Most cops are enabled by default. Some cops, configured in [config/disabled.yml](https://github.com/bbatsov/rubocop/blob/master/config/disabled.yml), are disabled by default. The cop enabling process can be altered by setting `DisabledByDefault` to `true`.
|
479
|
-
|
480
|
-
```yaml
|
481
|
-
AllCops:
|
482
|
-
DisabledByDefault: true
|
483
|
-
```
|
484
|
-
|
485
|
-
All cops are then disabled by default, and only cops appearing in user configuration files are enabled. `Enabled: true` does not have to be set for cops in user configuration. They will be enabled anyway.
|
486
|
-
|
487
|
-
#### Severity
|
488
|
-
|
489
|
-
Each cop has a default severity level based on which department it belongs
|
490
|
-
to. The level is `warning` for `Lint` and `convention` for all the others.
|
491
|
-
Cops can customize their severity level. Allowed params are `refactor`,
|
492
|
-
`convention`, `warning`, `error` and `fatal`.
|
493
|
-
|
494
|
-
There is one exception from the general rule above and that is `Lint/Syntax`, a
|
495
|
-
special cop that checks for syntax errors before the other cops are invoked. It
|
496
|
-
can not be disabled and its severity (`fatal`) can not be changed in
|
497
|
-
configuration.
|
498
|
-
|
499
|
-
```yaml
|
500
|
-
Metrics/CyclomaticComplexity:
|
501
|
-
Severity: warning
|
502
|
-
```
|
503
|
-
|
504
|
-
#### Details
|
505
|
-
|
506
|
-
Individual cops can be embellished with extra details in offense messages:
|
507
|
-
|
508
|
-
```yaml
|
509
|
-
Metrics/LineLength:
|
510
|
-
Details: >-
|
511
|
-
If lines are too short, text becomes hard to read because you must
|
512
|
-
constantly jump from one line to the next while reading. If lines are too
|
513
|
-
long, the line jumping becomes too hard because you "lose the line" while
|
514
|
-
going back to the start of the next line. 80 characters is a good
|
515
|
-
compromise.
|
516
|
-
```
|
517
|
-
|
518
|
-
#### AutoCorrect
|
519
|
-
|
520
|
-
Cops that support the `--auto-correct` option can have that support
|
521
|
-
disabled. For example:
|
522
|
-
|
523
|
-
```yaml
|
524
|
-
Style/PerlBackrefs:
|
525
|
-
AutoCorrect: false
|
526
|
-
```
|
527
|
-
|
528
|
-
### Setting the target Ruby version
|
529
|
-
|
530
|
-
Some checks are dependent on the version of the Ruby interpreter which the
|
531
|
-
inspected code must run on. For example, using Ruby 2.0+ keyword arguments
|
532
|
-
rather than an options hash can help make your code shorter and more
|
533
|
-
expressive... _unless_ it must run on Ruby 1.9.
|
534
|
-
|
535
|
-
Let RuboCop know the oldest version of Ruby which your project supports with:
|
536
|
-
|
537
|
-
```yaml
|
538
|
-
AllCops:
|
539
|
-
TargetRubyVersion: 1.9
|
540
|
-
```
|
541
|
-
|
542
|
-
### Automatically Generated Configuration
|
543
|
-
|
544
|
-
If you have a code base with an overwhelming amount of offenses, it can
|
545
|
-
be a good idea to use `rubocop --auto-gen-config` and add an
|
546
|
-
`inherit_from: .rubocop_todo.yml` in your `.rubocop.yml`. The generated
|
547
|
-
file `.rubocop_todo.yml` contains configuration to disable cops that
|
548
|
-
currently detect an offense in the code by excluding the offending
|
549
|
-
files, or disabling the cop altogether once a file count limit has been
|
550
|
-
reached.
|
551
|
-
|
552
|
-
By adding the option `--exclude-limit COUNT`, e.g., `rubocop
|
553
|
-
--auto-gen-config --exclude-limit 5`, you can change how many files are
|
554
|
-
excluded before the cop is entirely disabled. The default COUNT is 15.
|
555
|
-
|
556
|
-
Then you can start removing the entries in the generated
|
557
|
-
`.rubocop_todo.yml` file one by one as you work through all the offenses
|
558
|
-
in the code.
|
559
|
-
|
560
|
-
## Disabling Cops within Source Code
|
561
|
-
|
562
|
-
One or more individual cops can be disabled locally in a section of a
|
563
|
-
file by adding a comment such as
|
564
|
-
|
565
|
-
```ruby
|
566
|
-
# rubocop:disable Metrics/LineLength, Style/StringLiterals
|
567
|
-
[...]
|
568
|
-
# rubocop:enable Metrics/LineLength, Style/StringLiterals
|
569
|
-
```
|
570
|
-
|
571
|
-
You can also disable *all* cops with
|
572
|
-
|
573
|
-
```ruby
|
574
|
-
# rubocop:disable all
|
575
|
-
[...]
|
576
|
-
# rubocop:enable all
|
577
|
-
```
|
578
|
-
|
579
|
-
One or more cops can be disabled on a single line with an end-of-line
|
580
|
-
comment.
|
581
|
-
|
582
|
-
```ruby
|
583
|
-
for x in (0..19) # rubocop:disable Style/AvoidFor
|
584
|
-
```
|
585
|
-
|
586
|
-
## Formatters
|
587
|
-
|
588
|
-
You can change the output format of RuboCop by specifying formatters with the `-f/--format` option.
|
589
|
-
RuboCop ships with several built-in formatters, and also you can create your custom formatter.
|
590
|
-
|
591
|
-
Additionally the output can be redirected to a file instead of `$stdout` with the `-o/--out` option.
|
592
|
-
|
593
|
-
Some of the built-in formatters produce **machine-parsable** output
|
594
|
-
and they are considered public APIs.
|
595
|
-
The rest of the formatters are for humans, so parsing their outputs is discouraged.
|
596
|
-
|
597
|
-
You can enable multiple formatters at the same time by specifying `-f/--format` multiple times.
|
598
|
-
The `-o/--out` option applies to the previously specified `-f/--format`,
|
599
|
-
or the default `progress` format if no `-f/--format` is specified before the `-o/--out` option.
|
600
|
-
|
601
|
-
```sh
|
602
|
-
# Simple format to $stdout.
|
603
|
-
$ rubocop --format simple
|
604
|
-
|
605
|
-
# Progress (default) format to the file result.txt.
|
606
|
-
$ rubocop --out result.txt
|
607
|
-
|
608
|
-
# Both progress and offense count formats to $stdout.
|
609
|
-
# The offense count formatter outputs only the final summary,
|
610
|
-
# so you'll mostly see the outputs from the progress formatter,
|
611
|
-
# and at the end the offense count summary will be outputted.
|
612
|
-
$ rubocop --format progress --format offenses
|
613
|
-
|
614
|
-
# Progress format to $stdout, and JSON format to the file rubocop.json.
|
615
|
-
$ rubocop --format progress --format json --out rubocop.json
|
616
|
-
# ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
|
617
|
-
# | |_______________|
|
618
|
-
# $stdout
|
619
|
-
|
620
|
-
# Progress format to result.txt, and simple format to $stdout.
|
621
|
-
$ rubocop --output result.txt --format simple
|
622
|
-
# ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
|
623
|
-
# | |
|
624
|
-
# default format $stdout
|
625
|
-
```
|
626
|
-
|
627
|
-
You can also load [custom formatters](#custom-formatters).
|
628
|
-
|
629
|
-
### Progress Formatter (default)
|
630
|
-
|
631
|
-
The default `progress` formatter outputs a character for each inspected file,
|
632
|
-
and at the end it displays all detected offenses in the `clang` format.
|
633
|
-
A `.` represents a clean file, and each of the capital letters means
|
634
|
-
the severest offense (convention, warning, error or fatal) found in a file.
|
635
|
-
|
636
|
-
```sh
|
62
|
+
$ cd my/cool/ruby/project
|
637
63
|
$ rubocop
|
638
|
-
Inspecting 26 files
|
639
|
-
..W.C....C..CWCW.C...WC.CC
|
640
|
-
|
641
|
-
Offenses:
|
642
|
-
|
643
|
-
lib/foo.rb:6:5: C: Missing top-level class documentation comment.
|
644
|
-
class Foo
|
645
|
-
^^^^^
|
646
|
-
|
647
|
-
...
|
648
|
-
|
649
|
-
26 files inspected, 46 offenses detected
|
650
64
|
```
|
651
65
|
|
652
|
-
|
653
|
-
|
654
|
-
The `clang` formatter displays the offenses in a manner similar to `clang`:
|
655
|
-
|
656
|
-
```sh
|
657
|
-
$ rubocop test.rb
|
658
|
-
Inspecting 1 file
|
659
|
-
W
|
660
|
-
|
661
|
-
Offenses:
|
66
|
+
## Official manual
|
662
67
|
|
663
|
-
|
664
|
-
def badName
|
665
|
-
^^^^^^^
|
666
|
-
test.rb:2:3: C: Use a guard clause instead of wrapping the code inside a conditional expression.
|
667
|
-
if something
|
668
|
-
^^
|
669
|
-
test.rb:2:3: C: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
|
670
|
-
if something
|
671
|
-
^^
|
672
|
-
test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2
|
673
|
-
end
|
674
|
-
^^^
|
675
|
-
|
676
|
-
1 file inspected, 4 offenses detected
|
677
|
-
```
|
678
|
-
|
679
|
-
### Fuubar Style Formatter
|
680
|
-
|
681
|
-
The `fuubar` style formatter displays a progress bar
|
682
|
-
and shows details of offenses in the `clang` format as soon as they are detected.
|
683
|
-
This is inspired by the [Fuubar](https://github.com/thekompanee/fuubar) formatter for RSpec.
|
684
|
-
|
685
|
-
```sh
|
686
|
-
$ rubocop --format fuubar
|
687
|
-
lib/foo.rb.rb:1:1: C: Use snake_case for methods and variables.
|
688
|
-
def badName
|
689
|
-
^^^^^^^
|
690
|
-
lib/bar.rb:13:14: W: File.exists? is deprecated in favor of File.exist?.
|
691
|
-
File.exists?(path)
|
692
|
-
^^^^^^^
|
693
|
-
22/53 files |======== 43 ========> | ETA: 00:00:02
|
694
|
-
```
|
695
|
-
|
696
|
-
### Emacs Style Formatter
|
697
|
-
|
698
|
-
**Machine-parsable**
|
699
|
-
|
700
|
-
The `emacs` formatter displays the offenses in a format suitable for consumption by `Emacs` (and possibly other tools).
|
701
|
-
|
702
|
-
```sh
|
703
|
-
$ rubocop --format emacs test.rb
|
704
|
-
/Users/bozhidar/projects/test.rb:1:1: C: Use snake_case for methods and variables.
|
705
|
-
/Users/bozhidar/projects/test.rb:2:3: C: Favor modifier if/unless usage when you have a single-line body. Another good alternative is the usage of control flow &&/||.
|
706
|
-
/Users/bozhidar/projects/test.rb:4:5: W: end at 4, 4 is not aligned with if at 2, 2
|
707
|
-
```
|
708
|
-
|
709
|
-
### Simple Formatter
|
710
|
-
|
711
|
-
The name of the formatter says it all :-)
|
712
|
-
|
713
|
-
```sh
|
714
|
-
$ rubocop --format simple test.rb
|
715
|
-
== test.rb ==
|
716
|
-
C: 1: 5: Use snake_case for method names.
|
717
|
-
C: 2: 3: Use a guard clause instead of wrapping the code inside a conditional expression.
|
718
|
-
C: 2: 3: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
|
719
|
-
W: 4: 5: end at 4, 4 is not aligned with if at 2, 2
|
720
|
-
|
721
|
-
1 file inspected, 4 offenses detected
|
722
|
-
```
|
723
|
-
|
724
|
-
### File List Formatter
|
725
|
-
|
726
|
-
**Machine-parsable**
|
727
|
-
|
728
|
-
Sometimes you might want to just open all files with offenses in your
|
729
|
-
favorite editor. This formatter outputs just the names of the files
|
730
|
-
with offenses in them and makes it possible to do something like:
|
731
|
-
|
732
|
-
```sh
|
733
|
-
$ rubocop --format files | xargs vim
|
734
|
-
```
|
735
|
-
|
736
|
-
### JSON Formatter
|
737
|
-
|
738
|
-
**Machine-parsable**
|
739
|
-
|
740
|
-
You can get RuboCop's inspection result in JSON format by passing `--format json` option in command line.
|
741
|
-
The JSON structure is like the following example:
|
742
|
-
|
743
|
-
```javascript
|
744
|
-
{
|
745
|
-
"metadata": {
|
746
|
-
"rubocop_version": "0.9.0",
|
747
|
-
"ruby_engine": "ruby",
|
748
|
-
"ruby_version": "2.0.0",
|
749
|
-
"ruby_patchlevel": "195",
|
750
|
-
"ruby_platform": "x86_64-darwin12.3.0"
|
751
|
-
},
|
752
|
-
"files": [{
|
753
|
-
"path": "lib/foo.rb",
|
754
|
-
"offenses": []
|
755
|
-
}, {
|
756
|
-
"path": "lib/bar.rb",
|
757
|
-
"offenses": [{
|
758
|
-
"severity": "convention",
|
759
|
-
"message": "Line is too long. [81/80]",
|
760
|
-
"cop_name": "LineLength",
|
761
|
-
"corrected": true,
|
762
|
-
"location": {
|
763
|
-
"line": 546,
|
764
|
-
"column": 80,
|
765
|
-
"length": 4
|
766
|
-
}
|
767
|
-
}, {
|
768
|
-
"severity": "warning",
|
769
|
-
"message": "Unreachable code detected.",
|
770
|
-
"cop_name": "UnreachableCode",
|
771
|
-
"corrected": false,
|
772
|
-
"location": {
|
773
|
-
"line": 15,
|
774
|
-
"column": 9,
|
775
|
-
"length": 10
|
776
|
-
}
|
777
|
-
}
|
778
|
-
]
|
779
|
-
}
|
780
|
-
],
|
781
|
-
"summary": {
|
782
|
-
"offense_count": 2,
|
783
|
-
"target_file_count": 2,
|
784
|
-
"inspected_file_count": 2
|
785
|
-
}
|
786
|
-
}
|
787
|
-
```
|
788
|
-
|
789
|
-
### Offense Count Formatter
|
790
|
-
|
791
|
-
Sometimes when first applying RuboCop to a codebase, it's nice to be able to
|
792
|
-
see where most of your style cleanup is going to be spent.
|
793
|
-
|
794
|
-
With this in mind, you can use the offense count formatter to outline the offended
|
795
|
-
cops and the number of offenses found for each by running:
|
796
|
-
|
797
|
-
```sh
|
798
|
-
$ rubocop --format offenses
|
799
|
-
|
800
|
-
87 Documentation
|
801
|
-
12 DotPosition
|
802
|
-
8 AvoidGlobalVars
|
803
|
-
7 EmptyLines
|
804
|
-
6 AssignmentInCondition
|
805
|
-
4 Blocks
|
806
|
-
4 CommentAnnotation
|
807
|
-
3 BlockAlignment
|
808
|
-
1 IndentationWidth
|
809
|
-
1 AvoidPerlBackrefs
|
810
|
-
1 ColonMethodCall
|
811
|
-
--
|
812
|
-
134 Total
|
813
|
-
```
|
814
|
-
|
815
|
-
### Worst Offenders Formatter
|
816
|
-
|
817
|
-
Similar to the Offense Count formatter, but lists the files which need the most attention:
|
818
|
-
|
819
|
-
```sh
|
820
|
-
$ rubocop --format worst
|
821
|
-
|
822
|
-
89 this/file/is/really/bad.rb
|
823
|
-
2 much/better.rb
|
824
|
-
--
|
825
|
-
91 Total
|
826
|
-
```
|
827
|
-
|
828
|
-
### HTML Formatter
|
829
|
-
|
830
|
-
Useful for CI environments. It will create an HTML report like [this](http://f.cl.ly/items/0M3029412x3O091a1X1R/expected.html).
|
831
|
-
|
832
|
-
```sh
|
833
|
-
$ rubocop --format html -o rubocop.html
|
834
|
-
```
|
68
|
+
You can read a ton more about RuboCop in its [official manual](http://rubocop.readthedocs.io).
|
835
69
|
|
836
70
|
## Compatibility
|
837
71
|
|
@@ -845,237 +79,6 @@ RuboCop supports the following Ruby implementations:
|
|
845
79
|
* JRuby in 1.9 mode
|
846
80
|
* Rubinius 2.0+
|
847
81
|
|
848
|
-
## Editor integration
|
849
|
-
|
850
|
-
### Emacs
|
851
|
-
|
852
|
-
[rubocop.el](https://github.com/bbatsov/rubocop-emacs) is a simple
|
853
|
-
Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs
|
854
|
-
and quickly jump between problems in your code.
|
855
|
-
|
856
|
-
[flycheck](https://github.com/flycheck/flycheck) > 0.9 also supports
|
857
|
-
RuboCop and uses it by default when available.
|
858
|
-
|
859
|
-
### Vim
|
860
|
-
|
861
|
-
The [vim-rubocop](https://github.com/ngmy/vim-rubocop) plugin runs
|
862
|
-
RuboCop and displays the results in Vim.
|
863
|
-
|
864
|
-
There's also a RuboCop checker in
|
865
|
-
[syntastic](https://github.com/scrooloose/syntastic).
|
866
|
-
|
867
|
-
### Sublime Text
|
868
|
-
|
869
|
-
If you're a ST user you might find the
|
870
|
-
[Sublime RuboCop plugin](https://github.com/pderichs/sublime_rubocop)
|
871
|
-
useful.
|
872
|
-
|
873
|
-
### Brackets
|
874
|
-
|
875
|
-
The [brackets-rubocop](https://github.com/smockle-archive/brackets-rubocop)
|
876
|
-
extension displays RuboCop results in Brackets.
|
877
|
-
It can be installed via the extension manager in Brackets.
|
878
|
-
|
879
|
-
### TextMate2
|
880
|
-
|
881
|
-
The [textmate2-rubocop](https://github.com/mrdougal/textmate2-rubocop)
|
882
|
-
bundle displays formatted RuboCop results in a new window.
|
883
|
-
Installation instructions can be found [here](https://github.com/mrdougal/textmate2-rubocop#installation).
|
884
|
-
|
885
|
-
### Atom
|
886
|
-
|
887
|
-
The [linter-rubocop](https://github.com/AtomLinter/linter-rubocop) plugin for Atom's
|
888
|
-
[linter](https://github.com/AtomLinter/Linter) runs RuboCop and highlights the offenses in Atom.
|
889
|
-
|
890
|
-
### LightTable
|
891
|
-
|
892
|
-
The [lt-rubocop](https://github.com/seancaffery/lt-rubocop) plugin
|
893
|
-
provides LightTable integration.
|
894
|
-
|
895
|
-
### RubyMine
|
896
|
-
|
897
|
-
The [rubocop-for-rubymine](https://github.com/sirlantis/rubocop-for-rubymine) plugin
|
898
|
-
provides basic RuboCop integration for RubyMine/IntelliJ IDEA.
|
899
|
-
|
900
|
-
### Other Editors
|
901
|
-
|
902
|
-
Here's one great opportunity to contribute to RuboCop - implement
|
903
|
-
RuboCop integration for your favorite editor.
|
904
|
-
|
905
|
-
## Git pre-commit hook integration
|
906
|
-
|
907
|
-
[overcommit](https://github.com/brigade/overcommit) is a fully configurable and
|
908
|
-
extendable Git commit hook manager. To use RuboCop with overcommit, add the
|
909
|
-
following to your `.overcommit.yml` file:
|
910
|
-
|
911
|
-
```yaml
|
912
|
-
PreCommit:
|
913
|
-
RuboCop:
|
914
|
-
enabled: true
|
915
|
-
```
|
916
|
-
|
917
|
-
## Guard integration
|
918
|
-
|
919
|
-
If you're fond of [Guard](https://github.com/guard/guard) you might
|
920
|
-
like
|
921
|
-
[guard-rubocop](https://github.com/yujinakayama/guard-rubocop). It
|
922
|
-
allows you to automatically check Ruby code style with RuboCop when
|
923
|
-
files are modified.
|
924
|
-
|
925
|
-
## Rake integration
|
926
|
-
|
927
|
-
To use RuboCop in your `Rakefile` add the following:
|
928
|
-
|
929
|
-
```ruby
|
930
|
-
require 'rubocop/rake_task'
|
931
|
-
|
932
|
-
RuboCop::RakeTask.new
|
933
|
-
```
|
934
|
-
|
935
|
-
If you run `rake -T`, the following two RuboCop tasks should show up:
|
936
|
-
|
937
|
-
```sh
|
938
|
-
rake rubocop # Run RuboCop
|
939
|
-
rake rubocop:auto_correct # Auto-correct RuboCop offenses
|
940
|
-
```
|
941
|
-
|
942
|
-
The above will use default values
|
943
|
-
|
944
|
-
```ruby
|
945
|
-
require 'rubocop/rake_task'
|
946
|
-
|
947
|
-
desc 'Run RuboCop on the lib directory'
|
948
|
-
RuboCop::RakeTask.new(:rubocop) do |task|
|
949
|
-
task.patterns = ['lib/**/*.rb']
|
950
|
-
# only show the files with failures
|
951
|
-
task.formatters = ['files']
|
952
|
-
# don't abort rake on failure
|
953
|
-
task.fail_on_error = false
|
954
|
-
end
|
955
|
-
```
|
956
|
-
|
957
|
-
## Exit codes
|
958
|
-
|
959
|
-
RuboCop exits with the following status codes:
|
960
|
-
|
961
|
-
- 0 if no offenses are found, or if the severity of all offenses are less than
|
962
|
-
`--fail-level`. (By default, if you use `--auto-correct`, offenses which are
|
963
|
-
auto-corrected do not cause RuboCop to fail.)
|
964
|
-
- 1 if one or more offenses equal or greater to `--fail-level` are found. (By
|
965
|
-
default, this is any offense which is not auto-corrected.)
|
966
|
-
- 2 if RuboCop terminates abnormally due to invalid configuration, invalid CLI
|
967
|
-
options, or an internal error.
|
968
|
-
|
969
|
-
## Caching
|
970
|
-
|
971
|
-
Large projects containing hundreds or even thousands of files can take
|
972
|
-
a really long time to inspect, but RuboCop has functionality to
|
973
|
-
mitigate this problem. There's a caching mechanism that stores
|
974
|
-
information about offenses found in inspected files.
|
975
|
-
|
976
|
-
### Cache Validity
|
977
|
-
|
978
|
-
Later runs will be able to retrieve this information and present the
|
979
|
-
stored information instead of inspecting the file again. This will be
|
980
|
-
done if the cache for the file is still valid, which it is if there
|
981
|
-
are no changes in:
|
982
|
-
* the contents of the inspected file
|
983
|
-
* RuboCop configuration for the file
|
984
|
-
* the options given to `rubocop`, with some exceptions that have no
|
985
|
-
bearing on which offenses are reported
|
986
|
-
* the Ruby version used to invoke `rubocop`
|
987
|
-
* version of the `rubocop` program (or to be precise, anything in the
|
988
|
-
source code of the invoked `rubocop` program)
|
989
|
-
|
990
|
-
### Enabling and Disabling the Cache
|
991
|
-
|
992
|
-
The caching functionality is enabled if the configuration parameter
|
993
|
-
`AllCops: UseCache` is `true`, which it is by default. The command
|
994
|
-
line option `--cache false` can be used to turn off caching, thus
|
995
|
-
overriding the configuration parameter. If `AllCops: UseCache` is set
|
996
|
-
to `false` in the local `.rubocop.yml`, then it's `--cache true` that
|
997
|
-
overrides the setting.
|
998
|
-
|
999
|
-
### Cache Path
|
1000
|
-
|
1001
|
-
By default, the cache is stored in in a subdirectory of the temporary
|
1002
|
-
directory, `/tmp/rubocop_cache/` on Unix-like systems. The
|
1003
|
-
configuration parameter `AllCops: CacheRootDirectory` can be used to
|
1004
|
-
set it to a different path. One reason to use this option could be
|
1005
|
-
that there's a network disk where users on different machines want to
|
1006
|
-
have a common RuboCop cache. Another could be that a Continuous
|
1007
|
-
Integration system allows directories, but not a temporary directory,
|
1008
|
-
to be saved between runs.
|
1009
|
-
|
1010
|
-
### Cache Pruning
|
1011
|
-
|
1012
|
-
Each time a file has changed, its offenses will be stored under a new
|
1013
|
-
key in the cache. This means that the cache will continue to grow
|
1014
|
-
until we do something to stop it. The configuration parameter
|
1015
|
-
`AllCops: MaxFilesInCache` sets a limit, and when the number of files
|
1016
|
-
in the cache exceeds that limit, the oldest files will be automatically
|
1017
|
-
removed from the cache.
|
1018
|
-
|
1019
|
-
## Extensions
|
1020
|
-
|
1021
|
-
It's possible to extend RuboCop with custom cops and formatters.
|
1022
|
-
|
1023
|
-
### Loading Extensions
|
1024
|
-
|
1025
|
-
Besides the `--require` command line option you can also specify ruby
|
1026
|
-
files that should be loaded with the optional `require` directive in the
|
1027
|
-
`.rubocop.yml` file:
|
1028
|
-
|
1029
|
-
```yaml
|
1030
|
-
require:
|
1031
|
-
- ../my/custom/file.rb
|
1032
|
-
- rubocop-extension
|
1033
|
-
```
|
1034
|
-
|
1035
|
-
Note: The paths are directly passed to `Kernel.require`. If your
|
1036
|
-
extension file is not in `$LOAD_PATH`, you need to specify the path as
|
1037
|
-
relative path prefixed with `./` explicitly, or absolute path. Paths
|
1038
|
-
starting with a `.` are resolved relative to `.rubocop.yml`.
|
1039
|
-
|
1040
|
-
### Custom Cops
|
1041
|
-
|
1042
|
-
You can configure the custom cops in your `.rubocop.yml` just like any
|
1043
|
-
other cop.
|
1044
|
-
|
1045
|
-
#### Known Custom Cops
|
1046
|
-
|
1047
|
-
* [rubocop-rspec](https://github.com/nevir/rubocop-rspec) -
|
1048
|
-
RSpec-specific analysis
|
1049
|
-
* [rubocop-cask](https://github.com/caskroom/rubocop-cask) - Analysis
|
1050
|
-
for Homebrew-Cask files.
|
1051
|
-
|
1052
|
-
### Custom Formatters
|
1053
|
-
|
1054
|
-
You can customize RuboCop's output format with custom formatters.
|
1055
|
-
|
1056
|
-
#### Creating a Custom Formatter
|
1057
|
-
|
1058
|
-
To implement a custom formatter, you need to subclass
|
1059
|
-
`RuboCop::Formatter::BaseFormatter` and override some methods,
|
1060
|
-
or implement all formatter API methods by duck typing.
|
1061
|
-
|
1062
|
-
Please see the documents below for more formatter API details.
|
1063
|
-
|
1064
|
-
* [RuboCop::Formatter::BaseFormatter](http://www.rubydoc.info/gems/rubocop/RuboCop/Formatter/BaseFormatter)
|
1065
|
-
* [RuboCop::Cop::Offense](http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Offense)
|
1066
|
-
* [Parser::Source::Range](http://www.rubydoc.info/github/whitequark/parser/Parser/Source/Range)
|
1067
|
-
|
1068
|
-
#### Using a Custom Formatter from the Command Line
|
1069
|
-
|
1070
|
-
You can tell RuboCop to use your custom formatter with a combination of
|
1071
|
-
`--format` and `--require` option.
|
1072
|
-
For example, when you have defined `MyCustomFormatter` in
|
1073
|
-
`./path/to/my_custom_formatter.rb`, you would type this command:
|
1074
|
-
|
1075
|
-
```sh
|
1076
|
-
$ rubocop --require ./path/to/my_custom_formatter --format MyCustomFormatter
|
1077
|
-
```
|
1078
|
-
|
1079
82
|
## Team
|
1080
83
|
|
1081
84
|
Here's a list of RuboCop's core developers:
|
@@ -1116,16 +119,6 @@ You can also support my work on RuboCop via
|
|
1116
119
|
|
1117
120
|
[![Support via Gratipay](https://cdn.rawgit.com/gratipay/gratipay-badge/2.1.3/dist/gratipay.png)](https://gratipay.com/rubocop/)
|
1118
121
|
|
1119
|
-
## Mailing List
|
1120
|
-
|
1121
|
-
If you're interested in everything regarding RuboCop's development,
|
1122
|
-
consider joining its
|
1123
|
-
[Google Group](https://groups.google.com/forum/?fromgroups#!forum/rubocop).
|
1124
|
-
|
1125
|
-
## Freenode
|
1126
|
-
|
1127
|
-
If you're into IRC you can visit the `#rubocop` channel on Freenode.
|
1128
|
-
|
1129
122
|
## Changelog
|
1130
123
|
|
1131
124
|
RuboCop's changelog is available [here](CHANGELOG.md).
|