rbhint 0.85.1.rc1 → 0.85.1.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/assets/output.html.erb +1 -1
- data/config/default.yml +13 -1
- data/lib/rbhint.rb +8 -0
- data/lib/rbhint/version.rb +1 -1
- data/lib/rubocop.rb +2 -0
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/assignment_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +4 -4
- data/lib/rubocop/cop/layout/condition_position.rb +1 -1
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +6 -6
- data/lib/rubocop/cop/layout/space_around_operators.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -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/raise_exception.rb +4 -4
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/copyright.rb +3 -3
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/utils/format_string.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/name_similarity.rb +6 -0
- data/lib/rubocop/path_util.rb +2 -2
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +12 -2
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- metadata +10 -2
@@ -148,7 +148,7 @@ module RuboCop
|
|
148
148
|
end
|
149
149
|
|
150
150
|
next_token = processed_source.tokens[token_number]
|
151
|
-
token = next_token if
|
151
|
+
token = next_token if Encoding::ENCODING_PATTERN.match?(next_token&.text)
|
152
152
|
|
153
153
|
token
|
154
154
|
end
|
@@ -81,7 +81,7 @@ module RuboCop
|
|
81
81
|
|
82
82
|
private
|
83
83
|
|
84
|
-
def check_branches(branches)
|
84
|
+
def check_branches(branches) # rubocop:todo Metrics/CyclomaticComplexity
|
85
85
|
# return if any branch is empty. An empty branch can be an `if`
|
86
86
|
# without an `else` or a branch that contains only comments.
|
87
87
|
return if branches.any?(&:nil?)
|
@@ -61,7 +61,7 @@ module RuboCop
|
|
61
61
|
class IfInsideElse < Cop
|
62
62
|
MSG = 'Convert `if` nested inside `else` to `elsif`.'
|
63
63
|
|
64
|
-
def on_if(node)
|
64
|
+
def on_if(node) # rubocop:todo Metrics/CyclomaticComplexity
|
65
65
|
return if node.ternary? || node.unless?
|
66
66
|
|
67
67
|
else_branch = node.else_branch
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
34
34
|
# shortcut out if the string does not look like an IP address
|
35
35
|
return false unless could_be_ip?(contents)
|
36
36
|
|
37
|
-
|
37
|
+
::Resolv::IPv4::Regex.match?(contents) || ::Resolv::IPv6::Regex.match?(contents)
|
38
38
|
end
|
39
39
|
|
40
40
|
# Dummy implementation of method in ConfigurableEnforcedStyle that is
|
@@ -17,12 +17,11 @@ module RuboCop
|
|
17
17
|
#
|
18
18
|
# # good
|
19
19
|
# a = cond ? b : c
|
20
|
-
# a =
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# end
|
20
|
+
# a = if cond
|
21
|
+
# b
|
22
|
+
# else
|
23
|
+
# c
|
24
|
+
# end
|
26
25
|
class MultilineTernaryOperator < Cop
|
27
26
|
MSG = 'Avoid multi-line ternary operators, ' \
|
28
27
|
'use `if` or `unless` instead.'
|
@@ -32,6 +31,18 @@ module RuboCop
|
|
32
31
|
|
33
32
|
add_offense(node)
|
34
33
|
end
|
34
|
+
|
35
|
+
def autocorrect(node)
|
36
|
+
lambda do |corrector|
|
37
|
+
corrector.replace(node, <<~RUBY.chop)
|
38
|
+
if #{node.condition.source}
|
39
|
+
#{node.if_branch.source}
|
40
|
+
else
|
41
|
+
#{node.else_branch.source}
|
42
|
+
end
|
43
|
+
RUBY
|
44
|
+
end
|
45
|
+
end
|
35
46
|
end
|
36
47
|
end
|
37
48
|
end
|
@@ -26,6 +26,33 @@ module RuboCop
|
|
26
26
|
add_offense(nested_ternary)
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
def autocorrect(node)
|
31
|
+
if_node = if_node(node)
|
32
|
+
|
33
|
+
lambda do |corrector|
|
34
|
+
corrector.replace(if_node, <<~RUBY.chop)
|
35
|
+
if #{if_node.condition.source}
|
36
|
+
#{remove_parentheses(if_node.if_branch.source)}
|
37
|
+
else
|
38
|
+
#{if_node.else_branch.source}
|
39
|
+
end
|
40
|
+
RUBY
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def if_node(node)
|
47
|
+
node = node.parent
|
48
|
+
return node if node.if_type?
|
49
|
+
|
50
|
+
if_node(node)
|
51
|
+
end
|
52
|
+
|
53
|
+
def remove_parentheses(source)
|
54
|
+
source.gsub(/\A\(/, '').gsub(/\)\z/, '')
|
55
|
+
end
|
29
56
|
end
|
30
57
|
end
|
31
58
|
end
|
@@ -191,7 +191,7 @@ module RuboCop
|
|
191
191
|
end
|
192
192
|
|
193
193
|
def end_followed_by_whitespace_only?(source_buffer, end_pos)
|
194
|
-
source_buffer.source[end_pos..-1]
|
194
|
+
/\A\s*$/.match?(source_buffer.source[end_pos..-1])
|
195
195
|
end
|
196
196
|
|
197
197
|
def reindentable_lines(node)
|
@@ -201,7 +201,7 @@ module RuboCop
|
|
201
201
|
lines = (node.source_range.line + 1)...node.loc.end.line
|
202
202
|
lines = lines.to_a - heredoc_lines(node)
|
203
203
|
# Skip blank lines
|
204
|
-
lines.reject { |lineno| buffer.source_line(lineno)
|
204
|
+
lines.reject { |lineno| /\A\s*\z/.match?(buffer.source_line(lineno)) }
|
205
205
|
end
|
206
206
|
|
207
207
|
# Adjust indentation of `lines` to match `node`
|
@@ -77,9 +77,9 @@ module RuboCop
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def octal_literal_type(literal)
|
80
|
-
if literal
|
80
|
+
if OCTAL_ZERO_ONLY_REGEX.match?(literal) && octal_zero_only?
|
81
81
|
:octal_zero_only
|
82
|
-
elsif literal
|
82
|
+
elsif OCTAL_REGEX.match?(literal) && !octal_zero_only?
|
83
83
|
:octal
|
84
84
|
end
|
85
85
|
end
|
@@ -156,7 +156,7 @@ module RuboCop
|
|
156
156
|
source_buffer = node.source_range.source_buffer
|
157
157
|
line_range = source_buffer.line_range(node.loc.end.line)
|
158
158
|
|
159
|
-
|
159
|
+
/^\s*\)\s*,/.match?(line_range.source)
|
160
160
|
end
|
161
161
|
|
162
162
|
def disallowed_literal?(begin_node, node)
|
@@ -97,13 +97,13 @@ module RuboCop
|
|
97
97
|
|
98
98
|
return true if STRING_INTERPOLATION_REGEXP.match?(src)
|
99
99
|
|
100
|
-
src.scan(/\\./).any? { |s| s
|
100
|
+
src.scan(/\\./).any? { |s| ESCAPED_NON_BACKSLASH.match?(s) }
|
101
101
|
end
|
102
102
|
|
103
103
|
def acceptable_capital_q?(node)
|
104
104
|
src = node.source
|
105
105
|
src.include?(QUOTE) &&
|
106
|
-
(src
|
106
|
+
(STRING_INTERPOLATION_REGEXP.match?(src) ||
|
107
107
|
(node.str_type? && double_quotes_required?(src)))
|
108
108
|
end
|
109
109
|
end
|
@@ -67,8 +67,8 @@ module RuboCop
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def each_redundant_character_class(node)
|
70
|
-
|
71
|
-
yield loc
|
70
|
+
pattern_source(node).scan(PATTERN) do
|
71
|
+
yield match_range(node.loc.begin.end, Regexp.last_match)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -18,6 +18,9 @@ module RuboCop
|
|
18
18
|
# # good
|
19
19
|
# %r/foo\/bar/
|
20
20
|
#
|
21
|
+
# # good
|
22
|
+
# %r!foo\!bar!
|
23
|
+
#
|
21
24
|
# # bad
|
22
25
|
# /a\-b/
|
23
26
|
#
|
@@ -63,27 +66,30 @@ module RuboCop
|
|
63
66
|
|
64
67
|
private
|
65
68
|
|
66
|
-
def slash_literal?(node)
|
67
|
-
['/', '%r/'].include?(node.loc.begin.source)
|
68
|
-
end
|
69
|
-
|
70
69
|
def allowed_escape?(node, char, within_character_class)
|
71
70
|
# Strictly speaking a few single-letter metachars are currently
|
72
71
|
# unnecessary to "escape", e.g. g, i, E, F, but enumerating them is
|
73
72
|
# rather difficult, and their behaviour could change over time with
|
74
73
|
# different versions of Ruby so that e.g. /\g/ != /g/
|
75
74
|
return true if /[[:alnum:]]/.match?(char)
|
76
|
-
return true if ALLOWED_ALWAYS_ESCAPES.include?(char)
|
75
|
+
return true if ALLOWED_ALWAYS_ESCAPES.include?(char) || delimiter?(node, char)
|
77
76
|
|
78
|
-
if
|
79
|
-
slash_literal?(node)
|
80
|
-
elsif within_character_class
|
77
|
+
if within_character_class
|
81
78
|
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
82
79
|
else
|
83
80
|
ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
84
81
|
end
|
85
82
|
end
|
86
83
|
|
84
|
+
def delimiter?(node, char)
|
85
|
+
delimiters = [
|
86
|
+
node.loc.begin.source.chars.last,
|
87
|
+
node.loc.end.source.chars.first
|
88
|
+
]
|
89
|
+
|
90
|
+
delimiters.include?(char)
|
91
|
+
end
|
92
|
+
|
87
93
|
def each_escape(node)
|
88
94
|
pattern_source(node).each_char.with_index.reduce(
|
89
95
|
[nil, false]
|
@@ -109,21 +115,6 @@ module RuboCop
|
|
109
115
|
|
110
116
|
range_between(start, start + 2)
|
111
117
|
end
|
112
|
-
|
113
|
-
def pattern_source(node)
|
114
|
-
freespace_mode = freespace_mode_regexp?(node)
|
115
|
-
|
116
|
-
node.children.reject(&:regopt_type?).map do |child|
|
117
|
-
source = child.source
|
118
|
-
|
119
|
-
if freespace_mode
|
120
|
-
# Remove comments to avoid misleading results
|
121
|
-
source.sub(/(?<!\\)#.*/, '')
|
122
|
-
else
|
123
|
-
source
|
124
|
-
end
|
125
|
-
end.join
|
126
|
-
end
|
127
118
|
end
|
128
119
|
end
|
129
120
|
end
|
@@ -44,6 +44,11 @@ module RuboCop
|
|
44
44
|
class RedundantSelf < Cop
|
45
45
|
MSG = 'Redundant `self` detected.'
|
46
46
|
KERNEL_METHODS = Kernel.methods(false)
|
47
|
+
KEYWORDS = %i[alias and begin break case class def defined? do
|
48
|
+
else elsif end ensure false for if in module
|
49
|
+
next nil not or redo rescue retry return self
|
50
|
+
super then true undef unless until when while
|
51
|
+
yield __FILE__ __LINE__ __ENCODING__].freeze
|
47
52
|
|
48
53
|
def self.autocorrect_incompatible_with
|
49
54
|
[ColonMethodCall]
|
@@ -131,7 +136,7 @@ module RuboCop
|
|
131
136
|
|
132
137
|
def regular_method_call?(node)
|
133
138
|
!(node.operator_method? ||
|
134
|
-
|
139
|
+
KEYWORDS.include?(node.method_name) ||
|
135
140
|
node.camel_case_method? ||
|
136
141
|
node.setter_method? ||
|
137
142
|
node.implicit_call?)
|
@@ -142,14 +147,6 @@ module RuboCop
|
|
142
147
|
@local_variables_scopes[node] << name
|
143
148
|
end
|
144
149
|
|
145
|
-
def keyword?(method_name)
|
146
|
-
%i[alias and begin break case class def defined? do
|
147
|
-
else elsif end ensure false for if in module
|
148
|
-
next nil not or redo rescue retry return self
|
149
|
-
super then true undef unless until when while
|
150
|
-
yield __FILE__ __LINE__ __ENCODING__].include?(method_name)
|
151
|
-
end
|
152
|
-
|
153
150
|
def allow_self(node)
|
154
151
|
return unless node.send_type? && node.self_receiver?
|
155
152
|
|
@@ -20,6 +20,8 @@ module RuboCop
|
|
20
20
|
# end
|
21
21
|
# end
|
22
22
|
class StructInheritance < Cop
|
23
|
+
include RangeHelp
|
24
|
+
|
23
25
|
MSG = "Don't extend an instance initialized by `Struct.new`. " \
|
24
26
|
'Use a block to customize the struct.'
|
25
27
|
|
@@ -29,10 +31,29 @@ module RuboCop
|
|
29
31
|
add_offense(node, location: node.parent_class.source_range)
|
30
32
|
end
|
31
33
|
|
34
|
+
def autocorrect(node)
|
35
|
+
lambda do |corrector|
|
36
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.keyword))
|
37
|
+
corrector.replace(node.loc.operator, '=')
|
38
|
+
|
39
|
+
correct_parent(node.parent_class, corrector)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
32
43
|
def_node_matcher :struct_constructor?, <<~PATTERN
|
33
44
|
{(send (const nil? :Struct) :new ...)
|
34
45
|
(block (send (const nil? :Struct) :new ...) ...)}
|
35
46
|
PATTERN
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def correct_parent(parent, corrector)
|
51
|
+
if parent.block_type?
|
52
|
+
corrector.remove(range_with_surrounding_space(range: parent.loc.end, newlines: false))
|
53
|
+
else
|
54
|
+
corrector.insert_after(parent.loc.expression, ' do')
|
55
|
+
end
|
56
|
+
end
|
36
57
|
end
|
37
58
|
end
|
38
59
|
end
|
@@ -65,7 +65,7 @@ module RuboCop
|
|
65
65
|
def symbols_contain_spaces?(node)
|
66
66
|
node.children.any? do |sym|
|
67
67
|
content, = *sym
|
68
|
-
|
68
|
+
/ /.match?(content)
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -104,12 +104,12 @@ module RuboCop
|
|
104
104
|
)
|
105
105
|
|
106
106
|
# method name
|
107
|
-
|
107
|
+
/\A[a-zA-Z_]\w*[!?]?\z/.match?(string) ||
|
108
108
|
# instance / class variable
|
109
|
-
|
109
|
+
/\A@@?[a-zA-Z_]\w*\z/.match?(string) ||
|
110
110
|
# global variable
|
111
|
-
|
112
|
-
|
111
|
+
/\A\$[1-9]\d*\z/.match?(string) ||
|
112
|
+
/\A\$[a-zA-Z_]\w*\z/.match?(string) ||
|
113
113
|
special_gvars.include?(string) ||
|
114
114
|
redefinable_operators.include?(string)
|
115
115
|
end
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -14,7 +14,7 @@ module RuboCop
|
|
14
14
|
module_function
|
15
15
|
|
16
16
|
def comment_line?(line_source)
|
17
|
-
|
17
|
+
/^\s*#/.match?(line_source)
|
18
18
|
end
|
19
19
|
|
20
20
|
def comment_lines?(node)
|
@@ -88,7 +88,7 @@ module RuboCop
|
|
88
88
|
|
89
89
|
# Regex matches IF there is a ' or there is a \\ in the string that is
|
90
90
|
# not preceded/followed by another \\ (e.g. "\\x34") but not "\\\\".
|
91
|
-
|
91
|
+
/'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string)
|
92
92
|
end
|
93
93
|
|
94
94
|
def needs_escaping?(string)
|