rubocop 1.15.0 → 1.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +104 -31
  4. data/lib/rubocop.rb +7 -0
  5. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_validator.rb +5 -5
  8. data/lib/rubocop/cop/base.rb +2 -2
  9. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  10. data/lib/rubocop/cop/bundler/gem_version.rb +38 -4
  11. data/lib/rubocop/cop/corrector.rb +4 -4
  12. data/lib/rubocop/cop/generator.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
  14. data/lib/rubocop/cop/layout/argument_alignment.rb +4 -3
  15. data/lib/rubocop/cop/layout/array_alignment.rb +2 -2
  16. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  17. data/lib/rubocop/cop/layout/case_indentation.rb +57 -9
  18. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +7 -1
  19. data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/dot_position.rb +7 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +13 -15
  22. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  23. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  24. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/hash_alignment.rb +26 -8
  26. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  27. data/lib/rubocop/cop/layout/indentation_width.rb +8 -0
  28. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +122 -0
  29. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +6 -6
  30. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
  31. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +6 -6
  32. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -6
  33. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +6 -6
  34. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
  35. data/lib/rubocop/cop/layout/parameter_alignment.rb +2 -2
  36. data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -9
  37. data/lib/rubocop/cop/layout/space_around_keyword.rb +28 -0
  38. data/lib/rubocop/cop/layout/space_around_operators.rb +7 -1
  39. data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
  40. data/lib/rubocop/cop/lint/literal_as_condition.rb +13 -1
  41. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +32 -17
  42. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  43. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  44. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  45. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +105 -74
  46. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -0
  47. data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -13
  48. data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
  49. data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
  50. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  51. data/lib/rubocop/cop/migration/department_name.rb +3 -1
  52. data/lib/rubocop/cop/mixin/check_line_breakable.rb +10 -1
  53. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +14 -3
  54. data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -4
  55. data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
  56. data/lib/rubocop/cop/naming/inclusive_language.rb +249 -0
  57. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -2
  58. data/lib/rubocop/cop/style/class_and_module_children.rb +14 -0
  59. data/lib/rubocop/cop/style/hash_each_methods.rb +18 -1
  60. data/lib/rubocop/cop/style/identical_conditional_branches.rb +58 -8
  61. data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
  62. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -1
  63. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
  64. data/lib/rubocop/cop/style/multiline_when_then.rb +2 -11
  65. data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
  66. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  67. data/lib/rubocop/cop/style/quoted_symbols.rb +110 -0
  68. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  69. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
  70. data/lib/rubocop/cop/style/redundant_self.rb +24 -2
  71. data/lib/rubocop/cop/style/regexp_literal.rb +10 -1
  72. data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
  73. data/lib/rubocop/cop/style/string_concatenation.rb +32 -5
  74. data/lib/rubocop/cop/style/string_literals.rb +3 -2
  75. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -0
  76. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  77. data/lib/rubocop/cop/style/top_level_method_definition.rb +10 -2
  78. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  79. data/lib/rubocop/cop/style/when_then.rb +6 -2
  80. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
  81. data/lib/rubocop/directive_comment.rb +58 -6
  82. data/lib/rubocop/options.rb +4 -4
  83. data/lib/rubocop/rake_task.rb +1 -1
  84. data/lib/rubocop/remote_config.rb +10 -2
  85. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  86. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  87. data/lib/rubocop/version.rb +1 -1
  88. metadata +12 -5
@@ -54,6 +54,7 @@ module RuboCop
54
54
  directive = DirectiveComment.new(comment)
55
55
 
56
56
  cop_names.each do |name|
57
+ name = name.split('/').first if department?(directive, name)
57
58
  add_offense(
58
59
  range_of_offense(comment, name),
59
60
  message: format(MSG, cop: all_or_name(name))
@@ -119,6 +120,10 @@ module RuboCop
119
120
  def all_or_name(name)
120
121
  name == 'all' ? 'all cops' : name
121
122
  end
123
+
124
+ def department?(directive, name)
125
+ directive.in_directive_department?(name) && !directive.overridden_by_department?(name)
126
+ end
122
127
  end
123
128
  end
124
129
  end
@@ -66,10 +66,11 @@ module RuboCop
66
66
  class SymbolConversion < Base
67
67
  extend AutoCorrector
68
68
  include ConfigurableEnforcedStyle
69
+ include SymbolHelp
69
70
 
70
71
  MSG = 'Unnecessary symbol conversion; use `%<correction>s` instead.'
71
72
  MSG_CONSISTENCY = 'Symbol hash key should be quoted for consistency; ' \
72
- 'use `%<correction>s` instead.'
73
+ 'use `%<correction>s` instead.'
73
74
  RESTRICT_ON_SEND = %i[to_sym intern].freeze
74
75
 
75
76
  def on_send(node)
@@ -138,10 +139,6 @@ module RuboCop
138
139
  node.parent&.array_type? && node.parent&.percent_literal?
139
140
  end
140
141
 
141
- def hash_key?(node)
142
- node.parent&.pair_type? && node == node.parent.child_nodes.first
143
- end
144
-
145
142
  def correct_hash_key(node)
146
143
  # Although some operators can be converted to symbols normally
147
144
  # (ie. `:==`), these are not accepted as hash keys and will
@@ -167,7 +164,7 @@ module RuboCop
167
164
  next if requires_quotes?(key)
168
165
  next if properly_quoted?(key.source, %("#{key.value}"))
169
166
 
170
- correction = "#{quote_type}#{key.value}#{quote_type}"
167
+ correction = %("#{key.value}")
171
168
  register_offense(
172
169
  key,
173
170
  correction: correction,
@@ -175,13 +172,6 @@ module RuboCop
175
172
  )
176
173
  end
177
174
  end
178
-
179
- def quote_type
180
- # Use the `Style/StringLiterals` configuration for quoting symbols
181
- return '"' unless config.for_cop('Style/StringLiterals')['Enabled']
182
-
183
- config.for_cop('Style/StringLiterals')['EnforcedStyle'] == 'single_quotes' ? "'" : '"'
184
- end
185
175
  end
186
176
  end
187
177
  end
@@ -143,7 +143,7 @@ module RuboCop
143
143
 
144
144
  def message_for_underscore_prefix(variable)
145
145
  "If it's necessary, use `_` or `_#{variable.name}` " \
146
- "as an argument name to indicate that it won't be used."
146
+ "as an argument name to indicate that it won't be used."
147
147
  end
148
148
 
149
149
  def define_method_call?(variable)
@@ -85,7 +85,7 @@ module RuboCop
85
85
  return unless assignment.meta_assignment_node.equal?(return_value_node)
86
86
 
87
87
  " Use `#{assignment.operator.sub(/=$/, '')}` " \
88
- "instead of `#{assignment.operator}`."
88
+ "instead of `#{assignment.operator}`."
89
89
  end
90
90
 
91
91
  def similar_name_message(variable)
@@ -50,7 +50,7 @@ module RuboCop
50
50
  ->(node) { heredoc_node?(node) }
51
51
  else
52
52
  raise ArgumentError, "Unknown foldable type: #{type.inspect}. "\
53
- "Valid foldable types are: #{FOLDABLE_TYPES.join(', ')}."
53
+ "Valid foldable types are: #{FOLDABLE_TYPES.join(', ')}."
54
54
  end
55
55
  end
56
56
  end
@@ -61,7 +61,9 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def valid_content_token?(content_token)
64
- /\W+/.match?(content_token) || DISABLING_COPS_CONTENT_TOKEN.match?(content_token)
64
+ /\W+/.match?(content_token) ||
65
+ DISABLING_COPS_CONTENT_TOKEN.match?(content_token) ||
66
+ Registry.global.department?(content_token)
65
67
  end
66
68
 
67
69
  def contain_unexpected_character_for_department_name?(name)
@@ -72,7 +72,9 @@ module RuboCop
72
72
 
73
73
  # If a `send` node is not parenthesized, don't move the first element, because it
74
74
  # can result in changed behavior or a syntax error.
75
- elements = elements.drop(1) if node.send_type? && !node.parenthesized?
75
+ if node.send_type? && !node.parenthesized? && !first_argument_is_heredoc?(node)
76
+ elements = elements.drop(1)
77
+ end
76
78
 
77
79
  i = 0
78
80
  i += 1 while within_column_limit?(elements[i], max, line)
@@ -84,6 +86,13 @@ module RuboCop
84
86
  elements[i - 1]
85
87
  end
86
88
 
89
+ # @api private
90
+ def first_argument_is_heredoc?(node)
91
+ first_argument = node.first_argument
92
+
93
+ first_argument.respond_to?(:heredoc?) && first_argument.heredoc?
94
+ end
95
+
87
96
  # @api private
88
97
  # If a send node contains a heredoc argument, splitting cannot happen
89
98
  # after the heredoc or else it will cause a syntax error.
@@ -43,7 +43,7 @@ module RuboCop
43
43
  end
44
44
 
45
45
  def value_delta(pair)
46
- return 0 if pair.kwsplat_type? || pair.value_on_new_line?
46
+ return 0 if pair.value_on_new_line?
47
47
 
48
48
  correct_value_column = pair.loc.operator.end.column + 1
49
49
  actual_value_column = pair.value.loc.column
@@ -108,8 +108,6 @@ module RuboCop
108
108
  end
109
109
 
110
110
  def value_delta(first_pair, current_pair)
111
- return 0 if current_pair.kwsplat_type?
112
-
113
111
  correct_value_column = first_pair.key.loc.column +
114
112
  current_pair.delimiter(true).length +
115
113
  max_key_width
@@ -139,6 +137,19 @@ module RuboCop
139
137
  first_pair.value_delta(current_pair)
140
138
  end
141
139
  end
140
+
141
+ # Handles calculation of deltas for `kwsplat` nodes.
142
+ # This is a special case that just ensures the kwsplat is aligned with the rest of the hash
143
+ # since a `kwsplat` does not have a key, separator or value.
144
+ class KeywordSplatAlignment
145
+ def deltas(first_pair, current_pair)
146
+ if Util.begins_its_line?(current_pair.source_range)
147
+ { key: first_pair.key_delta(current_pair) }
148
+ else
149
+ {}
150
+ end
151
+ end
152
+ end
142
153
  end
143
154
  end
144
155
  end
@@ -4,12 +4,10 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for cops checking single/double quotes.
6
6
  module StringLiteralsHelp
7
- include StringHelp
8
-
9
7
  private
10
8
 
11
- def wrong_quotes?(node)
12
- src = node.source
9
+ def wrong_quotes?(src_or_node)
10
+ src = src_or_node.is_a?(RuboCop::AST::Node) ? src_or_node.source : src_or_node
13
11
  return false if src.start_with?('%', '?')
14
12
 
15
13
  if style == :single_quotes
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Classes that include this module just implement functions for working
6
+ # with symbol nodes.
7
+ module SymbolHelp
8
+ def hash_key?(node)
9
+ node.parent&.pair_type? && node == node.parent.child_nodes.first
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,249 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Naming
6
+ # This cops recommends the use of inclusive language instead of problematic terms.
7
+ # The cop can check the following locations for offenses:
8
+ # - identifiers
9
+ # - constants
10
+ # - variables
11
+ # - strings
12
+ # - symbols
13
+ # - comments
14
+ # - file paths
15
+ # Each of these locations can be individually enabled/disabled via configuration,
16
+ # for example CheckIdentifiers = true/false.
17
+ #
18
+ # Flagged terms are configurable for the cop. For each flagged term an optional
19
+ # Regex can be specified to identify offenses. Suggestions for replacing a flagged term can
20
+ # be configured and will be displayed as part of the offense message.
21
+ # An AllowedRegex can be specified for a flagged term to exempt allowed uses of the term.
22
+ #
23
+ # @example FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }
24
+ # # Suggest replacing identifier whitelist with allowlist
25
+ #
26
+ # # bad
27
+ # whitelist_users = %w(user1 user1)
28
+ #
29
+ # # good
30
+ # allowlist_users = %w(user1 user2)
31
+ #
32
+ # @example FlaggedTerms: { master: { Suggestions: ['main', 'primary', 'leader'] } }
33
+ # # Suggest replacing master in an instance variable name with main, primary, or leader
34
+ #
35
+ # # bad
36
+ # @master_node = 'node1.example.com'
37
+ #
38
+ # # good
39
+ # @primary_node = 'node1.example.com'
40
+ #
41
+ # @example FlaggedTerms: { whitelist: { Regex: !ruby/regexp '/white[-_\s]?list' } }
42
+ # # Identify problematic terms using a Regexp
43
+ #
44
+ # # bad
45
+ # white_list = %w(user1 user2)
46
+ #
47
+ # # good
48
+ # allow_list = %w(user1 user2)
49
+ #
50
+ # @example FlaggedTerms: { master: { AllowedRegex: 'master\'?s degree' } }
51
+ # # Specify allowed uses of the flagged term as a string or regexp.
52
+ #
53
+ # # bad
54
+ # # They had a masters
55
+ #
56
+ # # good
57
+ # # They had a master's degree
58
+ #
59
+ class InclusiveLanguage < Base
60
+ include RangeHelp
61
+
62
+ EMPTY_ARRAY = [].freeze
63
+
64
+ WordLocation = Struct.new(:word, :position)
65
+
66
+ def initialize(config = nil, options = nil)
67
+ super
68
+ @flagged_term_hash = {}
69
+ @flagged_terms_regex = nil
70
+ @allowed_regex = nil
71
+ @check_token = preprocess_check_config
72
+ preprocess_flagged_terms
73
+ end
74
+
75
+ def on_new_investigation
76
+ investigate_filepath if cop_config['CheckFilepaths']
77
+ investigate_tokens
78
+ end
79
+
80
+ private
81
+
82
+ def investigate_tokens
83
+ processed_source.each_token do |token|
84
+ next unless check_token?(token.type)
85
+
86
+ word_locations = scan_for_words(token.text)
87
+ next if word_locations.empty?
88
+
89
+ add_offenses_for_token(token, word_locations)
90
+ end
91
+ end
92
+
93
+ def add_offenses_for_token(token, word_locations)
94
+ word_locations.each do |word_location|
95
+ start_position = token.pos.begin_pos + token.pos.source.index(word_location.word)
96
+ range = range_between(start_position, start_position + word_location.word.length)
97
+ add_offense(range, message: create_message(word_location.word))
98
+ end
99
+ end
100
+
101
+ def check_token?(type)
102
+ !!@check_token[type]
103
+ end
104
+
105
+ def preprocess_check_config # rubocop:disable Metrics/AbcSize
106
+ {
107
+ tIDENTIFIER: cop_config['CheckIdentifiers'],
108
+ tCONSTANT: cop_config['CheckConstants'],
109
+ tIVAR: cop_config['CheckVariables'],
110
+ tCVAR: cop_config['CheckVariables'],
111
+ tGVAR: cop_config['CheckVariables'],
112
+ tSYMBOL: cop_config['CheckSymbols'],
113
+ tSTRING: cop_config['CheckStrings'],
114
+ tSTRING_CONTENT: cop_config['CheckStrings'],
115
+ tCOMMENT: cop_config['CheckComments']
116
+ }.freeze
117
+ end
118
+
119
+ def preprocess_flagged_terms
120
+ allowed_strings = []
121
+ flagged_term_strings = []
122
+ cop_config['FlaggedTerms'].each do |term, term_definition|
123
+ next if term_definition.nil?
124
+
125
+ allowed_strings.concat(process_allowed_regex(term_definition['AllowedRegex']))
126
+ regex_string = ensure_regex_string(term_definition['Regex'] || term)
127
+ flagged_term_strings << regex_string
128
+
129
+ add_to_flagged_term_hash(regex_string, term, term_definition)
130
+ end
131
+
132
+ set_regexes(flagged_term_strings, allowed_strings)
133
+ end
134
+
135
+ def add_to_flagged_term_hash(regex_string, term, term_definition)
136
+ @flagged_term_hash[Regexp.new(regex_string, Regexp::IGNORECASE)] =
137
+ term_definition.merge('Term' => term,
138
+ 'SuggestionString' =>
139
+ preprocess_suggestions(term_definition['Suggestions']))
140
+ end
141
+
142
+ def set_regexes(flagged_term_strings, allowed_strings)
143
+ @flagged_terms_regex = array_to_ignorecase_regex(flagged_term_strings)
144
+ @allowed_regex = array_to_ignorecase_regex(allowed_strings) unless allowed_strings.empty?
145
+ end
146
+
147
+ def process_allowed_regex(allowed)
148
+ return EMPTY_ARRAY if allowed.nil?
149
+
150
+ Array(allowed).map do |allowed_term|
151
+ next if allowed_term.is_a?(String) && allowed_term.strip.empty?
152
+
153
+ ensure_regex_string(allowed_term)
154
+ end
155
+ end
156
+
157
+ def ensure_regex_string(regex)
158
+ regex.is_a?(Regexp) ? regex.source : regex
159
+ end
160
+
161
+ def array_to_ignorecase_regex(strings)
162
+ Regexp.new(strings.join('|'), Regexp::IGNORECASE)
163
+ end
164
+
165
+ def investigate_filepath
166
+ word_locations = scan_for_words(processed_source.file_path)
167
+
168
+ case word_locations.length
169
+ when 0
170
+ return
171
+ when 1
172
+ message = create_single_word_message_for_file(word_locations.first.word)
173
+ else
174
+ words = word_locations.map(&:word)
175
+ message = create_multiple_word_message_for_file(words)
176
+ end
177
+
178
+ range = source_range(processed_source.buffer, 1, 0)
179
+ add_offense(range, message: message)
180
+ end
181
+
182
+ def create_single_word_message_for_file(word)
183
+ create_message(word).sub(/\.$/, ' in file path.')
184
+ end
185
+
186
+ def create_multiple_word_message_for_file(words)
187
+ quoted_words = words.map { |word| "'#{word}'" }
188
+ "Consider replacing problematic terms #{quoted_words.join(', ')} in file path."
189
+ end
190
+
191
+ def scan_for_words(input)
192
+ mask_input(input).enum_for(:scan, @flagged_terms_regex).map do
193
+ match = Regexp.last_match
194
+ WordLocation.new(match.to_s, match.offset(0).first)
195
+ end
196
+ end
197
+
198
+ def mask_input(str)
199
+ return str if @allowed_regex.nil?
200
+
201
+ safe_str = if str.valid_encoding?
202
+ str
203
+ else
204
+ str.encode('UTF-8', invalid: :replace, undef: :replace)
205
+ end
206
+ safe_str.gsub(@allowed_regex) { |match| '*' * match.size }
207
+ end
208
+
209
+ def create_message(word)
210
+ flagged_term = find_flagged_term(word)
211
+ "Consider replacing problematic term '#{word}'#{flagged_term['SuggestionString']}."
212
+ end
213
+
214
+ def find_flagged_term(word)
215
+ _regexp, flagged_term = @flagged_term_hash.find do |key, _term|
216
+ key.match?(word)
217
+ end
218
+ flagged_term
219
+ end
220
+
221
+ def create_message_for_file(word)
222
+ create_message(word).sub(/\.$/, ' in file path.')
223
+ end
224
+
225
+ def preprocess_suggestions(suggestions)
226
+ return '' if suggestions.nil? ||
227
+ (suggestions.is_a?(String) && suggestions.strip.empty?) || suggestions.empty?
228
+
229
+ format_suggestions(suggestions)
230
+ end
231
+
232
+ def format_suggestions(suggestions)
233
+ quoted_suggestions = Array(suggestions).map { |word| "'#{word}'" }
234
+ suggestion_str = case quoted_suggestions.size
235
+ when 1
236
+ quoted_suggestions.first
237
+ when 2
238
+ quoted_suggestions.join(' or ')
239
+ else
240
+ last_quoted = quoted_suggestions.pop
241
+ quoted_suggestions << "or #{last_quoted}"
242
+ quoted_suggestions.join(', ')
243
+ end
244
+ " with #{suggestion_str}"
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end