rubocop-rails 2.35.5 → 2.36.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5620166bbbdcac74d58be7dec4ea307fc841d7cd31ffd9684f422009d00b8754
4
- data.tar.gz: 7c156916d1a363419097086b305baf6010361473443602ea477ff03894c5e350
3
+ metadata.gz: 5fcf30164d8e9727b16702ab937a902890c342d02c1865e4588295a355380f7a
4
+ data.tar.gz: 8c88a959e8b7b07e6751c6a385302deabc39f8bf328278b979d62c46becaccc7
5
5
  SHA512:
6
- metadata.gz: fccc029e306ebaf72e1809005110005d18416b9d314c347b24b2381bdd84fa70c9e2d175633501618881c883e205ae0e79ed9aef3dcee4d4b16f6b02fce51818
7
- data.tar.gz: 704f46a2a6c8b53eeeb8ac8ac60bfb25c39aa096e3b99516b132405f604729f48a877066b0531697e6ab8f1f65a9638435013c6610c5be5767e0b955d7d17300
6
+ metadata.gz: bc070f9148d8b3b8d87c06cf95ae2b604eb81758548cfab082a38300760ac714ac0d1741e19c8c7d604563868be6b03b4c1b1bdc87af6d7999a4386aa37701d4
7
+ data.tar.gz: 14e8a2fa9886a93625e49678688e7b00e2075bfcfa00a04adc23c3de5d3b8e9fd3c5be88b19e532d4cd30b1dc746320b273f0a2c7f7b984625e1a6874f16d649
@@ -42,7 +42,7 @@ module RuboCop
42
42
  end
43
43
 
44
44
  def on_send(node)
45
- return if node.parent&.block_type?
45
+ return if node.block_literal?
46
46
 
47
47
  interpolated_string_passed_to_debug(node) do |arguments|
48
48
  range = replacement_range(node)
@@ -294,6 +294,8 @@ module RuboCop
294
294
  false
295
295
  when :remove
296
296
  target_rails_version >= 6.1 && all_hash_key?(node.last_argument, :type)
297
+ when :remove_index
298
+ reversible_remove_index?(node)
297
299
  when :change_default, :change_column_default, :change_table_comment,
298
300
  :change_column_comment
299
301
  all_hash_key?(node.last_argument, :from, :to)
@@ -302,6 +304,12 @@ module RuboCop
302
304
  end
303
305
  end
304
306
 
307
+ def reversible_remove_index?(node)
308
+ return true if node.arguments.any? { |arg| !arg.hash_type? }
309
+
310
+ all_hash_key?(node.last_argument, :column)
311
+ end
312
+
305
313
  def within_change_method?(node)
306
314
  node.each_ancestor(:def).any? do |ancestor|
307
315
  ancestor.method?(:change)
@@ -12,8 +12,10 @@ module RuboCop
12
12
  # foo.try!(:bar)
13
13
  # foo.try!(:bar, baz)
14
14
  # foo.try!(:bar) { |e| e.baz }
15
+ # foo.try!(&:bar)
15
16
  #
16
17
  # foo.try!(:[], 0)
18
+ # foo.try!(:==, baz)
17
19
  #
18
20
  # # good
19
21
  # foo.try(:bar)
@@ -23,20 +25,28 @@ module RuboCop
23
25
  # foo&.bar
24
26
  # foo&.bar(baz)
25
27
  # foo&.bar { |e| e.baz }
28
+ # foo&.[](0)
29
+ # foo&.==(baz)
26
30
  #
27
31
  # @example ConvertTry: true
28
32
  # # bad
29
33
  # foo.try!(:bar)
30
34
  # foo.try!(:bar, baz)
31
35
  # foo.try!(:bar) { |e| e.baz }
36
+ # foo.try!(&:bar)
32
37
  # foo.try(:bar)
33
38
  # foo.try(:bar, baz)
34
39
  # foo.try(:bar) { |e| e.baz }
40
+ # foo.try(&:bar)
41
+ # foo.try(:[], 0)
42
+ # foo.try(:==, baz)
35
43
  #
36
44
  # # good
37
45
  # foo&.bar
38
46
  # foo&.bar(baz)
39
47
  # foo&.bar { |e| e.baz }
48
+ # foo&.[](0)
49
+ # foo&.==(baz)
40
50
  class SafeNavigation < Base
41
51
  include RangeHelp
42
52
  extend AutoCorrector
@@ -51,6 +61,11 @@ module RuboCop
51
61
  (send _ ${:try :try!} $_ ...)
52
62
  PATTERN
53
63
 
64
+ # Extracts the method name from a symbol-to-proc block argument, e.g. `:foo` from `try(&:foo)`.
65
+ def_node_matcher :symbol_proc_method, <<~PATTERN
66
+ (block_pass (sym $_))
67
+ PATTERN
68
+
54
69
  def self.autocorrect_incompatible_with
55
70
  [Style::RedundantSelf]
56
71
  end
@@ -58,11 +73,16 @@ module RuboCop
58
73
  def on_send(node)
59
74
  try_call(node) do |try_method, dispatch|
60
75
  return if try_method == :try && !cop_config['ConvertTry']
61
- return unless dispatch.sym_type? && dispatch.value.match?(/\w+[=!?]?/)
76
+ return unless dispatch.sym_type? || symbol_proc_method(dispatch)
77
+ # When a `try` is nested in another `try`'s argument, correcting both at once
78
+ # produces overlapping replacements. Correct the outer one first and defer the
79
+ # inner one to a subsequent pass.
80
+ return if part_of_ignored_node?(node)
62
81
 
63
82
  add_offense(node, message: format(MSG, try: try_method)) do |corrector|
64
83
  autocorrect(corrector, node)
65
84
  end
85
+ ignore_node(node)
66
86
  end
67
87
  end
68
88
 
@@ -70,7 +90,6 @@ module RuboCop
70
90
 
71
91
  def autocorrect(corrector, node)
72
92
  method_node, *params = *node.arguments
73
- method = method_node.source[1..]
74
93
 
75
94
  range = if node.receiver
76
95
  range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
@@ -79,13 +98,16 @@ module RuboCop
79
98
  node
80
99
  end
81
100
 
82
- corrector.replace(range, replacement(method, params))
101
+ corrector.replace(range, replacement(method_node, params))
83
102
  end
84
103
 
85
- def replacement(method, params)
104
+ def replacement(method_node, params)
105
+ return "&.#{symbol_proc_method(method_node)}" if method_node.block_pass_type?
106
+
107
+ method = method_node.source[1..]
86
108
  new_params = params.map(&:source).join(', ')
87
109
 
88
- if method.end_with?('=')
110
+ if setter_method?(method)
89
111
  "&.#{method[0...-1]} = #{new_params}"
90
112
  elsif params.empty?
91
113
  "&.#{method}"
@@ -93,6 +115,12 @@ module RuboCop
93
115
  "&.#{method}(#{new_params})"
94
116
  end
95
117
  end
118
+
119
+ # Operator methods such as `==` and `[]=` also end with `=`, but they are not setters
120
+ # and must keep the explicit call form (e.g. `&.==(bar)`, `&.[]=(key, value)`).
121
+ def setter_method?(method)
122
+ method.match?(/\A\w+=\z/)
123
+ end
96
124
  end
97
125
  end
98
126
  end
@@ -29,10 +29,6 @@ module RuboCop
29
29
  super if method(__method__).super_method
30
30
  end
31
31
 
32
- def self.apply_to_cops!
33
- RuboCop::Cop::Registry.all.each { |cop| cop.prepend(MigrationFileSkippable) }
34
- end
35
-
36
32
  private
37
33
 
38
34
  def already_migrated_file?
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.35.5'
7
+ STRING = '2.36.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
data/lib/rubocop-rails.rb CHANGED
@@ -12,7 +12,8 @@ require_relative 'rubocop/rails/plugin'
12
12
  require_relative 'rubocop/cop/rails_cops'
13
13
 
14
14
  require_relative 'rubocop/rails/migration_file_skippable'
15
- RuboCop::Rails::MigrationFileSkippable.apply_to_cops!
15
+
16
+ RuboCop::Cop::Base.prepend(RuboCop::Rails::MigrationFileSkippable)
16
17
 
17
18
  RuboCop::Cop::Style::HashExcept.minimum_target_ruby_version(2.0)
18
19
 
@@ -39,3 +40,11 @@ RuboCop::Cop::Style::RedundantSelf.singleton_class.prepend(
39
40
  end
40
41
  end
41
42
  )
43
+
44
+ RuboCop::Cop::Style::TrailingCommaInArguments.singleton_class.prepend(
45
+ Module.new do
46
+ def autocorrect_incompatible_with
47
+ super.push(RuboCop::Cop::Rails::LinkToBlank)
48
+ end
49
+ end
50
+ )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.35.5
4
+ version: 2.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -269,7 +269,7 @@ metadata:
269
269
  homepage_uri: https://docs.rubocop.org/rubocop-rails/
270
270
  changelog_uri: https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md
271
271
  source_code_uri: https://github.com/rubocop/rubocop-rails/
272
- documentation_uri: https://docs.rubocop.org/rubocop-rails/2.35/
272
+ documentation_uri: https://docs.rubocop.org/rubocop-rails/2.36/
273
273
  bug_tracker_uri: https://github.com/rubocop/rubocop-rails/issues
274
274
  rubygems_mfa_required: 'true'
275
275
  default_lint_roller_plugin: RuboCop::Rails::Plugin