rubocop 1.19.0 → 1.19.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: 184ca6701160d34e5fe8f659e159842f397bfe07edbc27df159b7793fd8a0a3c
4
- data.tar.gz: d891042df8d86850afbf19b0d0e81f4255e909f0775b32eb675b3766d0266561
3
+ metadata.gz: 5866f1cc7ec3201aa598d3121997137dded621d0cf508430d3c9dd4b4c10274a
4
+ data.tar.gz: d857fd81c1d461020a67b6139f9684082f778c8ecb227200d7a0d9f3307940b4
5
5
  SHA512:
6
- metadata.gz: 6b0ed04638cfa650324684315fcf4c8a99b93fa66c1dce39efa7c76605bbd78a7c4e4b4d6fc6ac608d2ebbb52604f6f9fbccf23e5d097c637b1b6c879601d70b
7
- data.tar.gz: f1310e4f3a7285ecf642edf6e99500decb0d1aa74b676adf38ee6c65c9f1f4cf3124853102a50da51e896a6b9780b3db9156fa4e075f60139f01d239d13ab898
6
+ metadata.gz: 2162afd65c9b167710c41a61062d9e4daf67f7dcd58ab6ca1371454b89cd69b05316d27f0ded2ace8f458aeadda50d4618e4a09a56b51ccdfde97846943be45a
7
+ data.tar.gz: bd53f2af2d7abbee2fb73b951cea6617d2345c976d3fde9ff017d37a17e35c074bc162ae0118706f0049e018c4b8bfbf383cf2705c9cdce908a3083989ef56f1
@@ -143,7 +143,7 @@ module RuboCop
143
143
  PENDING_BANNER = <<~BANNER
144
144
  The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
145
145
 
146
- Please also note that can also opt-in to new cops by default by adding this to your config:
146
+ Please also note that you can opt-in to new cops by default by adding this to your config:
147
147
  AllCops:
148
148
  NewCops: enable
149
149
  BANNER
@@ -8,7 +8,7 @@ module RuboCop
8
8
 
9
9
  # @api private
10
10
  def department_to_basename(department)
11
- "cops_#{department.downcase}"
11
+ "cops_#{department.to_s.downcase.tr('/', '_')}"
12
12
  end
13
13
 
14
14
  # @api private
@@ -137,17 +137,18 @@ module RuboCop
137
137
  send_node_loc = ancestor_node.send_node.loc
138
138
  do_keyword_line = ancestor_node.loc.begin.line
139
139
  rescue_keyword_column = node.loc.keyword.column
140
- selector = send_node_loc.selector
140
+ selector = send_node_loc.respond_to?(:selector) ? send_node_loc.selector : send_node_loc
141
141
 
142
- if send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot) &&
143
- aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
142
+ if aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
144
143
  return true
145
144
  end
146
145
 
147
146
  do_keyword_line == selector.line && rescue_keyword_column == selector.column
148
147
  end
149
148
 
150
- def aligned_with_leading_dot?(do_keyword_line, dot, rescue_keyword_column)
149
+ def aligned_with_leading_dot?(do_keyword_line, send_node_loc, rescue_keyword_column)
150
+ return false unless send_node_loc.respond_to?(:dot) && (dot = send_node_loc.dot)
151
+
151
152
  do_keyword_line == dot.line && rescue_keyword_column == dot.column
152
153
  end
153
154
 
@@ -38,8 +38,11 @@ module RuboCop
38
38
 
39
39
  return unless style == :brackets || invalid_percent_array_contents?(node)
40
40
 
41
- add_offense(node, message: self.class::ARRAY_MSG) do |corrector|
42
- correct_bracketed(corrector, node)
41
+ bracketed_array = build_bracketed_array(node)
42
+ message = format(self.class::ARRAY_MSG, prefer: bracketed_array)
43
+
44
+ add_offense(node, message: message) do |corrector|
45
+ corrector.replace(node, bracketed_array)
43
46
  end
44
47
  end
45
48
 
@@ -62,7 +62,7 @@ module RuboCop
62
62
  def end_of_method_definition?(node)
63
63
  return false unless (def_node = find_def_node_from_ascendant(node))
64
64
 
65
- last_child = def_node.child_nodes.last
65
+ last_child = find_last_child(def_node.body)
66
66
 
67
67
  last_child.last_line == node.last_line
68
68
  end
@@ -73,6 +73,17 @@ module RuboCop
73
73
 
74
74
  find_def_node_from_ascendant(node.parent)
75
75
  end
76
+
77
+ def find_last_child(node)
78
+ case node.type
79
+ when :rescue
80
+ find_last_child(node.body)
81
+ when :ensure
82
+ find_last_child(node.child_nodes.first)
83
+ else
84
+ node.child_nodes.last
85
+ end
86
+ end
76
87
  end
77
88
  end
78
89
  end
@@ -13,38 +13,49 @@ module RuboCop
13
13
  include RangeHelp
14
14
  extend AutoCorrector
15
15
 
16
- MSG_UNNECESSARY = 'Unnecessary utf-8 encoding comment.'
16
+ MSG = 'Unnecessary utf-8 encoding comment.'
17
17
  ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/.freeze
18
18
  SHEBANG = '#!'
19
19
 
20
20
  def on_new_investigation
21
21
  return if processed_source.buffer.source.empty?
22
22
 
23
- line_number = encoding_line_number(processed_source)
24
- return unless (@message = offense(processed_source, line_number))
23
+ comments.each do |line_number, comment|
24
+ next unless offense?(comment)
25
25
 
26
- range = processed_source.buffer.line_range(line_number + 1)
27
- add_offense(range, message: @message) do |corrector|
28
- corrector.remove(range_with_surrounding_space(range: range, side: :right))
26
+ register_offense(line_number, comment)
29
27
  end
30
28
  end
31
29
 
32
30
  private
33
31
 
34
- def offense(processed_source, line_number)
35
- line = processed_source[line_number]
32
+ def comments
33
+ processed_source.lines.each.with_index.with_object({}) do |(line, line_number), comments|
34
+ next if line.start_with?(SHEBANG)
36
35
 
37
- MSG_UNNECESSARY if encoding_omitable?(line)
36
+ comment = MagicComment.parse(line)
37
+ return comments unless comment.valid?
38
+
39
+ comments[line_number + 1] = comment
40
+ end
38
41
  end
39
42
 
40
- def encoding_omitable?(line)
41
- ENCODING_PATTERN.match?(line)
43
+ def offense?(comment)
44
+ comment.encoding_specified? && comment.encoding.casecmp('utf-8').zero?
42
45
  end
43
46
 
44
- def encoding_line_number(processed_source)
45
- line_number = 0
46
- line_number += 1 if processed_source[line_number].start_with?(SHEBANG)
47
- line_number
47
+ def register_offense(line_number, comment)
48
+ range = processed_source.buffer.line_range(line_number)
49
+
50
+ add_offense(range) do |corrector|
51
+ text = comment.without(:encoding)
52
+
53
+ if text.blank?
54
+ corrector.remove(range_with_surrounding_space(range: range, side: :right))
55
+ else
56
+ corrector.replace(range, text)
57
+ end
58
+ end
48
59
  end
49
60
  end
50
61
  end
@@ -29,6 +29,7 @@ module RuboCop
29
29
  # # good
30
30
  # [{ one: 1 }, { two: 2 }]
31
31
  class HashAsLastArrayItem < Base
32
+ include RangeHelp
32
33
  include ConfigurableEnforcedStyle
33
34
  extend AutoCorrector
34
35
 
@@ -74,6 +75,7 @@ module RuboCop
74
75
  return if node.children.empty? # Empty hash cannot be "unbraced"
75
76
 
76
77
  add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
78
+ remove_last_element_trailing_comma(corrector, node.parent)
77
79
  corrector.remove(node.loc.begin)
78
80
  corrector.remove(node.loc.end)
79
81
  end
@@ -82,6 +84,15 @@ module RuboCop
82
84
  def braces_style?
83
85
  style == :braces
84
86
  end
87
+
88
+ def remove_last_element_trailing_comma(corrector, node)
89
+ range = range_with_surrounding_space(
90
+ range: node.children.last.source_range,
91
+ side: :right
92
+ ).end.resize(1)
93
+
94
+ corrector.remove(range) if range.source == ','
95
+ end
85
96
  end
86
97
  end
87
98
  end
@@ -84,6 +84,7 @@ module RuboCop
84
84
 
85
85
  def on_kwbegin(node)
86
86
  return if empty_begin?(node) ||
87
+ begin_block_has_multiline_statements?(node) ||
87
88
  contain_rescue_or_ensure?(node) ||
88
89
  valid_context_using_only_begin?(node)
89
90
 
@@ -102,6 +103,9 @@ module RuboCop
102
103
  corrector.remove(offense_range)
103
104
  end
104
105
 
106
+ if use_modifier_form_after_multiline_begin_block?(node)
107
+ correct_modifier_form_after_multiline_begin_block(corrector, node)
108
+ end
105
109
  corrector.remove(node.loc.end)
106
110
  end
107
111
  end
@@ -127,10 +131,31 @@ module RuboCop
127
131
  corrector.insert_before(node.parent, comments) unless comments.blank?
128
132
  end
129
133
 
134
+ def use_modifier_form_after_multiline_begin_block?(node)
135
+ return unless (parent = node.parent)
136
+
137
+ node.multiline? && parent.if_type? && parent.modifier_form?
138
+ end
139
+
140
+ def correct_modifier_form_after_multiline_begin_block(corrector, node)
141
+ condition_range = condition_range(node.parent)
142
+
143
+ corrector.insert_after(node.children.first, " #{condition_range.source}")
144
+ corrector.remove(range_by_whole_lines(condition_range, include_final_newline: true))
145
+ end
146
+
147
+ def condition_range(node)
148
+ range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
149
+ end
150
+
130
151
  def empty_begin?(node)
131
152
  node.children.empty?
132
153
  end
133
154
 
155
+ def begin_block_has_multiline_statements?(node)
156
+ node.children.count >= 2
157
+ end
158
+
134
159
  def contain_rescue_or_ensure?(node)
135
160
  first_child = node.children.first
136
161
 
@@ -5,6 +5,9 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for places where conditional branch makes redundant self-assignment.
7
7
  #
8
+ # It only detects local variable because it may replace state of instance variable,
9
+ # class variable, and global variable that have state across methods with `nil`.
10
+ #
8
11
  # @example
9
12
  #
10
13
  # # bad
@@ -32,11 +35,11 @@ module RuboCop
32
35
 
33
36
  def on_lvasgn(node)
34
37
  variable, expression = *node
35
- return unless expression&.if_type?
36
- return unless expression.ternary? || expression.else?
38
+ return unless use_if_and_else_branch?(expression)
37
39
 
38
40
  if_branch = expression.if_branch
39
41
  else_branch = expression.else_branch
42
+ return if inconvertible_to_modifier?(if_branch, else_branch)
40
43
 
41
44
  if self_assign?(variable, if_branch)
42
45
  register_offense(expression, if_branch, else_branch, 'unless')
@@ -45,41 +48,33 @@ module RuboCop
45
48
  end
46
49
  end
47
50
 
48
- alias on_ivasgn on_lvasgn
49
- alias on_cvasgn on_lvasgn
50
- alias on_gvasgn on_lvasgn
51
-
52
51
  private
53
52
 
54
- def self_assign?(variable, branch)
55
- variable.to_s == branch&.source
53
+ def use_if_and_else_branch?(expression)
54
+ return false unless expression&.if_type?
55
+
56
+ !expression.ternary? || !expression.else?
56
57
  end
57
58
 
58
- def register_offense(if_node, offense_branch, opposite_branch, keyword)
59
- add_offense(offense_branch) do |corrector|
60
- if if_node.ternary?
61
- replacement = "#{opposite_branch.source} #{keyword} #{if_node.condition.source}"
62
- corrector.replace(if_node, replacement)
63
- else
64
- if_node_loc = if_node.loc
59
+ def inconvertible_to_modifier?(if_branch, else_branch)
60
+ multiple_statements?(if_branch) || multiple_statements?(else_branch) ||
61
+ else_branch.respond_to?(:elsif?) && else_branch.elsif?
62
+ end
65
63
 
66
- range = range_by_whole_lines(offense_branch.source_range, include_final_newline: true)
67
- corrector.remove(range)
68
- range = range_by_whole_lines(if_node_loc.else, include_final_newline: true)
69
- corrector.remove(range)
64
+ def multiple_statements?(branch)
65
+ branch && branch.children.compact.count > 1
66
+ end
70
67
 
71
- autocorrect_if_condition(corrector, if_node, if_node_loc, keyword)
72
- end
73
- end
68
+ def self_assign?(variable, branch)
69
+ variable.to_s == branch&.source
74
70
  end
75
71
 
76
- def autocorrect_if_condition(corrector, if_node, if_node_loc, keyword)
77
- else_branch = if_node.else_branch
72
+ def register_offense(if_node, offense_branch, opposite_branch, keyword)
73
+ add_offense(offense_branch) do |corrector|
74
+ assignment_value = opposite_branch ? opposite_branch.source : 'nil'
75
+ replacement = "#{assignment_value} #{keyword} #{if_node.condition.source}"
78
76
 
79
- if else_branch.respond_to?(:elsif?) && else_branch.elsif?
80
- corrector.replace(if_node.condition, else_branch.condition.source)
81
- else
82
- corrector.replace(if_node_loc.keyword, keyword)
77
+ corrector.replace(if_node, replacement)
83
78
  end
84
79
  end
85
80
  end
@@ -38,6 +38,10 @@ module RuboCop
38
38
 
39
39
  MSG = 'Consider merging nested conditions into outer `%<conditional_type>s` conditions.'
40
40
 
41
+ def self.autocorrect_incompatible_with
42
+ [Style::NegatedIf, Style::NegatedUnless]
43
+ end
44
+
41
45
  def on_if(node)
42
46
  return if node.ternary? || node.else? || node.elsif?
43
47
 
@@ -35,7 +35,7 @@ module RuboCop
35
35
  extend AutoCorrector
36
36
 
37
37
  PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
38
- ARRAY_MSG = 'Use `[]` for an array of symbols.'
38
+ ARRAY_MSG = 'Use `%<prefer>s` for an array of symbols.'
39
39
 
40
40
  class << self
41
41
  attr_accessor :largest_brackets
@@ -60,7 +60,7 @@ module RuboCop
60
60
  end
61
61
  end
62
62
 
63
- def correct_bracketed(corrector, node)
63
+ def build_bracketed_array(node)
64
64
  syms = node.children.map do |c|
65
65
  if c.dsym_type?
66
66
  string_literal = to_string_literal(c.source)
@@ -71,7 +71,7 @@ module RuboCop
71
71
  end
72
72
  end
73
73
 
74
- corrector.replace(node, "[#{syms.join(', ')}]")
74
+ "[#{syms.join(', ')}]"
75
75
  end
76
76
 
77
77
  def to_symbol_literal(string)
@@ -44,7 +44,7 @@ module RuboCop
44
44
  extend AutoCorrector
45
45
 
46
46
  PERCENT_MSG = 'Use `%w` or `%W` for an array of words.'
47
- ARRAY_MSG = 'Use `[]` for an array of words.'
47
+ ARRAY_MSG = 'Use `%<prefer>s` for an array of words.'
48
48
 
49
49
  class << self
50
50
  attr_accessor :largest_brackets
@@ -82,7 +82,7 @@ module RuboCop
82
82
  Regexp.new(cop_config['WordRegex'])
83
83
  end
84
84
 
85
- def correct_bracketed(corrector, node)
85
+ def build_bracketed_array(node)
86
86
  words = node.children.map do |word|
87
87
  if word.dstr_type?
88
88
  string_literal = to_string_literal(word.source)
@@ -93,7 +93,7 @@ module RuboCop
93
93
  end
94
94
  end
95
95
 
96
- corrector.replace(node, "[#{words.join(', ')}]")
96
+ "[#{words.join(', ')}]"
97
97
  end
98
98
  end
99
99
  end
@@ -7,6 +7,11 @@ module RuboCop
7
7
  class MagicComment
8
8
  # @see https://git.io/vMC1C IRB's pattern for matching magic comment tokens
9
9
  TOKEN = /[[:alnum:]\-_]+/.freeze
10
+ KEYWORDS = {
11
+ encoding: '(?:en)?coding',
12
+ frozen_string_literal: 'frozen[_-]string[_-]literal',
13
+ shareable_constant_value: 'shareable[_-]constant[_-]value'
14
+ }.freeze
10
15
 
11
16
  # Detect magic comment format and pass it to the appropriate wrapper.
12
17
  #
@@ -15,8 +20,8 @@ module RuboCop
15
20
  # @return [RuboCop::MagicComment]
16
21
  def self.parse(comment)
17
22
  case comment
18
- when EmacsComment::FORMAT then EmacsComment.new(comment)
19
- when VimComment::FORMAT then VimComment.new(comment)
23
+ when EmacsComment::REGEXP then EmacsComment.new(comment)
24
+ when VimComment::REGEXP then VimComment.new(comment)
20
25
  else
21
26
  SimpleComment.new(comment)
22
27
  end
@@ -30,6 +35,10 @@ module RuboCop
30
35
  frozen_string_literal_specified? || encoding_specified? || shareable_constant_value_specified?
31
36
  end
32
37
 
38
+ def valid?
39
+ @comment.start_with?('#') && any?
40
+ end
41
+
33
42
  # Does the magic comment enable the frozen string literal feature.
34
43
  #
35
44
  # Test whether the frozen string literal value is `true`. Cannot
@@ -111,6 +120,18 @@ module RuboCop
111
120
  #
112
121
  # @abstract
113
122
  class EditorComment < MagicComment
123
+ def encoding
124
+ match(self.class::KEYWORDS[:encoding])
125
+ end
126
+
127
+ # Rewrite the comment without a given token type
128
+ def without(type)
129
+ remaining = tokens.grep_v(/\A#{self.class::KEYWORDS[type.to_sym]}/)
130
+ return '' if remaining.empty?
131
+
132
+ self.class::FORMAT % remaining.join(self.class::SEPARATOR)
133
+ end
134
+
114
135
  private
115
136
 
116
137
  # Find a token starting with the provided keyword and extract its value.
@@ -135,7 +156,7 @@ module RuboCop
135
156
  #
136
157
  # @return [Array<String>]
137
158
  def tokens
138
- extract(self.class::FORMAT).split(self.class::SEPARATOR).map(&:strip)
159
+ extract(self.class::REGEXP).split(self.class::SEPARATOR).map(&:strip)
139
160
  end
140
161
  end
141
162
 
@@ -151,22 +172,19 @@ module RuboCop
151
172
  # @see https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html
152
173
  # @see https://git.io/vMCXh Emacs handling in Ruby's parse.y
153
174
  class EmacsComment < EditorComment
154
- FORMAT = /-\*-(.+)-\*-/.freeze
175
+ REGEXP = /-\*-(.+)-\*-/.freeze
176
+ FORMAT = '# -*- %s -*-'
155
177
  SEPARATOR = ';'
156
178
  OPERATOR = ':'
157
179
 
158
- def encoding
159
- match('(?:en)?coding')
160
- end
161
-
162
180
  private
163
181
 
164
182
  def extract_frozen_string_literal
165
- match('frozen[_-]string[_-]literal')
183
+ match(KEYWORDS[:frozen_string_literal])
166
184
  end
167
185
 
168
186
  def extract_shareable_constant_value
169
- match('shareable[_-]constant[_-]values')
187
+ match(KEYWORDS[:shareable_constant_value])
170
188
  end
171
189
  end
172
190
 
@@ -179,9 +197,11 @@ module RuboCop
179
197
  #
180
198
  # comment.encoding # => 'ascii-8bit'
181
199
  class VimComment < EditorComment
182
- FORMAT = /#\s*vim:\s*(.+)/.freeze
200
+ REGEXP = /#\s*vim:\s*(.+)/.freeze
201
+ FORMAT = '# vim: %s'
183
202
  SEPARATOR = ', '
184
203
  OPERATOR = '='
204
+ KEYWORDS = MagicComment::KEYWORDS.merge(encoding: 'fileencoding').freeze
185
205
 
186
206
  # For some reason the fileencoding keyword only works if there
187
207
  # is at least one other token included in the string. For example
@@ -193,7 +213,7 @@ module RuboCop
193
213
  # # vim: foo=bar, fileencoding=ascii-8bit
194
214
  #
195
215
  def encoding
196
- match('fileencoding') if tokens.size > 1
216
+ super if tokens.size > 1
197
217
  end
198
218
 
199
219
  # Vim comments cannot specify frozen string literal behavior.
@@ -219,7 +239,16 @@ module RuboCop
219
239
  class SimpleComment < MagicComment
220
240
  # Match `encoding` or `coding`
221
241
  def encoding
222
- extract(/\A\s*\#.*\b(?:en)?coding: (#{TOKEN})/io)
242
+ extract(/\A\s*\#.*\b#{KEYWORDS[:encoding]}: (#{TOKEN})/io)
243
+ end
244
+
245
+ # Rewrite the comment without a given token type
246
+ def without(type)
247
+ if @comment.match?(/\A#\s*#{self.class::KEYWORDS[type.to_sym]}/)
248
+ ''
249
+ else
250
+ @comment
251
+ end
223
252
  end
224
253
 
225
254
  private
@@ -232,11 +261,11 @@ module RuboCop
232
261
  # Case-insensitive and dashes/underscores are acceptable.
233
262
  # @see https://git.io/vM7Mg
234
263
  def extract_frozen_string_literal
235
- extract(/\A\s*#\s*frozen[_-]string[_-]literal:\s*(#{TOKEN})\s*\z/io)
264
+ extract(/\A\s*#\s*#{KEYWORDS[:frozen_string_literal]}:\s*(#{TOKEN})\s*\z/io)
236
265
  end
237
266
 
238
267
  def extract_shareable_constant_value
239
- extract(/\A\s*#\s*shareable[_-]constant[_-]value:\s*(#{TOKEN})\s*\z/io)
268
+ extract(/\A\s*#\s*#{KEYWORDS[:shareable_constant_value]}:\s*(#{TOKEN})\s*\z/io)
240
269
  end
241
270
  end
242
271
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.19.0'
6
+ STRING = '1.19.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.19.0
4
+ version: 1.19.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: 2021-08-12 00:00:00.000000000 Z
13
+ date: 2021-08-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: parallel