rubocop 0.58.2 → 0.59.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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +22 -7
- data/config/disabled.yml +33 -4
- data/config/enabled.yml +4 -11
- data/lib/rubocop.rb +5 -0
- data/lib/rubocop/ast/builder.rb +1 -0
- data/lib/rubocop/ast/node.rb +11 -33
- data/lib/rubocop/ast/node/block_node.rb +8 -1
- data/lib/rubocop/ast/node/defined_node.rb +13 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +16 -5
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +21 -0
- data/lib/rubocop/ast/node/send_node.rb +3 -12
- data/lib/rubocop/ast/traversal.rb +10 -0
- data/lib/rubocop/cli.rb +4 -1
- data/lib/rubocop/config.rb +21 -5
- data/lib/rubocop/config_loader.rb +2 -0
- data/lib/rubocop/config_loader_resolver.rb +3 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +1 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +64 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -0
- data/lib/rubocop/cop/commissioner.rb +2 -0
- data/lib/rubocop/cop/cop.rb +3 -0
- data/lib/rubocop/cop/corrector.rb +2 -0
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -0
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -0
- data/lib/rubocop/cop/correctors/space_corrector.rb +2 -0
- data/lib/rubocop/cop/force.rb +1 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
- data/lib/rubocop/cop/generator.rb +1 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +4 -0
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +5 -4
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +35 -0
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -0
- data/lib/rubocop/cop/layout/indent_array.rb +1 -0
- data/lib/rubocop/cop/layout/indent_heredoc.rb +3 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +34 -11
- data/lib/rubocop/cop/layout/space_after_method_name.rb +1 -0
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +16 -8
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +2 -0
- data/lib/rubocop/cop/layout/tab.rb +1 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -0
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +9 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +2 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +3 -6
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +2 -0
- data/lib/rubocop/cop/lint/script_permission.rb +1 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -0
- data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -0
- data/lib/rubocop/cop/lint/unneeded_cop_disable_directive.rb +1 -0
- data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +1 -0
- data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +2 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +3 -0
- data/lib/rubocop/cop/lint/void.rb +1 -0
- data/lib/rubocop/cop/message_annotator.rb +1 -0
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -0
- data/lib/rubocop/cop/metrics/line_length.rb +6 -1
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -0
- data/lib/rubocop/cop/mixin/classish_length.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -6
- data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
- data/lib/rubocop/cop/mixin/ignored_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +25 -1
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +5 -3
- data/lib/rubocop/cop/mixin/percent_literal.rb +2 -0
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +2 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +2 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +6 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +4 -0
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -0
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
- data/lib/rubocop/cop/naming/file_name.rb +4 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -0
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
- data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +1 -0
- data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +1 -0
- data/lib/rubocop/cop/naming/variable_name.rb +1 -0
- data/lib/rubocop/cop/performance/case_when_splat.rb +11 -7
- data/lib/rubocop/cop/performance/casecmp.rb +33 -42
- data/lib/rubocop/cop/performance/chain_array_allocation.rb +77 -0
- data/lib/rubocop/cop/performance/compare_with_block.rb +3 -0
- data/lib/rubocop/cop/performance/regexp_match.rb +1 -0
- data/lib/rubocop/cop/performance/sample.rb +2 -0
- data/lib/rubocop/cop/performance/size.rb +8 -2
- data/lib/rubocop/cop/performance/string_replacement.rb +1 -0
- data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -0
- data/lib/rubocop/cop/rails/bulk_change_table.rb +9 -2
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +1 -0
- data/lib/rubocop/cop/rails/delegate.rb +7 -2
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +1 -0
- data/lib/rubocop/cop/rails/find_each.rb +7 -2
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +1 -1
- data/lib/rubocop/cop/rails/http_status.rb +2 -0
- data/lib/rubocop/cop/rails/inverse_of.rb +4 -0
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +1 -0
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -0
- data/lib/rubocop/cop/rails/save_bang.rb +189 -38
- data/lib/rubocop/cop/rails/time_zone.rb +1 -0
- data/lib/rubocop/cop/security/eval.rb +1 -0
- data/lib/rubocop/cop/security/json_load.rb +2 -2
- data/lib/rubocop/cop/security/open.rb +6 -3
- data/lib/rubocop/cop/severity.rb +1 -0
- data/lib/rubocop/cop/style/and_or.rb +3 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +3 -0
- data/lib/rubocop/cop/style/class_vars.rb +1 -1
- data/lib/rubocop/cop/style/colon_method_definition.rb +1 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
- data/lib/rubocop/cop/style/copyright.rb +7 -2
- data/lib/rubocop/cop/style/date_time.rb +40 -7
- data/lib/rubocop/cop/style/double_negation.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -0
- data/lib/rubocop/cop/style/empty_else.rb +2 -0
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +2 -0
- data/lib/rubocop/cop/style/for.rb +56 -10
- data/lib/rubocop/cop/style/format_string_token.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -0
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +3 -5
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +3 -5
- data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/missing_else.rb +1 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +65 -0
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +45 -5
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
- data/lib/rubocop/cop/style/or_assignment.rb +2 -0
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +1 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +13 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +6 -1
- data/lib/rubocop/cop/style/redundant_return.rb +1 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -0
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +4 -0
- data/lib/rubocop/cop/style/semicolon.rb +4 -0
- data/lib/rubocop/cop/style/signal_exception.rb +1 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +1 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -8
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
- data/lib/rubocop/cop/style/unneeded_condition.rb +13 -2
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -0
- data/lib/rubocop/cop/style/word_array.rb +13 -1
- data/lib/rubocop/cop/team.rb +1 -0
- data/lib/rubocop/cop/variable_force.rb +5 -0
- data/lib/rubocop/cop/variable_force/assignment.rb +4 -0
- data/lib/rubocop/cop/variable_force/branch.rb +4 -0
- data/lib/rubocop/cop/variable_force/branchable.rb +2 -0
- data/lib/rubocop/cop/variable_force/scope.rb +6 -0
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -0
- data/lib/rubocop/file_finder.rb +2 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +4 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +1 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -0
- data/lib/rubocop/options.rb +16 -0
- data/lib/rubocop/path_util.rb +16 -1
- data/lib/rubocop/processed_source.rb +4 -0
- data/lib/rubocop/remote_config.rb +6 -1
- data/lib/rubocop/result_cache.rb +1 -0
- data/lib/rubocop/rspec/cop_helper.rb +3 -5
- data/lib/rubocop/rspec/shared_examples.rb +1 -9
- data/lib/rubocop/runner.rb +4 -0
- data/lib/rubocop/target_finder.rb +2 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +7 -2
data/lib/rubocop/cop/force.rb
CHANGED
@@ -162,6 +162,7 @@ module RuboCop
|
|
162
162
|
# @return String otherwise trying to {humanize_node} of the current node
|
163
163
|
def classify(node)
|
164
164
|
return node.to_s unless node.respond_to?(:type)
|
165
|
+
|
165
166
|
case node.type
|
166
167
|
when :block
|
167
168
|
classify(node.send_node)
|
@@ -185,6 +186,7 @@ module RuboCop
|
|
185
186
|
class_elements(class_node).each do |node|
|
186
187
|
classification = classify(node)
|
187
188
|
next if ignore?(classification)
|
189
|
+
|
188
190
|
yield node, classification
|
189
191
|
end
|
190
192
|
end
|
@@ -192,6 +194,7 @@ module RuboCop
|
|
192
194
|
def class_elements(class_node)
|
193
195
|
*, class_def = class_node.children
|
194
196
|
return [] unless class_def
|
197
|
+
|
195
198
|
if class_def.def_type? || class_def.send_type?
|
196
199
|
[class_def]
|
197
200
|
else
|
@@ -241,6 +244,7 @@ module RuboCop
|
|
241
244
|
method_name, = *node
|
242
245
|
if node.def_type?
|
243
246
|
return :initializer if method_name == :initialize
|
247
|
+
|
244
248
|
return "#{node_visibility(node)}_methods"
|
245
249
|
end
|
246
250
|
HUMANIZED_NODE_TYPE[node.type] || node.type
|
@@ -79,10 +79,11 @@ module RuboCop
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def argument_indentation_correct?(node)
|
82
|
-
node.argument?
|
83
|
-
|
84
|
-
|
85
|
-
|
82
|
+
return unless node.argument? || node.chained?
|
83
|
+
|
84
|
+
opening_indentation(
|
85
|
+
find_node_used_heredoc_argument(node.parent)
|
86
|
+
) == closing_indentation(node)
|
86
87
|
end
|
87
88
|
|
88
89
|
def contents_indentation(node)
|
@@ -95,6 +95,14 @@ module RuboCop
|
|
95
95
|
private
|
96
96
|
|
97
97
|
def check(node, elements)
|
98
|
+
if elements.empty?
|
99
|
+
check_for_no_elements(node)
|
100
|
+
else
|
101
|
+
check_for_elements(node, elements)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def check_for_elements(node, elements)
|
98
106
|
left_paren = node.loc.begin
|
99
107
|
right_paren = node.loc.end
|
100
108
|
|
@@ -113,6 +121,25 @@ module RuboCop
|
|
113
121
|
right_paren))
|
114
122
|
end
|
115
123
|
|
124
|
+
def check_for_no_elements(node)
|
125
|
+
left_paren = node.loc.begin
|
126
|
+
right_paren = node.loc.end
|
127
|
+
return unless right_paren && begins_its_line?(right_paren)
|
128
|
+
|
129
|
+
candidates = correct_column_candidates(node, left_paren)
|
130
|
+
|
131
|
+
return if candidates.include?(right_paren.column)
|
132
|
+
|
133
|
+
# Although there are multiple choices for a correct column,
|
134
|
+
# select the first one of candidates to determine a specification.
|
135
|
+
correct_column = candidates.first
|
136
|
+
add_offense(right_paren,
|
137
|
+
location: right_paren,
|
138
|
+
message: message(correct_column,
|
139
|
+
left_paren,
|
140
|
+
right_paren))
|
141
|
+
end
|
142
|
+
|
116
143
|
def expected_column(left_paren, elements)
|
117
144
|
if !line_break_after_left_paren?(left_paren, elements) &&
|
118
145
|
all_elements_aligned?(elements)
|
@@ -140,6 +167,14 @@ module RuboCop
|
|
140
167
|
.first_line
|
141
168
|
end
|
142
169
|
|
170
|
+
def correct_column_candidates(node, left_paren)
|
171
|
+
[
|
172
|
+
processed_source.line_indentation(left_paren.line),
|
173
|
+
left_paren.column,
|
174
|
+
node.loc.column
|
175
|
+
]
|
176
|
+
end
|
177
|
+
|
143
178
|
def message(correct_column, left_paren, right_paren)
|
144
179
|
if correct_column == left_paren.column
|
145
180
|
MSG_ALIGN
|
@@ -80,6 +80,7 @@ module RuboCop
|
|
80
80
|
|
81
81
|
def def_node?(node)
|
82
82
|
return unless node
|
83
|
+
|
83
84
|
node.def_type? || node.defs_type?
|
84
85
|
end
|
85
86
|
|
@@ -88,6 +89,7 @@ module RuboCop
|
|
88
89
|
blank_start = lines.each_index.select { |i| lines[i].blank? }.max
|
89
90
|
non_blank_end = lines.each_index.reject { |i| lines[i].blank? }.min
|
90
91
|
return false if blank_start.nil? || non_blank_end.nil?
|
92
|
+
|
91
93
|
blank_start > non_blank_end
|
92
94
|
end
|
93
95
|
|
@@ -70,8 +70,11 @@ module RuboCop
|
|
70
70
|
KIND = 'class'.freeze
|
71
71
|
|
72
72
|
def on_class(node)
|
73
|
-
_name,
|
74
|
-
|
73
|
+
_name, superclass, body = *node
|
74
|
+
|
75
|
+
adjusted_first_line = superclass.first_line if superclass
|
76
|
+
|
77
|
+
check(node, body, adjusted_first_line: adjusted_first_line)
|
75
78
|
end
|
76
79
|
|
77
80
|
def on_sclass(node)
|
@@ -100,6 +100,7 @@ module RuboCop
|
|
100
100
|
end
|
101
101
|
|
102
102
|
return if too_long_line?(node)
|
103
|
+
|
103
104
|
add_offense(node, location: :heredoc_body)
|
104
105
|
end
|
105
106
|
|
@@ -174,6 +175,7 @@ module RuboCop
|
|
174
175
|
|
175
176
|
def too_long_line?(node)
|
176
177
|
return false if config.for_cop('Metrics/LineLength')['AllowHeredoc']
|
178
|
+
|
177
179
|
body = heredoc_body(node)
|
178
180
|
|
179
181
|
expected_indent = base_indent_level(node) + indentation_width
|
@@ -191,6 +193,7 @@ module RuboCop
|
|
191
193
|
|
192
194
|
def correct_by_squiggly(node)
|
193
195
|
return if target_ruby_version < 2.3
|
196
|
+
|
194
197
|
lambda do |corrector|
|
195
198
|
if heredoc_indent_type(node) == '~'
|
196
199
|
adjust_squiggly(corrector, node)
|
@@ -70,6 +70,7 @@ module RuboCop
|
|
70
70
|
# Check indentation against end keyword but only if it's first on its
|
71
71
|
# line.
|
72
72
|
return unless begins_its_line?(node.loc.end)
|
73
|
+
|
73
74
|
check_indentation(node.loc.end, node.children.first)
|
74
75
|
end
|
75
76
|
|
@@ -165,6 +166,7 @@ module RuboCop
|
|
165
166
|
else
|
166
167
|
members.first.children.each do |member|
|
167
168
|
next if member.send_type? && member.access_modifier?
|
169
|
+
|
168
170
|
check_indentation(base, member)
|
169
171
|
end
|
170
172
|
end
|
@@ -61,6 +61,8 @@ module RuboCop
|
|
61
61
|
private
|
62
62
|
|
63
63
|
def relevant_node?(node)
|
64
|
+
return false if node.unary_operation?
|
65
|
+
|
64
66
|
!node.loc.dot # Don't check method calls with dot operator.
|
65
67
|
end
|
66
68
|
|
@@ -72,7 +74,6 @@ module RuboCop
|
|
72
74
|
|
73
75
|
def offending_range(node, lhs, rhs, given_style)
|
74
76
|
return false unless begins_its_line?(rhs)
|
75
|
-
return false if lhs.first_line == rhs.line # Needed for unary op.
|
76
77
|
return false if not_for_this_cop?(node)
|
77
78
|
|
78
79
|
correct_column = if should_align?(node, rhs, given_style)
|
@@ -25,8 +25,10 @@ module RuboCop
|
|
25
25
|
include RangeHelp
|
26
26
|
|
27
27
|
MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
|
28
|
-
'aligned with `
|
29
|
-
.freeze
|
28
|
+
'aligned with `%<beginning>s` at ' \
|
29
|
+
'%<begin_loc_line>d, %<begin_loc_column>d.'.freeze
|
30
|
+
ANCESTOR_TYPES = %i[kwbegin def defs class module].freeze
|
31
|
+
RUBY_2_5_ANCESTOR_TYPES = (ANCESTOR_TYPES + [:block]).freeze
|
30
32
|
|
31
33
|
def on_resbody(node)
|
32
34
|
check(node) unless modifier?(node)
|
@@ -40,6 +42,7 @@ module RuboCop
|
|
40
42
|
@modifier_locations =
|
41
43
|
processed_source.tokens.each_with_object([]) do |token, locations|
|
42
44
|
next unless token.rescue_modifier?
|
45
|
+
|
43
46
|
locations << token.pos
|
44
47
|
end
|
45
48
|
end
|
@@ -55,28 +58,44 @@ module RuboCop
|
|
55
58
|
private
|
56
59
|
|
57
60
|
def check(node)
|
58
|
-
|
61
|
+
ancestor_node = ancestor_node(node)
|
62
|
+
alignment_loc = ancestor_node.loc.expression
|
59
63
|
kw_loc = node.loc.keyword
|
60
64
|
|
61
|
-
return if
|
62
|
-
return if
|
65
|
+
return if alignment_loc.column == kw_loc.column
|
66
|
+
return if alignment_loc.line == kw_loc.line
|
63
67
|
|
64
68
|
add_offense(node,
|
65
69
|
location: kw_loc,
|
66
|
-
message: format_message(kw_loc,
|
70
|
+
message: format_message(kw_loc,
|
71
|
+
alignment_loc,
|
72
|
+
ancestor_node))
|
67
73
|
end
|
68
74
|
|
69
|
-
def format_message(kw_loc,
|
75
|
+
def format_message(kw_loc, alignment_loc, ancestor_node)
|
70
76
|
format(MSG,
|
71
77
|
kw_loc: kw_loc.source,
|
72
78
|
kw_loc_line: kw_loc.line,
|
73
79
|
kw_loc_column: kw_loc.column,
|
74
|
-
|
75
|
-
|
80
|
+
beginning: alignment_source(ancestor_node),
|
81
|
+
begin_loc_line: alignment_loc.line,
|
82
|
+
begin_loc_column: alignment_loc.column)
|
83
|
+
end
|
84
|
+
|
85
|
+
def alignment_source(node)
|
86
|
+
ending = case node.type
|
87
|
+
when :def, :defs, :class, :module
|
88
|
+
node.loc.name
|
89
|
+
when :block, :kwbegin
|
90
|
+
node.loc.begin
|
91
|
+
end
|
92
|
+
|
93
|
+
range_between(node.loc.expression.begin_pos, ending.end_pos).source
|
76
94
|
end
|
77
95
|
|
78
96
|
def modifier?(node)
|
79
97
|
return false unless @modifier_locations.respond_to?(:include?)
|
98
|
+
|
80
99
|
@modifier_locations.include?(node.loc.keyword)
|
81
100
|
end
|
82
101
|
|
@@ -88,8 +107,12 @@ module RuboCop
|
|
88
107
|
end
|
89
108
|
|
90
109
|
def ancestor_node(node)
|
91
|
-
types =
|
92
|
-
|
110
|
+
types = if target_ruby_version >= 2.5
|
111
|
+
RUBY_2_5_ANCESTOR_TYPES
|
112
|
+
else
|
113
|
+
ANCESTOR_TYPES
|
114
|
+
end
|
115
|
+
|
93
116
|
node.each_ancestor(*types).first
|
94
117
|
end
|
95
118
|
end
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
MSG = 'Do not leave space between `!` and its argument.'.freeze
|
18
18
|
|
19
19
|
def on_send(node)
|
20
|
-
return unless node.
|
20
|
+
return unless node.prefix_bang? && whitespace_after_operator?(node)
|
21
21
|
|
22
22
|
add_offense(node)
|
23
23
|
end
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
end
|
97
97
|
|
98
98
|
def on_send(node)
|
99
|
-
check(node, [:selector].freeze) if node.
|
99
|
+
check(node, [:selector].freeze) if node.prefix_not?
|
100
100
|
end
|
101
101
|
|
102
102
|
def on_super(node)
|
@@ -140,6 +140,7 @@ module RuboCop
|
|
140
140
|
def check(node, locations, begin_keyword = DO)
|
141
141
|
locations.each do |loc|
|
142
142
|
next unless node.loc.respond_to?(loc)
|
143
|
+
|
143
144
|
range = node.loc.public_send(loc)
|
144
145
|
next unless range
|
145
146
|
|
@@ -182,6 +183,7 @@ module RuboCop
|
|
182
183
|
def space_before_missing?(range)
|
183
184
|
pos = range.begin_pos - 1
|
184
185
|
return false if pos < 0
|
186
|
+
|
185
187
|
range.source_buffer.source[pos] !~ /[\s\(\|\{\[;,\*\=]/
|
186
188
|
end
|
187
189
|
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Layout
|
6
|
-
# This cop checks for spaces between
|
7
|
-
#
|
6
|
+
# This cop checks for spaces between `->` and opening parameter
|
7
|
+
# parenthesis (`(`) in lambda literals.
|
8
8
|
#
|
9
9
|
# @example EnforcedStyle: require_no_space (default)
|
10
10
|
# # bad
|
@@ -24,20 +24,21 @@ module RuboCop
|
|
24
24
|
include RangeHelp
|
25
25
|
|
26
26
|
ARROW = '->'.freeze
|
27
|
-
MSG_REQUIRE_SPACE = 'Use a space between `->` and
|
28
|
-
'in lambda literals'.freeze
|
29
|
-
MSG_REQUIRE_NO_SPACE = 'Do not use spaces between `->` and
|
30
|
-
'
|
27
|
+
MSG_REQUIRE_SPACE = 'Use a space between `->` and ' \
|
28
|
+
'`(` in lambda literals.'.freeze
|
29
|
+
MSG_REQUIRE_NO_SPACE = 'Do not use spaces between `->` and ' \
|
30
|
+
'`(` in lambda literals.'.freeze
|
31
31
|
|
32
32
|
def on_send(node)
|
33
33
|
return unless arrow_lambda_with_args?(node)
|
34
|
+
|
34
35
|
if style == :require_space && !space_after_arrow?(node)
|
35
36
|
add_offense(node,
|
36
|
-
location: node
|
37
|
+
location: range_of_offense(node),
|
37
38
|
message: MSG_REQUIRE_SPACE)
|
38
39
|
elsif style == :require_no_space && space_after_arrow?(node)
|
39
40
|
add_offense(node,
|
40
|
-
location: node
|
41
|
+
location: range_of_offense(node),
|
41
42
|
message: MSG_REQUIRE_NO_SPACE)
|
42
43
|
end
|
43
44
|
end
|
@@ -80,6 +81,13 @@ module RuboCop
|
|
80
81
|
parentheses = lambda_node.parent.children[1]
|
81
82
|
parentheses.source_range.begin_pos - arrow.source_range.end_pos > 0
|
82
83
|
end
|
84
|
+
|
85
|
+
def range_of_offense(node)
|
86
|
+
range_between(
|
87
|
+
node.parent.loc.expression.begin_pos,
|
88
|
+
node.parent.arguments.loc.expression.end_pos
|
89
|
+
)
|
90
|
+
end
|
83
91
|
end
|
84
92
|
end
|
85
93
|
end
|