rubocop 1.18.2 → 1.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +23 -6
  4. data/lib/rubocop.rb +4 -0
  5. data/lib/rubocop/cli.rb +18 -0
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_loader_resolver.rb +22 -7
  8. data/lib/rubocop/config_validator.rb +18 -5
  9. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  10. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  11. data/lib/rubocop/cop/documentation.rb +1 -1
  12. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  14. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  15. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  16. data/lib/rubocop/cop/layout/class_structure.rb +5 -1
  17. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  18. data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
  19. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
  21. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  22. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  23. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  24. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -22
  25. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
  26. data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
  27. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  28. data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
  29. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  30. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  31. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  32. data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
  33. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  34. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  35. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  36. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  37. data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
  38. data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
  39. data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
  40. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  41. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  42. data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
  43. data/lib/rubocop/cop/style/block_delimiters.rb +31 -0
  44. data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
  45. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  46. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
  47. data/lib/rubocop/cop/style/double_negation.rb +12 -1
  48. data/lib/rubocop/cop/style/encoding.rb +26 -15
  49. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  50. data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
  51. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +8 -2
  52. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
  53. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  54. data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
  55. data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
  56. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  57. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  58. data/lib/rubocop/cop/style/mutable_constant.rb +6 -8
  59. data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
  60. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
  61. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  62. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  63. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  64. data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
  65. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
  66. data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
  67. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  68. data/lib/rubocop/cop/style/word_array.rb +23 -5
  69. data/lib/rubocop/cop/util.rb +7 -2
  70. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
  71. data/lib/rubocop/magic_comment.rb +44 -15
  72. data/lib/rubocop/options.rb +1 -1
  73. data/lib/rubocop/version.rb +1 -1
  74. metadata +15 -9
@@ -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)
@@ -9,6 +9,9 @@ module RuboCop
9
9
  # Alternatively, it can check for uses of the %w() syntax, in projects
10
10
  # which do not want to include that syntax.
11
11
  #
12
+ # NOTE: When using the `percent` style, %w() arrays containing a space
13
+ # will be registered as offenses.
14
+ #
12
15
  # Configuration option: MinSize
13
16
  # If set, arrays with fewer elements than this value will not trigger the
14
17
  # cop. For example, a `MinSize` of `3` will not enforce a style on an
@@ -21,12 +24,18 @@ module RuboCop
21
24
  # # bad
22
25
  # ['foo', 'bar', 'baz']
23
26
  #
27
+ # # bad (contains spaces)
28
+ # %w[foo\ bar baz\ quux]
29
+ #
24
30
  # @example EnforcedStyle: brackets
25
31
  # # good
26
32
  # ['foo', 'bar', 'baz']
27
33
  #
28
34
  # # bad
29
35
  # %w[foo bar baz]
36
+ #
37
+ # # good (contains spaces)
38
+ # ['foo bar', 'baz quux']
30
39
  class WordArray < Base
31
40
  include ArrayMinSize
32
41
  include ArraySyntax
@@ -35,7 +44,7 @@ module RuboCop
35
44
  extend AutoCorrector
36
45
 
37
46
  PERCENT_MSG = 'Use `%w` or `%W` for an array of words.'
38
- ARRAY_MSG = 'Use `[]` for an array of words.'
47
+ ARRAY_MSG = 'Use `%<prefer>s` for an array of words.'
39
48
 
40
49
  class << self
41
50
  attr_accessor :largest_brackets
@@ -53,18 +62,27 @@ module RuboCop
53
62
 
54
63
  private
55
64
 
56
- def complex_content?(strings)
65
+ def complex_content?(strings, complex_regex: word_regex)
57
66
  strings.any? do |s|
67
+ next unless s.str_content
68
+
58
69
  string = s.str_content.dup.force_encoding(::Encoding::UTF_8)
59
- !string.valid_encoding? || !word_regex.match?(string) || / /.match?(string)
70
+ !string.valid_encoding? ||
71
+ (complex_regex && !complex_regex.match?(string)) ||
72
+ / /.match?(string)
60
73
  end
61
74
  end
62
75
 
76
+ def invalid_percent_array_contents?(node)
77
+ # Disallow %w() arrays that contain invalid encoding or spaces
78
+ complex_content?(node.values, complex_regex: false)
79
+ end
80
+
63
81
  def word_regex
64
82
  Regexp.new(cop_config['WordRegex'])
65
83
  end
66
84
 
67
- def correct_bracketed(corrector, node)
85
+ def build_bracketed_array(node)
68
86
  words = node.children.map do |word|
69
87
  if word.dstr_type?
70
88
  string_literal = to_string_literal(word.source)
@@ -75,7 +93,7 @@ module RuboCop
75
93
  end
76
94
  end
77
95
 
78
- corrector.replace(node, "[#{words.join(', ')}]")
96
+ "[#{words.join(', ')}]"
79
97
  end
80
98
  end
81
99
  end
@@ -46,8 +46,13 @@ module RuboCop
46
46
 
47
47
  def args_begin(node)
48
48
  loc = node.loc
49
- selector =
50
- node.super_type? || node.yield_type? ? loc.keyword : loc.selector
49
+ selector = if node.super_type? || node.yield_type?
50
+ loc.keyword
51
+ elsif node.def_type? || node.defs_type?
52
+ loc.name
53
+ else
54
+ loc.selector
55
+ end
51
56
  selector.end.resize(1)
52
57
  end
53
58
 
@@ -33,7 +33,7 @@ module RuboCop
33
33
  output.printf(
34
34
  "\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s\n",
35
35
  severity: github_severity(offense),
36
- file: file,
36
+ file: PathUtil.smart_path(file),
37
37
  line: offense.line,
38
38
  column: offense.real_column,
39
39
  message: github_escape(offense.message)
@@ -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
@@ -492,7 +492,7 @@ module RuboCop
492
492
  version: 'Display version.',
493
493
  verbose_version: 'Display verbose version.',
494
494
  parallel: ['Use available CPUs to execute inspection in',
495
- 'parallel. Default is false.'],
495
+ 'parallel. Default is true.'],
496
496
  stdin: ['Pipe source from STDIN, using FILE in offense',
497
497
  'reports. This is useful for editor integration.'],
498
498
  init: 'Generate a .rubocop.yml file in the current directory.'
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.18.2'
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,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.18.2
4
+ version: 1.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-07-02 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
@@ -100,7 +100,7 @@ dependencies:
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: 1.7.0
103
+ version: 1.9.1
104
104
  - - "<"
105
105
  - !ruby/object:Gem::Version
106
106
  version: '2.0'
@@ -110,7 +110,7 @@ dependencies:
110
110
  requirements:
111
111
  - - ">="
112
112
  - !ruby/object:Gem::Version
113
- version: 1.7.0
113
+ version: 1.9.1
114
114
  - - "<"
115
115
  - !ruby/object:Gem::Version
116
116
  version: '2.0'
@@ -239,6 +239,7 @@ files:
239
239
  - lib/rubocop/cop/correctors/parentheses_corrector.rb
240
240
  - lib/rubocop/cop/correctors/percent_literal_corrector.rb
241
241
  - lib/rubocop/cop/correctors/punctuation_corrector.rb
242
+ - lib/rubocop/cop/correctors/require_library_corrector.rb
242
243
  - lib/rubocop/cop/correctors/space_corrector.rb
243
244
  - lib/rubocop/cop/correctors/string_literal_corrector.rb
244
245
  - lib/rubocop/cop/correctors/unused_arg_corrector.rb
@@ -257,6 +258,7 @@ files:
257
258
  - lib/rubocop/cop/internal_affairs.rb
258
259
  - lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
259
260
  - lib/rubocop/cop/internal_affairs/example_description.rb
261
+ - lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb
260
262
  - lib/rubocop/cop/internal_affairs/method_name_equal.rb
261
263
  - lib/rubocop/cop/internal_affairs/node_destructuring.rb
262
264
  - lib/rubocop/cop/internal_affairs/node_matcher_directive.rb
@@ -267,6 +269,7 @@ files:
267
269
  - lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
268
270
  - lib/rubocop/cop/internal_affairs/redundant_message_argument.rb
269
271
  - lib/rubocop/cop/internal_affairs/style_detected_api_use.rb
272
+ - lib/rubocop/cop/internal_affairs/undefined_config.rb
270
273
  - lib/rubocop/cop/internal_affairs/useless_message_assertion.rb
271
274
  - lib/rubocop/cop/layout/access_modifier_indentation.rb
272
275
  - lib/rubocop/cop/layout/argument_alignment.rb
@@ -369,6 +372,7 @@ files:
369
372
  - lib/rubocop/cop/lint/ambiguous_assignment.rb
370
373
  - lib/rubocop/cop/lint/ambiguous_block_association.rb
371
374
  - lib/rubocop/cop/lint/ambiguous_operator.rb
375
+ - lib/rubocop/cop/lint/ambiguous_range.rb
372
376
  - lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
373
377
  - lib/rubocop/cop/lint/assignment_in_condition.rb
374
378
  - lib/rubocop/cop/lint/big_decimal_new.rb
@@ -556,6 +560,7 @@ files:
556
560
  - lib/rubocop/cop/mixin/preferred_delimiters.rb
557
561
  - lib/rubocop/cop/mixin/range_help.rb
558
562
  - lib/rubocop/cop/mixin/rational_literal.rb
563
+ - lib/rubocop/cop/mixin/require_library.rb
559
564
  - lib/rubocop/cop/mixin/rescue_node.rb
560
565
  - lib/rubocop/cop/mixin/safe_assignment.rb
561
566
  - lib/rubocop/cop/mixin/space_after_punctuation.rb
@@ -759,6 +764,7 @@ files:
759
764
  - lib/rubocop/cop/style/redundant_return.rb
760
765
  - lib/rubocop/cop/style/redundant_self.rb
761
766
  - lib/rubocop/cop/style/redundant_self_assignment.rb
767
+ - lib/rubocop/cop/style/redundant_self_assignment_branch.rb
762
768
  - lib/rubocop/cop/style/redundant_sort.rb
763
769
  - lib/rubocop/cop/style/redundant_sort_by.rb
764
770
  - lib/rubocop/cop/style/regexp_literal.rb
@@ -883,9 +889,9 @@ metadata:
883
889
  homepage_uri: https://rubocop.org/
884
890
  changelog_uri: https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md
885
891
  source_code_uri: https://github.com/rubocop/rubocop/
886
- documentation_uri: https://docs.rubocop.org/rubocop/1.18/
892
+ documentation_uri: https://docs.rubocop.org/rubocop/1.19/
887
893
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
888
- post_install_message:
894
+ post_install_message:
889
895
  rdoc_options: []
890
896
  require_paths:
891
897
  - lib
@@ -900,8 +906,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
900
906
  - !ruby/object:Gem::Version
901
907
  version: '0'
902
908
  requirements: []
903
- rubygems_version: 3.2.18
904
- signing_key:
909
+ rubygems_version: 3.1.2
910
+ signing_key:
905
911
  specification_version: 4
906
912
  summary: Automatic Ruby code style checking tool.
907
913
  test_files: []