rubocop 0.75.0 → 0.79.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 +2 -2
- data/config/default.yml +341 -316
- data/lib/rubocop.rb +48 -31
- data/lib/rubocop/ast/builder.rb +43 -41
- data/lib/rubocop/ast/node.rb +5 -1
- 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/return_node.rb +24 -0
- data/lib/rubocop/ast/traversal.rb +11 -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 +2 -2
- 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 -11
- 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/percent_literal_corrector.rb +1 -1
- 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} +3 -2
- 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_after_guard_clause.rb +22 -7
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
- 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_blank_lines.rb → leading_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
- 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_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 +32 -7
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -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_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 +2 -2
- 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} +10 -12
- 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/unused_block_argument.rb +22 -6
- data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +7 -26
- 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 +16 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
- data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
- 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 +14 -9
- 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/{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/braces_around_hash_parameters.rb +6 -6
- 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/documentation_method.rb +44 -0
- 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/expand_path_arguments.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +10 -7
- data/lib/rubocop/cop/style/format_string_token.rb +15 -34
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
- data/lib/rubocop/cop/style/guard_clause.rb +3 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- 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 +24 -227
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -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/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 +39 -29
- data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
- data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
- data/lib/rubocop/cop/style/semicolon.rb +13 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
- 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 +7 -5
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
- data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
- data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +16 -16
- data/lib/rubocop/formatter/json_formatter.rb +6 -5
- data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
- data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
- data/lib/rubocop/formatter/tap_formatter.rb +9 -6
- data/lib/rubocop/node_pattern.rb +4 -2
- data/lib/rubocop/options.rb +20 -26
- 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 +50 -34
- data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -0,0 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
class MethodCallWithArgsParentheses
|
7
|
+
# Style omit_parentheses
|
8
|
+
module OmitParentheses
|
9
|
+
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
|
10
|
+
|
11
|
+
def on_send(node)
|
12
|
+
return unless node.parenthesized?
|
13
|
+
return if node.implicit_call?
|
14
|
+
return if super_call_without_arguments?(node)
|
15
|
+
return if allowed_camel_case_method_call?(node)
|
16
|
+
return if legitimate_call_with_parentheses?(node)
|
17
|
+
|
18
|
+
add_offense(node, location: node.loc.begin.join(node.loc.end))
|
19
|
+
end
|
20
|
+
alias on_csend on_send
|
21
|
+
alias on_super on_send
|
22
|
+
alias on_yield on_send
|
23
|
+
|
24
|
+
def autocorrect(node)
|
25
|
+
lambda do |corrector|
|
26
|
+
if parentheses_at_the_end_of_multiline_call?(node)
|
27
|
+
corrector.replace(args_begin(node), ' \\')
|
28
|
+
else
|
29
|
+
corrector.replace(args_begin(node), ' ')
|
30
|
+
end
|
31
|
+
corrector.remove(node.loc.end)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def message(_node = nil)
|
36
|
+
'Omit parentheses for method calls with arguments.'
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def super_call_without_arguments?(node)
|
42
|
+
node.super_type? && node.arguments.none?
|
43
|
+
end
|
44
|
+
|
45
|
+
def allowed_camel_case_method_call?(node)
|
46
|
+
node.camel_case_method? &&
|
47
|
+
(node.arguments.none? ||
|
48
|
+
cop_config['AllowParenthesesInCamelCaseMethod'])
|
49
|
+
end
|
50
|
+
|
51
|
+
def parentheses_at_the_end_of_multiline_call?(node)
|
52
|
+
node.multiline? &&
|
53
|
+
node.loc.begin.source_line
|
54
|
+
.gsub(TRAILING_WHITESPACE_REGEX, '')
|
55
|
+
.end_with?('(')
|
56
|
+
end
|
57
|
+
|
58
|
+
def legitimate_call_with_parentheses?(node)
|
59
|
+
call_in_literals?(node) ||
|
60
|
+
call_with_ambiguous_arguments?(node) ||
|
61
|
+
call_in_logical_operators?(node) ||
|
62
|
+
call_in_optional_arguments?(node) ||
|
63
|
+
allowed_multiline_call_with_parentheses?(node) ||
|
64
|
+
allowed_chained_call_with_parentheses?(node)
|
65
|
+
end
|
66
|
+
|
67
|
+
def call_in_literals?(node)
|
68
|
+
node.parent &&
|
69
|
+
(node.parent.pair_type? ||
|
70
|
+
node.parent.array_type? ||
|
71
|
+
node.parent.range_type? ||
|
72
|
+
splat?(node.parent) ||
|
73
|
+
ternary_if?(node.parent))
|
74
|
+
end
|
75
|
+
|
76
|
+
def call_in_logical_operators?(node)
|
77
|
+
node.parent &&
|
78
|
+
(logical_operator?(node.parent) ||
|
79
|
+
node.parent.send_type? &&
|
80
|
+
node.parent.arguments.any?(&method(:logical_operator?)))
|
81
|
+
end
|
82
|
+
|
83
|
+
def call_in_optional_arguments?(node)
|
84
|
+
node.parent &&
|
85
|
+
(node.parent.optarg_type? || node.parent.kwoptarg_type?)
|
86
|
+
end
|
87
|
+
|
88
|
+
def call_with_ambiguous_arguments?(node)
|
89
|
+
call_with_braced_block?(node) ||
|
90
|
+
call_as_argument_or_chain?(node) ||
|
91
|
+
hash_literal_in_arguments?(node) ||
|
92
|
+
node.descendants.any? do |n|
|
93
|
+
ambigious_literal?(n) || logical_operator?(n) ||
|
94
|
+
call_with_braced_block?(n)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def call_with_braced_block?(node)
|
99
|
+
(node.send_type? || node.super_type?) &&
|
100
|
+
node.block_node && node.block_node.braces?
|
101
|
+
end
|
102
|
+
|
103
|
+
def call_as_argument_or_chain?(node)
|
104
|
+
node.parent &&
|
105
|
+
(node.parent.send_type? && !assigned_before?(node.parent, node) ||
|
106
|
+
node.parent.csend_type? || node.parent.super_type?)
|
107
|
+
end
|
108
|
+
|
109
|
+
def hash_literal_in_arguments?(node)
|
110
|
+
node.arguments.any? do |n|
|
111
|
+
hash_literal?(n) ||
|
112
|
+
n.send_type? && node.descendants.any?(&method(:hash_literal?))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def allowed_multiline_call_with_parentheses?(node)
|
117
|
+
cop_config['AllowParenthesesInMultilineCall'] && node.multiline?
|
118
|
+
end
|
119
|
+
|
120
|
+
def allowed_chained_call_with_parentheses?(node)
|
121
|
+
return false unless cop_config['AllowParenthesesInChaining']
|
122
|
+
|
123
|
+
previous = node.descendants.first
|
124
|
+
return false unless previous&.send_type?
|
125
|
+
|
126
|
+
previous.parenthesized? ||
|
127
|
+
allowed_chained_call_with_parentheses?(previous)
|
128
|
+
end
|
129
|
+
|
130
|
+
def ambigious_literal?(node)
|
131
|
+
splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) ||
|
132
|
+
unary_literal?(node)
|
133
|
+
end
|
134
|
+
|
135
|
+
def splat?(node)
|
136
|
+
node.splat_type? || node.kwsplat_type? || node.block_pass_type?
|
137
|
+
end
|
138
|
+
|
139
|
+
def ternary_if?(node)
|
140
|
+
node.if_type? && node.ternary?
|
141
|
+
end
|
142
|
+
|
143
|
+
def logical_operator?(node)
|
144
|
+
(node.and_type? || node.or_type?) && node.logical_operator?
|
145
|
+
end
|
146
|
+
|
147
|
+
def hash_literal?(node)
|
148
|
+
node.hash_type? && node.braces?
|
149
|
+
end
|
150
|
+
|
151
|
+
def regexp_slash_literal?(node)
|
152
|
+
node.regexp_type? && node.loc.begin.source == '/'
|
153
|
+
end
|
154
|
+
|
155
|
+
def unary_literal?(node)
|
156
|
+
node.numeric_type? && node.sign? ||
|
157
|
+
node.parent&.send_type? && node.parent&.unary_operation?
|
158
|
+
end
|
159
|
+
|
160
|
+
def assigned_before?(node, target)
|
161
|
+
node.assignment? &&
|
162
|
+
node.loc.operator.begin < target.loc.begin
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -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
|