rubocop-github 0.19.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -5
- data/STYLEGUIDE.md +190 -4
- data/config/default.yml +72 -23
- data/config/default_pending.yml +116 -0
- data/config/rails.yml +15 -18
- data/config/rails_cops.yml +3 -15
- data/config/rails_pending.yml +102 -0
- data/lib/rubocop/cop/github/avoid_object_send_with_dynamic_method.rb +77 -0
- data/lib/rubocop/cop/github/rails_controller_render_literal.rb +1 -1
- data/lib/rubocop/cop/github/rails_view_render_shorthand.rb +8 -2
- data/lib/rubocop/cop/github/render_literal_helpers.rb +2 -1
- data/lib/rubocop-github-rails.rb +0 -2
- data/lib/rubocop-github.rb +1 -0
- data/lib/version.rb +3 -0
- metadata +19 -15
- data/guides/rails-render-inline.md +0 -27
- data/lib/rubocop/cop/github/rails_application_record.rb +0 -29
- data/lib/rubocop/cop/github/rails_render_inline.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e158f9c46a8771d2dced9060e0ca5ede3e55ffddb13d6e8cfa8c6b7bfdce2888
|
4
|
+
data.tar.gz: c0b8bbed2b058338dd2e39e9050943aed6b69683c88af4f615d733276f1f1582
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aafbf512fdadbffcac228fc45fc4877cfb6ea10bd3b35a7673eea11e8010888d1e7dd4686632a4239a63fd9d431a8e9a844dfcac4a62a129c8b410461cfe5389
|
7
|
+
data.tar.gz: e1961e828dbb86115b9969ba92c2a7bda764f9607effc0a25a3b729d4c21677fa0b25dedb126f25e0b7838a173eec30d782f264f4326723dc17a0a3367203e04
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
# RuboCop GitHub
|
1
|
+
# RuboCop GitHub
|
2
2
|
|
3
|
-
|
3
|
+
[](https://github.com/github/rubocop-github/actions/workflows/test.yml)
|
4
|
+
[](https://github.com/github/rubocop-github/actions/workflows/build.yml)
|
5
|
+
[](https://github.com/github/rubocop-github/actions/workflows/lint.yml)
|
6
|
+
[](https://github.com/github/rubocop-github/actions/workflows/release.yml)
|
7
|
+
|
8
|
+
This repository provides recommended RuboCop configuration and additional Cops for use on GitHub open source and internal Ruby projects, and is the home of [GitHub's Ruby Style Guide](./STYLEGUIDE.md).
|
4
9
|
|
5
10
|
## Usage
|
6
11
|
|
@@ -16,10 +21,10 @@ Inherit all of the stylistic rules and cops through an inheritance declaration i
|
|
16
21
|
|
17
22
|
```yaml
|
18
23
|
# .rubocop.yml
|
19
|
-
|
24
|
+
inherit_gem:
|
20
25
|
rubocop-github:
|
21
|
-
|
22
|
-
|
26
|
+
- config/default.yml # generic Ruby rules and cops
|
27
|
+
- config/rails.yml # Rails-specific rules and cops
|
23
28
|
```
|
24
29
|
|
25
30
|
Alternatively, only require the additional custom cops in your `.rubocop.yml` without inheriting/enabling the other stylistic rules:
|
data/STYLEGUIDE.md
CHANGED
@@ -27,6 +27,9 @@ This is GitHub's Ruby Style Guide, inspired by [RuboCop's guide][rubocop-guide].
|
|
27
27
|
1. [Conditional keywords](#conditional-keywords)
|
28
28
|
2. [Ternary operator](#ternary-operator)
|
29
29
|
17. [Syntax](#syntax)
|
30
|
+
18. [Rails](#rails)
|
31
|
+
1. [content_for](#content_for)
|
32
|
+
2. [Instance Variables in Views](#instance-variables-in-views)
|
30
33
|
|
31
34
|
## Layout
|
32
35
|
|
@@ -34,9 +37,11 @@ This is GitHub's Ruby Style Guide, inspired by [RuboCop's guide][rubocop-guide].
|
|
34
37
|
|
35
38
|
* Use soft-tabs with a two space indent.
|
36
39
|
<a name="default-indentation"></a><sup>[[link](#default-indentation)]</sup>
|
40
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutindentationstyle">RuboCop rule: Layout/IndentationStyle</a>
|
37
41
|
|
38
42
|
* Indent `when` with the start of the `case` expression.
|
39
43
|
<a name="indent-when-as-start-of-case"></a><sup>[[link](#indent-when-as-start-of-case)]</sup>
|
44
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutcaseindentation">RuboCop rule: Layout/CaseIndentation</a>
|
40
45
|
|
41
46
|
``` ruby
|
42
47
|
# bad
|
@@ -80,10 +85,18 @@ end
|
|
80
85
|
|
81
86
|
* Never leave trailing whitespace.
|
82
87
|
<a name="trailing-whitespace"></a><sup>[[link](#trailing-whitespace)]</sup>
|
88
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layouttrailingwhitespace">RuboCop rule: Layout/TrailingWhitespace</a>
|
83
89
|
|
84
90
|
* Use spaces around operators, after commas, colons and semicolons, around `{`
|
85
91
|
and before `}`.
|
86
92
|
<a name="spaces-operators"></a><sup>[[link](#spaces-operators)]</sup>
|
93
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspacearoundoperators">RuboCop rule: Layout/SpaceAroundOperators</a>
|
94
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceaftercomma">RuboCop rule: Layout/SpaceAfterComma</a>
|
95
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceaftercolon">RuboCop rule: Layout/SpaceAfterColon</a>
|
96
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspacebeforeblockbraces">RuboCop rule: Layout/SpaceBeforeBlockBraces</a>
|
97
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceinsidehashliteralbraces">RuboCop rule: Layout/SpaceInsideHashLiteralBraces</a>
|
98
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylehashsyntax">RuboCop rule: Style/HashSyntax</a>
|
99
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspacearoundoperators">RuboCop rule: Layout/SpaceAroundOperators</a>
|
87
100
|
|
88
101
|
``` ruby
|
89
102
|
sum = 1 + 2
|
@@ -94,6 +107,8 @@ a, b = 1, 2
|
|
94
107
|
|
95
108
|
* No spaces after `(`, `[` or before `]`, `)`.
|
96
109
|
<a name="no-spaces-braces"></a><sup>[[link](#no-spaces-braces)]</sup>
|
110
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceinsideparens">RuboCop rule: Layout/SpaceInsideParens</a>
|
111
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceinsidereferencebrackets">RuboCop rule: Layout/SpaceInsideReferenceBrackets</a>
|
97
112
|
|
98
113
|
``` ruby
|
99
114
|
some(arg).other
|
@@ -102,6 +117,7 @@ some(arg).other
|
|
102
117
|
|
103
118
|
* No spaces after `!`.
|
104
119
|
<a name="no-spaces-bang"></a><sup>[[link](#no-spaces-bang)]</sup>
|
120
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceafternot">RuboCop rule: Layout/SpaceAfterNot</a>
|
105
121
|
|
106
122
|
``` ruby
|
107
123
|
!array.include?(element)
|
@@ -111,10 +127,12 @@ some(arg).other
|
|
111
127
|
|
112
128
|
* End each file with a [newline](https://github.com/bbatsov/ruby-style-guide#newline-eof).
|
113
129
|
<a name="newline-eof"></a><sup>[[link](#newline-eof)]</sup>
|
130
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layouttrailingemptylines">RuboCop rule: Layout/TrailingEmptyLines</a>
|
114
131
|
|
115
132
|
* Use empty lines between `def`s and to break up a method into logical
|
116
133
|
paragraphs.
|
117
134
|
<a name="empty-lines-def"></a><sup>[[link](#empty-lines-def)]</sup>
|
135
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutemptylinebetweendefs">RuboCop rule: Layout/EmptyLineBetweenDefs</a>
|
118
136
|
|
119
137
|
``` ruby
|
120
138
|
def some_method
|
@@ -134,12 +152,14 @@ end
|
|
134
152
|
|
135
153
|
* Keep each line of code to a readable length. Unless you have a reason to, keep lines to a maximum of 118 characters. Why 118? That's the width at which the pull request diff UI needs horizontal scrolling (making pull requests harder to review).
|
136
154
|
<a name="line-length"></a><sup>[[link](#line-length)]</sup>
|
155
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutlinelength">RuboCop rule: Layout/LineLength</a>
|
137
156
|
|
138
157
|
## Classes
|
139
158
|
|
140
159
|
* Avoid the usage of class (`@@`) variables due to their unusual behavior
|
141
160
|
in inheritance.
|
142
161
|
<a name="class-variables"></a><sup>[[link](#class-variables)]</sup>
|
162
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleclassvars">RuboCop rule: Style/ClassVars</a>
|
143
163
|
|
144
164
|
``` ruby
|
145
165
|
class Parent
|
@@ -164,6 +184,7 @@ Parent.print_class_var # => will print "child"
|
|
164
184
|
* Use `def self.method` to define singleton methods. This makes the methods
|
165
185
|
more resistant to refactoring changes.
|
166
186
|
<a name="singleton-methods"></a><sup>[[link](#singleton-methods)]</sup>
|
187
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleclassmethodsdefinitions">RuboCop rule: Style/ClassMethodsDefinitions</a>
|
167
188
|
|
168
189
|
``` ruby
|
169
190
|
class TestClass
|
@@ -181,6 +202,7 @@ class TestClass
|
|
181
202
|
* Avoid `class << self` except when necessary, e.g. single accessors and aliased
|
182
203
|
attributes.
|
183
204
|
<a name="class-method-definitions"></a><sup>[[link](#class-method-definitions)]</sup>
|
205
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleclassmethodsdefinitions">RuboCop rule: Style/ClassMethodsDefinitions</a>
|
184
206
|
|
185
207
|
``` ruby
|
186
208
|
class TestClass
|
@@ -214,6 +236,8 @@ end
|
|
214
236
|
* Indent the `public`, `protected`, and `private` methods as much the
|
215
237
|
method definitions they apply to. Leave one blank line above them.
|
216
238
|
<a name="access-modifier-identation"></a><sup>[[link](#access-modifier-identation)]</sup>
|
239
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutaccessmodifierindentation">RuboCop rule: Layout/AccessModifierIndentation</a>
|
240
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_layout.html#layoutemptylinesaroundaccessmodifier">RuboCop rule: Layout/EmptyLinesAroundAccessModifier</a>
|
217
241
|
|
218
242
|
``` ruby
|
219
243
|
class SomeClass
|
@@ -231,6 +255,7 @@ end
|
|
231
255
|
* Avoid explicit use of `self` as the recipient of internal class or instance
|
232
256
|
messages unless to specify a method shadowed by a variable.
|
233
257
|
<a name="self-messages"></a><sup>[[link](#self-messages)]</sup>
|
258
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleredundantself">RuboCop rule: Style/RedundantSelf</a>
|
234
259
|
|
235
260
|
``` ruby
|
236
261
|
class SomeClass
|
@@ -248,6 +273,7 @@ end
|
|
248
273
|
* Prefer `%w` to the literal array syntax when you need an array of
|
249
274
|
strings.
|
250
275
|
<a name="percent-w"></a><sup>[[link](#percent-w)]</sup>
|
276
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylewordarray">RuboCop rule: Style/WordArray</a>
|
251
277
|
|
252
278
|
``` ruby
|
253
279
|
# bad
|
@@ -265,6 +291,7 @@ STATES = %w(draft open closed)
|
|
265
291
|
|
266
292
|
* Use symbols instead of strings as hash keys.
|
267
293
|
<a name="symbols-as-keys"></a><sup>[[link](#symbols-as-keys)]</sup>
|
294
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylestringhashkeys">RuboCop rule: Style/StringHashKeys</a>
|
268
295
|
|
269
296
|
``` ruby
|
270
297
|
# bad
|
@@ -300,9 +327,10 @@ end
|
|
300
327
|
|
301
328
|
Avoid calling `send` and its cousins unless you really need it. Metaprogramming can be extremely powerful, but in most cases you can write code that captures your meaning by being explicit:
|
302
329
|
<a name="avoid-send"></a><sup>[[link](#avoid-send)]</sup>
|
330
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylesend">RuboCop rule: Style/Send</a>
|
303
331
|
|
304
332
|
``` ruby
|
305
|
-
# avoid
|
333
|
+
# avoid
|
306
334
|
unless [:base, :head].include?(base_or_head)
|
307
335
|
raise ArgumentError, "base_or_head must be either :base or :head"
|
308
336
|
end
|
@@ -366,6 +394,7 @@ end
|
|
366
394
|
|
367
395
|
Use the Ruby 1.9 syntax for hash literals when all the keys are symbols:
|
368
396
|
<a name="symbols-as-hash-keys"></a><sup>[[link](#symbols-as-hash-keys)]</sup>
|
397
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylestringhashkeys">RuboCop rule: Style/StringHashKeys</a>
|
369
398
|
|
370
399
|
``` ruby
|
371
400
|
# bad
|
@@ -396,6 +425,7 @@ link_to("Account", controller: "users", action: "show", id: user)
|
|
396
425
|
|
397
426
|
If you have a hash with mixed key types, use the legacy hashrocket style to avoid mixing styles within the same hash:
|
398
427
|
<a name="consistent-hash-syntax"></a><sup>[[link](#consistent-hash-syntax)]</sup>
|
428
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylehashsyntax">RuboCop rule: Style/HashSyntax</a>
|
399
429
|
|
400
430
|
``` ruby
|
401
431
|
|
@@ -417,6 +447,7 @@ hsh = {
|
|
417
447
|
|
418
448
|
[Keyword arguments](http://magazine.rubyist.net/?Ruby200SpecialEn-kwarg) are recommended but not required when a method's arguments may otherwise be opaque or non-obvious when called. Additionally, prefer them over the old "Hash as pseudo-named args" style from pre-2.0 ruby.
|
419
449
|
<a name="keyword-arguments"></a><sup>[[link](#keyword-arguments)]</sup>
|
450
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleoptionalbooleanparameter">RuboCop rule: Style/OptionalBooleanParameter</a>
|
420
451
|
|
421
452
|
So instead of this:
|
422
453
|
|
@@ -444,21 +475,26 @@ remove_member(user, skip_membership_check: true)
|
|
444
475
|
|
445
476
|
* Use `snake_case` for methods and variables.
|
446
477
|
<a name="snake-case-methods-vars"></a><sup>[[link](#snake-case-methods-vars)]</sup>
|
478
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_naming.html#namingsnakecase">RuboCop rule: Naming/SnakeCase</a>
|
479
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_naming.html#namingvariablename">RuboCop rule: Naming/VariableName</a>
|
447
480
|
|
448
481
|
* Use `CamelCase` for classes and modules. (Keep acronyms like HTTP,
|
449
482
|
RFC, XML uppercase.)
|
450
483
|
<a name="camelcase-classes-modules"></a><sup>[[link](#camelcase-classes-modules)]</sup>
|
484
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_naming.html#namingclassandmodulecamelcase">RuboCop rule: Naming/ClassAndModuleCamelCase</a>
|
451
485
|
|
452
486
|
* Use `SCREAMING_SNAKE_CASE` for other constants.
|
453
487
|
<a name="screaming-snake-case-constants"></a><sup>[[link](#screaming-snake-case-constants)]</sup>
|
488
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_naming.html#namingconstantname">RuboCop rule: Naming/ConstantName</a>
|
454
489
|
|
455
490
|
* The names of predicate methods (methods that return a boolean value)
|
456
491
|
should end in a question mark. (i.e. `Array#empty?`).
|
457
492
|
<a name="bool-methods-qmark"></a><sup>[[link](#bool-methods-qmark)]</sup>
|
493
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_naming.html#namingpredicatename">RuboCop rule: Naming/PredicateName</a>
|
458
494
|
|
459
495
|
* The names of potentially "dangerous" methods (i.e. methods that modify `self` or the
|
460
496
|
arguments, `exit!`, etc.) should end with an exclamation mark. Bang methods
|
461
|
-
should only exist if a non-bang counterpart (method name which does NOT end with !)
|
497
|
+
should only exist if a non-bang counterpart (method name which does NOT end with !)
|
462
498
|
also exists.
|
463
499
|
<a name="dangerous-method-bang"></a><sup>[[link](#dangerous-method-bang)]</sup>
|
464
500
|
|
@@ -466,6 +502,7 @@ remove_member(user, skip_membership_check: true)
|
|
466
502
|
|
467
503
|
* Use `%w` freely.
|
468
504
|
<a name="use-percent-w-freely"></a><sup>[[link](#use-percent-w-freely)]</sup>
|
505
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylewordarray">RuboCop rule: Style/WordArray</a>
|
469
506
|
|
470
507
|
``` ruby
|
471
508
|
STATES = %w(draft open closed)
|
@@ -474,6 +511,7 @@ STATES = %w(draft open closed)
|
|
474
511
|
* Use `%()` for single-line strings which require both interpolation
|
475
512
|
and embedded double-quotes. For multi-line strings, prefer heredocs.
|
476
513
|
<a name="percent-parens-single-line"></a><sup>[[link](#percent-parens-single-line)]</sup>
|
514
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylebarepercentliterals">RuboCop rule: Style/BarePercentLiterals</a>
|
477
515
|
|
478
516
|
``` ruby
|
479
517
|
# bad (no interpolation needed)
|
@@ -494,6 +532,7 @@ STATES = %w(draft open closed)
|
|
494
532
|
|
495
533
|
* Use `%r` only for regular expressions matching *more than* one '/' character.
|
496
534
|
<a name="percent-r-regular-expressions"></a><sup>[[link](#percent-r-regular-expressions)]</sup>
|
535
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleregexpliteral">RuboCop rule: Style/RegexpLiteral</a>
|
497
536
|
|
498
537
|
``` ruby
|
499
538
|
# bad
|
@@ -512,7 +551,7 @@ STATES = %w(draft open closed)
|
|
512
551
|
* Avoid using $1-9 as it can be hard to track what they contain. Named groups
|
513
552
|
can be used instead.
|
514
553
|
<a name="capture-with-named-groups"></a><sup>[[link](#capture-with-named-groups)]</sup>
|
515
|
-
|
554
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_lint.html#mixedregexpcapturetypes">RuboCop rule: Lint/MixedRegexpCaptureTypes</a>
|
516
555
|
``` ruby
|
517
556
|
# bad
|
518
557
|
/(regexp)/ =~ string
|
@@ -571,6 +610,7 @@ documentation about the libraries that the current file uses.
|
|
571
610
|
|
572
611
|
* Prefer string interpolation instead of string concatenation:
|
573
612
|
<a name="string-interpolation"></a><sup>[[link](#string-interpolation)]</sup>
|
613
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylestringconcatenation">RuboCop rule: Style/StringConcatenation</a>
|
574
614
|
|
575
615
|
``` ruby
|
576
616
|
# bad
|
@@ -584,6 +624,7 @@ email_with_name = "#{user.name} <#{user.email}>"
|
|
584
624
|
will always work without a delimiter change, and `'` is a lot more
|
585
625
|
common than `"` in string literals.
|
586
626
|
<a name="double-quotes"></a><sup>[[link](#double-quotes)]</sup>
|
627
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylestringliterals">RuboCop rule: Style/StringLiterals</a>
|
587
628
|
|
588
629
|
``` ruby
|
589
630
|
# bad
|
@@ -615,6 +656,7 @@ end
|
|
615
656
|
* Use `def` with parentheses when there are arguments. Omit the
|
616
657
|
parentheses when the method doesn't accept any arguments.
|
617
658
|
<a name="method-parens-when-arguments"></a><sup>[[link](#method-parens-when-arguments)]</sup>
|
659
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styledefwithparentheses">RuboCop rule: Style/DefWithParentheses</a>
|
618
660
|
|
619
661
|
``` ruby
|
620
662
|
def some_method
|
@@ -632,9 +674,11 @@ end
|
|
632
674
|
always use parentheses in the method invocation. For example, write
|
633
675
|
`f((3 + 2) + 1)`.
|
634
676
|
<a name="parens-no-spaces"></a><sup>[[link](#parens-no-spaces)]</sup>
|
677
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylemethodcallwithargsparentheses">RuboCop rule: Style/MethodCallWithArgsParentheses</a>
|
635
678
|
|
636
679
|
* Never put a space between a method name and the opening parenthesis.
|
637
680
|
<a name="no-spaces-method-parens"></a><sup>[[link](#no-spaces-method-parens)]</sup>
|
681
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_lint.html#lintparenthesesasgroupedexpression">RuboCop rule: Lint/ParenthesesAsGroupedExpression</a>
|
638
682
|
|
639
683
|
``` ruby
|
640
684
|
# bad
|
@@ -650,6 +694,7 @@ f(3 + 2) + 1
|
|
650
694
|
|
651
695
|
* Never use `then` for multi-line `if/unless`.
|
652
696
|
<a name="no-then-for-multi-line-if-unless"></a><sup>[[link](#no-then-for-multi-line-if-unless)]</sup>
|
697
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylemultilineifthen">RuboCop rule: Style/MultilineIfThen</a>
|
653
698
|
|
654
699
|
``` ruby
|
655
700
|
# bad
|
@@ -665,10 +710,12 @@ end
|
|
665
710
|
|
666
711
|
* The `and` and `or` keywords are banned. It's just not worth it. Always use `&&` and `||` instead.
|
667
712
|
<a name="no-and-or-or"></a><sup>[[link](#no-and-or-or)]</sup>
|
713
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleandor">RuboCop rule: Style/AndOr</a>
|
668
714
|
|
669
715
|
* Favor modifier `if/unless` usage when you have a single-line
|
670
716
|
body.
|
671
717
|
<a name="favor-modifier-if-unless"></a><sup>[[link](#favor-modifier-if-unless)]</sup>
|
718
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylemultilineternaryoperator">RuboCop rule: Style/MultilineTernaryOperator</a>
|
672
719
|
|
673
720
|
``` ruby
|
674
721
|
# bad
|
@@ -682,6 +729,7 @@ do_something if some_condition
|
|
682
729
|
|
683
730
|
* Never use `unless` with `else`. Rewrite these with the positive case first.
|
684
731
|
<a name="no-else-with-unless"></a><sup>[[link](#no-else-with-unless)]</sup>
|
732
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleunlesselse">RuboCop rule: Style/UnlessElse</a>
|
685
733
|
|
686
734
|
``` ruby
|
687
735
|
# bad
|
@@ -701,6 +749,7 @@ end
|
|
701
749
|
|
702
750
|
* Don't use parentheses around the condition of an `if/unless/while`.
|
703
751
|
<a name="no-parens-if-unless-while"></a><sup>[[link](#no-parens-if-unless-while)]</sup>
|
752
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleparenthesesaroundcondition">RuboCop rule: Style/ParenthesesAroundCondition</a>
|
704
753
|
|
705
754
|
``` ruby
|
706
755
|
# bad
|
@@ -720,6 +769,7 @@ end
|
|
720
769
|
trivial. However, do use the ternary operator(`?:`) over `if/then/else/end` constructs
|
721
770
|
for single line conditionals.
|
722
771
|
<a name="trivial-ternary"></a><sup>[[link](#trivial-ternary)]</sup>
|
772
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylemultilineternaryoperator">RuboCop rule: Style/MultilineTernaryOperator</a>
|
723
773
|
|
724
774
|
``` ruby
|
725
775
|
# bad
|
@@ -731,11 +781,13 @@ result = some_condition ? something : something_else
|
|
731
781
|
|
732
782
|
* Avoid multi-line `?:` (the ternary operator), use `if/unless` instead.
|
733
783
|
<a name="no-multiline-ternary"></a><sup>[[link](#no-multiline-ternary)]</sup>
|
784
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylemultilineternaryoperator">RuboCop rule: Style/MultilineTernaryOperator</a>
|
734
785
|
|
735
786
|
* Use one expression per branch in a ternary operator. This
|
736
787
|
also means that ternary operators must not be nested. Prefer
|
737
788
|
`if/else` constructs in these cases.
|
738
789
|
<a name="one-expression-per-branch"></a><sup>[[link](#one-expression-per-branch)]</sup>
|
790
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylenestedternaryoperator">RuboCop rule: Style/NestedTernaryOperator</a>
|
739
791
|
|
740
792
|
``` ruby
|
741
793
|
# bad
|
@@ -757,6 +809,7 @@ end
|
|
757
809
|
doesn't introduce a new scope (unlike `each`) and variables defined
|
758
810
|
in its block will be visible outside it.
|
759
811
|
<a name="avoid-for"></a><sup>[[link](#avoid-for)]</sup>
|
812
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylefor">RuboCop rule: Style/For</a>
|
760
813
|
|
761
814
|
``` ruby
|
762
815
|
arr = [1, 2, 3]
|
@@ -776,6 +829,7 @@ arr.each { |elem| puts elem }
|
|
776
829
|
definitions" (e.g. in Rakefiles and certain DSLs). Avoid `do...end`
|
777
830
|
when chaining.
|
778
831
|
<a name="squiggly-braces"></a><sup>[[link](#squiggly-braces)]</sup>
|
832
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleblockdelimiters">RuboCop rule: Style/BlockDelimiters</a>
|
779
833
|
|
780
834
|
``` ruby
|
781
835
|
names = ["Bozhidar", "Steve", "Sarah"]
|
@@ -798,11 +852,12 @@ end.map { |name| name.upcase }
|
|
798
852
|
```
|
799
853
|
|
800
854
|
* Some will argue that multiline chaining would look OK with the use of `{...}`,
|
801
|
-
but they should ask themselves: is this code really readable and can't the block's
|
855
|
+
but they should ask themselves: is this code really readable and can't the block's
|
802
856
|
contents be extracted into nifty methods?
|
803
857
|
|
804
858
|
* Avoid `return` where not required.
|
805
859
|
<a name="avoid-return"></a><sup>[[link](#avoid-return)]</sup>
|
860
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleredundantreturn">RuboCop rule: Style/RedundantReturn</a>
|
806
861
|
|
807
862
|
``` ruby
|
808
863
|
# bad
|
@@ -818,6 +873,7 @@ end
|
|
818
873
|
|
819
874
|
* Use spaces around the `=` operator when assigning default values to method parameters:
|
820
875
|
<a name="spaces-around-equals"></a><sup>[[link](#spaces-around-equals)]</sup>
|
876
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylespacearoundequalsinparameterdefault">RuboCop rule: Style/SpaceAroundEqualsInParameterDefault</a>
|
821
877
|
|
822
878
|
``` ruby
|
823
879
|
# bad
|
@@ -850,6 +906,7 @@ if (v = next_value) == "hello" ...
|
|
850
906
|
|
851
907
|
* Use `||=` freely to initialize variables.
|
852
908
|
<a name="memoization-for-initialization"></a><sup>[[link](#memoize-away)]</sup>
|
909
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleorassignment">RuboCop rule: Style/OrAssignment</a>
|
853
910
|
|
854
911
|
``` ruby
|
855
912
|
# set name to Bozhidar, only if it's nil or false
|
@@ -859,6 +916,7 @@ name ||= "Bozhidar"
|
|
859
916
|
* Don't use `||=` to initialize boolean variables. (Consider what
|
860
917
|
would happen if the current value happened to be `false`.)
|
861
918
|
<a name="no-memoization-for-boolean"></a><sup>[[link](#no-memoization-for-boolean)]</sup>
|
919
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#styleorassignment">RuboCop rule: Style/OrAssignment</a>
|
862
920
|
|
863
921
|
``` ruby
|
864
922
|
# bad - would set enabled to true even if it was false
|
@@ -873,9 +931,11 @@ enabled = true if enabled.nil?
|
|
873
931
|
one-liner scripts is discouraged. Prefer long form versions such as
|
874
932
|
`$PROGRAM_NAME`.
|
875
933
|
<a name="no-cryptic-vars"></a><sup>[[link](#no-cryptic-vars)]</sup>
|
934
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylespecialglobalvars">RuboCop rule: Style/SpecialGlobalVars</a>
|
876
935
|
|
877
936
|
* Use `_` for unused block parameters.
|
878
937
|
<a name="underscore-unused-vars"></a><sup>[[link](#underscore-unused-vars)]</sup>
|
938
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_lint.html#lintunusedblockargument">RuboCop rule: Lint/UnusedBlockArgument</a>
|
879
939
|
|
880
940
|
``` ruby
|
881
941
|
# bad
|
@@ -890,7 +950,133 @@ result = hash.map { |_, v| v + 1 }
|
|
890
950
|
For example, `String === "hi"` is true and `"hi" === String` is false.
|
891
951
|
Instead, use `is_a?` or `kind_of?` if you must.
|
892
952
|
<a name="type-checking-is-a-kind-of"></a><sup>[[link](#type-checking-is-a-kind-of)]</sup>
|
953
|
+
* <a href="https://docs.rubocop.org/rubocop/cops_style.html#stylecaseequality">RuboCop rule: Style/CaseEquality</a>
|
893
954
|
|
894
955
|
Refactoring is even better. It's worth looking hard at any code that explicitly checks types.
|
895
956
|
|
957
|
+
## Rails
|
958
|
+
|
959
|
+
### content_for
|
960
|
+
|
961
|
+
Limit usage of `content_for` helper. The use of `content_for` is the same as setting an instance variable plus `capture`.
|
962
|
+
|
963
|
+
``` erb
|
964
|
+
<% content_for :foo do %>
|
965
|
+
Hello
|
966
|
+
<% end %>
|
967
|
+
```
|
968
|
+
|
969
|
+
Is effectively the same as
|
970
|
+
|
971
|
+
``` erb
|
972
|
+
<% @foo_content = capture do %>
|
973
|
+
Hello
|
974
|
+
<% end %>
|
975
|
+
```
|
976
|
+
|
977
|
+
See "Instance Variables in Views" below.
|
978
|
+
|
979
|
+
#### Common Anti-patterns
|
980
|
+
|
981
|
+
**Using `content_for` within the same template to capture data.**
|
982
|
+
|
983
|
+
Instead, just use `capture`.
|
984
|
+
|
985
|
+
``` erb
|
986
|
+
<!-- bad -->
|
987
|
+
<% content_for :page do %>
|
988
|
+
Hello
|
989
|
+
<% end %>
|
990
|
+
<% if foo? %>
|
991
|
+
<div class="container">
|
992
|
+
<%= yield :page %>
|
993
|
+
</div>
|
994
|
+
<% else %>
|
995
|
+
<%= yield :page %>
|
996
|
+
<% end %>
|
997
|
+
```
|
998
|
+
|
999
|
+
Simply capture and use a local variable since the result is only needed in this template.
|
1000
|
+
|
1001
|
+
``` erb
|
1002
|
+
<!-- good -->
|
1003
|
+
<% page = capture do %>
|
1004
|
+
Hello
|
1005
|
+
<% end %>
|
1006
|
+
<% if foo? %>
|
1007
|
+
<div class="container">
|
1008
|
+
<%= page %>
|
1009
|
+
</div>
|
1010
|
+
<% else %>
|
1011
|
+
<%= page %>
|
1012
|
+
<% end %>
|
1013
|
+
```
|
1014
|
+
|
1015
|
+
**Using `content_for` to pass content to a subtemplate.**
|
1016
|
+
|
1017
|
+
Instead, `render layout:` with a block.
|
1018
|
+
|
1019
|
+
``` erb
|
1020
|
+
<!-- bad -->
|
1021
|
+
<% content_for :page do %>
|
1022
|
+
Hello
|
1023
|
+
<% end %>
|
1024
|
+
<%= render partial: "page" %>
|
1025
|
+
<!-- _page.html.erb -->
|
1026
|
+
<div class="container">
|
1027
|
+
<%= yield :page %>
|
1028
|
+
</div>
|
1029
|
+
```
|
1030
|
+
|
1031
|
+
Pass the content in a block directly to the `render` function.
|
1032
|
+
|
1033
|
+
``` erb
|
1034
|
+
<!-- good -->
|
1035
|
+
<%= render layout: "page" do %>
|
1036
|
+
Hello
|
1037
|
+
<% end %>
|
1038
|
+
<!-- _page.html.erb -->
|
1039
|
+
<div class="container">
|
1040
|
+
<%= yield %>
|
1041
|
+
</div>
|
1042
|
+
```
|
1043
|
+
|
1044
|
+
### Instance Variables in Views
|
1045
|
+
|
1046
|
+
In general, passing data between templates with instance variables is discouraged. This even applies from controllers to templates, not just between partials.
|
1047
|
+
|
1048
|
+
`:locals` can be used to pass data from a controller just like partials.
|
1049
|
+
|
1050
|
+
``` ruby
|
1051
|
+
def show
|
1052
|
+
render "blob/show", locals: {
|
1053
|
+
:repository => current_repository,
|
1054
|
+
:commit => current_commit,
|
1055
|
+
:blob => current_blob
|
1056
|
+
}
|
1057
|
+
end
|
1058
|
+
```
|
1059
|
+
|
1060
|
+
Rails implicit renders are also discouraged.
|
1061
|
+
|
1062
|
+
Always explicitly render templates with a full directory path. This makes template callers easier to trace. You can find all the callers of `"app/view/site/hompage.html.erb"` with a simple project search for `"site/homepage"`.
|
1063
|
+
|
1064
|
+
``` ruby
|
1065
|
+
def homepage
|
1066
|
+
render "site/homepage"
|
1067
|
+
end
|
1068
|
+
```
|
1069
|
+
|
1070
|
+
#### Exceptions
|
1071
|
+
|
1072
|
+
There are some known edge cases where you might be forced to use instance variables. In these cases, its okay to do so.
|
1073
|
+
|
1074
|
+
##### Legacy templates
|
1075
|
+
|
1076
|
+
If you need to call a subview that expects an instance variable be set. If possible consider refactoring the subview to accept a local instead.
|
1077
|
+
|
1078
|
+
##### Layouts
|
1079
|
+
|
1080
|
+
Unfortunately the only way to get data into a layout template is with instance variables. You can't explicitly pass locals to them.
|
1081
|
+
|
896
1082
|
[rubocop-guide]: https://github.com/rubocop-hq/ruby-style-guide
|