rubocop 0.62.0 → 0.63.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +38 -12
  5. data/lib/rubocop.rb +5 -1
  6. data/lib/rubocop/ast/builder.rb +2 -0
  7. data/lib/rubocop/ast/node/if_node.rb +29 -0
  8. data/lib/rubocop/ast/node/range_node.rb +11 -0
  9. data/lib/rubocop/ast/traversal.rb +3 -1
  10. data/lib/rubocop/config.rb +2 -0
  11. data/lib/rubocop/cop/cop.rb +1 -1
  12. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -1
  13. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +80 -0
  14. data/lib/rubocop/cop/{style → lint}/flip_flop.rb +4 -3
  15. data/lib/rubocop/cop/lint/shadowed_exception.rb +22 -16
  16. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +11 -4
  17. data/lib/rubocop/cop/mixin/nil_methods.rb +9 -3
  18. data/lib/rubocop/cop/mixin/trailing_comma.rb +41 -6
  19. data/lib/rubocop/cop/naming/constant_name.rb +10 -1
  20. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  21. data/lib/rubocop/cop/performance/range_include.rb +3 -0
  22. data/lib/rubocop/cop/rails/belongs_to.rb +120 -0
  23. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +94 -0
  24. data/lib/rubocop/cop/rails/inverse_of.rb +4 -6
  25. data/lib/rubocop/cop/rails/link_to_blank.rb +37 -0
  26. data/lib/rubocop/cop/style/documentation_method.rb +6 -1
  27. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -2
  28. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -2
  29. data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
  30. data/lib/rubocop/cop/style/safe_navigation.rb +10 -1
  31. data/lib/rubocop/cop/style/ternary_parentheses.rb +4 -6
  32. data/lib/rubocop/cop/style/yoda_condition.rb +58 -21
  33. data/lib/rubocop/options.rb +10 -5
  34. data/lib/rubocop/target_finder.rb +8 -30
  35. data/lib/rubocop/version.rb +1 -1
  36. metadata +7 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd76265ab6d79dd4e54127c4969d0af755732cfc
4
- data.tar.gz: 12ff96a7380e7a32ea291237754f66d0450605f7
3
+ metadata.gz: 7aaecd68a4876e64c43f009e849b228a26805bfe
4
+ data.tar.gz: 68a8b80a3f44980737704d596a595548bc6cb086
5
5
  SHA512:
6
- metadata.gz: 91eb27b2b5c06d550cf06d83782b53e0ff05245ec079a01ec9158e06fc00ab61324c5d6b24db0bbdc68992bdce4d051375f9e9f5224e9401c3b2f20fa6023c1f
7
- data.tar.gz: c7a1c9f7f0e3d50f95c9b7e0a488cfdb31842b93e2f427ee14921429d4f64cae9ab9d0659b9775c60e5a19341615f29a4c5949451c7a4b563d7b9ca0d8994212
6
+ metadata.gz: e2f5aacc075c83cc70ff025a59f4ed5201de381be89da00c93ed23fceb2a43d09960a653407ee6a040a68e9f6f199bed50342fdb413c84d3b22ee8ff0f840f08
7
+ data.tar.gz: c9883abe7b2d4221e7af6a36f3d9aeeeca51de1a36d558c2a55a6a5b33cad9948a53bd9eeda38e4565f56b1aed85b7e98c4d2a8e2d939cb53984ad9eaec63088
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-18 Bozhidar Batsov
1
+ Copyright (c) 2012-19 Bozhidar Batsov
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -53,7 +53,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
53
53
  might want to use a conservative version locking in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 0.62.0', require: false
56
+ gem 'rubocop', '~> 0.63.0', require: false
57
57
  ```
58
58
 
59
59
  ## Quickstart
@@ -217,5 +217,5 @@ RuboCop's changelog is available [here](CHANGELOG.md).
217
217
 
218
218
  ## Copyright
219
219
 
220
- Copyright (c) 2012-2018 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
220
+ Copyright (c) 2012-2019 Bozhidar Batsov. See [LICENSE.txt](LICENSE.txt) for
221
221
  further details.
data/config/default.yml CHANGED
@@ -1191,6 +1191,12 @@ Lint/DeprecatedClassMethods:
1191
1191
  Enabled: true
1192
1192
  VersionAdded: '0.19'
1193
1193
 
1194
+ Lint/DisjunctiveAssignmentInConstructor:
1195
+ Description: 'In constructor, plain assignment is preferred over disjunctive.'
1196
+ Enabled: true
1197
+ Safe: false
1198
+ VersionAdded: '0.62'
1199
+
1194
1200
  Lint/DuplicateCaseCondition:
1195
1201
  Description: 'Do not repeat values in case conditionals.'
1196
1202
  Enabled: true
@@ -1255,6 +1261,12 @@ Lint/ErbNewArguments:
1255
1261
  Enabled: true
1256
1262
  VersionAdded: '0.56'
1257
1263
 
1264
+ Lint/FlipFlop:
1265
+ Description: 'Checks for flip-flops'
1266
+ StyleGuide: '#no-flip-flops'
1267
+ Enabled: true
1268
+ VersionAdded: '0.16'
1269
+
1258
1270
  Lint/FloatOutOfRange:
1259
1271
  Description: >-
1260
1272
  Catches floating-point literals too large or small for Ruby to
@@ -2074,6 +2086,7 @@ Performance/RangeInclude:
2074
2086
  Reference: 'https://github.com/JuanitoFatas/fast-ruby#cover-vs-include-code'
2075
2087
  Enabled: true
2076
2088
  VersionAdded: '0.36'
2089
+ Safe: false
2077
2090
 
2078
2091
  Performance/RedundantBlockCall:
2079
2092
  Description: 'Use `yield` instead of `block.call`.'
@@ -2228,6 +2241,13 @@ Rails/AssertNot:
2228
2241
  Include:
2229
2242
  - '**/test/**/*'
2230
2243
 
2244
+ Rails/BelongsTo:
2245
+ Description: >-
2246
+ Use `optional: true` instead of `required: false` for
2247
+ `belongs_to` relations'
2248
+ Enabled: true
2249
+ VersionAdded: '0.62'
2250
+
2231
2251
  Rails/Blank:
2232
2252
  Description: 'Enforces use of `blank?`.'
2233
2253
  Enabled: true
@@ -2384,6 +2404,14 @@ Rails/HttpStatus:
2384
2404
  - numeric
2385
2405
  - symbolic
2386
2406
 
2407
+ Rails/IgnoredSkipActionFilterOption:
2408
+ Description: 'Checks that `if` and `only` (or `except`) are not used together as options of `skip_*` action filter.'
2409
+ Reference: 'https://api.rubyonrails.org/classes/AbstractController/Callbacks/ClassMethods.html#method-i-_normalize_callback_options'
2410
+ Enabled: true
2411
+ VersionAdded: '0.63'
2412
+ Include:
2413
+ - app/controllers/**/*.rb
2414
+
2387
2415
  Rails/InverseOf:
2388
2416
  Description: 'Checks for associations where the inverse cannot be determined automatically.'
2389
2417
  Enabled: true
@@ -3128,12 +3156,6 @@ Style/ExpandPathArguments:
3128
3156
  Enabled: true
3129
3157
  VersionAdded: '0.53'
3130
3158
 
3131
- Style/FlipFlop:
3132
- Description: 'Checks for flip flops'
3133
- StyleGuide: '#no-flip-flops'
3134
- Enabled: true
3135
- VersionAdded: '0.16'
3136
-
3137
3159
  Style/For:
3138
3160
  Description: 'Checks use of for or each in multiline loops.'
3139
3161
  StyleGuide: '#no-for-loops'
@@ -3174,7 +3196,7 @@ Style/FormatStringToken:
3174
3196
  Style/FrozenStringLiteralComment:
3175
3197
  Description: >-
3176
3198
  Add the frozen_string_literal comment to the top of files
3177
- to help transition from Ruby 2.3.0 to Ruby 3.0.
3199
+ to help transition to frozen string literals by default.
3178
3200
  Enabled: true
3179
3201
  VersionAdded: '0.36'
3180
3202
  VersionChanged: '0.47'
@@ -4292,17 +4314,21 @@ Style/WordArray:
4292
4314
  WordRegex: !ruby/regexp '/\A[\p{Word}\n\t]+\z/'
4293
4315
 
4294
4316
  Style/YodaCondition:
4295
- Description: 'Do not use literals as the first operand of a comparison.'
4317
+ Description: 'Forbid or enforce yoda conditions.'
4296
4318
  Reference: 'https://en.wikipedia.org/wiki/Yoda_conditions'
4297
4319
  Enabled: true
4298
- EnforcedStyle: all_comparison_operators
4320
+ EnforcedStyle: forbid_for_all_comparison_operators
4299
4321
  SupportedStyles:
4300
4322
  # check all comparison operators
4301
- - all_comparison_operators
4323
+ - forbid_for_all_comparison_operators
4302
4324
  # check only equality operators: `!=` and `==`
4303
- - equality_operators_only
4325
+ - forbid_for_equality_operators_only
4326
+ # enforce yoda for all comparison operators
4327
+ - require_for_all_comparison_operators
4328
+ # enforce yoda only for equality operators: `!=` and `==`
4329
+ - require_for_equality_operators_only
4304
4330
  VersionAdded: '0.49'
4305
- VersionChanged: '0.50'
4331
+ VersionChanged: '0.63'
4306
4332
 
4307
4333
  Style/ZeroLengthPredicate:
4308
4334
  Description: 'Use #empty? when testing for objects of length 0.'
data/lib/rubocop.rb CHANGED
@@ -48,6 +48,7 @@ require_relative 'rubocop/ast/node/if_node'
48
48
  require_relative 'rubocop/ast/node/keyword_splat_node'
49
49
  require_relative 'rubocop/ast/node/or_node'
50
50
  require_relative 'rubocop/ast/node/pair_node'
51
+ require_relative 'rubocop/ast/node/range_node'
51
52
  require_relative 'rubocop/ast/node/regexp_node'
52
53
  require_relative 'rubocop/ast/node/resbody_node'
53
54
  require_relative 'rubocop/ast/node/send_node'
@@ -265,6 +266,7 @@ require_relative 'rubocop/cop/lint/boolean_symbol'
265
266
  require_relative 'rubocop/cop/lint/circular_argument_reference'
266
267
  require_relative 'rubocop/cop/lint/debugger'
267
268
  require_relative 'rubocop/cop/lint/deprecated_class_methods'
269
+ require_relative 'rubocop/cop/lint/disjunctive_assignment_in_constructor'
268
270
  require_relative 'rubocop/cop/lint/duplicate_case_condition'
269
271
  require_relative 'rubocop/cop/lint/duplicate_methods'
270
272
  require_relative 'rubocop/cop/lint/duplicated_key'
@@ -277,6 +279,7 @@ require_relative 'rubocop/cop/lint/empty_when'
277
279
  require_relative 'rubocop/cop/lint/end_in_method'
278
280
  require_relative 'rubocop/cop/lint/ensure_return'
279
281
  require_relative 'rubocop/cop/lint/erb_new_arguments'
282
+ require_relative 'rubocop/cop/lint/flip_flop'
280
283
  require_relative 'rubocop/cop/lint/float_out_of_range'
281
284
  require_relative 'rubocop/cop/lint/format_parameter_mismatch'
282
285
  require_relative 'rubocop/cop/lint/handle_exceptions'
@@ -436,7 +439,6 @@ require_relative 'rubocop/cop/style/end_block'
436
439
  require_relative 'rubocop/cop/style/eval_with_location'
437
440
  require_relative 'rubocop/cop/style/even_odd'
438
441
  require_relative 'rubocop/cop/style/expand_path_arguments'
439
- require_relative 'rubocop/cop/style/flip_flop'
440
442
  require_relative 'rubocop/cop/style/for'
441
443
  require_relative 'rubocop/cop/style/format_string'
442
444
  require_relative 'rubocop/cop/style/format_string_token'
@@ -560,6 +562,7 @@ require_relative 'rubocop/cop/rails/active_support_aliases'
560
562
  require_relative 'rubocop/cop/rails/application_job'
561
563
  require_relative 'rubocop/cop/rails/application_record'
562
564
  require_relative 'rubocop/cop/rails/assert_not'
565
+ require_relative 'rubocop/cop/rails/belongs_to'
563
566
  require_relative 'rubocop/cop/rails/blank'
564
567
  require_relative 'rubocop/cop/rails/bulk_change_table'
565
568
  require_relative 'rubocop/cop/rails/create_table_with_timestamps'
@@ -577,6 +580,7 @@ require_relative 'rubocop/cop/rails/has_and_belongs_to_many'
577
580
  require_relative 'rubocop/cop/rails/has_many_or_has_one_dependent'
578
581
  require_relative 'rubocop/cop/rails/http_positional_arguments'
579
582
  require_relative 'rubocop/cop/rails/http_status'
583
+ require_relative 'rubocop/cop/rails/ignored_skip_action_filter_option'
580
584
  require_relative 'rubocop/cop/rails/inverse_of'
581
585
  require_relative 'rubocop/cop/rails/lexically_scoped_action_filter'
582
586
  require_relative 'rubocop/cop/rails/link_to_blank'
@@ -27,6 +27,8 @@ module RuboCop
27
27
  for: ForNode,
28
28
  hash: HashNode,
29
29
  if: IfNode,
30
+ irange: RangeNode,
31
+ erange: RangeNode,
30
32
  kwsplat: KeywordSplatNode,
31
33
  or: OrNode,
32
34
  pair: PairNode,
@@ -141,6 +141,35 @@ module RuboCop
141
141
 
142
142
  [condition, true_branch, false_branch]
143
143
  end
144
+
145
+ # Returns an array of all the branches in the conditional statement.
146
+ #
147
+ # @return [Array<Node>] an array of branch nodes
148
+ def branches
149
+ branches = [if_branch]
150
+
151
+ return branches unless else_branch
152
+
153
+ other_branches = if elsif_conditional?
154
+ else_branch.branches
155
+ else
156
+ [else_branch]
157
+ end
158
+ branches.concat(other_branches)
159
+ end
160
+
161
+ # Calls the given block for each branch node in the conditional statement.
162
+ # If no block is given, an `Enumerator` is returned.
163
+ #
164
+ # @return [self] if a block is given
165
+ # @return [Enumerator] if no block is given
166
+ def each_branch
167
+ return branches.to_enum(__method__) unless block_given?
168
+
169
+ branches.each do |branch|
170
+ yield branch
171
+ end
172
+ end
144
173
  end
145
174
  end
146
175
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module AST
5
+ # A node extension for `irange` and `erange` nodes. This will be used in
6
+ # place of a plain node when the builder constructs the AST, making its
7
+ # methods available to all `irange` and `erange` nodes within RuboCop.
8
+ class RangeNode < Node
9
+ end
10
+ end
11
+ end
@@ -23,7 +23,7 @@ module RuboCop
23
23
  preexe postexe match_current_line defined?
24
24
  arg_expr].freeze
25
25
  MANY_CHILD_NODES = %i[dstr dsym xstr regexp array hash pair
26
- irange erange mlhs masgn or_asgn and_asgn
26
+ mlhs masgn or_asgn and_asgn
27
27
  undef alias args super yield or and
28
28
  while_post until_post iflipflop eflipflop
29
29
  match_with_lvasgn begin kwbegin return].freeze
@@ -176,6 +176,8 @@ module RuboCop
176
176
  alias on_ensure on_case
177
177
  alias on_for on_case
178
178
  alias on_when on_case
179
+ alias on_irange on_case
180
+ alias on_erange on_case
179
181
  end
180
182
  end
181
183
  end
@@ -26,6 +26,8 @@ module RuboCop
26
26
  RUBY_VERSION_FILENAME = '.ruby-version'.freeze
27
27
  DEFAULT_RAILS_VERSION = 5.0
28
28
  OBSOLETE_COPS = {
29
+ 'Style/FlipFlop' =>
30
+ 'The `Style/FlipFlop` cop has been moved to `Lint/FlipFlop`.',
29
31
  'Style/TrailingComma' =>
30
32
  'The `Style/TrailingComma` cop no longer exists. Please use ' \
31
33
  '`Style/TrailingCommaInArguments`, ' \
@@ -218,7 +218,7 @@ module RuboCop
218
218
  end
219
219
 
220
220
  def enabled_line?(line_number)
221
- return true unless @processed_source
221
+ return true if @options[:ignore_disable_comments] || !@processed_source
222
222
 
223
223
  @processed_source.comment_config.cop_enabled_at_line?(self, line_number)
224
224
  end
@@ -33,10 +33,11 @@ module RuboCop
33
33
  #
34
34
  # # bad
35
35
  # h = { a: { b: 2 } }
36
+ # foo = { { a: 1 } => { b: { c: 2 } } }
36
37
  #
37
38
  # # good
38
39
  # h = { a: { b: 2 }}
39
- #
40
+ # foo = {{ a: 1 } => { b: { c: 2 }}}
40
41
  #
41
42
  # @example EnforcedStyleForEmptyBraces: no_space (default)
42
43
  # # The `no_space` EnforcedStyleForEmptyBraces style enforces that
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # This cop checks constructors for disjunctive assignments that should
7
+ # be plain assignments.
8
+ #
9
+ # So far, this cop is only concerned with disjunctive assignment of
10
+ # instance variables.
11
+ #
12
+ # In ruby, an instance variable is nil until a value is assigned, so the
13
+ # disjunction is unnecessary. A plain assignment has the same effect.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # def initialize
18
+ # @x ||= 1
19
+ # end
20
+ #
21
+ # # good
22
+ # def initialize
23
+ # @x = 1
24
+ # end
25
+ class DisjunctiveAssignmentInConstructor < Cop
26
+ MSG = 'Unnecessary disjunctive assignment. Use plain assignment.'.freeze
27
+
28
+ def on_def(node)
29
+ check(node)
30
+ end
31
+
32
+ private
33
+
34
+ def check(node)
35
+ return unless node.method_name == :initialize
36
+
37
+ check_body(node)
38
+ end
39
+
40
+ # @param [DefNode] node a constructor definition
41
+ def check_body(node)
42
+ body = node.body
43
+ case body.type
44
+ when :begin
45
+ check_body_lines(body.child_nodes)
46
+ else
47
+ check_body_lines([body])
48
+ end
49
+ end
50
+
51
+ # @param [Array] lines the logical lines of the constructor
52
+ def check_body_lines(lines)
53
+ lines.each do |line|
54
+ case line.type
55
+ when :or_asgn
56
+ check_disjunctive_assignment(line)
57
+ else
58
+ # Once we encounter something other than a disjunctive
59
+ # assignment, we cease our investigation, because we can't be
60
+ # certain that any future disjunctive assignments are offensive.
61
+ # You're off the case, detective!
62
+ break
63
+ end
64
+ end
65
+ end
66
+
67
+ # Add an offense if the LHS of the given disjunctive assignment is
68
+ # an instance variable.
69
+ #
70
+ # For now, we only care about assignments to instance variables.
71
+ #
72
+ # @param [Node] node a disjunctive assignment
73
+ def check_disjunctive_assignment(node)
74
+ lhs = node.child_nodes.first
75
+ add_offense(node, location: :operator) if lhs.ivasgn_type?
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -2,8 +2,9 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- module Style
6
- # This cop looks for uses of flip flop operator
5
+ module Lint
6
+ # This cop looks for uses of flip-flop operator.
7
+ # flip-flop operator is deprecated since Ruby 2.6.0.
7
8
  #
8
9
  # @example
9
10
  # # bad
@@ -16,7 +17,7 @@ module RuboCop
16
17
  # puts x if (x >= 5) && (x <= 10)
17
18
  # end
18
19
  class FlipFlop < Cop
19
- MSG = 'Avoid the use of flip flop operators.'.freeze
20
+ MSG = 'Avoid the use of flip-flop operators.'.freeze
20
21
 
21
22
  def on_iflipflop(node)
22
23
  add_offense(node)
@@ -53,11 +53,7 @@ module RuboCop
53
53
  return if rescue_modifier?(node)
54
54
 
55
55
  _body, *rescues, _else = *node
56
- rescued_groups = rescues.each_with_object([]) do |group, exceptions|
57
- rescue_group, = *group
58
-
59
- exceptions << evaluate_exceptions(rescue_group)
60
- end
56
+ rescued_groups = rescued_groups_for(rescues)
61
57
 
62
58
  rescue_group_rescues_multiple_levels = rescued_groups.any? do |group|
63
59
  contains_multiple_levels_of_exceptions?(group)
@@ -72,17 +68,16 @@ module RuboCop
72
68
  private
73
69
 
74
70
  def offense_range(rescues)
75
- first_rescue = rescues.first
76
- last_rescue = rescues.last
77
- last_exceptions, = *last_rescue
78
- # last_rescue clause may not specify exception class
79
- end_pos = if last_exceptions
80
- last_exceptions.loc.expression.end_pos
81
- else
82
- last_rescue.loc.keyword.end_pos
83
- end
84
-
85
- range_between(first_rescue.loc.expression.begin_pos, end_pos)
71
+ shadowing_rescue = find_shadowing_rescue(rescues)
72
+ expression = shadowing_rescue.loc.expression
73
+ range_between(expression.begin_pos, expression.end_pos)
74
+ end
75
+
76
+ def rescued_groups_for(rescues)
77
+ rescues.map do |group|
78
+ rescue_group, = *group
79
+ evaluate_exceptions(rescue_group)
80
+ end
86
81
  end
87
82
 
88
83
  def contains_multiple_levels_of_exceptions?(group)
@@ -153,6 +148,17 @@ module RuboCop
153
148
  klass.source
154
149
  end.compact
155
150
  end
151
+
152
+ def find_shadowing_rescue(rescues)
153
+ rescued_groups = rescued_groups_for(rescues)
154
+ rescued_groups.zip(rescues).each do |group, res|
155
+ return res if contains_multiple_levels_of_exceptions?(group)
156
+ end
157
+
158
+ rescued_groups.each_cons(2).with_index do |group_pair, i|
159
+ return rescues[i] unless sorted?(group_pair)
160
+ end
161
+ end
156
162
  end
157
163
  end
158
164
  end