rubocop 0.42.0 → 0.43.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/assets/output.html.erb +21 -10
- data/config/default.yml +32 -2
- data/config/disabled.yml +8 -1
- data/config/enabled.yml +40 -12
- data/lib/rubocop.rb +14 -2
- data/lib/rubocop/ast_node.rb +2 -0
- data/lib/rubocop/cached_data.rb +13 -11
- data/lib/rubocop/cli.rb +5 -5
- data/lib/rubocop/config.rb +68 -24
- data/lib/rubocop/config_loader.rb +13 -11
- data/lib/rubocop/config_loader_resolver.rb +4 -2
- data/lib/rubocop/cop/cop.rb +16 -5
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
- data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
- data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
- data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
- data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
- data/lib/rubocop/cop/lint/rand_one.rb +3 -3
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
- data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
- data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
- data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
- data/lib/rubocop/cop/lint/void.rb +2 -4
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
- data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
- data/lib/rubocop/cop/mixin/def_node.rb +28 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
- data/lib/rubocop/cop/mixin/if_node.rb +6 -0
- data/lib/rubocop/cop/mixin/match_range.rb +2 -5
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
- data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
- data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
- data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
- data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
- data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
- data/lib/rubocop/cop/performance/casecmp.rb +28 -16
- data/lib/rubocop/cop/performance/count.rb +58 -34
- data/lib/rubocop/cop/performance/detect.rb +3 -7
- data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
- data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
- data/lib/rubocop/cop/performance/flat_map.rb +16 -9
- data/lib/rubocop/cop/performance/hash_each.rb +2 -3
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
- data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
- data/lib/rubocop/cop/performance/size.rb +21 -8
- data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
- data/lib/rubocop/cop/rails/delegate.rb +2 -3
- data/lib/rubocop/cop/rails/find_by.rb +4 -8
- data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
- data/lib/rubocop/cop/rails/request_referer.rb +3 -3
- data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
- data/lib/rubocop/cop/rails/save_bang.rb +78 -9
- data/lib/rubocop/cop/rails/scope_args.rb +3 -1
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/security/json_load.rb +36 -0
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/align_hash.rb +25 -14
- data/lib/rubocop/cop/style/and_or.rb +13 -3
- data/lib/rubocop/cop/style/array_join.rb +3 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/block_comments.rb +2 -6
- data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
- data/lib/rubocop/cop/style/case_indentation.rb +1 -3
- data/lib/rubocop/cop/style/class_methods.rb +3 -4
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +15 -8
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
- data/lib/rubocop/cop/style/copyright.rb +1 -5
- data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
- data/lib/rubocop/cop/style/documentation.rb +28 -56
- data/lib/rubocop/cop/style/documentation_method.rb +80 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/else_alignment.rb +10 -9
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
- data/lib/rubocop/cop/style/empty_else.rb +1 -4
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
- data/lib/rubocop/cop/style/encoding.rb +28 -14
- data/lib/rubocop/cop/style/even_odd.rb +28 -17
- data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
- data/lib/rubocop/cop/style/file_name.rb +19 -10
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
- data/lib/rubocop/cop/style/for.rb +12 -8
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +22 -56
- data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
- data/lib/rubocop/cop/style/indentation_width.rb +30 -16
- data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
- data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
- data/lib/rubocop/cop/style/inline_comment.rb +16 -3
- data/lib/rubocop/cop/style/lambda.rb +22 -10
- data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
- data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
- data/lib/rubocop/cop/style/method_missing.rb +10 -2
- data/lib/rubocop/cop/style/module_function.rb +14 -6
- data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
- data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
- data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +3 -3
- data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
- data/lib/rubocop/cop/style/next.rb +4 -4
- data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
- data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
- data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
- data/lib/rubocop/cop/style/op_method.rb +1 -1
- data/lib/rubocop/cop/style/option_hash.rb +8 -8
- data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
- data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
- data/lib/rubocop/cop/style/redundant_return.rb +5 -5
- data/lib/rubocop/cop/style/redundant_self.rb +20 -11
- data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
- data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
- data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +9 -10
- data/lib/rubocop/cop/style/signal_exception.rb +2 -4
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
- data/lib/rubocop/cop/style/space_after_not.rb +4 -6
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
- data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
- data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
- data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
- data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
- data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/symbol_array.rb +10 -10
- data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
- data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
- data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
- data/lib/rubocop/cop/style/unless_else.rb +2 -6
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
- data/lib/rubocop/cop/style/variable_number.rb +79 -0
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +25 -15
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
- data/lib/rubocop/cop/util.rb +23 -4
- data/lib/rubocop/cop/variable_force.rb +59 -25
- data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
- data/lib/rubocop/cop/variable_force/variable.rb +2 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
- data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
- data/lib/rubocop/formatter/formatter_set.rb +12 -10
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
- data/lib/rubocop/node_pattern.rb +79 -35
- data/lib/rubocop/options.rb +4 -4
- data/lib/rubocop/processed_source.rb +9 -5
- data/lib/rubocop/remote_config.rb +14 -10
- data/lib/rubocop/result_cache.rb +14 -6
- data/lib/rubocop/runner.rb +55 -34
- data/lib/rubocop/string_util.rb +9 -5
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/token.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +15 -4
- data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
- data/lib/rubocop/cop/performance/push_splat.rb +0 -47
@@ -69,14 +69,14 @@ module RuboCop
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def check_node(node)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
72
|
+
return unless node && node.begin_type?
|
73
|
+
|
74
|
+
clear
|
75
|
+
check_scope(node)
|
76
|
+
|
77
|
+
@useless.each do |_name, (defs_node, visibility, modifier)|
|
78
|
+
add_offense(defs_node, :keyword,
|
79
|
+
format_message(visibility, modifier))
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -4,9 +4,10 @@
|
|
4
4
|
module RuboCop
|
5
5
|
module Cop
|
6
6
|
module Lint
|
7
|
-
# This cop looks for error classes inheriting from `Exception
|
8
|
-
#
|
9
|
-
# `StandardError
|
7
|
+
# This cop looks for error classes inheriting from `Exception`
|
8
|
+
# and its standard library subclasses, excluding subclasses of
|
9
|
+
# `StandardError`. It is configurable to suggest using either
|
10
|
+
# `RuntimeError` (default) or `StandardError` instead.
|
10
11
|
#
|
11
12
|
# @example
|
12
13
|
#
|
@@ -32,11 +33,24 @@ module RuboCop
|
|
32
33
|
class InheritException < Cop
|
33
34
|
include ConfigurableEnforcedStyle
|
34
35
|
|
35
|
-
MSG = 'Inherit from `%s` instead of
|
36
|
+
MSG = 'Inherit from `%s` instead of `%s`.'.freeze
|
36
37
|
PREFERRED_BASE_CLASS = {
|
37
38
|
runtime_error: 'RuntimeError',
|
38
39
|
standard_error: 'StandardError'
|
39
40
|
}.freeze
|
41
|
+
ILLEGAL_CLASSES = %w(
|
42
|
+
Exception
|
43
|
+
SystemStackError
|
44
|
+
NoMemoryError
|
45
|
+
SecurityError
|
46
|
+
NotImplementedError
|
47
|
+
LoadError
|
48
|
+
SyntaxError
|
49
|
+
ScriptError
|
50
|
+
Interrupt
|
51
|
+
SignalException
|
52
|
+
SystemExit
|
53
|
+
).freeze
|
40
54
|
|
41
55
|
def on_class(node)
|
42
56
|
_class, base_class, _body = *node
|
@@ -49,9 +63,10 @@ module RuboCop
|
|
49
63
|
private
|
50
64
|
|
51
65
|
def check(node)
|
52
|
-
|
53
|
-
|
54
|
-
|
66
|
+
return unless ILLEGAL_CLASSES.include?(node.const_name)
|
67
|
+
|
68
|
+
msg = format(MSG, preferred_base_class, node.const_name)
|
69
|
+
add_offense(node, :expression, msg)
|
55
70
|
end
|
56
71
|
|
57
72
|
def autocorrect(node)
|
@@ -73,7 +73,7 @@ module RuboCop
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def not?(node)
|
76
|
-
return false unless node && node.
|
76
|
+
return false unless node && node.send_type?
|
77
77
|
|
78
78
|
_receiver, method_name, *_args = *node
|
79
79
|
|
@@ -81,7 +81,7 @@ module RuboCop
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def basic_literal?(node)
|
84
|
-
if node && node.
|
84
|
+
if node && node.array_type?
|
85
85
|
primitive_array?(node)
|
86
86
|
else
|
87
87
|
node.basic_literal?
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
operands.each do |op|
|
105
105
|
handle_node(op)
|
106
106
|
end
|
107
|
-
elsif node.
|
107
|
+
elsif node.begin_type? && node.children.one?
|
108
108
|
child_node = node.children.first
|
109
109
|
handle_node(child_node)
|
110
110
|
end
|
@@ -119,8 +119,8 @@ module RuboCop
|
|
119
119
|
end
|
120
120
|
|
121
121
|
def check_case_cond(node)
|
122
|
-
return if node.
|
123
|
-
return if node.
|
122
|
+
return if node.array_type? && !primitive_array?(node)
|
123
|
+
return if node.dstr_type?
|
124
124
|
|
125
125
|
handle_node(node)
|
126
126
|
end
|
@@ -14,7 +14,7 @@ module RuboCop
|
|
14
14
|
COMPOSITE = [:array, :hash, :pair, :irange, :erange].freeze
|
15
15
|
|
16
16
|
def on_dstr(node)
|
17
|
-
node.
|
17
|
+
node.each_child_node(:begin) do |begin_node|
|
18
18
|
final_node = begin_node.children.last
|
19
19
|
next unless final_node
|
20
20
|
next if special_keyword?(final_node)
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
|
36
36
|
def special_keyword?(node)
|
37
37
|
# handle strings like __FILE__
|
38
|
-
(node.
|
38
|
+
(node.str_type? && !node.loc.respond_to?(:begin)) ||
|
39
39
|
node.source_range.is?('__LINE__')
|
40
40
|
end
|
41
41
|
|
@@ -54,9 +54,7 @@ module RuboCop
|
|
54
54
|
end_pos =
|
55
55
|
node.loc.end ? node.loc.end.begin_pos : node.loc.expression.end_pos
|
56
56
|
|
57
|
-
|
58
|
-
node.loc.begin.end_pos,
|
59
|
-
end_pos).source
|
57
|
+
range_between(node.loc.begin.end_pos, end_pos).source
|
60
58
|
end
|
61
59
|
|
62
60
|
# Does node print its own source when converted to a string?
|
@@ -15,19 +15,16 @@ module RuboCop
|
|
15
15
|
|
16
16
|
def on_send(node)
|
17
17
|
_receiver, method_name, *args = *node
|
18
|
-
return
|
18
|
+
return unless args.one?
|
19
19
|
return if operator?(method_name) || node.asgn_method_call?
|
20
|
-
|
20
|
+
|
21
|
+
first_arg = args.first
|
22
|
+
return unless first_arg.source.start_with?('(')
|
21
23
|
|
22
24
|
space_length = spaces_before_left_parenthesis(node)
|
23
25
|
return unless space_length > 0
|
24
|
-
expr = args.first.source_range
|
25
|
-
space_range =
|
26
|
-
Parser::Source::Range.new(expr.source_buffer,
|
27
|
-
expr.begin_pos - space_length,
|
28
|
-
expr.begin_pos)
|
29
26
|
|
30
|
-
add_offense(nil, space_range)
|
27
|
+
add_offense(nil, space_range(first_arg.source_range, space_length))
|
31
28
|
end
|
32
29
|
|
33
30
|
private
|
@@ -47,6 +44,10 @@ module RuboCop
|
|
47
44
|
match = without_receiver.match(/^\s*\.?\s*#{method_regexp}(\s+)\(/)
|
48
45
|
match ? match.captures[0].length : 0
|
49
46
|
end
|
47
|
+
|
48
|
+
def space_range(expr, space_length)
|
49
|
+
range_between(expr.begin_pos - space_length, expr.begin_pos)
|
50
|
+
end
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
@@ -18,13 +18,13 @@ module RuboCop
|
|
18
18
|
'unwanted in the resulting strings.'.freeze
|
19
19
|
|
20
20
|
def on_array(node)
|
21
|
-
process(node,
|
21
|
+
process(node, '%w', '%W')
|
22
22
|
end
|
23
23
|
|
24
24
|
def on_percent_literal(node)
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
return unless contains_quotes_or_commas?(node)
|
26
|
+
|
27
|
+
add_offense(node, :expression, MSG)
|
28
28
|
end
|
29
29
|
|
30
30
|
private
|
@@ -33,10 +33,10 @@ module RuboCop
|
|
33
33
|
patterns = [/,$/, /^'.*'$/, /^".*"$/]
|
34
34
|
|
35
35
|
node.children.any? do |child|
|
36
|
-
literal = child.children.first
|
36
|
+
literal = scrub_string(child.children.first.to_s)
|
37
37
|
|
38
38
|
# To avoid likely false positives (e.g. a single ' or ")
|
39
|
-
next if literal.
|
39
|
+
next if literal.gsub(/[^\p{Alnum}]/, '').empty?
|
40
40
|
|
41
41
|
patterns.any? { |pat| literal =~ pat }
|
42
42
|
end
|
@@ -54,6 +54,17 @@ module RuboCop
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
|
+
|
58
|
+
def scrub_string(string)
|
59
|
+
if string.respond_to?(:scrub)
|
60
|
+
string.scrub
|
61
|
+
else
|
62
|
+
string
|
63
|
+
.encode('UTF-16BE', 'UTF-8',
|
64
|
+
invalid: :replace, undef: :replace, replace: '?')
|
65
|
+
.encode('UTF-8')
|
66
|
+
end
|
67
|
+
end
|
57
68
|
end
|
58
69
|
end
|
59
70
|
end
|
@@ -18,13 +18,13 @@ module RuboCop
|
|
18
18
|
'unwanted in the resulting symbols.'.freeze
|
19
19
|
|
20
20
|
def on_array(node)
|
21
|
-
process(node,
|
21
|
+
process(node, '%i', '%I')
|
22
22
|
end
|
23
23
|
|
24
24
|
def on_percent_literal(node)
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
return unless contains_colons_or_commas?(node)
|
26
|
+
|
27
|
+
add_offense(node, :expression, MSG)
|
28
28
|
end
|
29
29
|
|
30
30
|
private
|
@@ -26,9 +26,9 @@ module RuboCop
|
|
26
26
|
PATTERN
|
27
27
|
|
28
28
|
def on_send(node)
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
return unless rand_one?(node)
|
30
|
+
|
31
|
+
add_offense(node, :expression, format(MSG, node.source))
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -46,9 +46,7 @@ module RuboCop
|
|
46
46
|
return unless offense?(condition)
|
47
47
|
|
48
48
|
expr = node.source_range
|
49
|
-
range =
|
50
|
-
expr.begin_pos,
|
51
|
-
condition.source_range.end_pos)
|
49
|
+
range = range_between(expr.begin_pos, condition.source_range.end_pos)
|
52
50
|
add_offense(range, range)
|
53
51
|
end
|
54
52
|
|
@@ -8,24 +8,25 @@ module RuboCop
|
|
8
8
|
# less specific exception being rescued before a more specific
|
9
9
|
# exception is rescued.
|
10
10
|
#
|
11
|
-
#
|
12
|
-
# # bad
|
13
|
-
# begin
|
14
|
-
# something
|
15
|
-
# rescue Exception
|
16
|
-
# handle_exception
|
17
|
-
# rescue StandardError
|
18
|
-
# handle_standard_error
|
19
|
-
# end
|
11
|
+
# @example
|
20
12
|
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
13
|
+
# # bad
|
14
|
+
# begin
|
15
|
+
# something
|
16
|
+
# rescue Exception
|
17
|
+
# handle_exception
|
18
|
+
# rescue StandardError
|
19
|
+
# handle_standard_error
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# begin
|
24
|
+
# something
|
25
|
+
# rescue StandardError
|
26
|
+
# handle_standard_error
|
27
|
+
# rescue Exception
|
28
|
+
# handle_exception
|
29
|
+
# end
|
29
30
|
class ShadowedException < Cop
|
30
31
|
MSG = 'Do not shadow rescued Exceptions'.freeze
|
31
32
|
|
@@ -43,20 +44,25 @@ module RuboCop
|
|
43
44
|
end
|
44
45
|
|
45
46
|
return if !rescue_group_rescues_multiple_levels &&
|
46
|
-
|
47
|
+
sorted?(rescued_groups)
|
47
48
|
|
48
|
-
add_offense(node, offense_range(
|
49
|
+
add_offense(node, offense_range(rescues))
|
49
50
|
end
|
50
51
|
|
51
52
|
private
|
52
53
|
|
53
|
-
def offense_range(
|
54
|
+
def offense_range(rescues)
|
54
55
|
first_rescue = rescues.first
|
55
56
|
last_rescue = rescues.last
|
56
57
|
last_exceptions, = *last_rescue
|
57
|
-
|
58
|
-
|
59
|
-
|
58
|
+
# last_rescue clause may not specify exception class
|
59
|
+
end_pos = if last_exceptions
|
60
|
+
last_exceptions.loc.expression.end_pos
|
61
|
+
else
|
62
|
+
last_rescue.loc.keyword.end_pos
|
63
|
+
end
|
64
|
+
|
65
|
+
range_between(first_rescue.loc.expression.begin_pos, end_pos)
|
60
66
|
end
|
61
67
|
|
62
68
|
def rescue_modifier?(node)
|
@@ -72,20 +78,7 @@ module RuboCop
|
|
72
78
|
return !(group.size == 2 && group.include?(NilClass))
|
73
79
|
end
|
74
80
|
|
75
|
-
group.any?
|
76
|
-
higher_exception = false
|
77
|
-
group.each_with_index do |_e, i|
|
78
|
-
higher_exception ||= begin
|
79
|
-
if group[i].nil? || exception.nil?
|
80
|
-
false
|
81
|
-
else
|
82
|
-
group[i] < exception
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
higher_exception
|
88
|
-
end
|
81
|
+
group.combination(2).any? { |a, b| a && b && a <=> b }
|
89
82
|
end
|
90
83
|
|
91
84
|
def evaluate_exceptions(rescue_group)
|
@@ -104,16 +97,18 @@ module RuboCop
|
|
104
97
|
end
|
105
98
|
end
|
106
99
|
|
107
|
-
def
|
108
|
-
|
100
|
+
def sorted?(rescued_groups)
|
101
|
+
rescued_groups.each_cons(2).all? do |x, y|
|
109
102
|
if x.include?(Exception)
|
110
|
-
|
103
|
+
false
|
111
104
|
elsif y.include?(Exception)
|
112
|
-
|
113
|
-
elsif x.
|
114
|
-
|
105
|
+
true
|
106
|
+
elsif x.none? || y.none?
|
107
|
+
# consider sorted if a group is empty or only contains
|
108
|
+
# `nil`s
|
109
|
+
true
|
115
110
|
else
|
116
|
-
x <=> y || 0
|
111
|
+
(x <=> y || 0) <= 0
|
117
112
|
end
|
118
113
|
end
|
119
114
|
end
|
@@ -16,9 +16,9 @@ module RuboCop
|
|
16
16
|
'interpolation.'.freeze
|
17
17
|
|
18
18
|
def on_dstr(node)
|
19
|
-
node.
|
19
|
+
node.each_child_node(:begin) do |begin_node|
|
20
20
|
final_node = begin_node.children.last
|
21
|
-
next unless final_node && final_node.
|
21
|
+
next unless final_node && final_node.send_type?
|
22
22
|
|
23
23
|
receiver, method_name, *args = *final_node
|
24
24
|
next unless method_name == :to_s && args.empty?
|
@@ -21,12 +21,11 @@ module RuboCop
|
|
21
21
|
|
22
22
|
def check_variable(variable)
|
23
23
|
return unless variable.should_be_unused?
|
24
|
-
return if variable.references.empty?
|
25
24
|
return if variable.references.none?(&:explicit?)
|
26
25
|
|
27
26
|
node = variable.declaration_node
|
28
27
|
|
29
|
-
location = if node.
|
28
|
+
location = if node.match_with_lvasgn_type?
|
30
29
|
node.children.first.source_range
|
31
30
|
else
|
32
31
|
node.loc.name
|