rubocop 1.36.0 → 1.40.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +78 -12
- data/exe/rubocop +1 -1
- data/lib/rubocop/arguments_env.rb +17 -0
- data/lib/rubocop/arguments_file.rb +17 -0
- data/lib/rubocop/cli/command/execute_runner.rb +7 -7
- data/lib/rubocop/cli/command/suggest_extensions.rb +8 -1
- data/lib/rubocop/comment_config.rb +41 -1
- data/lib/rubocop/config.rb +5 -4
- data/lib/rubocop/config_loader.rb +5 -5
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/base.rb +2 -9
- data/lib/rubocop/cop/commissioner.rb +3 -1
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +22 -6
- data/lib/rubocop/cop/generator.rb +1 -2
- data/lib/rubocop/cop/internal_affairs/create_empty_file.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +111 -0
- data/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb +46 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +29 -8
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +13 -9
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +30 -3
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +34 -0
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +6 -2
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +11 -1
- data/lib/rubocop/cop/lint/deprecated_constants.rb +8 -1
- data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +28 -9
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +25 -6
- data/lib/rubocop/cop/lint/empty_block.rb +1 -5
- data/lib/rubocop/cop/lint/empty_class.rb +3 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +21 -9
- data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +18 -3
- data/lib/rubocop/cop/lint/nested_method_definition.rb +50 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +5 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +36 -4
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +38 -10
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +18 -8
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +5 -4
- data/lib/rubocop/cop/lint/shadowed_exception.rb +0 -10
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -3
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +4 -0
- data/lib/rubocop/cop/lint/void.rb +6 -6
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +9 -4
- data/lib/rubocop/cop/metrics/class_length.rb +9 -4
- data/lib/rubocop/cop/metrics/method_length.rb +9 -4
- data/lib/rubocop/cop/metrics/module_length.rb +9 -4
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +5 -2
- data/lib/rubocop/cop/mixin/comments_help.rb +12 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +30 -8
- data/lib/rubocop/cop/mixin/range_help.rb +23 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +3 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +15 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -8
- data/lib/rubocop/cop/mixin/visibility_help.rb +40 -5
- data/lib/rubocop/cop/naming/inclusive_language.rb +1 -1
- data/lib/rubocop/cop/registry.rb +32 -14
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +5 -7
- data/lib/rubocop/cop/style/accessor_grouping.rb +7 -3
- data/lib/rubocop/cop/style/array_intersect.rb +111 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -2
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +8 -6
- data/lib/rubocop/cop/style/collection_compact.rb +12 -3
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/endless_method.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +4 -0
- data/lib/rubocop/cop/style/format_string_token.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +90 -22
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +32 -10
- data/lib/rubocop/cop/style/hash_except.rb +4 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +25 -2
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +13 -2
- data/lib/rubocop/cop/style/module_function.rb +28 -6
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +7 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/object_then.rb +3 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +53 -0
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +3 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +5 -2
- data/lib/rubocop/cop/style/redundant_constant_base.rb +72 -0
- data/lib/rubocop/cop/style/redundant_each.rb +116 -0
- data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +8 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +12 -3
- data/lib/rubocop/cop/style/redundant_return.rb +7 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +181 -0
- data/lib/rubocop/cop/style/require_order.rb +88 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +35 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +8 -4
- data/lib/rubocop/cop/style/static_class.rb +32 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -5
- data/lib/rubocop/cop/style/symbol_array.rb +2 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +2 -0
- data/lib/rubocop/cop/team.rb +4 -5
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
- data/lib/rubocop/cop/variable_force.rb +20 -29
- data/lib/rubocop/cops_documentation_generator.rb +2 -1
- data/lib/rubocop/ext/processed_source.rb +2 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +25 -8
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +8 -5
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +6 -3
- data/lib/rubocop/formatter.rb +3 -1
- data/lib/rubocop/optimized_patterns.rb +38 -0
- data/lib/rubocop/options.rb +28 -16
- data/lib/rubocop/path_util.rb +14 -2
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/cop_helper.rb +24 -1
- data/lib/rubocop/rspec/shared_contexts.rb +14 -1
- data/lib/rubocop/rspec/support.rb +2 -2
- data/lib/rubocop/runner.rb +15 -11
- data/lib/rubocop/server/cache.rb +5 -1
- data/lib/rubocop/server/cli.rb +9 -2
- data/lib/rubocop/server/client_command/exec.rb +5 -0
- data/lib/rubocop/server/core.rb +19 -2
- data/lib/rubocop/server/socket_reader.rb +5 -1
- data/lib/rubocop/server.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +8 -3
- data/lib/rubocop.rb +18 -6
- metadata +18 -5
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
|
45
45
|
def on_regexp(node)
|
46
46
|
each_escape(node) do |char, index, within_character_class|
|
47
|
-
next if allowed_escape?(node, char, within_character_class)
|
47
|
+
next if allowed_escape?(node, char, index, within_character_class)
|
48
48
|
|
49
49
|
location = escape_range_at_index(node, index)
|
50
50
|
|
@@ -56,7 +56,7 @@ module RuboCop
|
|
56
56
|
|
57
57
|
private
|
58
58
|
|
59
|
-
def allowed_escape?(node, char, within_character_class)
|
59
|
+
def allowed_escape?(node, char, index, within_character_class)
|
60
60
|
# Strictly speaking a few single-letter metachars are currently
|
61
61
|
# unnecessary to "escape", e.g. i, E, F, but enumerating them is
|
62
62
|
# rather difficult, and their behavior could change over time with
|
@@ -65,12 +65,21 @@ module RuboCop
|
|
65
65
|
return true if ALLOWED_ALWAYS_ESCAPES.include?(char) || delimiter?(node, char)
|
66
66
|
|
67
67
|
if within_character_class
|
68
|
-
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
68
|
+
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char) &&
|
69
|
+
!char_class_begins_or_ends_with_escaped_hyphen?(node, index)
|
69
70
|
else
|
70
71
|
ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
75
|
+
def char_class_begins_or_ends_with_escaped_hyphen?(node, index)
|
76
|
+
# The hyphen character is allowed to be escaped within a character class
|
77
|
+
# but it's not necessry to escape hyphen if it's the first or last character
|
78
|
+
# within the character class. This method checks if that's the case.
|
79
|
+
# e.g. "[0-9\\-]" or "[\\-0-9]" would return true
|
80
|
+
node.source[index] == '[' || node.source[index + 3] == ']'
|
81
|
+
end
|
82
|
+
|
74
83
|
def delimiter?(node, char)
|
75
84
|
delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
|
76
85
|
|
@@ -53,6 +53,13 @@ module RuboCop
|
|
53
53
|
|
54
54
|
MSG = 'Redundant `return` detected.'
|
55
55
|
MULTI_RETURN_MSG = 'To return multiple values, use an array.'
|
56
|
+
RESTRICT_ON_SEND = %i[define_method define_singleton_method].freeze
|
57
|
+
|
58
|
+
def on_send(node)
|
59
|
+
return unless (parent = node.parent) && parent.block_type?
|
60
|
+
|
61
|
+
check_branch(parent.body)
|
62
|
+
end
|
56
63
|
|
57
64
|
def on_def(node)
|
58
65
|
check_branch(node.body)
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for redundant escapes in string literals.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad - no need to escape # without following {/$/@
|
10
|
+
# "\#foo"
|
11
|
+
#
|
12
|
+
# # bad - no need to escape single quotes inside double quoted string
|
13
|
+
# "\'foo\'"
|
14
|
+
#
|
15
|
+
# # bad - heredocs are also checked for unnecessary escapes
|
16
|
+
# <<~STR
|
17
|
+
# \#foo \"foo\"
|
18
|
+
# STR
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# "#foo"
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# "\#{no_interpolation}"
|
25
|
+
#
|
26
|
+
# # good
|
27
|
+
# "'foo'"
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# "foo\
|
31
|
+
# bar"
|
32
|
+
#
|
33
|
+
# # good
|
34
|
+
# <<~STR
|
35
|
+
# #foo "foo"
|
36
|
+
# STR
|
37
|
+
class RedundantStringEscape < Base
|
38
|
+
include MatchRange
|
39
|
+
include RangeHelp
|
40
|
+
extend AutoCorrector
|
41
|
+
|
42
|
+
MSG = 'Redundant escape of %<char>s inside string literal.'
|
43
|
+
|
44
|
+
def on_str(node)
|
45
|
+
return if node.parent&.regexp_type? || node.parent&.xstr_type? || node.character_literal?
|
46
|
+
|
47
|
+
str_contents_range = str_contents_range(node)
|
48
|
+
|
49
|
+
each_match_range(str_contents_range, /(\\.)/) do |range|
|
50
|
+
next if allowed_escape?(node, range.resize(3))
|
51
|
+
|
52
|
+
add_offense(range) do |corrector|
|
53
|
+
corrector.remove_leading(range, 1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def message(range)
|
61
|
+
format(MSG, char: range.source.chars.last)
|
62
|
+
end
|
63
|
+
|
64
|
+
def str_contents_range(node)
|
65
|
+
if heredoc?(node)
|
66
|
+
node.loc.heredoc_body
|
67
|
+
elsif begin_loc_present?(node)
|
68
|
+
contents_range(node)
|
69
|
+
else
|
70
|
+
node.loc.expression
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def begin_loc_present?(node)
|
75
|
+
# e.g. a __FILE__ literal has no begin loc so we can't query if it's nil
|
76
|
+
node.loc.to_hash.key?(:begin) && !node.loc.begin.nil?
|
77
|
+
end
|
78
|
+
|
79
|
+
def allowed_escape?(node, range)
|
80
|
+
escaped = range.source[(1..-1)]
|
81
|
+
|
82
|
+
# Inside a single-quoted string, escapes (except \\ and \') do not have special meaning,
|
83
|
+
# and so are not redundant, as they are a literal backslash.
|
84
|
+
return true if interpolation_not_enabled?(node)
|
85
|
+
|
86
|
+
# Strictly speaking a few single-letter chars are currently unnecessary to "escape", e.g.
|
87
|
+
# d, but enumerating them is rather difficult, and their behavior could change over time
|
88
|
+
# with different versions of Ruby so that e.g. /\d/ != /d/
|
89
|
+
return true if /[\n\\[[:alnum:]]]/.match?(escaped[0])
|
90
|
+
|
91
|
+
return true if escaped[0] == ' ' && percent_array_literal?(node)
|
92
|
+
|
93
|
+
return true if disabling_interpolation?(range)
|
94
|
+
return true if delimiter?(node, escaped[0])
|
95
|
+
|
96
|
+
false
|
97
|
+
end
|
98
|
+
|
99
|
+
def interpolation_not_enabled?(node)
|
100
|
+
single_quoted?(node) ||
|
101
|
+
percent_w_literal?(node) ||
|
102
|
+
percent_q_literal?(node) ||
|
103
|
+
heredoc_with_disabled_interpolation?(node)
|
104
|
+
end
|
105
|
+
|
106
|
+
def single_quoted?(node)
|
107
|
+
delimiter?(node, "'")
|
108
|
+
end
|
109
|
+
|
110
|
+
def percent_q_literal?(node)
|
111
|
+
if literal_in_interpolated_or_multiline_string?(node)
|
112
|
+
percent_q_literal?(node.parent)
|
113
|
+
else
|
114
|
+
node.source.start_with?('%q')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def array_literal?(node, prefix)
|
119
|
+
if literal_in_interpolated_or_multiline_string?(node)
|
120
|
+
array_literal?(node.parent, prefix)
|
121
|
+
else
|
122
|
+
node.parent&.array_type? && node.parent.source.start_with?(prefix)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def percent_w_literal?(node)
|
127
|
+
array_literal?(node, '%w')
|
128
|
+
end
|
129
|
+
|
130
|
+
def percent_w_upper_literal?(node)
|
131
|
+
array_literal?(node, '%W')
|
132
|
+
end
|
133
|
+
|
134
|
+
def percent_array_literal?(node)
|
135
|
+
(percent_w_literal?(node) || percent_w_upper_literal?(node))
|
136
|
+
end
|
137
|
+
|
138
|
+
def heredoc_with_disabled_interpolation?(node)
|
139
|
+
if heredoc?(node)
|
140
|
+
node.loc.expression.source.end_with?("'")
|
141
|
+
elsif node.parent&.dstr_type?
|
142
|
+
heredoc_with_disabled_interpolation?(node.parent)
|
143
|
+
else
|
144
|
+
false
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def heredoc?(node)
|
149
|
+
(node.str_type? || node.dstr_type?) && node.heredoc?
|
150
|
+
end
|
151
|
+
|
152
|
+
def delimiter?(node, char)
|
153
|
+
return false if heredoc?(node)
|
154
|
+
|
155
|
+
if literal_in_interpolated_or_multiline_string?(node) || percent_array_literal?(node)
|
156
|
+
return delimiter?(node.parent, char)
|
157
|
+
end
|
158
|
+
|
159
|
+
delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
|
160
|
+
|
161
|
+
delimiters.include?(char)
|
162
|
+
end
|
163
|
+
|
164
|
+
def literal_in_interpolated_or_multiline_string?(node)
|
165
|
+
node.str_type? && !begin_loc_present?(node) && node.parent&.dstr_type?
|
166
|
+
end
|
167
|
+
|
168
|
+
def disabling_interpolation?(range)
|
169
|
+
# Allow \#{foo}, \#$foo, \#@foo, and \#@@foo
|
170
|
+
# for escaping local, global, instance and class variable interpolations
|
171
|
+
return true if range.source.match?(/\A\\#[{$@]/)
|
172
|
+
|
173
|
+
# Also allow #\{foo}, #\$foo, #\@foo and #\@@foo
|
174
|
+
return true if range.adjust(begin_pos: -2).source.match?(/\A[^\\]#\\[{$@]/)
|
175
|
+
|
176
|
+
false
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Sort `require` and `require_relative` in alphabetical order.
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# This cop's autocorrection is unsafe because it will obviously change the execution order.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# # bad
|
13
|
+
# require 'b'
|
14
|
+
# require 'a'
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# require 'a'
|
18
|
+
# require 'b'
|
19
|
+
#
|
20
|
+
# # bad
|
21
|
+
# require_relative 'b'
|
22
|
+
# require_relative 'a'
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# require_relative 'a'
|
26
|
+
# require_relative 'b'
|
27
|
+
#
|
28
|
+
# # good (sorted within each section separated by a blank line)
|
29
|
+
# require 'a'
|
30
|
+
# require 'd'
|
31
|
+
#
|
32
|
+
# require 'b'
|
33
|
+
# require 'c'
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# require 'b'
|
37
|
+
# require_relative 'c'
|
38
|
+
# require 'a'
|
39
|
+
class RequireOrder < Base
|
40
|
+
extend AutoCorrector
|
41
|
+
|
42
|
+
include RangeHelp
|
43
|
+
|
44
|
+
RESTRICT_ON_SEND = %i[require require_relative].freeze
|
45
|
+
|
46
|
+
def on_send(node)
|
47
|
+
previous_older_sibling = find_previous_older_sibling(node)
|
48
|
+
return unless previous_older_sibling
|
49
|
+
|
50
|
+
add_offense(
|
51
|
+
node,
|
52
|
+
message: "Sort `#{node.method_name}` in alphabetical order."
|
53
|
+
) do |corrector|
|
54
|
+
swap(
|
55
|
+
range_with_comments_and_lines(previous_older_sibling),
|
56
|
+
range_with_comments_and_lines(node),
|
57
|
+
corrector: corrector
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def find_previous_older_sibling(node)
|
65
|
+
node.left_siblings.reverse.find do |sibling|
|
66
|
+
break unless sibling.send_type?
|
67
|
+
break unless sibling.method?(node.method_name)
|
68
|
+
break unless in_same_section?(sibling, node)
|
69
|
+
|
70
|
+
node.first_argument.source < sibling.first_argument.source
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def in_same_section?(node1, node2)
|
75
|
+
!node1.location.expression.with(
|
76
|
+
end_pos: node2.location.expression.end_pos
|
77
|
+
).source.include?("\n\n")
|
78
|
+
end
|
79
|
+
|
80
|
+
def swap(range1, range2, corrector:)
|
81
|
+
inserted = range2.source
|
82
|
+
corrector.insert_before(range1, inserted)
|
83
|
+
corrector.remove(range2)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# reasons:
|
10
10
|
#
|
11
11
|
# * The syntax of modifier form `rescue` can be misleading because it
|
12
|
-
# might
|
12
|
+
# might lead us to believe that `rescue` handles the given exception
|
13
13
|
# but it actually rescue all exceptions to return the given rescue
|
14
14
|
# block. In this case, value returned by handle_error or
|
15
15
|
# SomeException.
|
@@ -50,6 +50,11 @@ module RuboCop
|
|
50
50
|
# foo && foo.bar { |e| e.something }
|
51
51
|
# foo && foo.bar(param) { |e| e.something }
|
52
52
|
#
|
53
|
+
# foo ? foo.bar : nil
|
54
|
+
# foo.nil? ? nil : foo.bar
|
55
|
+
# !foo.nil? ? foo.bar : nil
|
56
|
+
# !foo ? nil : foo.bar
|
57
|
+
#
|
53
58
|
# # good
|
54
59
|
# foo&.bar
|
55
60
|
# foo&.bar&.baz
|
@@ -105,6 +110,17 @@ module RuboCop
|
|
105
110
|
}
|
106
111
|
PATTERN
|
107
112
|
|
113
|
+
# @!method ternary_safe_navigation_candidate(node)
|
114
|
+
def_node_matcher :ternary_safe_navigation_candidate, <<~PATTERN
|
115
|
+
{
|
116
|
+
(if (send $_ {:nil? :!}) nil $_)
|
117
|
+
|
118
|
+
(if (send (send $_ :nil?) :!) $_ nil)
|
119
|
+
|
120
|
+
(if $_ $_ nil)
|
121
|
+
}
|
122
|
+
PATTERN
|
123
|
+
|
108
124
|
# @!method not_nil_check?(node)
|
109
125
|
def_node_matcher :not_nil_check?, '(send (send $_ :nil?) :!)'
|
110
126
|
|
@@ -118,9 +134,11 @@ module RuboCop
|
|
118
134
|
check_node(node)
|
119
135
|
end
|
120
136
|
|
137
|
+
private
|
138
|
+
|
121
139
|
def check_node(node)
|
122
140
|
checked_variable, receiver, method_chain, method = extract_parts(node)
|
123
|
-
return
|
141
|
+
return if receiver != checked_variable || receiver.nil?
|
124
142
|
return if use_var_only_in_unless_modifier?(node, checked_variable)
|
125
143
|
return if chain_length(method_chain, method) > max_chain_length
|
126
144
|
return if unsafe_method_used?(method_chain, method)
|
@@ -133,10 +151,8 @@ module RuboCop
|
|
133
151
|
node.if_type? && node.unless? && !method_called?(variable)
|
134
152
|
end
|
135
153
|
|
136
|
-
private
|
137
|
-
|
138
154
|
def autocorrect(corrector, node)
|
139
|
-
body = node
|
155
|
+
body = extract_body(node)
|
140
156
|
method_call = method_call(node)
|
141
157
|
|
142
158
|
corrector.remove(begin_range(node, body))
|
@@ -147,6 +163,14 @@ module RuboCop
|
|
147
163
|
add_safe_nav_to_all_methods_in_chain(corrector, method_call, body)
|
148
164
|
end
|
149
165
|
|
166
|
+
def extract_body(node)
|
167
|
+
if node.if_type? && node.ternary?
|
168
|
+
node.branches.find { |branch| !branch.nil_type? }
|
169
|
+
else
|
170
|
+
node.node_parts[1]
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
150
174
|
def handle_comments(corrector, node, method_call)
|
151
175
|
comments = comments(node)
|
152
176
|
return if comments.empty?
|
@@ -174,7 +198,7 @@ module RuboCop
|
|
174
198
|
end
|
175
199
|
|
176
200
|
def allowed_if_condition?(node)
|
177
|
-
node.else? || node.elsif?
|
201
|
+
node.else? || node.elsif?
|
178
202
|
end
|
179
203
|
|
180
204
|
def method_call(node)
|
@@ -192,7 +216,12 @@ module RuboCop
|
|
192
216
|
end
|
193
217
|
|
194
218
|
def extract_parts_from_if(node)
|
195
|
-
variable, receiver =
|
219
|
+
variable, receiver =
|
220
|
+
if node.ternary?
|
221
|
+
ternary_safe_navigation_candidate(node)
|
222
|
+
else
|
223
|
+
modifier_if_safe_navigation_candidate(node)
|
224
|
+
end
|
196
225
|
|
197
226
|
checked_variable, matching_receiver, method = extract_common_parts(receiver, variable)
|
198
227
|
|
@@ -49,7 +49,8 @@ module RuboCop
|
|
49
49
|
MSG = 'Prefer `%<replacement>s` to `%<original_method>s` with a regexp match.'
|
50
50
|
RESTRICT_ON_SEND = %i[select find_all reject].freeze
|
51
51
|
REPLACEMENTS = { select: 'grep', find_all: 'grep', reject: 'grep_v' }.freeze
|
52
|
-
|
52
|
+
OPPOSITE_REPLACEMENTS = { select: 'grep_v', find_all: 'grep_v', reject: 'grep' }.freeze
|
53
|
+
REGEXP_METHODS = %i[match? =~ !~].to_set.freeze
|
53
54
|
|
54
55
|
# @!method regexp_match?(node)
|
55
56
|
def_node_matcher :regexp_match?, <<~PATTERN
|
@@ -90,8 +91,10 @@ module RuboCop
|
|
90
91
|
return unless (regexp_method_send_node = extract_send_node(block_node))
|
91
92
|
return if match_predicate_without_receiver?(regexp_method_send_node)
|
92
93
|
|
94
|
+
opposite = regexp_method_send_node.send_type? && regexp_method_send_node.method?(:!~)
|
93
95
|
regexp = find_regexp(regexp_method_send_node, block_node)
|
94
|
-
|
96
|
+
|
97
|
+
register_offense(node, block_node, regexp, opposite)
|
95
98
|
end
|
96
99
|
|
97
100
|
private
|
@@ -102,8 +105,9 @@ module RuboCop
|
|
102
105
|
node.hash_type? || creates_hash?(node) || env_const?(node)
|
103
106
|
end
|
104
107
|
|
105
|
-
def register_offense(node, block_node, regexp)
|
106
|
-
|
108
|
+
def register_offense(node, block_node, regexp, opposite)
|
109
|
+
method_name = node.method_name.to_sym
|
110
|
+
replacement = opposite ? OPPOSITE_REPLACEMENTS[method_name] : REPLACEMENTS[method_name]
|
107
111
|
message = format(MSG, replacement: replacement, original_method: node.method_name)
|
108
112
|
|
109
113
|
add_offense(block_node, message: message) do |corrector|
|
@@ -44,18 +44,49 @@ module RuboCop
|
|
44
44
|
# end
|
45
45
|
#
|
46
46
|
class StaticClass < Base
|
47
|
+
include RangeHelp
|
47
48
|
include VisibilityHelp
|
49
|
+
extend AutoCorrector
|
48
50
|
|
49
51
|
MSG = 'Prefer modules to classes with only class methods.'
|
50
52
|
|
51
53
|
def on_class(class_node)
|
52
54
|
return if class_node.parent_class
|
55
|
+
return unless class_convertible_to_module?(class_node)
|
53
56
|
|
54
|
-
add_offense(class_node)
|
57
|
+
add_offense(class_node) do |corrector|
|
58
|
+
autocorrect(corrector, class_node)
|
59
|
+
end
|
55
60
|
end
|
56
61
|
|
57
62
|
private
|
58
63
|
|
64
|
+
def autocorrect(corrector, class_node)
|
65
|
+
corrector.replace(class_node.loc.keyword, 'module')
|
66
|
+
corrector.insert_after(class_node.loc.name, "\nmodule_function\n")
|
67
|
+
|
68
|
+
class_elements(class_node).each do |node|
|
69
|
+
if node.defs_type?
|
70
|
+
autocorrect_def(corrector, node)
|
71
|
+
elsif node.sclass_type?
|
72
|
+
autocorrect_sclass(corrector, node)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def autocorrect_def(corrector, node)
|
78
|
+
corrector.remove(
|
79
|
+
range_between(node.receiver.source_range.begin_pos, node.loc.name.begin_pos)
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
def autocorrect_sclass(corrector, node)
|
84
|
+
corrector.remove(
|
85
|
+
range_between(node.loc.keyword.begin_pos, node.identifier.source_range.end_pos)
|
86
|
+
)
|
87
|
+
corrector.remove(node.loc.end)
|
88
|
+
end
|
89
|
+
|
59
90
|
def class_convertible_to_module?(class_node)
|
60
91
|
nodes = class_elements(class_node)
|
61
92
|
return false if nodes.empty?
|
@@ -95,11 +95,7 @@ module RuboCop
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def offense?(node)
|
98
|
-
|
99
|
-
# for this cop.
|
100
|
-
return false if inside_interpolation?(node)
|
101
|
-
|
102
|
-
wrong_quotes?(node)
|
98
|
+
wrong_quotes?(node) && !inside_interpolation?(node)
|
103
99
|
end
|
104
100
|
|
105
101
|
def consistent_multiline?
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
#
|
8
8
|
# If you prefer a style that allows block for method with arguments,
|
9
9
|
# please set `true` to `AllowMethodsWithArguments`.
|
10
|
-
#
|
10
|
+
# `define_method?` methods are allowed by default.
|
11
11
|
# These are customizable with `AllowedMethods` option.
|
12
12
|
#
|
13
13
|
# @safety
|
@@ -72,17 +72,15 @@ module RuboCop
|
|
72
72
|
# # some comment
|
73
73
|
# end
|
74
74
|
#
|
75
|
-
# @example AllowedMethods: [
|
75
|
+
# @example AllowedMethods: [define_method] (default)
|
76
76
|
# # good
|
77
|
-
# respond_to { |foo| foo.bar }
|
78
77
|
# define_method(:foo) { |foo| foo.bar }
|
79
78
|
#
|
80
|
-
#
|
81
79
|
# @example AllowedPatterns: [] (default)
|
82
80
|
# # bad
|
83
81
|
# something.map { |s| s.upcase }
|
84
82
|
#
|
85
|
-
# @example AllowedPatterns: [
|
83
|
+
# @example AllowedPatterns: ['map'] (default)
|
86
84
|
# # good
|
87
85
|
# something.map { |s| s.upcase }
|
88
86
|
#
|
data/lib/rubocop/cop/team.rb
CHANGED
@@ -38,12 +38,11 @@ module RuboCop
|
|
38
38
|
new(cops, config, options)
|
39
39
|
end
|
40
40
|
|
41
|
-
# @return [Array<Cop::
|
41
|
+
# @return [Array<Cop::Base>]
|
42
42
|
def self.mobilize_cops(cop_classes, config, options = {})
|
43
|
-
cop_classes = Registry.new(cop_classes.to_a) unless cop_classes.is_a?(Registry)
|
44
|
-
|
45
|
-
|
46
|
-
cop_classes.enabled(config, only, only_safe: safe).map do |cop_class|
|
43
|
+
cop_classes = Registry.new(cop_classes.to_a, options) unless cop_classes.is_a?(Registry)
|
44
|
+
|
45
|
+
cop_classes.enabled(config).map do |cop_class|
|
47
46
|
cop_class.new(config, options)
|
48
47
|
end
|
49
48
|
end
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -122,7 +122,7 @@ module RuboCop
|
|
122
122
|
string.inspect
|
123
123
|
else
|
124
124
|
# In a single-quoted strings, double quotes don't need to be escaped
|
125
|
-
"'#{string.gsub('
|
125
|
+
"'#{string.gsub('\\') { '\\\\' }.gsub('\"', '"')}'"
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
@@ -159,7 +159,7 @@ module RuboCop
|
|
159
159
|
private
|
160
160
|
|
161
161
|
def compatible_external_encoding_for?(src)
|
162
|
-
src = src.dup if
|
162
|
+
src = src.dup if RUBY_ENGINE == 'jruby'
|
163
163
|
src.force_encoding(Encoding.default_external).valid_encoding?
|
164
164
|
end
|
165
165
|
end
|
@@ -83,7 +83,7 @@ module RuboCop
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def multiple_assignment_node
|
86
|
-
grandparent_node = node.parent
|
86
|
+
grandparent_node = node.parent&.parent
|
87
87
|
return nil unless grandparent_node
|
88
88
|
return nil unless grandparent_node.type == MULTIPLE_ASSIGNMENT_TYPE
|
89
89
|
return nil unless node.parent.type == MULTIPLE_LEFT_HAND_SIDE_TYPE
|
@@ -109,7 +109,7 @@ module RuboCop
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def accessible_variables
|
112
|
-
scope_stack.reverse_each.
|
112
|
+
scope_stack.reverse_each.with_object([]) do |scope, variables|
|
113
113
|
variables.concat(scope.variables.values)
|
114
114
|
break variables unless scope.node.block_type?
|
115
115
|
end
|