rubocop 1.50.0 → 1.50.1

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: b29d7b756beef5910c678a948e4c5e6a852978e39b6d4436eadaf9087731a742
4
- data.tar.gz: 139e8da95a5f9f5d84c6286bfccad4f41aa93790c7387b5e9737f5e8a3b0dd6b
3
+ metadata.gz: cc74785ce6643999994c7595e6ab7b6508abc7ab33532032d09fb854808d3ab5
4
+ data.tar.gz: 3313880579d587260cae9f718b148160a597da1a239464c9ae86ce8a773065fc
5
5
  SHA512:
6
- metadata.gz: 4c642f1f939b5fd5c91669422f4cc901bc889b1999d0f44ab6e576a4b9aa84c5a0f77540a8b9ad613dd42d71c560699b7218fa5f5d9320721bb8f2ccf9b97aee
7
- data.tar.gz: 7980c7461a8e5d7d7cb1e19bdcba86696490b3c016adaca99011c100373e1db5944fb20642d270db11f68aa5604dc9b26fcd25733f4285e777f5ab8a504b025f
6
+ metadata.gz: 0b4ea0d9447499f45e99d333c05f72edd634980d506717117cb35029ac2f185de08681c68ce40a2d319efe163b23a168e6c9a55f8178dbf25f847ecc7710cb04
7
+ data.tar.gz: e1c57df60ec75958f78eec411b0697496ea34b0a10626b3fa79453a64328162292cb621319557b0aac59fe88d447fa32a5e3fbcf1d4d4efcf9ef9388191a6fee
@@ -71,6 +71,22 @@ module RuboCop
71
71
  # second_method
72
72
  # end
73
73
  #
74
+ # # bad - repeated the same patterns and guard conditions
75
+ # case x
76
+ # in foo if bar
77
+ # first_method
78
+ # in foo if bar
79
+ # second_method
80
+ # end
81
+ #
82
+ # # good
83
+ # case x
84
+ # in foo if bar
85
+ # first_method
86
+ # in foo if baz
87
+ # second_method
88
+ # end
89
+ #
74
90
  class DuplicateMatchPattern < Base
75
91
  extend TargetRubyVersion
76
92
 
@@ -90,11 +106,15 @@ module RuboCop
90
106
  private
91
107
 
92
108
  def pattern_identity(pattern)
93
- if pattern.hash_pattern_type? || pattern.match_alt_type?
94
- pattern.children.map(&:source).sort
95
- else
96
- pattern.source
97
- end
109
+ pattern_source = if pattern.hash_pattern_type? || pattern.match_alt_type?
110
+ pattern.children.map(&:source).sort
111
+ else
112
+ pattern.source
113
+ end
114
+
115
+ return pattern_source unless (guard = pattern.parent.children[1])
116
+
117
+ pattern_source + guard.source
98
118
  end
99
119
  end
100
120
  end
@@ -31,22 +31,26 @@ module RuboCop
31
31
  def on_masgn(node)
32
32
  lhs, rhs = *node
33
33
  lhs_elements = *lhs
34
+ rhs = rhs.body if rhs.rescue_type?
34
35
  rhs_elements = Array(rhs).compact # edge case for one constant
35
36
 
36
37
  return if allowed_lhs?(lhs) || allowed_rhs?(rhs) ||
37
38
  allowed_masign?(lhs_elements, rhs_elements)
38
39
 
39
- add_offense(node) { |corrector| autocorrect(corrector, node) }
40
+ range = node.source_range.begin.join(rhs.source_range.end)
41
+
42
+ add_offense(range) do |corrector|
43
+ autocorrect(corrector, node, lhs, rhs)
44
+ end
40
45
  end
41
46
 
42
47
  private
43
48
 
44
- def autocorrect(corrector, node)
45
- left, right = *node
46
- left_elements = *left
47
- right_elements = Array(right).compact
49
+ def autocorrect(corrector, node, lhs, rhs)
50
+ left_elements = *lhs
51
+ right_elements = Array(rhs).compact
48
52
  order = find_valid_order(left_elements, right_elements)
49
- correction = assignment_corrector(node, order)
53
+ correction = assignment_corrector(node, rhs, order)
50
54
 
51
55
  corrector.replace(correction.correction_range, correction.correction)
52
56
  end
@@ -77,14 +81,19 @@ module RuboCop
77
81
  node.block_type? || node.send_type?
78
82
  end
79
83
 
80
- def assignment_corrector(node, order)
81
- _assignment, modifier = *node.parent
84
+ def assignment_corrector(node, rhs, order)
85
+ if node.parent&.rescue_type?
86
+ _assignment, modifier = *node.parent
87
+ else
88
+ _assignment, modifier = *rhs.parent
89
+ end
90
+
82
91
  if modifier_statement?(node.parent)
83
- ModifierCorrector.new(node, config, order)
92
+ ModifierCorrector.new(node, rhs, modifier, config, order)
84
93
  elsif rescue_modifier?(modifier)
85
- RescueCorrector.new(node, config, order)
94
+ RescueCorrector.new(node, rhs, modifier, config, order)
86
95
  else
87
- GenericCorrector.new(node, config, order)
96
+ GenericCorrector.new(node, rhs, modifier, config, order)
88
97
  end
89
98
  end
90
99
 
@@ -181,10 +190,12 @@ module RuboCop
181
190
  class GenericCorrector
182
191
  include Alignment
183
192
 
184
- attr_reader :config, :node
193
+ attr_reader :node, :rhs, :rescue_result, :config
185
194
 
186
- def initialize(node, config, new_elements)
195
+ def initialize(node, rhs, modifier, config, new_elements)
187
196
  @node = node
197
+ @rhs = rhs
198
+ _, _, @rescue_result = *modifier
188
199
  @config = config
189
200
  @new_elements = new_elements
190
201
  end
@@ -228,13 +239,10 @@ module RuboCop
228
239
  # protected by rescue
229
240
  class RescueCorrector < GenericCorrector
230
241
  def correction
231
- _node, rescue_clause = *node.parent
232
- _, _, rescue_result = *rescue_clause
233
-
234
242
  # If the parallel assignment uses a rescue modifier and it is the
235
243
  # only contents of a method, then we want to make use of the
236
244
  # implicit begin
237
- if node.parent.parent&.def_type?
245
+ if rhs.parent.parent.parent&.def_type?
238
246
  super + def_correction(rescue_result)
239
247
  else
240
248
  begin_correction(rescue_result)
@@ -242,7 +250,7 @@ module RuboCop
242
250
  end
243
251
 
244
252
  def correction_range
245
- node.parent.source_range
253
+ rhs.parent.parent.source_range
246
254
  end
247
255
 
248
256
  private
@@ -90,7 +90,7 @@ module RuboCop
90
90
  !ends_with_backslash_without_comment?(range.source_line) ||
91
91
  string_concatenation?(range.source_line) ||
92
92
  start_with_arithmetic_operator?(processed_source[range.line]) ||
93
- inside_string_literal?(range)
93
+ inside_string_literal_or_method_with_argument?(range)
94
94
  end
95
95
 
96
96
  def ends_with_backslash_without_comment?(source_line)
@@ -101,9 +101,9 @@ module RuboCop
101
101
  /["']\s*\\\z/.match?(source_line)
102
102
  end
103
103
 
104
- def inside_string_literal?(range)
105
- processed_source.tokens.each.any? do |token|
106
- ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
104
+ def inside_string_literal_or_method_with_argument?(range)
105
+ processed_source.tokens.each_cons(2).any? do |token, next_token|
106
+ inside_string_literal?(range, token) || method_with_argument?(token, next_token)
107
107
  end
108
108
  end
109
109
 
@@ -115,6 +115,18 @@ module RuboCop
115
115
  parse(source.gsub(/\\\n/, "\n")).valid_syntax?
116
116
  end
117
117
 
118
+ def inside_string_literal?(range, token)
119
+ ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
120
+ end
121
+
122
+ # A method call without parentheses such as the following cannot remove `\`:
123
+ #
124
+ # do_something \
125
+ # argument
126
+ def method_with_argument?(current_token, next_token)
127
+ current_token.type == :tIDENTIFIER && next_token.type == :tIDENTIFIER
128
+ end
129
+
118
130
  def argument_newline?(node)
119
131
  node = node.children.first if node.root? && node.begin_type?
120
132
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.50.0'
6
+ STRING = '1.50.1'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.50.0
4
+ version: 1.50.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2023-04-11 00:00:00.000000000 Z
13
+ date: 2023-04-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json