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
@@ -34,10 +34,10 @@ module RuboCop
|
|
34
34
|
# private def my_method
|
35
35
|
# which are allowed in Ruby 2.1 and later.
|
36
36
|
def modifier_and_def_on_same_line?(send_node)
|
37
|
-
send_node.receiver
|
37
|
+
!send_node.receiver &&
|
38
38
|
send_node.method_name != :def &&
|
39
|
-
send_node.
|
40
|
-
|
39
|
+
send_node.arguments.one? &&
|
40
|
+
%i(def defs).include?(send_node.first_argument.type)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -4,6 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
# Common functionality for handling percent literals.
|
6
6
|
module PercentLiteral
|
7
|
+
private
|
8
|
+
|
7
9
|
def percent_literal?(node)
|
8
10
|
return unless (begin_source = begin_source(node))
|
9
11
|
begin_source.start_with?('%')
|
@@ -27,6 +29,31 @@ module RuboCop
|
|
27
29
|
def contents_range(node)
|
28
30
|
range_between(node.loc.begin.end_pos, node.loc.end.begin_pos)
|
29
31
|
end
|
32
|
+
|
33
|
+
# ['a', 'b', 'c'] => %w(a b c)
|
34
|
+
def correct_percent(node, char)
|
35
|
+
words = node.children
|
36
|
+
escape = words.any? { |w| needs_escaping?(w.children[0]) }
|
37
|
+
char = char.upcase if escape
|
38
|
+
contents = autocorrect_words(words, escape, node.loc.line)
|
39
|
+
|
40
|
+
lambda do |corrector|
|
41
|
+
corrector.replace(node.source_range, "%#{char}(#{contents})")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def autocorrect_words(word_nodes, escape, base_line_number)
|
46
|
+
previous_node_line_number = base_line_number
|
47
|
+
word_nodes.map do |node|
|
48
|
+
number_of_line_breaks = node.loc.line - previous_node_line_number
|
49
|
+
line_breaks = "\n" * number_of_line_breaks
|
50
|
+
previous_node_line_number = node.loc.line
|
51
|
+
content = node.children.first.to_s
|
52
|
+
content = escape ? escape_string(content) : content
|
53
|
+
content.gsub!(/\)/, '\\)')
|
54
|
+
line_breaks + content
|
55
|
+
end.join(' ')
|
56
|
+
end
|
30
57
|
end
|
31
58
|
end
|
32
59
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common functionality for checking `rescue` nodes.
|
6
|
+
module RescueNode
|
7
|
+
def investigate(processed_source)
|
8
|
+
@modifier_locations = processed_source
|
9
|
+
.tokens
|
10
|
+
.select { |t| t.type == :kRESCUE_MOD }
|
11
|
+
.map(&:pos)
|
12
|
+
end
|
13
|
+
|
14
|
+
def rescue_modifier?(node)
|
15
|
+
node &&
|
16
|
+
node.resbody_type? &&
|
17
|
+
@modifier_locations.include?(node.loc.keyword)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common functionality for checking target rails version.
|
6
|
+
module TargetRailsVersion
|
7
|
+
def minimum_target_rails_version(version)
|
8
|
+
@minimum_target_rails_version = version
|
9
|
+
end
|
10
|
+
|
11
|
+
def support_target_rails_version?(version)
|
12
|
+
@minimum_target_rails_version <= version
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/rubocop/cop/offense.rb
CHANGED
@@ -7,8 +7,8 @@ module RuboCop
|
|
7
7
|
include Comparable
|
8
8
|
|
9
9
|
# @api private
|
10
|
-
COMPARISON_ATTRIBUTES =
|
11
|
-
|
10
|
+
COMPARISON_ATTRIBUTES = %i(line column cop_name
|
11
|
+
message severity).freeze
|
12
12
|
|
13
13
|
# @api public
|
14
14
|
#
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
# @return [Boolean]
|
73
73
|
# whether this offense is automatically corrected.
|
74
74
|
def corrected
|
75
|
-
@status == :unsupported ?
|
75
|
+
@status == :unsupported ? false : @status == :corrected
|
76
76
|
end
|
77
77
|
alias corrected? corrected
|
78
78
|
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# 'abc'.casecmp(str).zero?
|
20
20
|
class Casecmp < Cop
|
21
21
|
MSG = 'Use `casecmp` instead of `%s %s`.'.freeze
|
22
|
-
CASE_METHODS =
|
22
|
+
CASE_METHODS = %i(downcase upcase).freeze
|
23
23
|
|
24
24
|
def_node_matcher :downcase_eq, <<-END
|
25
25
|
(send
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
receiver,
|
31
31
|
method,
|
32
32
|
first_call_args,
|
33
|
-
second_call_args =
|
33
|
+
second_call_args = process_source(node)
|
34
34
|
|
35
35
|
return unless receiver && second_call_args.all?(&:pure?)
|
36
36
|
|
@@ -39,8 +39,30 @@ module RuboCop
|
|
39
39
|
add_offense_for_double_call(node, receiver, method, combined_args)
|
40
40
|
end
|
41
41
|
|
42
|
+
def autocorrect(node)
|
43
|
+
_receiver, _method,
|
44
|
+
first_call_args, second_call_args = process_source(node)
|
45
|
+
|
46
|
+
combined_args = combine_args(first_call_args, second_call_args)
|
47
|
+
first_argument = first_call_args.first.loc.expression
|
48
|
+
last_argument = second_call_args.last.loc.expression
|
49
|
+
range = first_argument.join(last_argument)
|
50
|
+
|
51
|
+
lambda do |corrector|
|
52
|
+
corrector.replace(range, combined_args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
42
56
|
private
|
43
57
|
|
58
|
+
def process_source(node)
|
59
|
+
if check_for_active_support_aliases?
|
60
|
+
check_with_active_support_aliases(node)
|
61
|
+
else
|
62
|
+
two_start_end_with_calls(node)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
44
66
|
def combine_args(first_call_args, second_call_args)
|
45
67
|
(first_call_args + second_call_args).map(&:source).join(', ')
|
46
68
|
end
|
@@ -57,11 +79,23 @@ module RuboCop
|
|
57
79
|
))
|
58
80
|
end
|
59
81
|
|
82
|
+
def check_for_active_support_aliases?
|
83
|
+
cop_config['IncludeActiveSupportAliases']
|
84
|
+
end
|
85
|
+
|
60
86
|
def_node_matcher :two_start_end_with_calls, <<-END
|
61
87
|
(or
|
62
88
|
(send $_recv [{:start_with? :end_with?} $_method] $...)
|
63
89
|
(send _recv _method $...))
|
64
90
|
END
|
91
|
+
|
92
|
+
def_node_matcher :check_with_active_support_aliases, <<-END
|
93
|
+
(or
|
94
|
+
(send $_recv
|
95
|
+
[{:start_with? :starts_with? :end_with? :ends_with?} $_method]
|
96
|
+
$...)
|
97
|
+
(send _recv _method $...))
|
98
|
+
END
|
65
99
|
end
|
66
100
|
end
|
67
101
|
end
|
@@ -27,8 +27,7 @@ module RuboCop
|
|
27
27
|
def on_send(node)
|
28
28
|
flat_map_candidate?(node) do |map_node, first_method, flatten, params|
|
29
29
|
flatten_level, = *params.first
|
30
|
-
if cop_config['EnabledForFlattenWithoutParams'] &&
|
31
|
-
flatten_level.nil?
|
30
|
+
if cop_config['EnabledForFlattenWithoutParams'] && !flatten_level
|
32
31
|
offense_for_levels(node, map_node, first_method, flatten)
|
33
32
|
elsif flatten_level == 1
|
34
33
|
offense_for_method(node, map_node, first_method, flatten)
|
@@ -39,12 +38,13 @@ module RuboCop
|
|
39
38
|
def autocorrect(node)
|
40
39
|
map_node, _first_method, _flatten, params = flat_map_candidate?(node)
|
41
40
|
flatten_level, = *params.first
|
42
|
-
return if flatten_level.nil?
|
43
41
|
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
return unless flatten_level
|
43
|
+
|
44
|
+
range = range_between(node.loc.dot.begin_pos,
|
45
|
+
node.source_range.end_pos)
|
47
46
|
|
47
|
+
lambda do |corrector|
|
48
48
|
corrector.remove(range)
|
49
49
|
corrector.replace(map_node.loc.selector, 'flat_map')
|
50
50
|
end
|
@@ -30,9 +30,9 @@ module RuboCop
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def autocorrect(node)
|
33
|
-
|
34
|
-
range = range_between(first_send.loc.selector.begin_pos,
|
33
|
+
range = range_between(node.receiver.loc.selector.begin_pos,
|
35
34
|
node.source_range.end_pos)
|
35
|
+
|
36
36
|
->(corrector) { corrector.replace(range, 'strip') }
|
37
37
|
end
|
38
38
|
end
|
@@ -35,17 +35,18 @@ module RuboCop
|
|
35
35
|
return unless match_call?(node) &&
|
36
36
|
(!node.value_used? || only_truthiness_matters?(node)) &&
|
37
37
|
!(node.parent && node.parent.block_type?)
|
38
|
-
|
38
|
+
|
39
|
+
add_offense(node, :expression)
|
39
40
|
end
|
40
41
|
|
41
42
|
def autocorrect(node)
|
42
|
-
receiver, _method, arg = *node
|
43
|
-
|
44
43
|
# Regexp#match can take a second argument, but this cop doesn't
|
45
44
|
# register an offense in that case
|
46
|
-
return unless
|
45
|
+
return unless node.first_argument.regexp_type?
|
46
|
+
|
47
|
+
new_source =
|
48
|
+
node.receiver.source + ' =~ ' + node.first_argument.source
|
47
49
|
|
48
|
-
new_source = receiver.source + ' =~ ' + arg.source
|
49
50
|
->(corrector) { corrector.replace(node.source_range, new_source) }
|
50
51
|
end
|
51
52
|
end
|
@@ -102,7 +102,7 @@ module RuboCop
|
|
102
102
|
(send (const nil :Regexp) :last_match)
|
103
103
|
(send (const nil :Regexp) :last_match _)
|
104
104
|
({back_ref nth_ref} _)
|
105
|
-
(gvar #
|
105
|
+
(gvar #match_gvar?)
|
106
106
|
}
|
107
107
|
PATTERN
|
108
108
|
|
@@ -190,8 +190,15 @@ module RuboCop
|
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
193
|
-
def
|
194
|
-
|
193
|
+
def match_gvar?(sym)
|
194
|
+
%i(
|
195
|
+
$~
|
196
|
+
$MATCH
|
197
|
+
$PREMATCH
|
198
|
+
$POSTMATCH
|
199
|
+
$LAST_PAREN_MATCH
|
200
|
+
$LAST_MATCH_INFO
|
201
|
+
).include?(sym)
|
195
202
|
end
|
196
203
|
|
197
204
|
def correct_operator(corrector, recv, arg)
|
@@ -39,12 +39,9 @@ module RuboCop
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def eligible_node?(node)
|
42
|
-
|
42
|
+
return false unless node.method?(:count) && !node.arguments?
|
43
43
|
|
44
|
-
|
45
|
-
return false if args
|
46
|
-
|
47
|
-
eligible_receiver?(receiver) && !allowed_parent?(node.parent)
|
44
|
+
eligible_receiver?(node.receiver) && !allowed_parent?(node.parent)
|
48
45
|
end
|
49
46
|
|
50
47
|
def eligible_receiver?(node)
|
@@ -58,17 +55,15 @@ module RuboCop
|
|
58
55
|
end
|
59
56
|
|
60
57
|
def array?(node)
|
61
|
-
|
62
|
-
_, constant = *receiver
|
58
|
+
_, constant = *node.receiver
|
63
59
|
|
64
|
-
node.array_type? || constant == :Array ||
|
60
|
+
node.array_type? || constant == :Array || node.method_name == :to_a
|
65
61
|
end
|
66
62
|
|
67
63
|
def hash?(node)
|
68
|
-
|
69
|
-
_, constant = *receiver
|
64
|
+
_, constant = *node.receiver
|
70
65
|
|
71
|
-
node.hash_type? || constant == :Hash ||
|
66
|
+
node.hash_type? || constant == :Hash || node.method_name == :to_h
|
72
67
|
end
|
73
68
|
end
|
74
69
|
end
|
@@ -27,22 +27,22 @@ module RuboCop
|
|
27
27
|
SINGLE_QUOTE = "'".freeze
|
28
28
|
|
29
29
|
def_node_matcher :string_replacement?, <<-PATTERN
|
30
|
-
(send _
|
30
|
+
(send _ {:gsub :gsub!}
|
31
31
|
${regexp str (send (const nil :Regexp) {:new :compile} _)}
|
32
32
|
$str)
|
33
33
|
PATTERN
|
34
34
|
|
35
35
|
def on_send(node)
|
36
|
-
string_replacement?(node) do |
|
36
|
+
string_replacement?(node) do |first_param, second_param|
|
37
37
|
return if accept_second_param?(second_param)
|
38
38
|
return if accept_first_param?(first_param)
|
39
39
|
|
40
|
-
offense(node,
|
40
|
+
offense(node, first_param, second_param)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
def autocorrect(node)
|
45
|
-
_string,
|
45
|
+
_string, _method, first_param, second_param = *node
|
46
46
|
first_source, = first_source(first_param)
|
47
47
|
second_source, = *second_param
|
48
48
|
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
end
|
52
52
|
|
53
53
|
replacement_method =
|
54
|
-
replacement_method(
|
54
|
+
replacement_method(node, first_source, second_source)
|
55
55
|
|
56
56
|
replace_method(node, first_source, second_source, first_param,
|
57
57
|
replacement_method)
|
@@ -93,13 +93,13 @@ module RuboCop
|
|
93
93
|
first_source.length != 1
|
94
94
|
end
|
95
95
|
|
96
|
-
def offense(node,
|
96
|
+
def offense(node, first_param, second_param)
|
97
97
|
first_source, = first_source(first_param)
|
98
98
|
unless first_param.str_type?
|
99
99
|
first_source = interpret_string_escapes(first_source)
|
100
100
|
end
|
101
101
|
second_source, = *second_param
|
102
|
-
message = message(
|
102
|
+
message = message(node, first_source, second_source)
|
103
103
|
|
104
104
|
add_offense(node, range(node), message)
|
105
105
|
end
|
@@ -137,26 +137,21 @@ module RuboCop
|
|
137
137
|
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
|
138
138
|
end
|
139
139
|
|
140
|
-
def replacement_method(
|
140
|
+
def replacement_method(node, first_source, second_source)
|
141
141
|
replacement = if second_source.empty? && first_source.length == 1
|
142
142
|
DELETE
|
143
143
|
else
|
144
144
|
TR
|
145
145
|
end
|
146
146
|
|
147
|
-
"#{replacement}#{BANG if bang_method?
|
147
|
+
"#{replacement}#{BANG if node.bang_method?}"
|
148
148
|
end
|
149
149
|
|
150
|
-
def message(
|
151
|
-
replacement_method =
|
152
|
-
|
153
|
-
second_source)
|
154
|
-
|
155
|
-
format(MSG, replacement_method, method)
|
156
|
-
end
|
150
|
+
def message(node, first_source, second_source)
|
151
|
+
replacement_method =
|
152
|
+
replacement_method(node, first_source, second_source)
|
157
153
|
|
158
|
-
|
159
|
-
method.to_s.end_with?(BANG)
|
154
|
+
format(MSG, replacement_method, node.method_name)
|
160
155
|
end
|
161
156
|
|
162
157
|
def method_suffix(node)
|