rubocop 0.85.0 → 0.88.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 +25 -17
- data/bin/rubocop-profile +31 -0
- data/config/default.yml +132 -11
- data/lib/rubocop.rb +17 -1
- data/lib/rubocop/cli.rb +2 -4
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader.rb +39 -67
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/config_obsoletion.rb +0 -1
- data/lib/rubocop/config_store.rb +4 -0
- data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
- data/lib/rubocop/cop/badge.rb +1 -1
- data/lib/rubocop/cop/base.rb +407 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
- data/lib/rubocop/cop/commissioner.rb +48 -50
- data/lib/rubocop/cop/cop.rb +91 -235
- data/lib/rubocop/cop/corrector.rb +38 -115
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +7 -2
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
- data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
- data/lib/rubocop/cop/layout/class_structure.rb +2 -37
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -8
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +2 -3
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -3
- 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/legacy/corrections_proxy.rb +49 -0
- data/lib/rubocop/cop/legacy/corrector.rb +29 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +8 -2
- data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +8 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +69 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
- 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 +12 -4
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +11 -26
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +22 -0
- data/lib/rubocop/cop/metrics/class_length.rb +25 -2
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/method_length.rb +23 -0
- data/lib/rubocop/cop/metrics/module_length.rb +25 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/code_length.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
- 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/enforce_superclass.rb +3 -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/nil_methods.rb +3 -5
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -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/statement_modifier.rb +3 -3
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +6 -4
- data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
- 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/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/offense.rb +16 -2
- data/lib/rubocop/cop/registry.rb +62 -7
- data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
- data/lib/rubocop/cop/style/array_coercion.rb +63 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/case_like_if.rb +217 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +21 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +3 -3
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +2 -2
- 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/empty_literal.rb +5 -5
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +8 -10
- data/lib/rubocop/cop/style/float_division.rb +7 -10
- data/lib/rubocop/cop/style/format_string_token.rb +5 -5
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
- 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/if_unless_modifier.rb +11 -11
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
- 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/missing_else.rb +1 -11
- data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -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/mutable_constant.rb +4 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
- 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/numeric_predicate.rb +3 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
- data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
- data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +4 -3
- 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/redundant_sort.rb +3 -2
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
- data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
- data/lib/rubocop/cop/team.rb +105 -81
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/utils/format_string.rb +19 -2
- data/lib/rubocop/cop/variable_force/variable.rb +5 -3
- data/lib/rubocop/file_finder.rb +12 -12
- data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/name_similarity.rb +7 -3
- data/lib/rubocop/options.rb +15 -8
- data/lib/rubocop/path_util.rb +4 -19
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/result_cache.rb +12 -8
- data/lib/rubocop/rspec/cop_helper.rb +4 -4
- data/lib/rubocop/rspec/expect_offense.rb +65 -21
- data/lib/rubocop/rspec/shared_contexts.rb +19 -16
- data/lib/rubocop/runner.rb +34 -33
- data/lib/rubocop/target_finder.rb +3 -3
- data/lib/rubocop/target_ruby.rb +2 -2
- data/lib/rubocop/version.rb +1 -1
- metadata +34 -9
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -30,6 +30,19 @@ module RuboCop
|
|
30
30
|
add_offense(node)
|
31
31
|
end
|
32
32
|
|
33
|
+
def autocorrect(node)
|
34
|
+
lambda do |corrector|
|
35
|
+
starting_token, ending_token = if node.source.include?('"')
|
36
|
+
['%{', '}']
|
37
|
+
else
|
38
|
+
['"', '"']
|
39
|
+
end
|
40
|
+
|
41
|
+
corrector.replace(node.loc.begin, starting_token)
|
42
|
+
corrector.replace(node.loc.end, ending_token)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
33
46
|
def heredoc?(node)
|
34
47
|
node.loc.is_a?(Parser::Source::Map::Heredoc) ||
|
35
48
|
(node.parent && heredoc?(node.parent))
|
@@ -30,6 +30,8 @@ module RuboCop
|
|
30
30
|
# break if condition
|
31
31
|
# end
|
32
32
|
class LiteralAsCondition < Cop
|
33
|
+
include RangeHelp
|
34
|
+
|
33
35
|
MSG = 'Literal `%<literal>s` appeared as a condition.'
|
34
36
|
|
35
37
|
def on_if(node)
|
@@ -57,7 +59,8 @@ module RuboCop
|
|
57
59
|
case_node.each_when do |when_node|
|
58
60
|
next unless when_node.conditions.all?(&:literal?)
|
59
61
|
|
60
|
-
|
62
|
+
range = when_conditions_range(when_node)
|
63
|
+
add_offense(when_node, location: range, message: message(range))
|
61
64
|
end
|
62
65
|
end
|
63
66
|
end
|
@@ -129,6 +132,13 @@ module RuboCop
|
|
129
132
|
node.condition
|
130
133
|
end
|
131
134
|
end
|
135
|
+
|
136
|
+
def when_conditions_range(when_node)
|
137
|
+
range_between(
|
138
|
+
when_node.conditions.first.source_range.begin_pos,
|
139
|
+
when_node.conditions.last.source_range.end_pos
|
140
|
+
)
|
141
|
+
end
|
132
142
|
end
|
133
143
|
end
|
134
144
|
end
|
@@ -27,7 +27,14 @@ module RuboCop
|
|
27
27
|
def on_regexp(node)
|
28
28
|
return if contain_non_literal?(node)
|
29
29
|
|
30
|
-
|
30
|
+
begin
|
31
|
+
tree = Regexp::Parser.parse(node.content)
|
32
|
+
# Returns if a regular expression that cannot be processed by regexp_parser gem.
|
33
|
+
# https://github.com/rubocop-hq/rubocop/issues/8083
|
34
|
+
rescue Regexp::Scanner::ScannerError
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
31
38
|
return unless named_capture?(tree)
|
32
39
|
return unless numbered_capture?(tree)
|
33
40
|
|
@@ -59,31 +59,25 @@ module RuboCop
|
|
59
59
|
'Use `lambda` instead.'
|
60
60
|
|
61
61
|
def on_def(node)
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
end
|
66
|
-
alias on_defs on_def
|
67
|
-
|
68
|
-
private
|
62
|
+
subject, = *node
|
63
|
+
return if node.defs_type? && subject.lvar_type?
|
69
64
|
|
70
|
-
|
71
|
-
|
72
|
-
if child.def_type?
|
73
|
-
yield child
|
74
|
-
elsif child.defs_type?
|
75
|
-
subject, = *child
|
76
|
-
next if subject.lvar_type?
|
65
|
+
def_ancestor = node.each_ancestor(:def, :defs).first
|
66
|
+
return unless def_ancestor
|
77
67
|
|
78
|
-
|
79
|
-
|
80
|
-
|
68
|
+
within_scoping_def =
|
69
|
+
node.each_ancestor(:block, :sclass).any? do |ancestor|
|
70
|
+
scoping_method_call?(ancestor)
|
81
71
|
end
|
82
|
-
|
72
|
+
|
73
|
+
add_offense(node) if def_ancestor && !within_scoping_def
|
83
74
|
end
|
75
|
+
alias on_defs on_def
|
76
|
+
|
77
|
+
private
|
84
78
|
|
85
79
|
def scoping_method_call?(child)
|
86
|
-
eval_call?(child) || exec_call?(child) ||
|
80
|
+
child.sclass_type? || eval_call?(child) || exec_call?(child) ||
|
87
81
|
class_or_module_or_struct_new_call?(child)
|
88
82
|
end
|
89
83
|
|
@@ -96,7 +90,7 @@ module RuboCop
|
|
96
90
|
PATTERN
|
97
91
|
|
98
92
|
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
|
99
|
-
(block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
|
93
|
+
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
|
100
94
|
PATTERN
|
101
95
|
end
|
102
96
|
end
|
@@ -35,6 +35,22 @@ module RuboCop
|
|
35
35
|
# require file
|
36
36
|
# end
|
37
37
|
#
|
38
|
+
# @example
|
39
|
+
#
|
40
|
+
# # bad
|
41
|
+
# Dir['./lib/**/*.rb'].each(&method(:require))
|
42
|
+
#
|
43
|
+
# # good
|
44
|
+
# Dir['./lib/**/*.rb'].sort.each(&method(:require))
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
#
|
48
|
+
# # bad
|
49
|
+
# Dir.glob(Rails.root.join('test', '*.rb'), &method(:require))
|
50
|
+
#
|
51
|
+
# # good
|
52
|
+
# Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require))
|
53
|
+
#
|
38
54
|
class NonDeterministicRequireOrder < Cop
|
39
55
|
MSG = 'Sort files before requiring them.'
|
40
56
|
|
@@ -49,7 +65,16 @@ module RuboCop
|
|
49
65
|
end
|
50
66
|
end
|
51
67
|
|
68
|
+
def on_block_pass(node)
|
69
|
+
return unless method_require?(node)
|
70
|
+
return unless unsorted_dir_pass?(node.parent)
|
71
|
+
|
72
|
+
add_offense(node.parent)
|
73
|
+
end
|
74
|
+
|
52
75
|
def autocorrect(node)
|
76
|
+
return correct_block_pass(node) if node.arguments.last&.block_pass_type?
|
77
|
+
|
53
78
|
if unsorted_dir_block?(node)
|
54
79
|
lambda do |corrector|
|
55
80
|
corrector.replace(node, "#{node.source}.sort.each")
|
@@ -64,16 +89,58 @@ module RuboCop
|
|
64
89
|
|
65
90
|
private
|
66
91
|
|
92
|
+
def correct_block_pass(node)
|
93
|
+
if unsorted_dir_glob_pass?(node)
|
94
|
+
lambda do |corrector|
|
95
|
+
block_arg = node.arguments.last
|
96
|
+
corrector.remove(last_arg_range(node))
|
97
|
+
corrector.insert_after(node, ".sort.each(#{block_arg.source})")
|
98
|
+
end
|
99
|
+
else
|
100
|
+
lambda do |corrector|
|
101
|
+
corrector.replace(node.loc.selector, 'sort.each')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns range of last argument including comma and whitespace.
|
107
|
+
#
|
108
|
+
# @return [Parser::Source::Range]
|
109
|
+
#
|
110
|
+
def last_arg_range(node)
|
111
|
+
node.arguments.last.source_range.with(
|
112
|
+
begin_pos: node.arguments[-2].source_range.end_pos
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
67
116
|
def unsorted_dir_loop?(node)
|
68
117
|
unsorted_dir_block?(node) || unsorted_dir_each?(node)
|
69
118
|
end
|
70
119
|
|
120
|
+
def unsorted_dir_pass?(node)
|
121
|
+
unsorted_dir_glob_pass?(node) || unsorted_dir_each_pass?(node)
|
122
|
+
end
|
123
|
+
|
71
124
|
def_node_matcher :unsorted_dir_block?, <<~PATTERN
|
72
|
-
(send (const nil? :Dir) :glob ...)
|
125
|
+
(send (const {nil? cbase} :Dir) :glob ...)
|
73
126
|
PATTERN
|
74
127
|
|
75
128
|
def_node_matcher :unsorted_dir_each?, <<~PATTERN
|
76
|
-
(send (send (const nil? :Dir) {:[] :glob} ...) :each)
|
129
|
+
(send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each)
|
130
|
+
PATTERN
|
131
|
+
|
132
|
+
def_node_matcher :method_require?, <<~PATTERN
|
133
|
+
(block-pass (send nil? :method (sym :require)))
|
134
|
+
PATTERN
|
135
|
+
|
136
|
+
def_node_matcher :unsorted_dir_glob_pass?, <<~PATTERN
|
137
|
+
(send (const {nil? cbase} :Dir) :glob ...
|
138
|
+
(block-pass (send nil? :method (sym :require))))
|
139
|
+
PATTERN
|
140
|
+
|
141
|
+
def_node_matcher :unsorted_dir_each_pass?, <<~PATTERN
|
142
|
+
(send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each
|
143
|
+
(block-pass (send nil? :method (sym :require))))
|
77
144
|
PATTERN
|
78
145
|
|
79
146
|
def_node_matcher :loop_variable, <<~PATTERN
|
@@ -48,17 +48,22 @@ module RuboCop
|
|
48
48
|
return true
|
49
49
|
end
|
50
50
|
|
51
|
-
node.operator_method? || node.setter_method? ||
|
51
|
+
node.operator_method? || node.setter_method? || chained_calls?(node) ||
|
52
|
+
operator_keyword?(node)
|
52
53
|
end
|
53
54
|
|
54
55
|
def first_argument_starts_with_left_parenthesis?(node)
|
55
56
|
node.first_argument.source.start_with?('(')
|
56
57
|
end
|
57
58
|
|
58
|
-
def
|
59
|
+
def chained_calls?(node)
|
59
60
|
first_argument = node.first_argument
|
61
|
+
first_argument.send_type? && (node.children.last&.children&.count || 0) > 1
|
62
|
+
end
|
60
63
|
|
61
|
-
|
64
|
+
def operator_keyword?(node)
|
65
|
+
first_argument = node.first_argument
|
66
|
+
first_argument.operator_keyword?
|
62
67
|
end
|
63
68
|
|
64
69
|
def spaces_before_left_parenthesis(node)
|
@@ -31,12 +31,12 @@ module RuboCop
|
|
31
31
|
MSG = 'Use `StandardError` over `Exception`.'
|
32
32
|
|
33
33
|
def_node_matcher :exception?, <<~PATTERN
|
34
|
-
(send nil? {:raise :fail} (const ${cbase nil?} :Exception) ... )
|
34
|
+
(send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
|
35
35
|
PATTERN
|
36
36
|
|
37
37
|
def_node_matcher :exception_new_with_message?, <<~PATTERN
|
38
38
|
(send nil? {:raise :fail}
|
39
|
-
(send (const ${cbase nil?} :Exception) :new ... ))
|
39
|
+
(send $(const ${cbase nil?} :Exception) :new ... ))
|
40
40
|
PATTERN
|
41
41
|
|
42
42
|
def on_send(node)
|
@@ -44,13 +44,21 @@ module RuboCop
|
|
44
44
|
exception_new_with_message?(node, &check(node))
|
45
45
|
end
|
46
46
|
|
47
|
+
def autocorrect(node)
|
48
|
+
lambda do |corrector|
|
49
|
+
exception_class = node.children.first&.cbase_type? ? '::StandardError' : 'StandardError'
|
50
|
+
|
51
|
+
corrector.replace(node, exception_class)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
private
|
48
56
|
|
49
57
|
def check(node)
|
50
|
-
lambda do |cbase|
|
58
|
+
lambda do |exception_class, cbase|
|
51
59
|
return if cbase.nil? && implicit_namespace?(node)
|
52
60
|
|
53
|
-
add_offense(
|
61
|
+
add_offense(exception_class)
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
@@ -25,7 +25,7 @@ module RuboCop
|
|
25
25
|
'Perhaps you meant `rand(2)` or `rand`?'
|
26
26
|
|
27
27
|
def_node_matcher :rand_one?, <<~PATTERN
|
28
|
-
(send {(const nil? :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
|
28
|
+
(send {(const {nil? cbase} :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
|
29
29
|
PATTERN
|
30
30
|
|
31
31
|
def on_send(node)
|
@@ -25,34 +25,34 @@ module RuboCop
|
|
25
25
|
#
|
26
26
|
# # good
|
27
27
|
# x += 1
|
28
|
-
class RedundantCopDisableDirective <
|
28
|
+
class RedundantCopDisableDirective < Base
|
29
29
|
include RangeHelp
|
30
|
+
extend AutoCorrector
|
30
31
|
|
31
32
|
COP_NAME = 'Lint/RedundantCopDisableDirective'
|
32
33
|
|
33
|
-
|
34
|
+
attr_accessor :offenses_to_check
|
35
|
+
|
36
|
+
def initialize(config = nil, options = nil, offenses = nil)
|
37
|
+
@offenses_to_check = offenses
|
38
|
+
super(config, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def on_new_investigation
|
42
|
+
return unless offenses_to_check
|
43
|
+
|
44
|
+
comments = processed_source.comments
|
45
|
+
cop_disabled_line_ranges = processed_source.disabled_line_ranges
|
46
|
+
|
34
47
|
redundant_cops = Hash.new { |h, k| h[k] = Set.new }
|
35
48
|
|
36
49
|
each_redundant_disable(cop_disabled_line_ranges,
|
37
|
-
|
50
|
+
offenses_to_check, comments) do |comment, redundant_cop|
|
38
51
|
redundant_cops[comment].add(redundant_cop)
|
39
52
|
end
|
40
53
|
|
41
54
|
add_offenses(redundant_cops)
|
42
|
-
|
43
|
-
|
44
|
-
def autocorrect(args)
|
45
|
-
lambda do |corrector|
|
46
|
-
ranges, range = *args # Ranges are sorted by position.
|
47
|
-
|
48
|
-
range = if range.source.start_with?('#')
|
49
|
-
comment_range_with_surrounding_space(range)
|
50
|
-
else
|
51
|
-
directive_range_in_list(range, ranges)
|
52
|
-
end
|
53
|
-
|
54
|
-
corrector.remove(range)
|
55
|
-
end
|
55
|
+
super
|
56
56
|
end
|
57
57
|
|
58
58
|
private
|
@@ -135,6 +135,7 @@ module RuboCop
|
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
|
+
# rubocop:todo Metrics/CyclomaticComplexity
|
138
139
|
def find_redundant(comment, offenses, cop, line_range, next_line_range)
|
139
140
|
if all_disabled?(comment)
|
140
141
|
# If there's a disable all comment followed by a comment
|
@@ -152,9 +153,10 @@ module RuboCop
|
|
152
153
|
cop if cop_offenses.none? { |o| line_range.cover?(o.line) }
|
153
154
|
end
|
154
155
|
end
|
156
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
155
157
|
|
156
158
|
def all_disabled?(comment)
|
157
|
-
|
159
|
+
/rubocop\s*:\s*(?:disable|todo)\s+all\b/.match?(comment.text)
|
158
160
|
end
|
159
161
|
|
160
162
|
def ignore_offense?(disabled_ranges, line_range)
|
@@ -185,10 +187,12 @@ module RuboCop
|
|
185
187
|
cop_list = cops.sort.map { |c| describe(c) }
|
186
188
|
|
187
189
|
add_offense(
|
188
|
-
|
189
|
-
location: location,
|
190
|
+
location,
|
190
191
|
message: "Unnecessary disabling of #{cop_list.join(', ')}."
|
191
|
-
)
|
192
|
+
) do |corrector|
|
193
|
+
range = comment_range_with_surrounding_space(location)
|
194
|
+
corrector.remove(range)
|
195
|
+
end
|
192
196
|
end
|
193
197
|
|
194
198
|
def add_offense_for_some_cops(comment, cops)
|
@@ -198,10 +202,12 @@ module RuboCop
|
|
198
202
|
|
199
203
|
cop_ranges.each do |cop, range|
|
200
204
|
add_offense(
|
201
|
-
|
202
|
-
location: range,
|
205
|
+
range,
|
203
206
|
message: "Unnecessary disabling of #{describe(cop)}."
|
204
|
-
)
|
207
|
+
) do |corrector|
|
208
|
+
range = directive_range_in_list(range, ranges)
|
209
|
+
corrector.remove(range)
|
210
|
+
end
|
205
211
|
end
|
206
212
|
end
|
207
213
|
|
@@ -225,7 +231,7 @@ module RuboCop
|
|
225
231
|
.drop_while { |r| !r.equal?(range) }
|
226
232
|
.each_cons(2)
|
227
233
|
.map { |range1, range2| range1.end.join(range2.begin).source }
|
228
|
-
.all? { |intervening|
|
234
|
+
.all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
|
229
235
|
end
|
230
236
|
|
231
237
|
def describe(cop)
|
@@ -60,8 +60,8 @@ module RuboCop
|
|
60
60
|
|
61
61
|
def_node_matcher :array_new?, <<~PATTERN
|
62
62
|
{
|
63
|
-
$(send (const nil? :Array) :new ...)
|
64
|
-
$(block (send (const nil? :Array) :new ...) ...)
|
63
|
+
$(send (const {nil? cbase} :Array) :new ...)
|
64
|
+
$(block (send (const {nil? cbase} :Array) :new ...) ...)
|
65
65
|
}
|
66
66
|
PATTERN
|
67
67
|
|