rubocop 0.49.1 → 0.50.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/bin/rubocop +1 -1
- data/config/default.yml +160 -99
- data/config/disabled.yml +4 -5
- data/config/enabled.yml +149 -47
- data/lib/rubocop.rb +41 -14
- data/lib/rubocop/ast/builder.rb +4 -1
- data/lib/rubocop/ast/node.rb +36 -42
- data/lib/rubocop/ast/node/args_node.rb +1 -13
- data/lib/rubocop/ast/node/array_node.rb +9 -0
- data/lib/rubocop/ast/node/block_node.rb +9 -0
- data/lib/rubocop/ast/node/def_node.rb +71 -0
- data/lib/rubocop/ast/node/for_node.rb +8 -0
- data/lib/rubocop/ast/node/if_node.rb +10 -2
- data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
- data/lib/rubocop/ast/node/regexp_node.rb +35 -0
- data/lib/rubocop/ast/node/send_node.rb +1 -154
- data/lib/rubocop/ast/node/super_node.rb +3 -24
- data/lib/rubocop/ast/node/yield_node.rb +21 -0
- data/lib/rubocop/ast/traversal.rb +6 -6
- data/lib/rubocop/cli.rb +7 -3
- data/lib/rubocop/config.rb +45 -8
- data/lib/rubocop/config_loader.rb +7 -5
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
- data/lib/rubocop/cop/commissioner.rb +8 -2
- data/lib/rubocop/cop/cop.rb +3 -1
- data/lib/rubocop/cop/generator.rb +94 -21
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
- data/lib/rubocop/cop/layout/align_array.rb +2 -2
- data/lib/rubocop/cop/layout/align_hash.rb +2 -2
- data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +9 -0
- data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
- data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
- data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
- data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
- data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
- data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
- data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
- data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
- data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
- data/lib/rubocop/cop/layout/tab.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
- data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
- data/lib/rubocop/cop/lint/condition_position.rb +5 -1
- data/lib/rubocop/cop/lint/debugger.rb +16 -9
- data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
- data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/empty_when.rb +1 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
- data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
- data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
- data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/rand_one.rb +7 -1
- data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
- data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
- data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
- data/lib/rubocop/cop/lint/script_permission.rb +6 -0
- data/lib/rubocop/cop/lint/syntax.rb +17 -20
- data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
- data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
- data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
- data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
- data/lib/rubocop/cop/lint/void.rb +29 -23
- data/lib/rubocop/cop/metrics/line_length.rb +2 -2
- data/lib/rubocop/cop/metrics/method_length.rb +8 -3
- data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
- data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
- data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
- data/lib/rubocop/cop/mixin/string_help.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
- data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
- data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
- data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
- data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
- data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
- data/lib/rubocop/cop/performance/caller.rb +39 -11
- data/lib/rubocop/cop/performance/casecmp.rb +4 -4
- data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
- data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
- data/lib/rubocop/cop/performance/end_with.rb +3 -3
- data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
- data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +2 -2
- data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
- data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
- data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
- data/lib/rubocop/cop/performance/size.rb +1 -1
- data/lib/rubocop/cop/performance/start_with.rb +3 -3
- data/lib/rubocop/cop/performance/times_map.rb +23 -12
- data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
- data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
- data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
- data/lib/rubocop/cop/rails/delegate.rb +36 -7
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
- data/lib/rubocop/cop/rails/file_path.rb +3 -4
- data/lib/rubocop/cop/rails/find_each.rb +1 -1
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
- data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
- data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
- data/lib/rubocop/cop/rails/request_referer.rb +2 -2
- data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
- data/lib/rubocop/cop/rails/save_bang.rb +8 -6
- data/lib/rubocop/cop/rails/scope_args.rb +1 -1
- data/lib/rubocop/cop/security/eval.rb +2 -2
- data/lib/rubocop/cop/security/json_load.rb +2 -2
- data/lib/rubocop/cop/security/marshal_load.rb +2 -2
- data/lib/rubocop/cop/security/yaml_load.rb +2 -2
- data/lib/rubocop/cop/style/alias.rb +44 -20
- data/lib/rubocop/cop/style/and_or.rb +48 -34
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +3 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
- data/lib/rubocop/cop/style/command_literal.rb +20 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
- data/lib/rubocop/cop/style/copyright.rb +10 -10
- data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
- data/lib/rubocop/cop/style/dir.rb +52 -0
- data/lib/rubocop/cop/style/documentation_method.rb +2 -6
- data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +3 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -2
- data/lib/rubocop/cop/style/empty_method.rb +27 -17
- data/lib/rubocop/cop/style/flip_flop.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +4 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
- data/lib/rubocop/cop/style/lambda.rb +19 -9
- data/lib/rubocop/cop/style/lambda_call.rb +22 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
- data/lib/rubocop/cop/style/method_missing.rb +5 -18
- data/lib/rubocop/cop/style/min_max.rb +67 -0
- data/lib/rubocop/cop/style/missing_else.rb +16 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
- data/lib/rubocop/cop/style/module_function.rb +8 -4
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
- data/lib/rubocop/cop/style/negated_if.rb +8 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
- data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
- data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
- data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
- data/lib/rubocop/cop/style/option_hash.rb +1 -1
- data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
- data/lib/rubocop/cop/style/or_assignment.rb +88 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +16 -17
- data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
- data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
- data/lib/rubocop/cop/style/redundant_return.rb +23 -11
- data/lib/rubocop/cop/style/redundant_self.rb +18 -9
- data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/return_nil.rb +98 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
- data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
- data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +5 -25
- data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
- data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
- data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
- data/lib/rubocop/cop/style/word_array.rb +7 -24
- data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
- data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
- data/lib/rubocop/cop/team.rb +16 -8
- data/lib/rubocop/cop/util.rb +11 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +71 -0
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
- data/lib/rubocop/node_pattern.rb +44 -26
- data/lib/rubocop/options.rb +1 -0
- data/lib/rubocop/processed_source.rb +3 -1
- data/lib/rubocop/remote_config.rb +5 -1
- data/lib/rubocop/result_cache.rb +1 -0
- data/lib/rubocop/rspec/cop_helper.rb +10 -10
- data/lib/rubocop/rspec/expect_offense.rb +6 -8
- data/lib/rubocop/rspec/shared_examples.rb +8 -8
- data/lib/rubocop/string_util.rb +2 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +51 -18
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
- data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -9,18 +9,34 @@ module RuboCop
|
|
9
9
|
#
|
10
10
|
# @example
|
11
11
|
#
|
12
|
+
# # EnforcedStyle: all_comparison_operators
|
13
|
+
#
|
12
14
|
# # bad
|
13
15
|
# 99 == foo
|
14
|
-
# "bar"
|
16
|
+
# "bar" != foo
|
15
17
|
# 42 >= foo
|
16
|
-
#
|
17
|
-
# @example
|
18
|
+
# 10 < bar
|
18
19
|
#
|
19
20
|
# # good
|
20
21
|
# foo == 99
|
21
22
|
# foo == "bar"
|
22
|
-
#
|
23
|
+
# foo <= 42
|
24
|
+
# bar > 10
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
#
|
28
|
+
# # EnforcedStyle: equality_operators_only
|
29
|
+
#
|
30
|
+
# # bad
|
31
|
+
# 99 == foo
|
32
|
+
# "bar" != foo
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# 99 >= foo
|
36
|
+
# 3 < a && a < 5
|
23
37
|
class YodaCondition < Cop
|
38
|
+
include ConfigurableEnforcedStyle
|
39
|
+
|
24
40
|
MSG = 'Reverse the order of the operands `%s`.'.freeze
|
25
41
|
|
26
42
|
REVERSE_COMPARISON = {
|
@@ -30,26 +46,33 @@ module RuboCop
|
|
30
46
|
'>=' => '<='
|
31
47
|
}.freeze
|
32
48
|
|
49
|
+
EQUALITY_OPERATORS = %i[== !=].freeze
|
50
|
+
|
51
|
+
NONCOMMUTATIVE_OPERATORS = %i[===].freeze
|
52
|
+
|
33
53
|
def on_send(node)
|
34
54
|
return unless yoda_condition?(node)
|
35
55
|
|
36
|
-
|
56
|
+
add_offense(node)
|
37
57
|
end
|
38
58
|
|
39
59
|
private
|
40
60
|
|
41
61
|
def yoda_condition?(node)
|
42
|
-
return false unless
|
62
|
+
return false unless node.comparison_method?
|
43
63
|
|
44
|
-
|
45
|
-
|
64
|
+
lhs, operator, rhs = *node
|
65
|
+
if check_equality_only?
|
66
|
+
return false if non_equality_operator?(operator)
|
67
|
+
end
|
68
|
+
|
69
|
+
return false if noncommutative_operator?(operator)
|
46
70
|
|
47
|
-
|
48
|
-
RuboCop::AST::Node::COMPARISON_OPERATORS.include?(node.method_name)
|
71
|
+
lhs.literal? && !rhs.literal?
|
49
72
|
end
|
50
73
|
|
51
|
-
def
|
52
|
-
|
74
|
+
def message(node)
|
75
|
+
format(MSG, node.source)
|
53
76
|
end
|
54
77
|
|
55
78
|
def autocorrect(node)
|
@@ -59,8 +82,8 @@ module RuboCop
|
|
59
82
|
end
|
60
83
|
|
61
84
|
def corrected_code(node)
|
62
|
-
|
63
|
-
"#{
|
85
|
+
lhs, operator, rhs = *node
|
86
|
+
"#{rhs.source} #{reverse_comparison(operator)} #{lhs.source}"
|
64
87
|
end
|
65
88
|
|
66
89
|
def actual_code_range(node)
|
@@ -72,6 +95,18 @@ module RuboCop
|
|
72
95
|
def reverse_comparison(operator)
|
73
96
|
REVERSE_COMPARISON.fetch(operator.to_s, operator)
|
74
97
|
end
|
98
|
+
|
99
|
+
def check_equality_only?
|
100
|
+
style == :equality_operators_only
|
101
|
+
end
|
102
|
+
|
103
|
+
def non_equality_operator?(operator)
|
104
|
+
!EQUALITY_OPERATORS.include?(operator)
|
105
|
+
end
|
106
|
+
|
107
|
+
def noncommutative_operator?(operator)
|
108
|
+
NONCOMMUTATIVE_OPERATORS.include?(operator)
|
109
|
+
end
|
75
110
|
end
|
76
111
|
end
|
77
112
|
end
|
@@ -31,34 +31,41 @@ module RuboCop
|
|
31
31
|
NONZERO_MSG = 'Use `!empty?` instead of `%s %s %s`.'.freeze
|
32
32
|
|
33
33
|
def on_send(node)
|
34
|
+
check_zero_length_predicate(node)
|
35
|
+
check_nonzero_length_predicate(node)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def check_zero_length_predicate(node)
|
34
41
|
zero_length_predicate = zero_length_predicate(node)
|
35
42
|
|
36
|
-
|
37
|
-
add_offense(node, :expression,
|
38
|
-
format(ZERO_MSG, *zero_length_predicate))
|
39
|
-
end
|
43
|
+
return unless zero_length_predicate
|
40
44
|
|
45
|
+
add_offense(node, :expression,
|
46
|
+
format(ZERO_MSG, *zero_length_predicate))
|
47
|
+
end
|
48
|
+
|
49
|
+
def check_nonzero_length_predicate(node)
|
41
50
|
nonzero_length_predicate = nonzero_length_predicate(node)
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
48
|
-
# rubocop:enable Style/GuardClause
|
52
|
+
return unless nonzero_length_predicate
|
53
|
+
|
54
|
+
add_offense(node, :expression,
|
55
|
+
format(NONZERO_MSG, *nonzero_length_predicate))
|
49
56
|
end
|
50
57
|
|
51
|
-
def_node_matcher :zero_length_predicate, <<-
|
58
|
+
def_node_matcher :zero_length_predicate, <<-PATTERN
|
52
59
|
{(send (send (...) ${:length :size}) $:== (int $0))
|
53
60
|
(send (int $0) $:== (send (...) ${:length :size}))
|
54
61
|
(send (send (...) ${:length :size}) $:< (int $1))
|
55
62
|
(send (int $1) $:> (send (...) ${:length :size}))}
|
56
|
-
|
63
|
+
PATTERN
|
57
64
|
|
58
|
-
def_node_matcher :nonzero_length_predicate, <<-
|
65
|
+
def_node_matcher :nonzero_length_predicate, <<-PATTERN
|
59
66
|
{(send (send (...) ${:length :size}) ${:> :!=} (int $0))
|
60
67
|
(send (int $0) ${:< :!=} (send (...) ${:length :size}))}
|
61
|
-
|
68
|
+
PATTERN
|
62
69
|
|
63
70
|
def autocorrect(node)
|
64
71
|
lambda do |corrector|
|
@@ -73,17 +80,17 @@ module RuboCop
|
|
73
80
|
"!#{other_receiver(node).source}.empty?"
|
74
81
|
end
|
75
82
|
|
76
|
-
def_node_matcher :zero_length_receiver, <<-
|
83
|
+
def_node_matcher :zero_length_receiver, <<-PATTERN
|
77
84
|
{(send (send $_ _) :== (int 0))
|
78
85
|
(send (int 0) :== (send $_ _))
|
79
86
|
(send (send $_ _) :< (int 1))
|
80
87
|
(send (int 1) :> (send $_ _))}
|
81
|
-
|
88
|
+
PATTERN
|
82
89
|
|
83
|
-
def_node_matcher :other_receiver, <<-
|
90
|
+
def_node_matcher :other_receiver, <<-PATTERN
|
84
91
|
{(send (send $_ _) _ _)
|
85
92
|
(send _ _ (send $_ _))}
|
86
|
-
|
93
|
+
PATTERN
|
87
94
|
end
|
88
95
|
end
|
89
96
|
end
|
data/lib/rubocop/cop/team.rb
CHANGED
@@ -36,7 +36,9 @@ module RuboCop
|
|
36
36
|
def inspect_file(processed_source)
|
37
37
|
# If we got any syntax errors, return only the syntax offenses.
|
38
38
|
unless processed_source.valid_syntax?
|
39
|
-
return Lint::Syntax.offenses_from_processed_source(
|
39
|
+
return Lint::Syntax.offenses_from_processed_source(
|
40
|
+
processed_source, @config, @options
|
41
|
+
)
|
40
42
|
end
|
41
43
|
|
42
44
|
offenses(processed_source)
|
@@ -117,14 +119,8 @@ module RuboCop
|
|
117
119
|
|
118
120
|
def autocorrect_all_cops(buffer, cops)
|
119
121
|
corrector = Corrector.new(buffer)
|
120
|
-
skip = Set.new
|
121
122
|
|
122
|
-
cops
|
123
|
-
next if cop.corrections.empty?
|
124
|
-
next if skip.include?(cop.class)
|
125
|
-
corrector.corrections.concat(cop.corrections)
|
126
|
-
skip.merge(cop.class.autocorrect_incompatible_with)
|
127
|
-
end
|
123
|
+
collate_corrections(corrector, cops)
|
128
124
|
|
129
125
|
if !corrector.corrections.empty?
|
130
126
|
corrector.rewrite
|
@@ -133,6 +129,18 @@ module RuboCop
|
|
133
129
|
end
|
134
130
|
end
|
135
131
|
|
132
|
+
def collate_corrections(corrector, cops)
|
133
|
+
skips = Set.new
|
134
|
+
|
135
|
+
cops.each do |cop|
|
136
|
+
next if cop.corrections.empty?
|
137
|
+
next if skips.include?(cop.class)
|
138
|
+
|
139
|
+
corrector.corrections.concat(cop.corrections)
|
140
|
+
skips.merge(cop.class.autocorrect_incompatible_with)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
136
144
|
def validate_config
|
137
145
|
cops.each do |cop|
|
138
146
|
cop.validate_config if cop.respond_to?(:validate_config)
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -304,6 +304,17 @@ module RuboCop
|
|
304
304
|
.sub(/^Enforced/, 'Supported')
|
305
305
|
.sub('Style', 'Styles')
|
306
306
|
end
|
307
|
+
|
308
|
+
def scrub_string(string)
|
309
|
+
if string.respond_to?(:scrub)
|
310
|
+
string.scrub
|
311
|
+
else
|
312
|
+
string
|
313
|
+
.encode('UTF-16BE', 'UTF-8',
|
314
|
+
invalid: :replace, undef: :replace, replace: '?')
|
315
|
+
.encode('UTF-8')
|
316
|
+
end
|
317
|
+
end
|
307
318
|
end
|
308
319
|
end
|
309
320
|
end
|
@@ -19,7 +19,8 @@ module RuboCop
|
|
19
19
|
'files' => FileListFormatter,
|
20
20
|
'offenses' => OffenseCountFormatter,
|
21
21
|
'disabled' => DisabledLinesFormatter,
|
22
|
-
'worst' => WorstOffendersFormatter
|
22
|
+
'worst' => WorstOffendersFormatter,
|
23
|
+
'tap' => TapFormatter
|
23
24
|
}.freeze
|
24
25
|
|
25
26
|
FORMATTER_APIS = %i[started finished].freeze
|
@@ -27,6 +27,7 @@ module RuboCop
|
|
27
27
|
report_summary(@offense_counts)
|
28
28
|
end
|
29
29
|
|
30
|
+
# rubocop:disable Metrics/AbcSize
|
30
31
|
def report_summary(offense_counts)
|
31
32
|
per_cop_counts = ordered_offense_counts(offense_counts)
|
32
33
|
total_count = total_offense_count(offense_counts)
|
@@ -42,6 +43,7 @@ module RuboCop
|
|
42
43
|
|
43
44
|
output.puts
|
44
45
|
end
|
46
|
+
# rubocop:enable Metrics/AbcSize
|
45
47
|
|
46
48
|
def ordered_offense_counts(offense_counts)
|
47
49
|
Hash[offense_counts.sort_by { |k, v| [-v, k] }]
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Formatter
|
5
|
+
# This formatter formats report data using the Test Anything Protocol.
|
6
|
+
# TAP allows for to communicate tests results in a language agnostics way.
|
7
|
+
class TapFormatter < ClangStyleFormatter
|
8
|
+
def started(target_files)
|
9
|
+
super
|
10
|
+
@progress_count = 1
|
11
|
+
output.puts "1..#{target_files.size}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_finished(file, offenses)
|
15
|
+
if offenses.empty?
|
16
|
+
output.puts "ok #{@progress_count} - #{smart_path(file)}"
|
17
|
+
else
|
18
|
+
output.puts "not ok #{@progress_count} - #{smart_path(file)}"
|
19
|
+
|
20
|
+
count_stats(offenses)
|
21
|
+
report_file(file, offenses)
|
22
|
+
end
|
23
|
+
|
24
|
+
@progress_count += 1
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def report_line(location)
|
30
|
+
source_line = location.source_line
|
31
|
+
|
32
|
+
if location.first_line == location.last_line
|
33
|
+
output.puts("# #{source_line}")
|
34
|
+
else
|
35
|
+
output.puts("# #{source_line} #{yellow(ELLIPSES)}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def report_highlighted_area(highlighted_area)
|
40
|
+
output.puts("# #{' ' * highlighted_area.begin_pos}" \
|
41
|
+
"#{'^' * highlighted_area.size}")
|
42
|
+
end
|
43
|
+
|
44
|
+
def report_offense(file, offense)
|
45
|
+
output.printf("# %s:%d:%d: %s: %s\n",
|
46
|
+
cyan(smart_path(file)), offense.line, offense.real_column,
|
47
|
+
colored_severity_code(offense), message(offense))
|
48
|
+
|
49
|
+
# rubocop:disable Lint/HandleExceptions
|
50
|
+
begin
|
51
|
+
return unless valid_line?(offense)
|
52
|
+
|
53
|
+
report_line(offense.location)
|
54
|
+
report_highlighted_area(offense.highlighted_area)
|
55
|
+
rescue IndexError
|
56
|
+
# range is not on a valid line; perhaps the source file is empty
|
57
|
+
end
|
58
|
+
# rubocop:enable Lint/HandleExceptions
|
59
|
+
end
|
60
|
+
|
61
|
+
def annotate_message(msg)
|
62
|
+
msg.gsub(/`(.*?)`/, '\1')
|
63
|
+
end
|
64
|
+
|
65
|
+
def message(offense)
|
66
|
+
message = offense.corrected? ? '[Corrected] ' : ''
|
67
|
+
"#{message}#{annotate_message(offense.message)}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -32,6 +32,7 @@ module RuboCop
|
|
32
32
|
report_summary(@offense_counts)
|
33
33
|
end
|
34
34
|
|
35
|
+
# rubocop:disable Metrics/AbcSize
|
35
36
|
def report_summary(offense_counts)
|
36
37
|
per_file_counts = ordered_offense_counts(offense_counts)
|
37
38
|
total_count = total_offense_count(offense_counts)
|
@@ -47,6 +48,7 @@ module RuboCop
|
|
47
48
|
|
48
49
|
output.puts
|
49
50
|
end
|
51
|
+
# rubocop:enable Metrics/AbcSize
|
50
52
|
|
51
53
|
def ordered_offense_counts(offense_counts)
|
52
54
|
Hash[offense_counts.sort_by { |k, v| [-v, k] }]
|
data/lib/rubocop/node_pattern.rb
CHANGED
@@ -98,19 +98,26 @@ module RuboCop
|
|
98
98
|
# @private
|
99
99
|
# Builds Ruby code which implements a pattern
|
100
100
|
class Compiler
|
101
|
-
|
102
|
-
|
103
|
-
META
|
104
|
-
NUMBER
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
101
|
+
SYMBOL = %r{:(?:[\w+@*/?!<>=~|%^-]+|\[\]=?)}
|
102
|
+
IDENTIFIER = /[a-zA-Z_-]/
|
103
|
+
META = /\(|\)|\{|\}|\[|\]|\$\.\.\.|\$|!|\^|\.\.\./
|
104
|
+
NUMBER = /-?\d+(?:\.\d+)?/
|
105
|
+
STRING = /".+?"/
|
106
|
+
METHOD_NAME = /\#?#{IDENTIFIER}+[\!\?]?\(?/
|
107
|
+
PARAM_NUMBER = /%\d*/
|
108
|
+
|
109
|
+
SEPARATORS = /[\s]+/
|
110
|
+
TOKENS = Regexp.union(META, PARAM_NUMBER, NUMBER,
|
111
|
+
METHOD_NAME, SYMBOL, STRING)
|
112
|
+
|
113
|
+
TOKEN = /\G(?:#{SEPARATORS}|#{TOKENS}|.)/
|
114
|
+
|
115
|
+
NODE = /\A#{IDENTIFIER}+\Z/
|
116
|
+
PREDICATE = /\A#{IDENTIFIER}+\?\(?\Z/
|
117
|
+
WILDCARD = /\A_#{IDENTIFIER}*\Z/
|
118
|
+
FUNCALL = /\A\##{METHOD_NAME}/
|
119
|
+
LITERAL = /\A(?:#{SYMBOL}|#{NUMBER}|#{STRING}|nil)\Z/
|
120
|
+
PARAM = /\A#{PARAM_NUMBER}\Z/
|
114
121
|
CLOSING = /\A(?:\)|\}|\])\Z/
|
115
122
|
|
116
123
|
attr_reader :match_code
|
@@ -128,9 +135,11 @@ module RuboCop
|
|
128
135
|
end
|
129
136
|
|
130
137
|
def run(node_var)
|
131
|
-
tokens =
|
132
|
-
|
138
|
+
tokens =
|
139
|
+
@string.scan(TOKEN).reject { |token| token =~ /\A#{SEPARATORS}\Z/ }
|
140
|
+
|
133
141
|
@match_code = compile_expr(tokens, node_var, false)
|
142
|
+
|
134
143
|
fail_due_to('unbalanced pattern') unless tokens.empty?
|
135
144
|
end
|
136
145
|
|
@@ -158,7 +167,7 @@ module RuboCop
|
|
158
167
|
when PARAM then compile_param(cur_node, token[1..-1], seq_head)
|
159
168
|
when CLOSING then fail_due_to("#{token} in invalid position")
|
160
169
|
when nil then fail_due_to('pattern ended prematurely')
|
161
|
-
else
|
170
|
+
else fail_due_to("invalid token #{token.inspect}")
|
162
171
|
end
|
163
172
|
end
|
164
173
|
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
@@ -336,7 +345,8 @@ module RuboCop
|
|
336
345
|
else
|
337
346
|
n = @unify[name] = next_temp_value
|
338
347
|
# double assign to temp#{n} to avoid "assigned but unused variable"
|
339
|
-
"(temp#{n} =
|
348
|
+
"(temp#{n} = #{cur_node}#{'.type' if seq_head}; " \
|
349
|
+
"temp#{n} = temp#{n}; true)"
|
340
350
|
end
|
341
351
|
end
|
342
352
|
|
@@ -433,11 +443,17 @@ module RuboCop
|
|
433
443
|
params.empty? ? '' : ",#{params}"
|
434
444
|
end
|
435
445
|
|
446
|
+
def emit_guard_clause
|
447
|
+
<<-RUBY
|
448
|
+
return unless node.is_a?(RuboCop::AST::Node)
|
449
|
+
RUBY
|
450
|
+
end
|
451
|
+
|
436
452
|
def emit_method_code
|
437
|
-
<<-
|
438
|
-
return
|
453
|
+
<<-RUBY
|
454
|
+
return unless #{@match_code}
|
439
455
|
block_given? ? yield(#{emit_capture_list}) : (return #{emit_retval})
|
440
|
-
|
456
|
+
RUBY
|
441
457
|
end
|
442
458
|
|
443
459
|
def fail_due_to(message)
|
@@ -447,7 +463,7 @@ module RuboCop
|
|
447
463
|
def with_temp_node(cur_node)
|
448
464
|
with_temp_variable do |temp_var|
|
449
465
|
# double assign to temp#{n} to avoid "assigned but unused variable"
|
450
|
-
yield "#{temp_var} = #{temp_var} = #{
|
466
|
+
yield "#{temp_var} = #{cur_node}; #{temp_var} = #{temp_var}", temp_var
|
451
467
|
end
|
452
468
|
end
|
453
469
|
|
@@ -459,6 +475,7 @@ module RuboCop
|
|
459
475
|
@temps += 1
|
460
476
|
end
|
461
477
|
end
|
478
|
+
private_constant :Compiler
|
462
479
|
|
463
480
|
# Helpers for defining methods based on a pattern string
|
464
481
|
module Macros
|
@@ -470,9 +487,10 @@ module RuboCop
|
|
470
487
|
# If the node matches, and no block is provided, the new method will
|
471
488
|
# return the captures, or `true` if there were none.
|
472
489
|
def def_node_matcher(method_name, pattern_str)
|
473
|
-
compiler =
|
474
|
-
src = "def #{method_name}(node" \
|
490
|
+
compiler = Compiler.new(pattern_str, 'node')
|
491
|
+
src = "def #{method_name}(node = self" \
|
475
492
|
"#{compiler.emit_trailing_params});" \
|
493
|
+
"#{compiler.emit_guard_clause}" \
|
476
494
|
"#{compiler.emit_method_code};end"
|
477
495
|
|
478
496
|
location = caller_locations(1, 1).first
|
@@ -486,7 +504,7 @@ module RuboCop
|
|
486
504
|
# as soon as it finds a descendant which matches. Otherwise, it will
|
487
505
|
# yield all descendants which match.
|
488
506
|
def def_node_search(method_name, pattern_str)
|
489
|
-
compiler =
|
507
|
+
compiler = Compiler.new(pattern_str, 'node')
|
490
508
|
called_from = caller(1..1).first.split(':')
|
491
509
|
|
492
510
|
if method_name.to_s.end_with?('?')
|
@@ -519,7 +537,7 @@ module RuboCop
|
|
519
537
|
|
520
538
|
def node_search_body(method_name, trailing_params, prelude, match_code,
|
521
539
|
on_match)
|
522
|
-
<<-
|
540
|
+
<<-RUBY
|
523
541
|
def #{method_name}(node0#{trailing_params})
|
524
542
|
#{prelude}
|
525
543
|
node0.each_node do |node|
|
@@ -529,7 +547,7 @@ module RuboCop
|
|
529
547
|
end
|
530
548
|
nil
|
531
549
|
end
|
532
|
-
|
550
|
+
RUBY
|
533
551
|
end
|
534
552
|
end
|
535
553
|
|