rubocop-github 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/STYLEGUIDE.md +190 -4
- data/config/default.yml +46 -15
- data/config/default_pending.yml +6 -0
- data/config/rails.yml +15 -18
- data/config/rails_cops.yml +3 -15
- data/config/rails_pending.yml +20 -0
- data/lib/rubocop/cop/github/rails_view_render_shorthand.rb +8 -2
- data/lib/rubocop-github-rails.rb +0 -2
- metadata +10 -11
- 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: d23c4950774e5fe3dd1dea8eb07c44ab795b16987b3915eb50456cb9d4826ad3
|
4
|
+
data.tar.gz: 2003404eb801cc43c6ef0a7625f4536e334503577dafaa26d0ecaa3a0ea873e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 105a5f9824d72e61041aae7559753e008e4337cf3a1d2a0b646e649647bbf42d8ded35d1a495a757500c02de31e9d44aeef4e25476c5a0b10b045c5712ef893d
|
7
|
+
data.tar.gz: bd5d04dd886cf8804834fea5128036dc601d02c4f984c50e3f485ccf2bc8da329f27eda1a0ad0c088556fc4857c88d71f8a3a020b67176af99a562ed2d4eb5ca
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RuboCop GitHub 
|
2
2
|
|
3
|
-
This repository provides recommended RuboCop configuration and additional Cops for use on GitHub open source and internal Ruby projects.
|
3
|
+
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
4
|
|
5
5
|
## Usage
|
6
6
|
|
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_style.html#styleparenthesesasgroupedexpression">RuboCop rule: Style/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
|
data/config/default.yml
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
inherit_from:
|
2
|
+
- ./default_pending.yml
|
3
|
+
|
1
4
|
require:
|
2
5
|
- rubocop-github
|
3
6
|
- rubocop-performance
|
@@ -65,8 +68,10 @@ Layout/BlockAlignment:
|
|
65
68
|
Layout/BlockEndNewline:
|
66
69
|
Enabled: true
|
67
70
|
|
71
|
+
# TODO: Enable this since it's in the styleguide.
|
68
72
|
Layout/CaseIndentation:
|
69
73
|
Enabled: false
|
74
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#indent-when-as-start-of-case
|
70
75
|
|
71
76
|
Layout/ClassStructure:
|
72
77
|
Enabled: false
|
@@ -180,16 +185,16 @@ Layout/HeredocIndentation:
|
|
180
185
|
Enabled: false
|
181
186
|
|
182
187
|
Layout/IndentationConsistency:
|
183
|
-
|
188
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#indentation
|
184
189
|
|
185
190
|
Layout/IndentationStyle:
|
186
|
-
Enabled: true
|
187
191
|
EnforcedStyle: spaces
|
188
192
|
IndentationWidth: 2
|
193
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#default-indentation
|
189
194
|
|
190
195
|
Layout/IndentationWidth:
|
191
|
-
Enabled: true
|
192
196
|
Width: 2
|
197
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#default-indentation
|
193
198
|
|
194
199
|
Layout/InitialIndentation:
|
195
200
|
Enabled: true
|
@@ -258,10 +263,10 @@ Layout/SingleLineBlockChain:
|
|
258
263
|
Enabled: false
|
259
264
|
|
260
265
|
Layout/SpaceAfterColon:
|
261
|
-
|
266
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#spaces-operators
|
262
267
|
|
263
268
|
Layout/SpaceAfterComma:
|
264
|
-
|
269
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#spaces-operators
|
265
270
|
|
266
271
|
Layout/SpaceAfterMethodName:
|
267
272
|
Enabled: true
|
@@ -270,16 +275,16 @@ Layout/SpaceAfterNot:
|
|
270
275
|
Enabled: true
|
271
276
|
|
272
277
|
Layout/SpaceAfterSemicolon:
|
273
|
-
|
278
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#spaces-operators
|
274
279
|
|
275
280
|
Layout/SpaceAroundBlockParameters:
|
276
281
|
Enabled: true
|
277
282
|
|
278
283
|
Layout/SpaceAroundEqualsInParameterDefault:
|
279
|
-
|
284
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#spaces-around-equals
|
280
285
|
|
281
286
|
Layout/SpaceAroundKeyword:
|
282
|
-
|
287
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#spaces-operators
|
283
288
|
|
284
289
|
Layout/SpaceAroundMethodCallOperator:
|
285
290
|
Enabled: false
|
@@ -309,8 +314,8 @@ Layout/SpaceInLambdaLiteral:
|
|
309
314
|
Enabled: false
|
310
315
|
|
311
316
|
Layout/SpaceInsideArrayLiteralBrackets:
|
312
|
-
Enabled: true
|
313
317
|
EnforcedStyle: no_space
|
318
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#no-spaces-braces
|
314
319
|
|
315
320
|
Layout/SpaceInsideArrayPercentLiteral:
|
316
321
|
Enabled: true
|
@@ -340,7 +345,7 @@ Layout/TrailingEmptyLines:
|
|
340
345
|
Enabled: true
|
341
346
|
|
342
347
|
Layout/TrailingWhitespace:
|
343
|
-
|
348
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#trailing-whitespace
|
344
349
|
|
345
350
|
Lint/AmbiguousAssignment:
|
346
351
|
Enabled: false
|
@@ -444,6 +449,15 @@ Lint/EmptyConditionalBody:
|
|
444
449
|
Lint/EmptyEnsure:
|
445
450
|
Enabled: true
|
446
451
|
|
452
|
+
Lint/EmptyExpression:
|
453
|
+
Enabled: false
|
454
|
+
|
455
|
+
Lint/EmptyFile:
|
456
|
+
Enabled: false
|
457
|
+
|
458
|
+
Lint/EmptyWhen:
|
459
|
+
Enabled: false
|
460
|
+
|
447
461
|
Lint/EmptyInterpolation:
|
448
462
|
Enabled: true
|
449
463
|
|
@@ -597,6 +611,9 @@ Lint/RegexpAsCondition:
|
|
597
611
|
Lint/RequireParentheses:
|
598
612
|
Enabled: true
|
599
613
|
|
614
|
+
Lint/RequireRangeParentheses:
|
615
|
+
Enabled: false
|
616
|
+
|
600
617
|
Lint/RequireRelativeSelfPath:
|
601
618
|
Enabled: false
|
602
619
|
|
@@ -678,8 +695,10 @@ Lint/UnreachableCode:
|
|
678
695
|
Lint/UnreachableLoop:
|
679
696
|
Enabled: false
|
680
697
|
|
698
|
+
# TODO: Enable this since it's in the styleguide.
|
681
699
|
Lint/UnusedBlockArgument:
|
682
700
|
Enabled: false
|
701
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#underscore-unused-vars
|
683
702
|
|
684
703
|
Lint/UnusedMethodArgument:
|
685
704
|
Enabled: false
|
@@ -976,8 +995,10 @@ Style/AccessorGrouping:
|
|
976
995
|
Style/Alias:
|
977
996
|
Enabled: false
|
978
997
|
|
998
|
+
# TODO: Enable this since it's in the styleguide.
|
979
999
|
Style/AndOr:
|
980
1000
|
Enabled: false
|
1001
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#no-and-or-or
|
981
1002
|
|
982
1003
|
Style/ArgumentsForwarding:
|
983
1004
|
Enabled: false
|
@@ -1076,7 +1097,7 @@ Style/DateTime:
|
|
1076
1097
|
Enabled: false
|
1077
1098
|
|
1078
1099
|
Style/DefWithParentheses:
|
1079
|
-
|
1100
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#method-parens-when-arguments
|
1080
1101
|
|
1081
1102
|
Style/Dir:
|
1082
1103
|
Enabled: false
|
@@ -1114,6 +1135,9 @@ Style/EmptyCaseCondition:
|
|
1114
1135
|
Style/EmptyElse:
|
1115
1136
|
Enabled: false
|
1116
1137
|
|
1138
|
+
Style/EmptyHeredoc:
|
1139
|
+
Enabled: false
|
1140
|
+
|
1117
1141
|
Style/EmptyLambdaParameter:
|
1118
1142
|
Enabled: false
|
1119
1143
|
|
@@ -1163,7 +1187,7 @@ Style/FloatDivision:
|
|
1163
1187
|
Enabled: false
|
1164
1188
|
|
1165
1189
|
Style/For:
|
1166
|
-
|
1190
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#avoid-for
|
1167
1191
|
|
1168
1192
|
Style/FormatString:
|
1169
1193
|
Enabled: false
|
@@ -1199,8 +1223,8 @@ Style/HashLikeCase:
|
|
1199
1223
|
Enabled: false
|
1200
1224
|
|
1201
1225
|
Style/HashSyntax:
|
1202
|
-
Enabled: true
|
1203
1226
|
EnforcedStyle: ruby19_no_mixed_keys
|
1227
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#symbols-as-keys
|
1204
1228
|
|
1205
1229
|
Style/HashTransformKeys:
|
1206
1230
|
Enabled: false
|
@@ -1256,6 +1280,9 @@ Style/LambdaCall:
|
|
1256
1280
|
Style/LineEndConcatenation:
|
1257
1281
|
Enabled: false
|
1258
1282
|
|
1283
|
+
Style/MagicCommentFormat:
|
1284
|
+
Enabled: false
|
1285
|
+
|
1259
1286
|
Style/MapCompactWithConditionalBlock:
|
1260
1287
|
Enabled: false
|
1261
1288
|
|
@@ -1299,7 +1326,7 @@ Style/MultilineIfModifier:
|
|
1299
1326
|
Enabled: false
|
1300
1327
|
|
1301
1328
|
Style/MultilineIfThen:
|
1302
|
-
|
1329
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#no-then-for-multi-line-if-unless
|
1303
1330
|
|
1304
1331
|
Style/MultilineInPatternThen:
|
1305
1332
|
Enabled: false
|
@@ -1475,8 +1502,10 @@ Style/RedundantRegexpCharacterClass:
|
|
1475
1502
|
Style/RedundantRegexpEscape:
|
1476
1503
|
Enabled: false
|
1477
1504
|
|
1505
|
+
# TODO: Enable this since it's in the styleguide.
|
1478
1506
|
Style/RedundantReturn:
|
1479
1507
|
Enabled: false
|
1508
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#avoid-return
|
1480
1509
|
|
1481
1510
|
Style/RedundantSelf:
|
1482
1511
|
Enabled: false
|
@@ -1563,8 +1592,8 @@ Style/StringHashKeys:
|
|
1563
1592
|
Enabled: false
|
1564
1593
|
|
1565
1594
|
Style/StringLiterals:
|
1566
|
-
Enabled: true
|
1567
1595
|
EnforcedStyle: double_quotes
|
1596
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#double-quotes
|
1568
1597
|
|
1569
1598
|
Style/StringLiteralsInInterpolation:
|
1570
1599
|
Enabled: false
|
@@ -1626,8 +1655,10 @@ Style/TrailingUnderscoreVariable:
|
|
1626
1655
|
Style/TrivialAccessors:
|
1627
1656
|
Enabled: false
|
1628
1657
|
|
1658
|
+
# TODO: Enable this since it's in the styleguide.
|
1629
1659
|
Style/UnlessElse:
|
1630
1660
|
Enabled: false
|
1661
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/STYLEGUIDE.md#no-else-with-unless
|
1631
1662
|
|
1632
1663
|
Style/UnlessLogicalOperators:
|
1633
1664
|
Enabled: false
|
data/config/rails.yml
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
inherit_from:
|
2
|
+
- ./rails_pending.yml
|
3
|
+
|
1
4
|
require:
|
2
5
|
- rubocop-github-rails
|
3
6
|
- rubocop-rails
|
4
7
|
|
5
|
-
GitHub/RailsApplicationRecord:
|
6
|
-
Enabled: true
|
7
|
-
|
8
8
|
GitHub/RailsControllerRenderActionSymbol:
|
9
9
|
Enabled: true
|
10
10
|
|
@@ -17,9 +17,6 @@ GitHub/RailsControllerRenderPathsExist:
|
|
17
17
|
GitHub/RailsControllerRenderShorthand:
|
18
18
|
Enabled: true
|
19
19
|
|
20
|
-
GitHub/RailsRenderInline:
|
21
|
-
Enabled: true
|
22
|
-
|
23
20
|
GitHub/RailsRenderObjectCollection:
|
24
21
|
Enabled: false
|
25
22
|
|
@@ -34,27 +31,27 @@ GitHub/RailsViewRenderShorthand:
|
|
34
31
|
|
35
32
|
Layout/BlockAlignment:
|
36
33
|
Exclude:
|
37
|
-
-
|
34
|
+
- "**/*.erb"
|
38
35
|
|
39
36
|
Layout/IndentationWidth:
|
40
37
|
Exclude:
|
41
|
-
-
|
38
|
+
- "**/*.erb"
|
42
39
|
|
43
40
|
Layout/InitialIndentation:
|
44
41
|
Exclude:
|
45
|
-
-
|
42
|
+
- "**/*.erb"
|
46
43
|
|
47
44
|
Layout/SpaceInsideParens:
|
48
45
|
Exclude:
|
49
|
-
-
|
46
|
+
- "**/*.erb"
|
50
47
|
|
51
48
|
Layout/TrailingEmptyLines:
|
52
49
|
Exclude:
|
53
|
-
-
|
50
|
+
- "**/*.erb"
|
54
51
|
|
55
52
|
Layout/TrailingWhitespace:
|
56
53
|
Exclude:
|
57
|
-
-
|
54
|
+
- "**/*.erb"
|
58
55
|
|
59
56
|
Lint/UselessAccessModifier:
|
60
57
|
ContextCreatingMethods:
|
@@ -94,7 +91,7 @@ Rails/ApplicationMailer:
|
|
94
91
|
Enabled: false
|
95
92
|
|
96
93
|
Rails/ApplicationRecord:
|
97
|
-
Enabled:
|
94
|
+
Enabled: true
|
98
95
|
|
99
96
|
Rails/ArelStar:
|
100
97
|
Enabled: false
|
@@ -307,7 +304,7 @@ Rails/RelativeDateConstant:
|
|
307
304
|
Enabled: false
|
308
305
|
|
309
306
|
Rails/RenderInline:
|
310
|
-
Enabled:
|
307
|
+
Enabled: true
|
311
308
|
|
312
309
|
Rails/RenderPlainText:
|
313
310
|
Enabled: false
|
@@ -399,16 +396,16 @@ Rails/WhereNot:
|
|
399
396
|
|
400
397
|
Style/For:
|
401
398
|
Exclude:
|
402
|
-
-
|
399
|
+
- "**/*.erb"
|
403
400
|
|
404
401
|
Style/OneLineConditional:
|
405
402
|
Exclude:
|
406
|
-
-
|
403
|
+
- "**/*.erb"
|
407
404
|
|
408
405
|
Style/Semicolon:
|
409
406
|
Exclude:
|
410
|
-
-
|
407
|
+
- "**/*.erb"
|
411
408
|
|
412
409
|
Style/StringLiterals:
|
413
410
|
Exclude:
|
414
|
-
-
|
411
|
+
- "**/*.erb"
|
data/config/rails_cops.yml
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
GitHub/RailsApplicationRecord:
|
2
|
-
Enabled: pending
|
3
|
-
|
4
1
|
GitHub/RailsControllerRenderActionSymbol:
|
5
2
|
Enabled: pending
|
6
3
|
Include:
|
@@ -8,7 +5,7 @@ GitHub/RailsControllerRenderActionSymbol:
|
|
8
5
|
|
9
6
|
GitHub/RailsControllerRenderLiteral:
|
10
7
|
Enabled: pending
|
11
|
-
StyleGuide: https://github.com/github/rubocop-github/blob/
|
8
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/guides/rails-render-literal.md
|
12
9
|
Include:
|
13
10
|
- 'app/controllers/**/*.rb'
|
14
11
|
|
@@ -21,25 +18,16 @@ GitHub/RailsControllerRenderPathsExist:
|
|
21
18
|
|
22
19
|
GitHub/RailsControllerRenderShorthand:
|
23
20
|
Enabled: pending
|
24
|
-
StyleGuide: https://github.com/github/rubocop-github/blob/
|
21
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/guides/rails-controller-render-shorthand.md
|
25
22
|
Include:
|
26
23
|
- 'app/controllers/**/*.rb'
|
27
24
|
|
28
|
-
GitHub/RailsRenderInline:
|
29
|
-
Enabled: pending
|
30
|
-
StyleGuide: https://github.com/github/rubocop-github/blob/master/guides/rails-controller-render-inline.md
|
31
|
-
Include:
|
32
|
-
- 'app/controllers/**/*.rb'
|
33
|
-
- 'app/helpers/**/*.rb'
|
34
|
-
- 'app/view_models/**/*.rb'
|
35
|
-
- 'app/views/**/*.erb'
|
36
|
-
|
37
25
|
GitHub/RailsRenderObjectCollection:
|
38
26
|
Enabled: pending
|
39
27
|
|
40
28
|
GitHub/RailsViewRenderLiteral:
|
41
29
|
Enabled: pending
|
42
|
-
StyleGuide: https://github.com/github/rubocop-github/blob/
|
30
|
+
StyleGuide: https://github.com/github/rubocop-github/blob/main/guides/rails-render-literal.md
|
43
31
|
Include:
|
44
32
|
- 'app/helpers/**/*.rb'
|
45
33
|
- 'app/view_models/**/*.rb'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Rails/ActionControllerFlashBeforeRender: # new in 2.16
|
2
|
+
Enabled: false
|
3
|
+
Rails/ActionOrder: # new in 2.17
|
4
|
+
Enabled: false
|
5
|
+
Rails/ActiveSupportOnLoad: # new in 2.16
|
6
|
+
Enabled: false
|
7
|
+
Rails/FreezeTime: # new in 2.16
|
8
|
+
Enabled: false
|
9
|
+
Rails/IgnoredColumnsAssignment: # new in 2.17
|
10
|
+
Enabled: false
|
11
|
+
Rails/RootPathnameMethods: # new in 2.16
|
12
|
+
Enabled: false
|
13
|
+
Rails/ToSWithArgument: # new in 2.16
|
14
|
+
Enabled: false
|
15
|
+
Rails/TopLevelHashWithIndifferentAccess: # new in 2.16
|
16
|
+
Enabled: false
|
17
|
+
Rails/WhereMissing: # new in 2.16
|
18
|
+
Enabled: false
|
19
|
+
Rails/WhereNotWithMultipleConditions: # new in 2.17
|
20
|
+
Enabled: false
|
@@ -6,6 +6,8 @@ module RuboCop
|
|
6
6
|
module Cop
|
7
7
|
module GitHub
|
8
8
|
class RailsViewRenderShorthand < Base
|
9
|
+
extend AutoCorrector
|
10
|
+
|
9
11
|
MSG = "Prefer `render` partial shorthand"
|
10
12
|
|
11
13
|
def_node_matcher :render_with_options?, <<-PATTERN
|
@@ -26,9 +28,13 @@ module RuboCop
|
|
26
28
|
locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first
|
27
29
|
|
28
30
|
if option_pairs.length == 1 && partial_key
|
29
|
-
add_offense(node, message: "Use `render #{partial_key.source}` instead")
|
31
|
+
add_offense(node, message: "Use `render #{partial_key.source}` instead") do |corrector|
|
32
|
+
corrector.replace(node.source_range, "render #{partial_key.source}")
|
33
|
+
end
|
30
34
|
elsif option_pairs.length == 2 && partial_key && locals_key
|
31
|
-
add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
|
35
|
+
add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead") do |corrector|
|
36
|
+
corrector.replace(node.source_range, "render #{partial_key.source}, #{locals_key.source}")
|
37
|
+
end
|
32
38
|
end
|
33
39
|
end
|
34
40
|
end
|
data/lib/rubocop-github-rails.rb
CHANGED
@@ -6,12 +6,10 @@ require "rubocop/github/inject"
|
|
6
6
|
|
7
7
|
RuboCop::GitHub::Inject.rails_defaults!
|
8
8
|
|
9
|
-
require "rubocop/cop/github/rails_application_record"
|
10
9
|
require "rubocop/cop/github/rails_controller_render_action_symbol"
|
11
10
|
require "rubocop/cop/github/rails_controller_render_literal"
|
12
11
|
require "rubocop/cop/github/rails_controller_render_paths_exist"
|
13
12
|
require "rubocop/cop/github/rails_controller_render_shorthand"
|
14
|
-
require "rubocop/cop/github/rails_render_inline"
|
15
13
|
require "rubocop/cop/github/rails_render_object_collection"
|
16
14
|
require "rubocop/cop/github/rails_view_render_literal"
|
17
15
|
require "rubocop/cop/github/rails_view_render_paths_exist"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-github
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.20.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -16,42 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.
|
19
|
+
version: '1.37'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.
|
26
|
+
version: '1.37'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rubocop-performance
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.15'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '1.15'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rubocop-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2.17'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2.17'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: actionview
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,21 +105,20 @@ files:
|
|
105
105
|
- STYLEGUIDE.md
|
106
106
|
- config/default.yml
|
107
107
|
- config/default_cops.yml
|
108
|
+
- config/default_pending.yml
|
108
109
|
- config/rails.yml
|
109
110
|
- config/rails_cops.yml
|
111
|
+
- config/rails_pending.yml
|
110
112
|
- guides/rails-controller-render-shorthand.md
|
111
|
-
- guides/rails-render-inline.md
|
112
113
|
- guides/rails-render-literal.md
|
113
114
|
- lib/rubocop-github-rails.rb
|
114
115
|
- lib/rubocop-github.rb
|
115
116
|
- lib/rubocop/cop/github.rb
|
116
117
|
- lib/rubocop/cop/github/insecure_hash_algorithm.rb
|
117
|
-
- lib/rubocop/cop/github/rails_application_record.rb
|
118
118
|
- lib/rubocop/cop/github/rails_controller_render_action_symbol.rb
|
119
119
|
- lib/rubocop/cop/github/rails_controller_render_literal.rb
|
120
120
|
- lib/rubocop/cop/github/rails_controller_render_paths_exist.rb
|
121
121
|
- lib/rubocop/cop/github/rails_controller_render_shorthand.rb
|
122
|
-
- lib/rubocop/cop/github/rails_render_inline.rb
|
123
122
|
- lib/rubocop/cop/github/rails_render_object_collection.rb
|
124
123
|
- lib/rubocop/cop/github/rails_view_render_literal.rb
|
125
124
|
- lib/rubocop/cop/github/rails_view_render_paths_exist.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# GitHub/RailsRenderInline
|
2
|
-
|
3
|
-
tldr; Do not use `render inline:`.
|
4
|
-
|
5
|
-
## Rendering plain text
|
6
|
-
|
7
|
-
``` ruby
|
8
|
-
render inline: "Just plain text" # bad
|
9
|
-
```
|
10
|
-
|
11
|
-
The `inline:` option is often misused when plain text is being returned. Instead use `render plain: "Just plain text"`.
|
12
|
-
|
13
|
-
## Rendering a dynamic ERB string
|
14
|
-
|
15
|
-
String `#{}` interpolation is often misused with `render inline:` instead of using `<%= %>` interpolation. This will lead to a memory leak where an ERB method will be compiled and cached for each invocation of `render inline:`.
|
16
|
-
|
17
|
-
``` ruby
|
18
|
-
render inline: "Hello #{@name}" # bad
|
19
|
-
```
|
20
|
-
|
21
|
-
## Rendering static ERB strings
|
22
|
-
|
23
|
-
``` ruby
|
24
|
-
render inline: "Hello <%= @name %>" # bad
|
25
|
-
```
|
26
|
-
|
27
|
-
If you are passing a static ERB string to `render inline:`, this string is best moved to a `.erb` template under `app/views`. Template files are able to be precompiled at boot time.
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rubocop"
|
4
|
-
|
5
|
-
module RuboCop
|
6
|
-
module Cop
|
7
|
-
module GitHub
|
8
|
-
class RailsApplicationRecord < Base
|
9
|
-
MSG = "Models should subclass from ApplicationRecord"
|
10
|
-
|
11
|
-
def_node_matcher :active_record_base_const?, <<-PATTERN
|
12
|
-
(const (const nil? :ActiveRecord) :Base)
|
13
|
-
PATTERN
|
14
|
-
|
15
|
-
def_node_matcher :application_record_const?, <<-PATTERN
|
16
|
-
(const nil? :ApplicationRecord)
|
17
|
-
PATTERN
|
18
|
-
|
19
|
-
def on_class(node)
|
20
|
-
klass, superclass, _ = *node
|
21
|
-
|
22
|
-
if active_record_base_const?(superclass) && !(application_record_const?(klass))
|
23
|
-
add_offense(superclass)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rubocop"
|
4
|
-
|
5
|
-
module RuboCop
|
6
|
-
module Cop
|
7
|
-
module GitHub
|
8
|
-
class RailsRenderInline < Base
|
9
|
-
MSG = "Avoid `render inline:`"
|
10
|
-
|
11
|
-
def_node_matcher :render_with_options?, <<-PATTERN
|
12
|
-
(send nil? {:render :render_to_string} (hash $...))
|
13
|
-
PATTERN
|
14
|
-
|
15
|
-
def_node_matcher :inline_key?, <<-PATTERN
|
16
|
-
(pair (sym :inline) $_)
|
17
|
-
PATTERN
|
18
|
-
|
19
|
-
def on_send(node)
|
20
|
-
if option_pairs = render_with_options?(node)
|
21
|
-
if option_pairs.detect { |pair| inline_key?(pair) }
|
22
|
-
add_offense(node)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|