rubocop 0.75.1 → 0.80.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +4 -4
- data/config/default.yml +374 -335
- data/lib/rubocop.rb +53 -32
- data/lib/rubocop/ast/builder.rb +43 -41
- data/lib/rubocop/ast/node.rb +5 -13
- data/lib/rubocop/ast/node/block_node.rb +2 -0
- data/lib/rubocop/ast/node/def_node.rb +11 -0
- data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
- data/lib/rubocop/ast/node/regexp_node.rb +2 -4
- data/lib/rubocop/ast/node/return_node.rb +24 -0
- data/lib/rubocop/ast/traversal.rb +20 -3
- data/lib/rubocop/cli.rb +11 -227
- data/lib/rubocop/cli/command.rb +21 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
- data/lib/rubocop/cli/command/base.rb +33 -0
- data/lib/rubocop/cli/command/execute_runner.rb +76 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
- data/lib/rubocop/cli/command/show_cops.rb +80 -0
- data/lib/rubocop/cli/command/version.rb +17 -0
- data/lib/rubocop/cli/environment.rb +21 -0
- data/lib/rubocop/comment_config.rb +8 -3
- data/lib/rubocop/config.rb +8 -1
- data/lib/rubocop/config_loader.rb +20 -20
- data/lib/rubocop/config_loader_resolver.rb +2 -1
- data/lib/rubocop/config_obsoletion.rb +73 -10
- data/lib/rubocop/config_validator.rb +77 -110
- data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
- data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
- data/lib/rubocop/cop/commissioner.rb +15 -7
- data/lib/rubocop/cop/cop.rb +31 -6
- data/lib/rubocop/cop/corrector.rb +8 -7
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -4
- data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
- data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
- data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
- data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
- data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
- data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
- data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
- data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/{metrics → layout}/line_length.rb +68 -112
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +50 -7
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
- data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
- data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
- data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
- data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
- data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
- data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +18 -15
- data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
- data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
- data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
- data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
- data/lib/rubocop/cop/lint/useless_setter_call.rb +5 -1
- data/lib/rubocop/cop/lint/void.rb +4 -4
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
- data/lib/rubocop/cop/migration/department_name.rb +30 -2
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
- data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +172 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
- data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
- data/lib/rubocop/cop/mixin/trailing_comma.rb +16 -18
- data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +12 -5
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
- data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
- data/lib/rubocop/cop/offense.rb +11 -0
- data/lib/rubocop/cop/registry.rb +8 -3
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +10 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +11 -7
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +2 -2
- data/lib/rubocop/cop/style/empty_method.rb +5 -5
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +10 -7
- data/lib/rubocop/cop/style/format_string_token.rb +2 -0
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +99 -11
- data/lib/rubocop/cop/style/guard_clause.rb +3 -2
- data/lib/rubocop/cop/style/hash_each_methods.rb +87 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_keys.rb +79 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +79 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
- data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
- data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
- data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
- data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
- data/lib/rubocop/cop/style/option_hash.rb +3 -3
- data/lib/rubocop/cop/style/or_assignment.rb +3 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
- data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
- data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
- data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
- data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +27 -29
- data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
- data/lib/rubocop/cop/style/safe_navigation.rb +13 -10
- data/lib/rubocop/cop/style/semicolon.rb +2 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
- data/lib/rubocop/cop/style/symbol_array.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -22
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
- data/lib/rubocop/cop/team.rb +5 -0
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +10 -18
- data/lib/rubocop/cop/variable_force.rb +11 -6
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -3
- data/lib/rubocop/formatter/formatter_set.rb +1 -0
- data/lib/rubocop/formatter/json_formatter.rb +6 -5
- data/lib/rubocop/formatter/junit_formatter.rb +63 -0
- data/lib/rubocop/formatter/tap_formatter.rb +1 -3
- data/lib/rubocop/node_pattern.rb +100 -12
- data/lib/rubocop/options.rb +17 -11
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/rake_task.rb +1 -0
- data/lib/rubocop/result_cache.rb +24 -8
- data/lib/rubocop/rspec/shared_contexts.rb +5 -0
- data/lib/rubocop/runner.rb +50 -29
- data/lib/rubocop/target_finder.rb +12 -6
- data/lib/rubocop/target_ruby.rb +151 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +69 -35
- data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
class MethodCallWithArgsParentheses
|
7
|
+
# Style require_parentheses
|
8
|
+
module RequireParentheses
|
9
|
+
def on_send(node)
|
10
|
+
return if ignored_method?(node.method_name)
|
11
|
+
return if matches_ignored_pattern?(node.method_name)
|
12
|
+
return if eligible_for_parentheses_omission?(node)
|
13
|
+
return unless node.arguments? && !node.parenthesized?
|
14
|
+
|
15
|
+
add_offense(node)
|
16
|
+
end
|
17
|
+
alias on_csend on_send
|
18
|
+
alias on_super on_send
|
19
|
+
alias on_yield on_send
|
20
|
+
|
21
|
+
def autocorrect(node)
|
22
|
+
lambda do |corrector|
|
23
|
+
corrector.replace(args_begin(node), '(')
|
24
|
+
|
25
|
+
unless args_parenthesized?(node)
|
26
|
+
corrector.insert_after(args_end(node), ')')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def message(_node = nil)
|
32
|
+
'Use parentheses for method calls with arguments.'
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def eligible_for_parentheses_omission?(node)
|
38
|
+
node.operator_method? || node.setter_method? || ignored_macro?(node)
|
39
|
+
end
|
40
|
+
|
41
|
+
def included_macros_list
|
42
|
+
cop_config.fetch('IncludedMacros', []).map(&:to_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
def ignored_macro?(node)
|
46
|
+
cop_config['IgnoreMacros'] &&
|
47
|
+
node.macro? &&
|
48
|
+
!included_macros_list.include?(node.method_name)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -113,22 +113,30 @@ module RuboCop
|
|
113
113
|
lambda do |corrector|
|
114
114
|
if node.args_type?
|
115
115
|
# offense is registered on args node when parentheses are unwanted
|
116
|
-
|
117
|
-
corrector.remove(node.loc.end)
|
116
|
+
correct_arguments(node, corrector)
|
118
117
|
else
|
119
|
-
|
120
|
-
args_with_space = range_with_surrounding_space(range: args_expr,
|
121
|
-
side: :left)
|
122
|
-
just_space = range_between(args_with_space.begin_pos,
|
123
|
-
args_expr.begin_pos)
|
124
|
-
corrector.replace(just_space, '(')
|
125
|
-
corrector.insert_after(args_expr, ')')
|
118
|
+
correct_definition(node, corrector)
|
126
119
|
end
|
127
120
|
end
|
128
121
|
end
|
129
122
|
|
130
123
|
private
|
131
124
|
|
125
|
+
def correct_arguments(arg_node, corrector)
|
126
|
+
corrector.replace(arg_node.loc.begin, ' ')
|
127
|
+
corrector.remove(arg_node.loc.end)
|
128
|
+
end
|
129
|
+
|
130
|
+
def correct_definition(def_node, corrector)
|
131
|
+
arguments_range = def_node.arguments.source_range
|
132
|
+
args_with_space = range_with_surrounding_space(range: arguments_range,
|
133
|
+
side: :left)
|
134
|
+
leading_space = range_between(args_with_space.begin_pos,
|
135
|
+
arguments_range.begin_pos)
|
136
|
+
corrector.replace(leading_space, '(')
|
137
|
+
corrector.insert_after(arguments_range, ')')
|
138
|
+
end
|
139
|
+
|
132
140
|
def require_parentheses?(args)
|
133
141
|
style == :require_parentheses ||
|
134
142
|
(style == :require_no_parentheses_except_multiline &&
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
return if !node.children.last.nil? && !node.multiline? && node.then?
|
36
36
|
|
37
37
|
# With more than one statements after then, there's not offense
|
38
|
-
return if node.
|
38
|
+
return if accept_node_type?(node.body)
|
39
39
|
|
40
40
|
add_offense(node, location: :begin)
|
41
41
|
end
|
@@ -44,11 +44,15 @@ module RuboCop
|
|
44
44
|
lambda do |corrector|
|
45
45
|
corrector.remove(
|
46
46
|
range_with_surrounding_space(
|
47
|
-
range: node.loc.begin, side: :left
|
47
|
+
range: node.loc.begin, side: :left, newlines: false
|
48
48
|
)
|
49
49
|
)
|
50
50
|
end
|
51
51
|
end
|
52
|
+
|
53
|
+
def accept_node_type?(node)
|
54
|
+
node&.begin_type? || node&.array_type? || node&.hash_type?
|
55
|
+
end
|
52
56
|
end
|
53
57
|
end
|
54
58
|
end
|
@@ -49,11 +49,13 @@ module RuboCop
|
|
49
49
|
node.parent.condition.source_range.end_pos)
|
50
50
|
|
51
51
|
lambda do |corrector|
|
52
|
-
corrector.replace(range, new_expression(node
|
52
|
+
corrector.replace(range, new_expression(node))
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
def new_expression(
|
56
|
+
def new_expression(inner_node)
|
57
|
+
outer_node = inner_node.parent
|
58
|
+
|
57
59
|
operator = replacement_operator(outer_node.keyword)
|
58
60
|
lh_operand = left_hand_operand(outer_node, operator)
|
59
61
|
rh_operand = right_hand_operand(inner_node, outer_node.keyword)
|
@@ -49,17 +49,17 @@ module RuboCop
|
|
49
49
|
def allowed_omission?(send_node)
|
50
50
|
!send_node.arguments? || send_node.parenthesized? ||
|
51
51
|
send_node.setter_method? || send_node.operator_method? ||
|
52
|
-
|
52
|
+
allowed?(send_node)
|
53
53
|
end
|
54
54
|
|
55
|
-
def
|
55
|
+
def allowed?(send_node)
|
56
56
|
send_node.parent.arguments.one? &&
|
57
|
-
|
57
|
+
allowed_methods.include?(send_node.method_name.to_s) &&
|
58
58
|
send_node.arguments.one?
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
62
|
-
cop_config['
|
61
|
+
def allowed_methods
|
62
|
+
cop_config['AllowedMethods'] || []
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
@@ -20,18 +20,18 @@ module RuboCop
|
|
20
20
|
# end
|
21
21
|
#
|
22
22
|
# # good
|
23
|
-
# [1, 2].each do |
|
24
|
-
# puts
|
23
|
+
# [1, 2].each do |a|
|
24
|
+
# puts a if a == 1
|
25
25
|
# end
|
26
26
|
#
|
27
27
|
# @example EnforcedStyle: always
|
28
28
|
# # With `always` all conditions at the end of an iteration needs to be
|
29
29
|
# # replaced by next - with `skip_modifier_ifs` the modifier if like
|
30
|
-
# # this one are ignored: `[1, 2].each { |a|
|
30
|
+
# # this one are ignored: `[1, 2].each { |a| puts a if a == 1 }`
|
31
31
|
#
|
32
32
|
# # bad
|
33
|
-
# [1, 2].each do |
|
34
|
-
# puts
|
33
|
+
# [1, 2].each do |a|
|
34
|
+
# puts a if a == 1
|
35
35
|
# end
|
36
36
|
#
|
37
37
|
# # bad
|
@@ -5,27 +5,39 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for non-nil checks, which are usually redundant.
|
7
7
|
#
|
8
|
-
#
|
8
|
+
# With `IncludeSemanticChanges` set to `false` by default, this cop
|
9
|
+
# does not report offenses for `!x.nil?` and does no changes that might
|
10
|
+
# change behavior.
|
11
|
+
#
|
12
|
+
# With `IncludeSemanticChanges` set to `true`, this cop reports offenses
|
13
|
+
# for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
|
14
|
+
# is **usually** OK, but might change behavior.
|
9
15
|
#
|
16
|
+
# @example
|
10
17
|
# # bad
|
11
18
|
# if x != nil
|
12
19
|
# end
|
13
20
|
#
|
14
|
-
# # good
|
15
|
-
# # bad (when allowing semantic changes)
|
16
|
-
# if !x.nil?
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# # good (when allowing semantic changes)
|
21
|
+
# # good
|
20
22
|
# if x
|
21
23
|
# end
|
22
24
|
#
|
23
|
-
# Non-nil checks are allowed if they are the final nodes of predicate.
|
24
|
-
#
|
25
|
+
# # Non-nil checks are allowed if they are the final nodes of predicate.
|
25
26
|
# # good
|
26
27
|
# def signed_in?
|
27
28
|
# !current_user.nil?
|
28
29
|
# end
|
30
|
+
#
|
31
|
+
# @example IncludeSemanticChanges: false (default)
|
32
|
+
# # good
|
33
|
+
# if !x.nil?
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# @example IncludeSemanticChanges: true
|
37
|
+
# # bad
|
38
|
+
# if !x.nil?
|
39
|
+
# end
|
40
|
+
#
|
29
41
|
class NonNilCheck < Cop
|
30
42
|
def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
|
31
43
|
def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
|
@@ -9,18 +9,22 @@ module RuboCop
|
|
9
9
|
# @example
|
10
10
|
#
|
11
11
|
# # bad
|
12
|
-
#
|
13
12
|
# 1000000
|
14
13
|
# 1_00_000
|
15
14
|
# 1_0000
|
16
15
|
#
|
17
16
|
# # good
|
18
|
-
#
|
19
17
|
# 1_000_000
|
20
18
|
# 1000
|
21
19
|
#
|
22
|
-
#
|
20
|
+
# @example Strict: false (default)
|
23
21
|
#
|
22
|
+
# # good
|
23
|
+
# 10_000_00 # typical representation of $10,000 in cents
|
24
|
+
#
|
25
|
+
# @example Strict: true
|
26
|
+
#
|
27
|
+
# # bad
|
24
28
|
# 10_000_00 # typical representation of $10,000 in cents
|
25
29
|
#
|
26
30
|
class NumericLiterals < Cop
|
@@ -54,9 +54,10 @@ module RuboCop
|
|
54
54
|
}.freeze
|
55
55
|
|
56
56
|
def on_send(node)
|
57
|
-
return if node.
|
58
|
-
|
59
|
-
|
57
|
+
return if ignored_method?(node.method_name) ||
|
58
|
+
node.each_ancestor(:send, :block).any? do |ancestor|
|
59
|
+
ignored_method?(ancestor.method_name)
|
60
|
+
end
|
60
61
|
|
61
62
|
numeric, replacement = check(node)
|
62
63
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
|
29
29
|
def on_args(node)
|
30
30
|
return if super_used?(node)
|
31
|
-
return if
|
31
|
+
return if allowlist.include?(node.parent.method_name.to_s)
|
32
32
|
|
33
33
|
option_hash(node) do |options|
|
34
34
|
add_offense(options)
|
@@ -37,8 +37,8 @@ module RuboCop
|
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
-
def
|
41
|
-
cop_config['
|
40
|
+
def allowlist
|
41
|
+
cop_config['Allowlist'] || []
|
42
42
|
end
|
43
43
|
|
44
44
|
def suspicious_name?(arg_name)
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
34
34
|
(if
|
35
35
|
({lvar ivar cvar gvar} _var)
|
36
36
|
({lvar ivar cvar gvar} _var)
|
37
|
-
_))
|
37
|
+
$_))
|
38
38
|
PATTERN
|
39
39
|
|
40
40
|
def_node_matcher :unless_assignment?, <<~PATTERN
|
@@ -51,7 +51,8 @@ module RuboCop
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def on_lvasgn(node)
|
54
|
-
return unless ternary_assignment?(node)
|
54
|
+
return unless (else_branch = ternary_assignment?(node))
|
55
|
+
return if else_branch.if_type?
|
55
56
|
|
56
57
|
add_offense(node)
|
57
58
|
end
|
@@ -88,27 +88,27 @@ module RuboCop
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def contains_preferred_delimiter?(node, type)
|
91
|
-
|
92
|
-
node
|
93
|
-
.children.map { |n| string_source(n) }.compact
|
94
|
-
.any? { |s| preferred_delimiters.any? { |d| s.include?(d) } }
|
91
|
+
contains_delimiter?(node, preferred_delimiters_for(type))
|
95
92
|
end
|
96
93
|
|
97
94
|
def include_same_character_as_used_for_delimiter?(node, type)
|
98
95
|
return false unless %w[%w %i].include?(type)
|
99
96
|
|
100
97
|
used_delimiters = matchpairs(begin_source(node)[-1])
|
101
|
-
|
98
|
+
contains_delimiter?(node, used_delimiters)
|
99
|
+
end
|
102
100
|
|
101
|
+
def contains_delimiter?(node, delimiters)
|
102
|
+
delimiters_regexp = Regexp.union(delimiters)
|
103
103
|
node
|
104
104
|
.children.map { |n| string_source(n) }.compact
|
105
|
-
.any? { |s|
|
105
|
+
.any? { |s| delimiters_regexp =~ s }
|
106
106
|
end
|
107
107
|
|
108
108
|
def string_source(node)
|
109
109
|
if node.is_a?(String)
|
110
110
|
node
|
111
|
-
elsif node.respond_to?(:type) && node.str_type?
|
111
|
+
elsif node.respond_to?(:type) && (node.str_type? || node.sym_type?)
|
112
112
|
node.source
|
113
113
|
end
|
114
114
|
end
|
@@ -30,11 +30,11 @@ module RuboCop
|
|
30
30
|
# c
|
31
31
|
# end
|
32
32
|
#
|
33
|
-
class
|
33
|
+
class RedundantCondition < Cop
|
34
34
|
include RangeHelp
|
35
35
|
|
36
36
|
MSG = 'Use double pipes `||` instead.'
|
37
|
-
|
37
|
+
REDUNDANT_CONDITION = 'This condition is not needed.'
|
38
38
|
|
39
39
|
def on_if(node)
|
40
40
|
return if node.elsif_conditional?
|
@@ -61,7 +61,7 @@ module RuboCop
|
|
61
61
|
|
62
62
|
def message(node)
|
63
63
|
if node.modifier_form? || !node.else_branch
|
64
|
-
|
64
|
+
REDUNDANT_CONDITION
|
65
65
|
else
|
66
66
|
MSG
|
67
67
|
end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
def_node_matcher :square_brackets?,
|
20
20
|
'(send {(send _recv _msg) str array hash} :[] ...)'
|
21
21
|
def_node_matcher :range_end?, '^^{irange erange}'
|
22
|
-
def_node_matcher :method_node_and_args, '$(
|
22
|
+
def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
|
23
23
|
def_node_matcher :rescue?, '{^resbody ^^resbody}'
|
24
24
|
def_node_matcher :arg_in_call_with_block?,
|
25
25
|
'^^(block (send _ _ equal?(%0) ...) ...)'
|
@@ -102,7 +102,7 @@ module RuboCop
|
|
102
102
|
return offense(begin_node, 'a variable') if node.variable?
|
103
103
|
return offense(begin_node, 'a constant') if node.const_type?
|
104
104
|
|
105
|
-
check_send(begin_node, node) if node.
|
105
|
+
check_send(begin_node, node) if node.call_type?
|
106
106
|
end
|
107
107
|
|
108
108
|
def check_send(begin_node, node)
|
@@ -195,7 +195,7 @@ module RuboCop
|
|
195
195
|
end
|
196
196
|
|
197
197
|
def method_call_with_redundant_parentheses?(node)
|
198
|
-
return false unless node.
|
198
|
+
return false unless node.call_type?
|
199
199
|
return false if node.prefix_not?
|
200
200
|
return false if range_end?(node)
|
201
201
|
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
# time = "8 o'clock"
|
18
18
|
# question = '"What did you say?"'
|
19
19
|
#
|
20
|
-
class
|
20
|
+
class RedundantPercentQ < Cop
|
21
21
|
MSG = 'Use `%<q_type>s` only for strings that contain both ' \
|
22
22
|
'single quotes and double quotes%<extra>s.'
|
23
23
|
DYNAMIC_MSG = ', or for dynamic strings that contain ' \
|