rubocop 0.47.1 → 0.48.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.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +28 -16
- data/config/default.yml +203 -115
- data/config/disabled.yml +0 -5
- data/config/enabled.yml +92 -8
- data/lib/rubocop.rb +24 -1
- data/lib/rubocop/ast/builder.rb +7 -2
- data/lib/rubocop/ast/node.rb +23 -20
- data/lib/rubocop/ast/node/and_node.rb +37 -0
- data/lib/rubocop/ast/node/array_node.rb +4 -1
- data/lib/rubocop/ast/node/case_node.rb +1 -0
- data/lib/rubocop/ast/node/ensure_node.rb +25 -0
- data/lib/rubocop/ast/node/hash_node.rb +10 -3
- data/lib/rubocop/ast/node/if_node.rb +2 -0
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
- data/lib/rubocop/ast/node/or_node.rb +37 -0
- data/lib/rubocop/ast/node/resbody_node.rb +25 -0
- data/lib/rubocop/ast/node/send_node.rb +190 -0
- data/lib/rubocop/ast/node/when_node.rb +1 -1
- data/lib/rubocop/ast/traversal.rb +15 -15
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +39 -15
- data/lib/rubocop/config_loader.rb +34 -13
- data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
- data/lib/rubocop/cop/commissioner.rb +4 -0
- data/lib/rubocop/cop/cop.rb +5 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
- data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
- data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
- data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
- data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/line_length.rb +2 -9
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
- data/lib/rubocop/cop/mixin/duplication.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
- data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
- data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
- data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
- data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
- data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/performance/casecmp.rb +1 -1
- data/lib/rubocop/cop/performance/detect.rb +2 -1
- data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
- data/lib/rubocop/cop/performance/end_with.rb +3 -1
- data/lib/rubocop/cop/performance/flat_map.rb +6 -6
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +3 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
- data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
- data/lib/rubocop/cop/performance/size.rb +6 -11
- data/lib/rubocop/cop/performance/start_with.rb +3 -1
- data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
- data/lib/rubocop/cop/performance/times_map.rb +4 -4
- data/lib/rubocop/cop/rails/action_filter.rb +42 -42
- data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
- data/lib/rubocop/cop/rails/blank.rb +131 -0
- data/lib/rubocop/cop/rails/date.rb +25 -28
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
- data/lib/rubocop/cop/rails/exit.rb +9 -9
- data/lib/rubocop/cop/rails/file_path.rb +5 -14
- data/lib/rubocop/cop/rails/find_by.rb +8 -10
- data/lib/rubocop/cop/rails/find_each.rb +6 -9
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
- data/lib/rubocop/cop/rails/output.rb +3 -5
- data/lib/rubocop/cop/rails/output_safety.rb +4 -8
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
- data/lib/rubocop/cop/rails/present.rb +137 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
- data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
- data/lib/rubocop/cop/rails/request_referer.rb +7 -4
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
- data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
- data/lib/rubocop/cop/rails/save_bang.rb +10 -10
- data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
- data/lib/rubocop/cop/rails/time_zone.rb +20 -18
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
- data/lib/rubocop/cop/rails/validation.rb +8 -11
- data/lib/rubocop/cop/registry.rb +3 -3
- data/lib/rubocop/cop/security/json_load.rb +1 -1
- data/lib/rubocop/cop/security/marshal_load.rb +5 -1
- data/lib/rubocop/cop/security/yaml_load.rb +3 -3
- data/lib/rubocop/cop/severity.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +5 -5
- data/lib/rubocop/cop/style/align_hash.rb +1 -1
- data/lib/rubocop/cop/style/align_parameters.rb +5 -5
- data/lib/rubocop/cop/style/and_or.rb +16 -31
- data/lib/rubocop/cop/style/attr.rb +14 -8
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
- data/lib/rubocop/cop/style/case_indentation.rb +2 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_check.rb +5 -7
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
- data/lib/rubocop/cop/style/collection_methods.rb +8 -8
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
- data/lib/rubocop/cop/style/constant_name.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/dot_position.rb +3 -7
- data/lib/rubocop/cop/style/double_negation.rb +2 -1
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +2 -2
- data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
- data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
- data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
- data/lib/rubocop/cop/style/empty_literal.rb +17 -9
- data/lib/rubocop/cop/style/end_of_line.rb +25 -3
- data/lib/rubocop/cop/style/file_name.rb +1 -1
- data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
- data/lib/rubocop/cop/style/for.rb +2 -4
- data/lib/rubocop/cop/style/format_string.rb +5 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
- data/lib/rubocop/cop/style/indent_hash.rb +2 -1
- data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
- data/lib/rubocop/cop/style/indentation_width.rb +61 -29
- data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
- data/lib/rubocop/cop/style/lambda_call.rb +15 -11
- data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/missing_else.rb +4 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
- data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
- data/lib/rubocop/cop/style/negated_if.rb +73 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
- data/lib/rubocop/cop/style/not.rb +3 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
- data/lib/rubocop/cop/style/raise_args.rb +28 -24
- data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +17 -35
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/send.rb +4 -5
- data/lib/rubocop/cop/style/space_after_not.rb +7 -8
- data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
- data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
- data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +10 -5
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
- data/lib/rubocop/cop/style/symbol_array.rb +31 -35
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +12 -34
- data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
- data/lib/rubocop/cop/team.rb +4 -1
- data/lib/rubocop/cop/util.rb +33 -26
- data/lib/rubocop/cop/variable_force.rb +13 -13
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
- data/lib/rubocop/cop/variable_force/branch.rb +318 -0
- data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -3
- data/lib/rubocop/cop/variable_force/scope.rb +36 -20
- data/lib/rubocop/cop/variable_force/variable.rb +9 -8
- data/lib/rubocop/formatter/colorizable.rb +10 -10
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +2 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
- data/lib/rubocop/magic_comment.rb +20 -6
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/platform.rb +11 -0
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/remote_config.rb +18 -6
- data/lib/rubocop/result_cache.rb +8 -8
- data/lib/rubocop/rspec/cop_helper.rb +2 -0
- data/lib/rubocop/rspec/shared_contexts.rb +20 -0
- data/lib/rubocop/rspec/shared_examples.rb +1 -1
- data/lib/rubocop/runner.rb +2 -2
- data/lib/rubocop/target_finder.rb +64 -6
- data/lib/rubocop/version.rb +2 -4
- metadata +27 -4
- data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
PERCENT_I = '%i'.freeze
|
58
58
|
PERCENT_CAPITAL_I = '%I'.freeze
|
59
59
|
ARRAY_NEW_PATTERN = '$(send (const nil :Array) :new ...)'.freeze
|
60
|
-
ASSIGNMENT_TYPES =
|
60
|
+
ASSIGNMENT_TYPES = %i(lvasgn ivasgn cvasgn gvasgn).freeze
|
61
61
|
|
62
62
|
def_node_matcher :literal_expansion?, <<-PATTERN
|
63
63
|
(splat {$({str dstr int float array} ...) (block #{ARRAY_NEW_PATTERN} ...) #{ARRAY_NEW_PATTERN}} ...)
|
@@ -26,14 +26,15 @@ module RuboCop
|
|
26
26
|
class UnreachableCode < Cop
|
27
27
|
MSG = 'Unreachable code detected.'.freeze
|
28
28
|
|
29
|
-
NODE_TYPES =
|
30
|
-
FLOW_COMMANDS =
|
29
|
+
NODE_TYPES = %i(return next break retry redo).freeze
|
30
|
+
FLOW_COMMANDS = %i(throw raise fail).freeze
|
31
31
|
|
32
32
|
def on_begin(node)
|
33
33
|
expressions = *node
|
34
34
|
|
35
35
|
expressions.each_cons(2) do |e1, e2|
|
36
36
|
next unless NODE_TYPES.include?(e1.type) || flow_command?(e1)
|
37
|
+
|
37
38
|
add_offense(e2, :expression)
|
38
39
|
end
|
39
40
|
end
|
@@ -41,6 +42,8 @@ module RuboCop
|
|
41
42
|
private
|
42
43
|
|
43
44
|
def flow_command?(node)
|
45
|
+
return false unless node.send_type?
|
46
|
+
|
44
47
|
FLOW_COMMANDS.any? { |c| node.command?(c) }
|
45
48
|
end
|
46
49
|
end
|
@@ -7,33 +7,33 @@ module RuboCop
|
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
#
|
10
|
-
# #
|
10
|
+
# # bad
|
11
11
|
#
|
12
12
|
# do_something do |used, unused|
|
13
13
|
# puts used
|
14
14
|
# end
|
15
15
|
#
|
16
|
-
# do_something do
|
16
|
+
# do_something do |bar|
|
17
17
|
# puts :foo
|
18
18
|
# end
|
19
19
|
#
|
20
|
-
# define_method(:foo) do |
|
20
|
+
# define_method(:foo) do |bar|
|
21
21
|
# puts :baz
|
22
22
|
# end
|
23
23
|
#
|
24
24
|
# @example
|
25
25
|
#
|
26
|
-
# #
|
26
|
+
# #good
|
27
27
|
#
|
28
28
|
# do_something do |used, _unused|
|
29
29
|
# puts used
|
30
30
|
# end
|
31
31
|
#
|
32
|
-
# do_something do
|
32
|
+
# do_something do
|
33
33
|
# puts :foo
|
34
34
|
# end
|
35
35
|
#
|
36
|
-
# define_method(:foo) do |
|
36
|
+
# define_method(:foo) do |_bar|
|
37
37
|
# puts :baz
|
38
38
|
# end
|
39
39
|
class UnusedBlockArgument < Cop
|
@@ -89,7 +89,8 @@ module RuboCop
|
|
89
89
|
return unless assignment.meta_assignment_node
|
90
90
|
.equal?(return_value_node)
|
91
91
|
|
92
|
-
" Use
|
92
|
+
" Use `#{assignment.operator.sub(/=$/, '')}` " \
|
93
|
+
"instead of `#{assignment.operator}`."
|
93
94
|
end
|
94
95
|
|
95
96
|
def similar_name_message(variable)
|
@@ -14,12 +14,13 @@ module RuboCop
|
|
14
14
|
MSG = 'Comparison of something with itself detected.'.freeze
|
15
15
|
OPS = %w(== === != < > <= >= <=>).freeze
|
16
16
|
|
17
|
-
def_node_matcher :
|
17
|
+
def_node_matcher :useless_comparison?,
|
18
|
+
"(send $_match {:#{OPS.join(' :')}} $_match)"
|
18
19
|
|
19
20
|
def on_send(node)
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
return unless useless_comparison?(node)
|
22
|
+
|
23
|
+
add_offense(node, :selector)
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
# @option cop_config [String] :Reference Full reference URL
|
29
29
|
# @option cop_config [String] :Details
|
30
30
|
#
|
31
|
-
# @param [Hash] options
|
31
|
+
# @param [Hash, nil] optional options
|
32
32
|
# @option option [Boolean] :display_style_guide
|
33
33
|
# Include style guide and reference URLs
|
34
34
|
# @option option [Boolean] :extra_details
|
@@ -51,12 +51,16 @@ module RuboCop
|
|
51
51
|
message = "#{name}: #{message}" if display_cop_names?
|
52
52
|
message += " #{details}" if extra_details?
|
53
53
|
if display_style_guide?
|
54
|
-
links =
|
54
|
+
links = urls.join(', ')
|
55
55
|
message = "#{message} (#{links})"
|
56
56
|
end
|
57
57
|
message
|
58
58
|
end
|
59
59
|
|
60
|
+
def urls
|
61
|
+
[style_guide_url, reference_url].compact
|
62
|
+
end
|
63
|
+
|
60
64
|
private
|
61
65
|
|
62
66
|
def style_guide_url
|
@@ -70,7 +74,7 @@ module RuboCop
|
|
70
74
|
end
|
71
75
|
|
72
76
|
def display_style_guide?
|
73
|
-
|
77
|
+
!urls.empty? &&
|
74
78
|
(options[:display_style_guide] ||
|
75
79
|
config.for_all_cops['DisplayStyleGuide'])
|
76
80
|
end
|
@@ -11,7 +11,7 @@ module RuboCop
|
|
11
11
|
|
12
12
|
MSG = 'Assignment Branch Condition size for %s is too high. ' \
|
13
13
|
'[%.4g/%.4g]'.freeze
|
14
|
-
BRANCH_NODES =
|
14
|
+
BRANCH_NODES = %i(send csend).freeze
|
15
15
|
CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
|
16
16
|
|
17
17
|
private
|
@@ -14,10 +14,10 @@ module RuboCop
|
|
14
14
|
class BlockNesting < Cop
|
15
15
|
include ConfigurableMax
|
16
16
|
|
17
|
-
NESTING_BLOCKS =
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
NESTING_BLOCKS = %i(
|
18
|
+
case if while while_post
|
19
|
+
until until_post for resbody
|
20
|
+
).freeze
|
21
21
|
|
22
22
|
def investigate(processed_source)
|
23
23
|
return unless processed_source.ast
|
@@ -17,8 +17,8 @@ module RuboCop
|
|
17
17
|
include MethodComplexity
|
18
18
|
|
19
19
|
MSG = 'Cyclomatic complexity for %s is too high. [%d/%d]'.freeze
|
20
|
-
COUNTED_NODES =
|
21
|
-
|
20
|
+
COUNTED_NODES = %i(if while until for
|
21
|
+
rescue when and or).freeze
|
22
22
|
|
23
23
|
private
|
24
24
|
|
@@ -9,6 +9,7 @@ module RuboCop
|
|
9
9
|
# The maximum length is configurable.
|
10
10
|
class LineLength < Cop
|
11
11
|
include ConfigurableMax
|
12
|
+
include IgnoredPattern
|
12
13
|
|
13
14
|
MSG = 'Line is too long. [%d/%d]'.freeze
|
14
15
|
|
@@ -86,14 +87,6 @@ module RuboCop
|
|
86
87
|
end
|
87
88
|
end
|
88
89
|
|
89
|
-
def matches_ignored_pattern?(line)
|
90
|
-
ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) }
|
91
|
-
end
|
92
|
-
|
93
|
-
def ignored_patterns
|
94
|
-
cop_config['IgnoredPatterns'] || []
|
95
|
-
end
|
96
|
-
|
97
90
|
def allow_uri?
|
98
91
|
cop_config['AllowURI']
|
99
92
|
end
|
@@ -159,7 +152,7 @@ module RuboCop
|
|
159
152
|
end
|
160
153
|
|
161
154
|
def line_length_without_directive(line)
|
162
|
-
before_comment, = line.split(
|
155
|
+
before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
|
163
156
|
before_comment.rstrip.length
|
164
157
|
end
|
165
158
|
|
@@ -10,13 +10,14 @@ module RuboCop
|
|
10
10
|
class ParameterLists < Cop
|
11
11
|
include ConfigurableMax
|
12
12
|
|
13
|
-
MSG = 'Avoid parameter lists longer than %d parameters.'.freeze
|
13
|
+
MSG = 'Avoid parameter lists longer than %d parameters. [%d/%d]'.freeze
|
14
14
|
|
15
15
|
def on_args(node)
|
16
16
|
count = args_count(node)
|
17
17
|
return unless count > max_params
|
18
18
|
|
19
|
-
|
19
|
+
message = format(MSG, max_params, count, max_params)
|
20
|
+
add_offense(node, :expression, message) do
|
20
21
|
self.max = count
|
21
22
|
end
|
22
23
|
end
|
@@ -27,7 +28,7 @@ module RuboCop
|
|
27
28
|
if count_keyword_args?
|
28
29
|
node.children.size
|
29
30
|
else
|
30
|
-
node.children.count { |a|
|
31
|
+
node.children.count { |a| !%i(kwoptarg kwarg).include?(a.type) }
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -30,8 +30,8 @@ module RuboCop
|
|
30
30
|
include MethodComplexity
|
31
31
|
|
32
32
|
MSG = 'Perceived complexity for %s is too high. [%d/%d]'.freeze
|
33
|
-
COUNTED_NODES =
|
34
|
-
|
33
|
+
COUNTED_NODES = %i(if case while until
|
34
|
+
for rescue and or).freeze
|
35
35
|
|
36
36
|
private
|
37
37
|
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
if ancestor.block_type?
|
33
33
|
return true if ancestor.class_constructor?
|
34
34
|
elsif !ancestor.begin_type?
|
35
|
-
return
|
35
|
+
return %i(casgn sclass class module).include?(ancestor.type)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -5,11 +5,11 @@ module RuboCop
|
|
5
5
|
# Common code for indenting literal arrays and hashes.
|
6
6
|
module ArrayHashIndentation
|
7
7
|
def each_argument_node(node, type)
|
8
|
-
_receiver, _method_name, *args = *node
|
9
8
|
left_parenthesis = node.loc.begin
|
9
|
+
|
10
10
|
return unless left_parenthesis
|
11
11
|
|
12
|
-
|
12
|
+
node.arguments.each do |arg|
|
13
13
|
on_node(type, arg, :send) do |type_node|
|
14
14
|
left_brace = type_node.loc.begin
|
15
15
|
if left_brace && left_brace.line == left_parenthesis.line
|
@@ -11,13 +11,13 @@ module RuboCop
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def on_send(node)
|
14
|
-
|
15
|
-
# when the method called looks like a setter
|
16
|
-
return unless node.asgn_method_call?
|
14
|
+
return unless node.setter_method?
|
17
15
|
|
18
|
-
# This will match if, case, begin, blocks, etc.
|
19
16
|
rhs = extract_rhs(node)
|
20
|
-
|
17
|
+
|
18
|
+
return unless rhs
|
19
|
+
|
20
|
+
check_assignment(node, rhs)
|
21
21
|
end
|
22
22
|
|
23
23
|
module_function
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
elsif Util::ASGN_NODES.include?(node.type)
|
31
31
|
_lhs, rhs = *node
|
32
32
|
elsif node.send_type?
|
33
|
-
rhs = node.
|
33
|
+
rhs = node.last_argument
|
34
34
|
end
|
35
35
|
|
36
36
|
rhs
|
@@ -37,7 +37,7 @@ module RuboCop
|
|
37
37
|
# element and all duplicate instances.
|
38
38
|
#
|
39
39
|
# @param [Array] collection an array to group duplicates for
|
40
|
-
# @return [
|
40
|
+
# @return [Array] the grouped duplicates
|
41
41
|
def grouped_duplicates(collection)
|
42
42
|
collection.group_by { |item| item }.values.reject(&:one?)
|
43
43
|
end
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
|
9
9
|
FROZEN_STRING_LITERAL = '# frozen_string_literal:'.freeze
|
10
10
|
FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'.freeze
|
11
|
-
FROZEN_STRING_LITERAL_TYPES =
|
11
|
+
FROZEN_STRING_LITERAL_TYPES = %i(str dstr).freeze
|
12
12
|
|
13
13
|
def frozen_string_literal_comment_exists?
|
14
14
|
leading_comment_lines.any? do |line|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# This module encapsulates the ability to ignore certain lines when
|
6
|
+
# parsing.
|
7
|
+
module IgnoredPattern
|
8
|
+
def ignored_line?(line)
|
9
|
+
line = if line.respond_to?(:source_line)
|
10
|
+
line.source_line
|
11
|
+
elsif line.respond_to?(:node)
|
12
|
+
line.node.source_range.source_line
|
13
|
+
end
|
14
|
+
|
15
|
+
matches_ignored_pattern?(line)
|
16
|
+
end
|
17
|
+
|
18
|
+
def matches_ignored_pattern?(line)
|
19
|
+
ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def ignored_patterns
|
23
|
+
cop_config['IgnoredPatterns'] || []
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -6,13 +6,10 @@ module RuboCop
|
|
6
6
|
# operations.
|
7
7
|
module MultilineExpressionIndentation
|
8
8
|
def on_send(node)
|
9
|
+
return if !node.receiver || node.method?(:[])
|
9
10
|
return unless relevant_node?(node)
|
10
11
|
|
11
|
-
|
12
|
-
return unless receiver
|
13
|
-
return if method_name == :[] # Don't check parameters inside [].
|
14
|
-
|
15
|
-
lhs = left_hand_side(receiver)
|
12
|
+
lhs = left_hand_side(node.receiver)
|
16
13
|
rhs = right_hand_side(node)
|
17
14
|
range = offending_range(node, lhs, rhs, style)
|
18
15
|
check(range, node, lhs, rhs)
|
@@ -29,9 +26,8 @@ module RuboCop
|
|
29
26
|
end
|
30
27
|
|
31
28
|
def right_hand_side(send_node)
|
32
|
-
|
33
|
-
|
34
|
-
args.first.source_range # not used for method calls
|
29
|
+
if send_node.operator_method? && send_node.arguments?
|
30
|
+
send_node.first_argument.source_range # not used for method calls
|
35
31
|
else
|
36
32
|
regular_method_right_hand_side(send_node)
|
37
33
|
end
|
@@ -40,12 +36,11 @@ module RuboCop
|
|
40
36
|
def regular_method_right_hand_side(send_node)
|
41
37
|
dot = send_node.loc.dot
|
42
38
|
selector = send_node.loc.selector
|
43
|
-
if dot && selector && dot.line == selector.line
|
39
|
+
if send_node.dot? && selector && dot.line == selector.line
|
44
40
|
dot.join(selector)
|
45
41
|
elsif selector
|
46
42
|
selector
|
47
|
-
elsif
|
48
|
-
# lambda.(args)
|
43
|
+
elsif send_node.implicit_call?
|
49
44
|
dot.join(send_node.loc.begin)
|
50
45
|
end
|
51
46
|
end
|
@@ -116,9 +111,9 @@ module RuboCop
|
|
116
111
|
# is an argument in a method call. It doesn't count.
|
117
112
|
break false if a.block_type?
|
118
113
|
|
119
|
-
|
120
|
-
|
121
|
-
|
114
|
+
next if a.setter_method?
|
115
|
+
|
116
|
+
a.arguments.any? { |arg| within_node?(node, arg) }
|
122
117
|
end
|
123
118
|
end
|
124
119
|
|
@@ -139,20 +134,14 @@ module RuboCop
|
|
139
134
|
|
140
135
|
# The []= operator and setters (a.b = c) are parsed as :send nodes.
|
141
136
|
def valid_method_rhs_candidate?(candidate, node)
|
142
|
-
|
143
|
-
|
144
|
-
assignment_call?(method_name) &&
|
145
|
-
valid_rhs_candidate?(candidate, args.last)
|
137
|
+
node.setter_method? &&
|
138
|
+
valid_rhs_candidate?(candidate, node.last_argument)
|
146
139
|
end
|
147
140
|
|
148
141
|
def valid_rhs_candidate?(candidate, node)
|
149
142
|
!candidate || within_node?(candidate, node)
|
150
143
|
end
|
151
144
|
|
152
|
-
def assignment_call?(method_name)
|
153
|
-
method_name == :[]= || method_name.to_s =~ /^\w.*=$/
|
154
|
-
end
|
155
|
-
|
156
145
|
def part_of_block_body?(candidate, node)
|
157
146
|
_method, _args, body = *node
|
158
147
|
body && within_node?(candidate, body)
|
@@ -162,14 +151,14 @@ module RuboCop
|
|
162
151
|
case node.type
|
163
152
|
when :casgn then _scope, _lhs, rhs = *node
|
164
153
|
when :op_asgn then _lhs, _op, rhs = *node
|
165
|
-
when :send then
|
154
|
+
when :send then rhs = node.last_argument
|
166
155
|
else _lhs, rhs = *node
|
167
156
|
end
|
168
157
|
rhs
|
169
158
|
end
|
170
159
|
|
171
160
|
def not_for_this_cop?(node)
|
172
|
-
node.
|
161
|
+
node.ancestors.any? do |ancestor|
|
173
162
|
grouped_expression?(ancestor) ||
|
174
163
|
inside_arg_list_parentheses?(node, ancestor)
|
175
164
|
end
|
@@ -180,11 +169,10 @@ module RuboCop
|
|
180
169
|
end
|
181
170
|
|
182
171
|
def inside_arg_list_parentheses?(node, ancestor)
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
n.begin_pos > a.begin.begin_pos && n.end_pos < a.end.end_pos
|
172
|
+
return false unless ancestor.send_type? && ancestor.parenthesized?
|
173
|
+
|
174
|
+
node.source_range.begin_pos > ancestor.loc.begin.begin_pos &&
|
175
|
+
node.source_range.end_pos < ancestor.loc.end.end_pos
|
188
176
|
end
|
189
177
|
end
|
190
178
|
end
|