rubocop 1.14.0 → 1.18.0
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 +4 -4
- data/README.md +3 -1
- data/config/default.yml +120 -34
- data/lib/rubocop.rb +8 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
- data/lib/rubocop/config_loader.rb +1 -1
- data/lib/rubocop/config_validator.rb +5 -5
- data/lib/rubocop/cop/base.rb +2 -2
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/bundler/gem_version.rb +38 -4
- data/lib/rubocop/cop/corrector.rb +4 -4
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
- data/lib/rubocop/cop/layout/argument_alignment.rb +30 -12
- data/lib/rubocop/cop/layout/array_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +57 -9
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +7 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +13 -15
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +14 -2
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +40 -14
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +13 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +122 -0
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/parameter_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -9
- data/lib/rubocop/cop/layout/space_around_keyword.rb +28 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +7 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -0
- data/lib/rubocop/cop/lint/empty_block.rb +18 -2
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +13 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +32 -17
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +105 -74
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -13
- data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/migration/department_name.rb +3 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +28 -3
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +6 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +14 -3
- data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -4
- data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
- data/lib/rubocop/cop/naming/inclusive_language.rb +249 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +28 -2
- data/lib/rubocop/cop/style/empty_literal.rb +8 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +18 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +58 -8
- data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
- data/lib/rubocop/cop/style/multiline_when_then.rb +2 -11
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +29 -12
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/quoted_symbols.rb +110 -0
- data/lib/rubocop/cop/style/raise_args.rb +2 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +24 -2
- data/lib/rubocop/cop/style/regexp_literal.rb +10 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/string_concatenation.rb +32 -5
- data/lib/rubocop/cop/style/string_literals.rb +3 -2
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -0
- data/lib/rubocop/cop/style/swap_values.rb +1 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +83 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +65 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +6 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
- data/lib/rubocop/directive_comment.rb +58 -6
- data/lib/rubocop/formatter/junit_formatter.rb +21 -6
- data/lib/rubocop/options.rb +18 -24
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/remote_config.rb +10 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +13 -5
@@ -35,6 +35,12 @@ module RuboCop
|
|
35
35
|
leading_comment_lines.any? { |line| MagicComment.parse(line).frozen_string_literal? }
|
36
36
|
end
|
37
37
|
|
38
|
+
def frozen_string_literals_disabled?
|
39
|
+
leading_comment_lines.any? do |line|
|
40
|
+
MagicComment.parse(line).frozen_string_literal == false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
38
44
|
def frozen_string_literal_specified?
|
39
45
|
leading_comment_lines.any? do |line|
|
40
46
|
MagicComment.parse(line).frozen_string_literal_specified?
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def value_delta(pair)
|
46
|
-
return 0 if pair.
|
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?(
|
12
|
-
src =
|
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
|
@@ -147,9 +147,9 @@ module RuboCop
|
|
147
147
|
include ConfigurableEnforcedStyle
|
148
148
|
|
149
149
|
MSG = 'Memoized variable `%<var>s` does not match ' \
|
150
|
-
|
150
|
+
'method name `%<method>s`. Use `@%<suggested_var>s` instead.'
|
151
151
|
UNDERSCORE_REQUIRED = 'Memoized variable `%<var>s` does not start ' \
|
152
|
-
|
152
|
+
'with `_`. Use `@%<suggested_var>s` instead.'
|
153
153
|
DYNAMIC_DEFINE_METHODS = %i[define_method define_singleton_method].to_set.freeze
|
154
154
|
|
155
155
|
# @!method method_definition?(node)
|
@@ -84,12 +84,22 @@ module RuboCop
|
|
84
84
|
def compact_definition(corrector, node)
|
85
85
|
compact_node(corrector, node)
|
86
86
|
remove_end(corrector, node.body)
|
87
|
+
unindent(corrector, node)
|
87
88
|
end
|
88
89
|
|
89
90
|
def compact_node(corrector, node)
|
90
|
-
replacement = "#{node.body.type} #{compact_identifier_name(node)}"
|
91
91
|
range = range_between(node.loc.keyword.begin_pos, node.body.loc.name.end_pos)
|
92
|
-
corrector.replace(range,
|
92
|
+
corrector.replace(range, compact_replacement(node))
|
93
|
+
end
|
94
|
+
|
95
|
+
def compact_replacement(node)
|
96
|
+
replacement = "#{node.body.type} #{compact_identifier_name(node)}"
|
97
|
+
|
98
|
+
body_comments = processed_source.ast_with_comments[node.body]
|
99
|
+
unless body_comments.empty?
|
100
|
+
replacement = body_comments.map(&:text).push(replacement).join("\n")
|
101
|
+
end
|
102
|
+
replacement
|
93
103
|
end
|
94
104
|
|
95
105
|
def compact_identifier_name(node)
|
@@ -105,6 +115,19 @@ module RuboCop
|
|
105
115
|
corrector.remove(range)
|
106
116
|
end
|
107
117
|
|
118
|
+
def configured_indentation_width
|
119
|
+
config.for_badge(Layout::IndentationWidth.badge).fetch('Width', 2)
|
120
|
+
end
|
121
|
+
|
122
|
+
def unindent(corrector, node)
|
123
|
+
return if node.body.children.last.nil?
|
124
|
+
|
125
|
+
column_delta = configured_indentation_width - leading_spaces(node.body.children.last).size
|
126
|
+
return if column_delta.zero?
|
127
|
+
|
128
|
+
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
129
|
+
end
|
130
|
+
|
108
131
|
def leading_spaces(node)
|
109
132
|
node.source_range.source_line[/\A\s*/]
|
110
133
|
end
|
@@ -132,6 +155,9 @@ module RuboCop
|
|
132
155
|
end
|
133
156
|
|
134
157
|
def check_compact_style(node, body)
|
158
|
+
parent = node.parent
|
159
|
+
return if parent&.class_type? || parent&.module_type?
|
160
|
+
|
135
161
|
return unless needs_compacting?(body)
|
136
162
|
|
137
163
|
add_offense(node.loc.name, message: COMPACT_MSG) do |corrector|
|
@@ -62,7 +62,7 @@ module RuboCop
|
|
62
62
|
ARR_MSG
|
63
63
|
elsif offense_hash_node?(node)
|
64
64
|
HASH_MSG
|
65
|
-
elsif str_node(node) && !
|
65
|
+
elsif str_node(node) && !frozen_strings?
|
66
66
|
format(STR_MSG, prefer: preferred_string_literal)
|
67
67
|
end
|
68
68
|
end
|
@@ -125,6 +125,13 @@ module RuboCop
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|
128
|
+
|
129
|
+
def frozen_strings?
|
130
|
+
return true if frozen_string_literals_enabled?
|
131
|
+
|
132
|
+
frozen_string_cop_enabled = config.for_cop('Style/FrozenStringLiteral')['Enabled']
|
133
|
+
frozen_string_cop_enabled && !frozen_string_literals_disabled?
|
134
|
+
end
|
128
135
|
end
|
129
136
|
end
|
130
137
|
end
|
@@ -17,6 +17,11 @@ module RuboCop
|
|
17
17
|
# # good
|
18
18
|
# hash.each_key { |k| p k }
|
19
19
|
# hash.each_value { |v| p v }
|
20
|
+
#
|
21
|
+
# @example AllowedReceivers: ['execute']
|
22
|
+
# # good
|
23
|
+
# execute(sql).keys.each { |v| p v }
|
24
|
+
# execute(sql).values.each { |v| p v }
|
20
25
|
class HashEachMethods < Base
|
21
26
|
include Lint::UnusedArgument
|
22
27
|
extend AutoCorrector
|
@@ -36,7 +41,9 @@ module RuboCop
|
|
36
41
|
|
37
42
|
def register_kv_offense(node)
|
38
43
|
kv_each(node) do |target, method|
|
39
|
-
|
44
|
+
parent_receiver = target.receiver.receiver
|
45
|
+
return unless parent_receiver
|
46
|
+
return if allowed_receiver?(parent_receiver)
|
40
47
|
|
41
48
|
msg = format(message, prefer: "each_#{method[0..-2]}", current: "#{method}.each")
|
42
49
|
|
@@ -80,6 +87,16 @@ module RuboCop
|
|
80
87
|
def kv_range(outer_node)
|
81
88
|
outer_node.receiver.loc.selector.join(outer_node.loc.selector)
|
82
89
|
end
|
90
|
+
|
91
|
+
def allowed_receiver?(receiver)
|
92
|
+
receiver_name = receiver.send_type? ? receiver.method_name.to_s : receiver.source
|
93
|
+
|
94
|
+
allowed_receivers.include?(receiver_name)
|
95
|
+
end
|
96
|
+
|
97
|
+
def allowed_receivers
|
98
|
+
cop_config.fetch('AllowedReceivers', [])
|
99
|
+
end
|
83
100
|
end
|
84
101
|
end
|
85
102
|
end
|