rubocop 1.57.1 → 1.58.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +37 -2
  4. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  5. data/lib/rubocop/config_obsoletion.rb +11 -8
  6. data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
  7. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
  8. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
  9. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
  10. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
  11. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  12. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  13. data/lib/rubocop/cop/layout/end_alignment.rb +7 -1
  14. data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
  15. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +6 -6
  16. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  17. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  18. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
  19. data/lib/rubocop/cop/layout/single_line_block_chain.rb +4 -0
  20. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -20
  21. data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
  22. data/lib/rubocop/cop/lint/debugger.rb +2 -1
  23. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  24. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -3
  25. data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
  26. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +64 -0
  27. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
  28. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +43 -0
  29. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
  30. data/lib/rubocop/cop/lint/self_assignment.rb +37 -0
  31. data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
  32. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  33. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  34. data/lib/rubocop/cop/lint/void.rb +43 -12
  35. data/lib/rubocop/cop/metrics/class_length.rb +6 -1
  36. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  37. data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
  38. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  39. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  40. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  41. data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
  42. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  43. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
  44. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  45. data/lib/rubocop/cop/style/arguments_forwarding.rb +68 -6
  46. data/lib/rubocop/cop/style/array_first_last.rb +64 -0
  47. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
  48. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
  49. data/lib/rubocop/cop/style/case_like_if.rb +4 -4
  50. data/lib/rubocop/cop/style/combinable_loops.rb +2 -7
  51. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  52. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  53. data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
  54. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  55. data/lib/rubocop/cop/style/hash_each_methods.rb +58 -10
  56. data/lib/rubocop/cop/style/identical_conditional_branches.rb +9 -1
  57. data/lib/rubocop/cop/style/inverse_methods.rb +6 -5
  58. data/lib/rubocop/cop/style/map_to_hash.rb +9 -4
  59. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +1 -1
  60. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  61. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
  62. data/lib/rubocop/cop/style/redundant_argument.rb +2 -2
  63. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +17 -10
  64. data/lib/rubocop/cop/style/redundant_filter_chain.rb +4 -3
  65. data/lib/rubocop/cop/style/redundant_line_continuation.rb +2 -0
  66. data/lib/rubocop/cop/style/redundant_parentheses.rb +28 -12
  67. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  68. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  69. data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
  70. data/lib/rubocop/cop/style/select_by_regexp.rb +1 -1
  71. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  72. data/lib/rubocop/cop/style/semicolon.rb +8 -0
  73. data/lib/rubocop/cop/style/single_argument_dig.rb +2 -1
  74. data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
  75. data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
  76. data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
  77. data/lib/rubocop/formatter/html_formatter.rb +5 -4
  78. data/lib/rubocop/result_cache.rb +0 -1
  79. data/lib/rubocop/runner.rb +1 -1
  80. data/lib/rubocop/version.rb +1 -1
  81. data/lib/rubocop.rb +3 -0
  82. metadata +13 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d38c0bf9376bd86de4bdf5561b74b11e895bc90ac263a8c29520afec2fb7d0b0
4
- data.tar.gz: 5e624dd813953b2058306d07986c19d55fa170fa8f3960844c4d62f2f87b3277
3
+ metadata.gz: b262d82d17790d0993cf662a3ede317cbddfbc38704ae0bbf3108d411fac02d1
4
+ data.tar.gz: e9e7fd1969decd87a3b5e4760331f98bb5b12a48b44b91d9f5a71cb5b79edb89
5
5
  SHA512:
6
- metadata.gz: dee188b376a118be4c08956361312fc91bf4e55cdadfa8129611f8a3b19fef80fb8241658778a5f6fb042336ba4137c6c64d2a65189ad00567730934e3dcde74
7
- data.tar.gz: 4dbf33a643a4f945d3564426b49338d463a55e734812d8c3113df04aa430577c2142b7685990bdd12915e537a1add4b181dd96488632dd89a544a33c5f712944
6
+ metadata.gz: 9e6a605d355ff38cdd29726c5e81aeb884757f3fb431beff0dd64f83b05e015f56ec429eccca953142fd85745f3bdbc33b838f1541f7253d422c30ab38b5041d
7
+ data.tar.gz: 2fb1647376fcb6c193fecd74478a3d8913b8efa2ede1bac4c7895d0c42ee94f0f5ec52628e85c3461f930cb9795d18240116f2972d406df136cf4a2fc73f1454
data/README.md CHANGED
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.57', require: false
56
+ gem 'rubocop', '~> 1.58', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
@@ -67,7 +67,7 @@ $ cd my/cool/ruby/project
67
67
  $ rubocop
68
68
  ```
69
69
 
70
- You can also use this magic in your favorite editor with RuboCop's [built-in LSP](https://docs.rubocop.org/rubocop/usage/lsp.html).
70
+ You can also use this magic in your favorite editor with RuboCop's [built-in LSP server](https://docs.rubocop.org/rubocop/usage/lsp.html).
71
71
 
72
72
  ## Documentation
73
73
 
data/config/default.yml CHANGED
@@ -1351,6 +1351,10 @@ Layout/SpaceAroundOperators:
1351
1351
  SupportedStylesForExponentOperator:
1352
1352
  - space
1353
1353
  - no_space
1354
+ EnforcedStyleForRationalLiterals: no_space
1355
+ SupportedStylesForRationalLiterals:
1356
+ - space
1357
+ - no_space
1354
1358
 
1355
1359
  Layout/SpaceBeforeBlockBraces:
1356
1360
  Description: >-
@@ -1969,6 +1973,11 @@ Lint/LiteralAsCondition:
1969
1973
  Enabled: true
1970
1974
  VersionAdded: '0.51'
1971
1975
 
1976
+ Lint/LiteralAssignmentInCondition:
1977
+ Description: 'Checks for literal assignments in the conditions.'
1978
+ Enabled: pending
1979
+ VersionAdded: '1.58'
1980
+
1972
1981
  Lint/LiteralInInterpolation:
1973
1982
  Description: 'Checks for literals used in interpolation.'
1974
1983
  Enabled: true
@@ -3085,7 +3094,19 @@ Style/ArgumentsForwarding:
3085
3094
  Enabled: pending
3086
3095
  AllowOnlyRestArgument: true
3087
3096
  UseAnonymousForwarding: true
3097
+ RedundantRestArgumentNames:
3098
+ - args
3099
+ - arguments
3100
+ RedundantKeywordRestArgumentNames:
3101
+ - kwargs
3102
+ - options
3103
+ - opts
3104
+ RedundantBlockArgumentNames:
3105
+ - blk
3106
+ - block
3107
+ - proc
3088
3108
  VersionAdded: '1.1'
3109
+ VersionChanged: '1.58'
3089
3110
 
3090
3111
  Style/ArrayCoercion:
3091
3112
  Description: >-
@@ -3096,6 +3117,13 @@ Style/ArrayCoercion:
3096
3117
  Enabled: false
3097
3118
  VersionAdded: '0.88'
3098
3119
 
3120
+ Style/ArrayFirstLast:
3121
+ Description: 'Use `arr.first` and `arr.last` instead of `arr[0]` and `arr[-1]`.'
3122
+ Reference: '#first-and-last'
3123
+ Enabled: false
3124
+ VersionAdded: '1.58'
3125
+ Safe: false
3126
+
3099
3127
  Style/ArrayIntersect:
3100
3128
  Description: 'Use `array1.intersect?(array2)` instead of `(array1 & array2).any?`.'
3101
3129
  Enabled: 'pending'
@@ -4902,7 +4930,7 @@ Style/RedundantFetchBlock:
4902
4930
  Description: >-
4903
4931
  Use `fetch(key, value)` instead of `fetch(key) { value }`
4904
4932
  when value has Numeric, Rational, Complex, Symbol or String type, `false`, `true`, `nil` or is a constant.
4905
- Reference: 'https://github.com/JuanitoFatas/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
4933
+ Reference: 'https://github.com/fastruby/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
4906
4934
  Enabled: true
4907
4935
  Safe: false
4908
4936
  # If enabled, this cop will autocorrect usages of
@@ -5123,7 +5151,7 @@ Style/Sample:
5123
5151
  Description: >-
5124
5152
  Use `sample` instead of `shuffle.first`,
5125
5153
  `shuffle.last`, and `shuffle[Integer]`.
5126
- Reference: 'https://github.com/JuanitoFatas/fast-ruby#arrayshufflefirst-vs-arraysample-code'
5154
+ Reference: 'https://github.com/fastruby/fast-ruby#arrayshufflefirst-vs-arraysample-code'
5127
5155
  Enabled: true
5128
5156
  VersionAdded: '0.30'
5129
5157
 
@@ -5190,6 +5218,7 @@ Style/SingleLineBlockParams:
5190
5218
 
5191
5219
  Style/SingleLineDoEndBlock:
5192
5220
  Description: 'Checks for single-line `do`...`end` blocks.'
5221
+ StyleGuide: '#single-line-do-end-block'
5193
5222
  Enabled: pending
5194
5223
  VersionAdded: '1.57'
5195
5224
 
@@ -5329,6 +5358,12 @@ Style/StructInheritance:
5329
5358
  VersionAdded: '0.29'
5330
5359
  VersionChanged: '1.20'
5331
5360
 
5361
+ Style/SuperWithArgsParentheses:
5362
+ Description: 'Use parentheses for `super` with arguments.'
5363
+ StyleGuide: '#super-with-args'
5364
+ Enabled: pending
5365
+ VersionAdded: '1.58'
5366
+
5332
5367
  Style/SwapValues:
5333
5368
  Description: 'Enforces the use of shorthand-style swapping of 2 variables.'
5334
5369
  StyleGuide: '#values-swapping'
@@ -10,6 +10,7 @@ module RuboCop
10
10
 
11
11
  AUTO_GENERATED_FILE = '.rubocop_todo.yml'
12
12
  YAML_OPTIONAL_DOC_START = /\A---(\s+#|\s*\z)/.freeze
13
+ PLACEHOLDER = '###rubocop:inherit_here'
13
14
 
14
15
  PHASE_1 = 'Phase 1 of 2: run Layout/LineLength cop'
15
16
  PHASE_2 = 'Phase 2 of 2: run all cops'
@@ -125,15 +126,19 @@ module RuboCop
125
126
 
126
127
  def existing_configuration(config_file)
127
128
  File.read(config_file, encoding: Encoding::UTF_8)
128
- .sub(/^inherit_from: *[^\n]+/, '')
129
- .sub(/^inherit_from: *(\n *- *[^\n]+)+/, '')
129
+ .sub(/^inherit_from: *[^\n]+/, PLACEHOLDER)
130
+ .sub(/^inherit_from: *(\n *- *[^\n]+)+/, PLACEHOLDER)
130
131
  end
131
132
 
132
133
  def write_config_file(file_name, file_string, rubocop_yml_contents)
133
134
  lines = /\S/.match?(rubocop_yml_contents) ? rubocop_yml_contents.split("\n", -1) : []
134
- doc_start_index = lines.index { |line| YAML_OPTIONAL_DOC_START.match?(line) } || -1
135
- lines.insert(doc_start_index + 1, "inherit_from:#{file_string}\n")
136
- File.write(file_name, lines.join("\n"))
135
+ unless rubocop_yml_contents&.include?(PLACEHOLDER)
136
+ doc_start_index = lines.index { |line| YAML_OPTIONAL_DOC_START.match?(line) } || -1
137
+ lines.insert(doc_start_index + 1, PLACEHOLDER)
138
+ end
139
+ File.write(file_name, lines.join("\n")
140
+ .sub(/#{PLACEHOLDER}\n*/o, "inherit_from:#{file_string}\n\n")
141
+ .sub(/\n\n+\Z/, "\n"))
137
142
  end
138
143
 
139
144
  def relative_path_to_todo_from_options_config
@@ -15,6 +15,8 @@ module RuboCop
15
15
  'changed_parameters' => ChangedParameter,
16
16
  'changed_enforced_styles' => ChangedEnforcedStyles
17
17
  }.freeze
18
+ LOAD_RULES_CACHE = {} # rubocop:disable Style/MutableConstant
19
+ private_constant :LOAD_RULES_CACHE
18
20
 
19
21
  attr_reader :rules, :warnings
20
22
 
@@ -48,16 +50,17 @@ module RuboCop
48
50
  # Default rules for obsoletions are in config/obsoletion.yml
49
51
  # Additional rules files can be added with `RuboCop::ConfigObsoletion.files << filename`
50
52
  def load_rules # rubocop:disable Metrics/AbcSize
51
- rules = self.class.files.each_with_object({}) do |filename, hash|
52
- hash.merge!(YAML.safe_load(File.read(filename))) do |_key, first, second|
53
- case first
54
- when Hash
55
- first.merge(second)
56
- when Array
57
- first.concat(second)
53
+ rules = LOAD_RULES_CACHE[self.class.files] ||=
54
+ self.class.files.each_with_object({}) do |filename, hash|
55
+ hash.merge!(YAML.safe_load(File.read(filename))) do |_key, first, second|
56
+ case first
57
+ when Hash
58
+ first.merge(second)
59
+ when Array
60
+ first.concat(second)
61
+ end
58
62
  end
59
63
  end
60
- end
61
64
 
62
65
  cop_rules = rules.slice(*COP_RULE_CLASSES.keys)
63
66
  parameter_rules = rules.slice(*PARAMETER_RULE_CLASSES.keys)
@@ -161,9 +161,9 @@ module RuboCop
161
161
  end
162
162
 
163
163
  def gem_options(node)
164
- return [] unless node.arguments.last&.type == :hash
164
+ return [] unless node.last_argument&.type == :hash
165
165
 
166
- node.arguments.last.keys.map(&:value)
166
+ node.last_argument.keys.map(&:value)
167
167
  end
168
168
  end
169
169
  end
@@ -43,7 +43,7 @@ module RuboCop
43
43
  def on_block(block_node)
44
44
  return unless gem_specification(block_node)
45
45
 
46
- block_parameter = block_node.arguments.first.source
46
+ block_parameter = block_node.first_argument.source
47
47
 
48
48
  assignment = block_node.descendants.detect do |node|
49
49
  use_deprecated_attributes?(node, block_parameter)
@@ -65,7 +65,7 @@ module RuboCop
65
65
  lhs, _op, _rhs = *node
66
66
  [lhs, attribute]
67
67
  else
68
- [node, "#{attribute}=".to_sym]
68
+ [node, :"#{attribute}="]
69
69
  end
70
70
  end
71
71
 
@@ -12,38 +12,37 @@ module RuboCop
12
12
  # # good
13
13
  # node.method?(:do_something)
14
14
  #
15
+ # # bad
16
+ # node.method_name != :do_something
17
+ #
18
+ # # good
19
+ # !node.method?(:do_something)
20
+ #
15
21
  class MethodNameEqual < Base
16
- include RangeHelp
17
22
  extend AutoCorrector
18
23
 
19
- MSG = 'Use `method?(%<method_name>s)` instead of `method_name == %<method_name>s`.'
20
- RESTRICT_ON_SEND = %i[==].freeze
24
+ MSG = 'Use `%<prefer>s` instead.'
25
+ RESTRICT_ON_SEND = %i[== !=].freeze
21
26
 
22
- # @!method method_name?(node)
23
- def_node_matcher :method_name?, <<~PATTERN
27
+ # @!method method_name(node)
28
+ def_node_matcher :method_name, <<~PATTERN
24
29
  (send
25
- $(send
26
- (...) :method_name) :==
27
- $...)
30
+ (send
31
+ (...) :method_name) {:== :!=}
32
+ $_)
28
33
  PATTERN
29
34
 
30
35
  def on_send(node)
31
- method_name?(node) do |method_name_node, method_name_arg|
32
- message = format(MSG, method_name: method_name_arg.first.source)
36
+ method_name(node) do |method_name_arg|
37
+ bang = node.method?(:!=) ? '!' : ''
38
+ prefer = "#{bang}#{node.receiver.receiver.source}.method?(#{method_name_arg.source})"
39
+ message = format(MSG, prefer: prefer)
33
40
 
34
- range = range(method_name_node, node)
35
-
36
- add_offense(range, message: message) do |corrector|
37
- corrector.replace(range, "method?(#{method_name_arg.first.source})")
41
+ add_offense(node, message: message) do |corrector|
42
+ corrector.replace(node, prefer)
38
43
  end
39
44
  end
40
45
  end
41
-
42
- private
43
-
44
- def range(method_name_node, node)
45
- range_between(method_name_node.loc.selector.begin_pos, node.source_range.end_pos)
46
- end
47
46
  end
48
47
  end
49
48
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks for the use of `node.arguments.first` or `node.arguments.last` and
7
+ # suggests the use of `node.first_argument` or `node.last_argument` instead.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # node.arguments.first
12
+ # node.arguments[0]
13
+ # node.arguments.last
14
+ # node.arguments[-1]
15
+ #
16
+ # # good
17
+ # node.first_argument
18
+ # node.last_argument
19
+ #
20
+ class NodeFirstOrLastArgument < Base
21
+ extend AutoCorrector
22
+ include RangeHelp
23
+
24
+ MSG = 'Use `#%<correct>s` instead of `#%<incorrect>s`.'
25
+ RESTRICT_ON_SEND = %i[arguments].freeze
26
+
27
+ # @!method arguments_first_or_last?(node)
28
+ def_node_matcher :arguments_first_or_last?, <<~PATTERN
29
+ {
30
+ (send (send !nil? :arguments) ${:first :last})
31
+ (send (send !nil? :arguments) :[] (int ${0 -1}))
32
+ }
33
+ PATTERN
34
+
35
+ def on_send(node)
36
+ arguments_first_or_last?(node.parent) do |end_or_index|
37
+ range = range_between(node.loc.selector.begin_pos, node.parent.source_range.end_pos)
38
+ correct = case end_or_index
39
+ when :first, 0 then 'first_argument'
40
+ when :last, -1 then 'last_argument'
41
+ else raise "Unknown end_or_index: #{end_or_index}"
42
+ end
43
+ message = format(MSG, correct: correct, incorrect: range.source)
44
+
45
+ add_offense(range, message: message) do |corrector|
46
+ corrector.replace(range, correct)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -40,7 +40,7 @@ module RuboCop
40
40
  return if node.arguments.none?
41
41
  return unless valid_method_name?(node)
42
42
 
43
- actual_name = node.arguments.first.value
43
+ actual_name = node.first_argument.value
44
44
  directives = method_directives(node)
45
45
  return too_many_directives(node) if directives.size > 1
46
46
 
@@ -53,7 +53,7 @@ module RuboCop
53
53
  private
54
54
 
55
55
  def valid_method_name?(node)
56
- node.arguments.first.str_type? || node.arguments.first.sym_type?
56
+ node.first_argument.str_type? || node.first_argument.sym_type?
57
57
  end
58
58
 
59
59
  def method_directives(node)
@@ -12,6 +12,7 @@ require_relative 'internal_affairs/location_line_equality_comparison'
12
12
  require_relative 'internal_affairs/method_name_end_with'
13
13
  require_relative 'internal_affairs/method_name_equal'
14
14
  require_relative 'internal_affairs/node_destructuring'
15
+ require_relative 'internal_affairs/node_first_or_last_argument'
15
16
  require_relative 'internal_affairs/node_matcher_directive'
16
17
  require_relative 'internal_affairs/node_type_predicate'
17
18
  require_relative 'internal_affairs/numblock_handler'
@@ -79,7 +79,7 @@ module RuboCop
79
79
 
80
80
  def arguments_with_last_arg_pairs(node)
81
81
  items = node.arguments[0..-2]
82
- last_arg = node.arguments.last
82
+ last_arg = node.last_argument
83
83
 
84
84
  if last_arg.hash_type? && !last_arg.braces?
85
85
  items += last_arg.pairs
@@ -163,7 +163,13 @@ module RuboCop
163
163
  when :keyword
164
164
  node
165
165
  when :variable
166
- alignment_node_for_variable_style(node)
166
+ align_to = alignment_node_for_variable_style(node)
167
+
168
+ while (parent = align_to.parent) && parent.send_type? && same_line?(align_to, parent)
169
+ align_to = parent
170
+ end
171
+
172
+ align_to
167
173
  else
168
174
  start_line_range(node)
169
175
  end
@@ -49,19 +49,13 @@ module RuboCop
49
49
 
50
50
  private
51
51
 
52
- def aligned_locations(locs) # rubocop:disable Metrics/AbcSize
52
+ def aligned_locations(locs)
53
53
  return [] if locs.empty?
54
54
 
55
- aligned = Set[locs.first.line, locs.last.line]
56
- locs.each_cons(3) do |before, loc, after|
57
- col = loc.column
58
- aligned << loc.line if col == before.column || col == after.column
55
+ aligned = Set.new
56
+ locs.each_cons(2) do |loc1, loc2|
57
+ aligned << loc1.line << loc2.line if loc1.column == loc2.column
59
58
  end
60
-
61
- # if locs.size > 2 and the size of variable `aligned`
62
- # has not increased from its initial value, there are not aligned lines.
63
- return [] if locs.size > 2 && aligned.size == 2
64
-
65
59
  aligned
66
60
  end
67
61
 
@@ -25,7 +25,7 @@ module RuboCop
25
25
  # # element are on separate lines is indented one step (two spaces) more
26
26
  # # than the position inside the opening parenthesis.
27
27
  #
28
- # #bad
28
+ # # bad
29
29
  # array = [
30
30
  # :value
31
31
  # ]
@@ -33,7 +33,7 @@ module RuboCop
33
33
  # :no_difference
34
34
  # ])
35
35
  #
36
- # #good
36
+ # # good
37
37
  # array = [
38
38
  # :value
39
39
  # ]
@@ -47,7 +47,7 @@ module RuboCop
47
47
  # # separate lines is indented the same as an array literal which is not
48
48
  # # defined inside a method call.
49
49
  #
50
- # #bad
50
+ # # bad
51
51
  # # consistent
52
52
  # array = [
53
53
  # :value
@@ -56,7 +56,7 @@ module RuboCop
56
56
  # :its_like_this
57
57
  # ])
58
58
  #
59
- # #good
59
+ # # good
60
60
  # array = [
61
61
  # :value
62
62
  # ]
@@ -68,13 +68,13 @@ module RuboCop
68
68
  # # The `align_brackets` style enforces that the opening and closing
69
69
  # # brackets are indented to the same position.
70
70
  #
71
- # #bad
71
+ # # bad
72
72
  # # align_brackets
73
73
  # and_now_for_something = [
74
74
  # :completely_different
75
75
  # ]
76
76
  #
77
- # #good
77
+ # # good
78
78
  # # align_brackets
79
79
  # and_now_for_something = [
80
80
  # :completely_different
@@ -72,7 +72,7 @@ module RuboCop
72
72
  return if ignored_node?(def_node)
73
73
 
74
74
  left_parenthesis = def_node.arguments.loc.begin
75
- first_elem = def_node.arguments.first
75
+ first_elem = def_node.first_argument
76
76
  return unless first_elem
77
77
  return if same_line?(first_elem, left_parenthesis)
78
78
 
@@ -182,7 +182,7 @@ module RuboCop
182
182
  end
183
183
 
184
184
  def add_correct_closing_paren(node, corrector)
185
- corrector.insert_after(node.arguments.last, ')')
185
+ corrector.insert_after(node.last_argument, ')')
186
186
  end
187
187
 
188
188
  def remove_incorrect_closing_paren(node, corrector)
@@ -271,7 +271,7 @@ module RuboCop
271
271
  def add_correct_external_trailing_comma(node, corrector)
272
272
  return unless external_trailing_comma?(node)
273
273
 
274
- corrector.insert_after(node.arguments.last, ',')
274
+ corrector.insert_after(node.last_argument, ',')
275
275
  end
276
276
 
277
277
  def remove_incorrect_external_trailing_comma(node, corrector)
@@ -29,7 +29,7 @@ module RuboCop
29
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
30
30
  'aligned with `%<beginning>s` at ' \
31
31
  '%<begin_loc_line>d, %<begin_loc_column>d.'
32
- ANCESTOR_TYPES = %i[kwbegin def defs class module block].freeze
32
+ ANCESTOR_TYPES = %i[kwbegin def defs class module block numblock].freeze
33
33
  ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
34
34
  ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method private_class_method].freeze
35
35
 
@@ -95,7 +95,7 @@ module RuboCop
95
95
  def alignment_source(node, starting_loc)
96
96
  ending_loc =
97
97
  case node.type
98
- when :block, :kwbegin
98
+ when :block, :numblock, :kwbegin
99
99
  node.loc.begin
100
100
  when :def, :defs, :class, :module,
101
101
  :lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn
@@ -104,8 +104,8 @@ module RuboCop
104
104
  mlhs_node, = *node
105
105
  mlhs_node.source_range
106
106
  else
107
- # It is a wrapper with access modifier.
108
- node.child_nodes.first.loc.name
107
+ # It is a wrapper with receiver of object attribute or access modifier.
108
+ node.receiver&.source_range || node.child_nodes.first.loc.name
109
109
  end
110
110
 
111
111
  range_between(starting_loc.begin_pos, ending_loc.end_pos).source
@@ -25,6 +25,10 @@ module RuboCop
25
25
 
26
26
  MSG = 'Put method call on a separate line if chained to a single line block.'
27
27
 
28
+ def self.autocorrect_incompatible_with
29
+ [Style::MapToHash]
30
+ end
31
+
28
32
  def on_send(node)
29
33
  range = offending_range(node)
30
34
  add_offense(range) { |corrector| corrector.insert_before(range, "\n") } if range