rubocop 1.5.2 → 1.8.1
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/LICENSE.txt +1 -1
- data/README.md +3 -2
- data/config/default.yml +112 -16
- data/config/obsoletion.yml +196 -0
- data/lib/rubocop.rb +20 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +16 -44
- data/lib/rubocop/comment_config.rb +6 -6
- data/lib/rubocop/config.rb +8 -5
- data/lib/rubocop/config_loader.rb +11 -7
- data/lib/rubocop/config_loader_resolver.rb +21 -4
- data/lib/rubocop/config_obsoletion.rb +64 -262
- data/lib/rubocop/config_obsoletion/changed_enforced_styles.rb +33 -0
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +21 -0
- data/lib/rubocop/config_obsoletion/cop_rule.rb +34 -0
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +44 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +44 -0
- data/lib/rubocop/config_obsoletion/removed_cop.rb +41 -0
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +34 -0
- data/lib/rubocop/config_obsoletion/rule.rb +41 -0
- data/lib/rubocop/config_obsoletion/split_cop.rb +27 -0
- data/lib/rubocop/config_validator.rb +11 -4
- data/lib/rubocop/cop/base.rb +17 -15
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +6 -8
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +145 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +19 -3
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +14 -0
- data/lib/rubocop/cop/layout/line_length.rb +6 -16
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -10
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_before_brackets.rb +62 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +13 -10
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_assignment.rb +59 -0
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +7 -2
- data/lib/rubocop/cop/lint/deprecated_constants.rb +75 -0
- data/lib/rubocop/cop/lint/duplicate_branch.rb +64 -2
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +44 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +10 -6
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +2 -1
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +50 -0
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +50 -17
- data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -11
- data/lib/rubocop/cop/lint/unreachable_loop.rb +17 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/migration/department_name.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_identifiers.rb +18 -0
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -10
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/string_help.rb +4 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
- data/lib/rubocop/cop/naming/accessor_method_name.rb +15 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +59 -5
- data/lib/rubocop/cop/naming/variable_name.rb +2 -0
- data/lib/rubocop/cop/naming/variable_number.rb +1 -8
- data/lib/rubocop/cop/registry.rb +10 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -1
- data/lib/rubocop/cop/style/character_literal.rb +10 -11
- data/lib/rubocop/cop/style/collection_methods.rb +14 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +22 -5
- data/lib/rubocop/cop/style/empty_literal.rb +6 -2
- data/lib/rubocop/cop/style/endless_method.rb +102 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +10 -0
- data/lib/rubocop/cop/style/float_division.rb +44 -1
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/hash_except.rb +95 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +2 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +8 -3
- data/lib/rubocop/cop/style/if_unless_modifier.rb +4 -0
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -2
- data/lib/rubocop/cop/style/lambda_call.rb +2 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +16 -6
- data/lib/rubocop/cop/style/method_def_parentheses.rb +7 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +26 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +3 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +13 -3
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -0
- data/lib/rubocop/cop/style/perl_backrefs.rb +86 -9
- data/lib/rubocop/cop/style/raise_args.rb +5 -2
- data/lib/rubocop/cop/style/redundant_argument.rb +21 -2
- data/lib/rubocop/cop/style/redundant_freeze.rb +8 -4
- data/lib/rubocop/cop/style/redundant_return.rb +1 -1
- data/lib/rubocop/cop/style/single_line_block_params.rb +30 -7
- data/lib/rubocop/cop/style/single_line_methods.rb +34 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +15 -9
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -13
- data/lib/rubocop/cop/style/string_concatenation.rb +20 -1
- data/lib/rubocop/cop/style/string_literals.rb +14 -8
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +5 -4
- data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
- data/lib/rubocop/cop/util.rb +3 -1
- data/lib/rubocop/formatter/emacs_style_formatter.rb +2 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -0
- data/lib/rubocop/formatter/tap_formatter.rb +2 -0
- data/lib/rubocop/lockfile.rb +40 -0
- data/lib/rubocop/options.rb +9 -9
- data/lib/rubocop/rspec/cop_helper.rb +0 -4
- data/lib/rubocop/rspec/expect_offense.rb +34 -22
- data/lib/rubocop/runner.rb +16 -1
- data/lib/rubocop/target_finder.rb +4 -2
- data/lib/rubocop/target_ruby.rb +47 -11
- data/lib/rubocop/util.rb +16 -0
- data/lib/rubocop/version.rb +8 -2
- metadata +28 -7
@@ -36,12 +36,13 @@ module RuboCop
|
|
36
36
|
# spec.required_ruby_version = '>= 2.5'
|
37
37
|
# end
|
38
38
|
#
|
39
|
-
# #
|
39
|
+
# # accepted but not recommended
|
40
40
|
# Gem::Specification.new do |spec|
|
41
41
|
# spec.required_ruby_version = ['>= 2.5.0', '< 2.7.0']
|
42
42
|
# end
|
43
43
|
#
|
44
|
-
# #
|
44
|
+
# # accepted but not recommended, since
|
45
|
+
# # Ruby does not really follow semantic versionning
|
45
46
|
# Gem::Specification.new do |spec|
|
46
47
|
# spec.required_ruby_version = '~> 2.5'
|
47
48
|
# end
|
@@ -6,4 +6,5 @@ require_relative 'internal_affairs/node_type_predicate'
|
|
6
6
|
require_relative 'internal_affairs/offense_location_keyword'
|
7
7
|
require_relative 'internal_affairs/redundant_message_argument'
|
8
8
|
require_relative 'internal_affairs/redundant_location_argument'
|
9
|
+
require_relative 'internal_affairs/style_detected_api_use'
|
9
10
|
require_relative 'internal_affairs/useless_message_assertion'
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Checks for correct use of the style_detected API provided by
|
7
|
+
# `ConfigurableEnforcedStyle`. If `correct_style_detected` is used
|
8
|
+
# then `opposite_style_detected`, `unexpected_style_detected`,
|
9
|
+
# `ambiguous_style_detected`, `conflicting_styles_detected`,
|
10
|
+
# `unrecognized_style_detected` or `no_acceptable_style!` should be
|
11
|
+
# used too, and vice versa. The `xxx_style_detected` methods
|
12
|
+
# should not be used as predicates either.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
# def on_send(node)
|
18
|
+
# return add_offense(node) if opposite_style_detected
|
19
|
+
#
|
20
|
+
# correct_style_detected
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def on_send(node)
|
24
|
+
# if offense?
|
25
|
+
# add_offense(node)
|
26
|
+
# else
|
27
|
+
# correct_style_detected
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# def on_send(node)
|
32
|
+
# return unless offense?
|
33
|
+
#
|
34
|
+
# add_offense(node)
|
35
|
+
# opposite_style_detected
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# # good
|
39
|
+
# def on_send(node)
|
40
|
+
# if offense?
|
41
|
+
# add_offense(node)
|
42
|
+
# opposite_style_detected
|
43
|
+
# else
|
44
|
+
# correct_style_detected
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# def on_send(node)
|
49
|
+
# add_offense(node) if offense?
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
class StyleDetectedApiUse < Base
|
53
|
+
include RangeHelp
|
54
|
+
|
55
|
+
MSG_FOR_POSITIVE_WITHOUT_NEGATIVE =
|
56
|
+
'`correct_style_detected` method called without ' \
|
57
|
+
'calling a negative `*_style_detected` method.'
|
58
|
+
MSG_FOR_NEGATIVE_WITHOUT_POSITIVE =
|
59
|
+
'negative `*_style_detected` methods called without ' \
|
60
|
+
'calling `correct_style_detected` method.'
|
61
|
+
MSG_FOR_CONDITIONAL_USE =
|
62
|
+
'`*_style_detected` method called in conditional.'
|
63
|
+
RESTRICT_ON_SEND = %i[
|
64
|
+
correct_style_detected opposite_style_detected
|
65
|
+
unexpected_style_detected ambiguous_style_detected
|
66
|
+
conflicting_styles_detected unrecognized_style_detected
|
67
|
+
no_acceptable_style! style_detected
|
68
|
+
].freeze
|
69
|
+
|
70
|
+
def_node_matcher :correct_style_detected_check, <<~PATTERN
|
71
|
+
(send nil? :correct_style_detected)
|
72
|
+
PATTERN
|
73
|
+
|
74
|
+
def_node_matcher :negative_style_detected_method_check, <<~PATTERN
|
75
|
+
(send nil? /(?:opposite|unexpected|ambiguous|unrecognized)_style_detected|conflicting_styles_detected/ ...)
|
76
|
+
PATTERN
|
77
|
+
|
78
|
+
def_node_matcher :no_acceptable_style_check, <<~PATTERN
|
79
|
+
(send nil? :no_acceptable_style!)
|
80
|
+
PATTERN
|
81
|
+
|
82
|
+
def_node_matcher :style_detected_check, <<~PATTERN
|
83
|
+
(send nil? :style_detected ...)
|
84
|
+
PATTERN
|
85
|
+
|
86
|
+
def on_new_investigation
|
87
|
+
@correct_style_detected_called = false
|
88
|
+
@negative_style_detected_methods_called = false
|
89
|
+
@style_detected_called = false
|
90
|
+
end
|
91
|
+
|
92
|
+
def on_investigation_end
|
93
|
+
return if style_detected_called
|
94
|
+
return unless correct_style_detected_called ^ negative_style_detected_methods_called
|
95
|
+
|
96
|
+
add_global_offense(MSG_FOR_POSITIVE_WITHOUT_NEGATIVE) if positive_without_negative?
|
97
|
+
add_global_offense(MSG_FOR_NEGATIVE_WITHOUT_POSITIVE) if negative_without_positive?
|
98
|
+
end
|
99
|
+
|
100
|
+
def on_send(node)
|
101
|
+
if correct_style_detected_check(node)
|
102
|
+
@correct_style_detected_called = true
|
103
|
+
elsif negative_style_detected_method_check(node) || no_acceptable_style_check(node)
|
104
|
+
@negative_style_detected_methods_called = true
|
105
|
+
elsif style_detected_check(node)
|
106
|
+
@style_detected_called = true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def on_if(node)
|
111
|
+
traverse_condition(node.condition) do |cond|
|
112
|
+
add_offense(cond, message: MSG_FOR_CONDITIONAL_USE) if style_detected_api_used?(cond)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
attr_reader :correct_style_detected_called,
|
119
|
+
:negative_style_detected_methods_called,
|
120
|
+
:style_detected_called
|
121
|
+
|
122
|
+
def positive_without_negative?
|
123
|
+
correct_style_detected_called && !negative_style_detected_methods_called
|
124
|
+
end
|
125
|
+
|
126
|
+
def negative_without_positive?
|
127
|
+
negative_style_detected_methods_called && !correct_style_detected_called
|
128
|
+
end
|
129
|
+
|
130
|
+
def style_detected_api_used?(node)
|
131
|
+
correct_style_detected_check(node) ||
|
132
|
+
negative_style_detected_method_check(node) ||
|
133
|
+
no_acceptable_style_check(node) ||
|
134
|
+
style_detected_check(node)
|
135
|
+
end
|
136
|
+
|
137
|
+
def traverse_condition(condition, &block)
|
138
|
+
yield condition if condition.send_type?
|
139
|
+
|
140
|
+
condition.each_child_node { |child| traverse_condition(child, &block) }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -76,6 +76,14 @@ module RuboCop
|
|
76
76
|
#
|
77
77
|
# def b
|
78
78
|
# end
|
79
|
+
#
|
80
|
+
# @example AllowAdjacentOneLineDefs: true
|
81
|
+
#
|
82
|
+
# # good
|
83
|
+
# class ErrorA < BaseError; end
|
84
|
+
# class ErrorB < BaseError; end
|
85
|
+
# class ErrorC < BaseError; end
|
86
|
+
#
|
79
87
|
class EmptyLineBetweenDefs < Base
|
80
88
|
include RangeHelp
|
81
89
|
extend AutoCorrector
|
@@ -113,8 +121,8 @@ module RuboCop
|
|
113
121
|
|
114
122
|
def autocorrect(corrector, prev_def, node)
|
115
123
|
# finds position of first newline
|
116
|
-
end_pos = prev_def.
|
117
|
-
source_buffer = prev_def.
|
124
|
+
end_pos = end_loc(prev_def).end_pos
|
125
|
+
source_buffer = end_loc(prev_def).source_buffer
|
118
126
|
newline_pos = source_buffer.source.index("\n", end_pos)
|
119
127
|
|
120
128
|
# Handle the case when multiple one-liners are on the same line.
|
@@ -198,7 +206,15 @@ module RuboCop
|
|
198
206
|
end
|
199
207
|
|
200
208
|
def def_end(node)
|
201
|
-
node.
|
209
|
+
end_loc(node).line
|
210
|
+
end
|
211
|
+
|
212
|
+
def end_loc(node)
|
213
|
+
if (node.def_type? || node.defs_type?) && node.endless?
|
214
|
+
node.loc.expression.end
|
215
|
+
else
|
216
|
+
node.loc.end
|
217
|
+
end
|
202
218
|
end
|
203
219
|
|
204
220
|
def autocorrect_remove_lines(corrector, newline_pos, count)
|
@@ -70,6 +70,7 @@ module RuboCop
|
|
70
70
|
return unless outermost_send.loc.end
|
71
71
|
return unless heredoc_arg.first_line != outermost_send.loc.end.line
|
72
72
|
return if subsequent_closing_parentheses_in_same_line?(outermost_send)
|
73
|
+
return if exist_argument_between_heredoc_end_and_closing_parentheses?(node)
|
73
74
|
|
74
75
|
add_offense(outermost_send.loc.end) do |corrector|
|
75
76
|
autocorrect(corrector, outermost_send)
|
@@ -215,6 +216,19 @@ module RuboCop
|
|
215
216
|
end
|
216
217
|
end
|
217
218
|
|
219
|
+
def exist_argument_between_heredoc_end_and_closing_parentheses?(node)
|
220
|
+
return false unless (heredoc_end = find_most_bottom_of_heredoc_end(node.arguments))
|
221
|
+
|
222
|
+
heredoc_end < node.loc.end.begin_pos &&
|
223
|
+
range_between(heredoc_end, node.loc.end.begin_pos).source.strip != ''
|
224
|
+
end
|
225
|
+
|
226
|
+
def find_most_bottom_of_heredoc_end(arguments)
|
227
|
+
arguments.map do |argument|
|
228
|
+
argument.loc.heredoc_end.end_pos if argument.loc.respond_to?(:heredoc_end)
|
229
|
+
end.compact.max
|
230
|
+
end
|
231
|
+
|
218
232
|
# Internal trailing comma helpers.
|
219
233
|
|
220
234
|
def remove_internal_trailing_comma(node, corrector)
|
@@ -58,12 +58,13 @@ module RuboCop
|
|
58
58
|
# bar: "0000000000",
|
59
59
|
# baz: "0000000000",
|
60
60
|
# }
|
61
|
-
class LineLength <
|
61
|
+
class LineLength < Base
|
62
62
|
include CheckLineBreakable
|
63
63
|
include ConfigurableMax
|
64
64
|
include IgnoredPattern
|
65
65
|
include RangeHelp
|
66
66
|
include LineLengthHelp
|
67
|
+
extend AutoCorrector
|
67
68
|
|
68
69
|
MSG = 'Line is too long. [%<length>d/%<max>d]'
|
69
70
|
|
@@ -78,28 +79,16 @@ module RuboCop
|
|
78
79
|
alias on_hash on_potential_breakable_node
|
79
80
|
alias on_send on_potential_breakable_node
|
80
81
|
|
81
|
-
def
|
82
|
+
def on_new_investigation
|
82
83
|
check_for_breakable_semicolons(processed_source)
|
83
84
|
end
|
84
85
|
|
85
|
-
def
|
86
|
+
def on_investigation_end
|
86
87
|
processed_source.lines.each_with_index do |line, line_index|
|
87
88
|
check_line(line, line_index)
|
88
89
|
end
|
89
90
|
end
|
90
91
|
|
91
|
-
def correctable?
|
92
|
-
super && !breakable_range.nil?
|
93
|
-
end
|
94
|
-
|
95
|
-
def autocorrect(range)
|
96
|
-
return if range.nil?
|
97
|
-
|
98
|
-
lambda do |corrector|
|
99
|
-
corrector.insert_before(range, "\n")
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
92
|
private
|
104
93
|
|
105
94
|
attr_accessor :breakable_range
|
@@ -203,8 +192,9 @@ module RuboCop
|
|
203
192
|
|
204
193
|
self.breakable_range = breakable_range_by_line_index[line_index]
|
205
194
|
|
206
|
-
add_offense(
|
195
|
+
add_offense(loc, message: message) do |corrector|
|
207
196
|
self.max = line_length(line)
|
197
|
+
corrector.insert_before(breakable_range, "\n") unless breakable_range.nil?
|
208
198
|
end
|
209
199
|
end
|
210
200
|
|
@@ -16,14 +16,14 @@ module RuboCop
|
|
16
16
|
# if a +
|
17
17
|
# b
|
18
18
|
# something &&
|
19
|
-
#
|
19
|
+
# something_else
|
20
20
|
# end
|
21
21
|
#
|
22
22
|
# # good
|
23
23
|
# if a +
|
24
24
|
# b
|
25
25
|
# something &&
|
26
|
-
#
|
26
|
+
# something_else
|
27
27
|
# end
|
28
28
|
#
|
29
29
|
# @example EnforcedStyle: indented
|
@@ -191,16 +191,9 @@ module RuboCop
|
|
191
191
|
end
|
192
192
|
|
193
193
|
def begin_end_alignment_style
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
# config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
|
198
|
-
# config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
|
199
|
-
if config.for_all_cops['NewCops'] == 'enable' ||
|
200
|
-
config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
|
201
|
-
config.for_cop('Layout/BeginEndAlignment')['Enabled'] != 'pending'
|
202
|
-
config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
|
203
|
-
end
|
194
|
+
begin_end_alignment_conf = config.for_cop('Layout/BeginEndAlignment')
|
195
|
+
|
196
|
+
begin_end_alignment_conf['Enabled'] && begin_end_alignment_conf['EnforcedStyleAlignWith']
|
204
197
|
end
|
205
198
|
end
|
206
199
|
end
|
@@ -106,6 +106,7 @@ module RuboCop
|
|
106
106
|
def space_missing(left_brace)
|
107
107
|
add_offense(left_brace, message: MISSING_MSG) do |corrector|
|
108
108
|
autocorrect(corrector, left_brace)
|
109
|
+
opposite_style_detected
|
109
110
|
end
|
110
111
|
end
|
111
112
|
|
@@ -114,6 +115,7 @@ module RuboCop
|
|
114
115
|
|
115
116
|
add_offense(space, message: DETECTED_MSG) do |corrector|
|
116
117
|
autocorrect(corrector, space)
|
118
|
+
opposite_style_detected
|
117
119
|
end
|
118
120
|
end
|
119
121
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Layout
|
6
|
+
# Checks for space between the name of a receiver and a left
|
7
|
+
# brackets.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# collection [index_or_key]
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# collection[index_or_key]
|
16
|
+
#
|
17
|
+
class SpaceBeforeBrackets < Base
|
18
|
+
include RangeHelp
|
19
|
+
extend AutoCorrector
|
20
|
+
|
21
|
+
MSG = 'Remove the space before the opening brackets.'
|
22
|
+
|
23
|
+
def on_send(node)
|
24
|
+
return unless (first_argument = node.first_argument)
|
25
|
+
|
26
|
+
begin_pos = first_argument.source_range.begin_pos
|
27
|
+
return unless (range = offense_range(node, begin_pos))
|
28
|
+
|
29
|
+
register_offense(range)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def offense_range(node, begin_pos)
|
35
|
+
if reference_variable_with_brackets?(node)
|
36
|
+
receiver_end_pos = node.receiver.source_range.end_pos
|
37
|
+
selector_begin_pos = node.loc.selector.begin_pos
|
38
|
+
return if receiver_end_pos >= selector_begin_pos
|
39
|
+
|
40
|
+
range_between(receiver_end_pos, selector_begin_pos)
|
41
|
+
elsif node.method?(:[]=)
|
42
|
+
end_pos = node.receiver.source_range.end_pos
|
43
|
+
|
44
|
+
return if begin_pos - end_pos == 1
|
45
|
+
|
46
|
+
range_between(end_pos, begin_pos - 1)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def register_offense(range)
|
51
|
+
add_offense(range) do |corrector|
|
52
|
+
corrector.remove(range)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def reference_variable_with_brackets?(node)
|
57
|
+
node.receiver&.variable? && node.method?(:[]) && node.arguments.size == 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -111,7 +111,8 @@ module RuboCop
|
|
111
111
|
braces_with_contents_inside(node, inner)
|
112
112
|
elsif style_for_empty_braces == :no_space
|
113
113
|
offense(range.begin_pos, range.end_pos,
|
114
|
-
'Space inside empty braces detected.'
|
114
|
+
'Space inside empty braces detected.',
|
115
|
+
'EnforcedStyleForEmptyBraces')
|
115
116
|
end
|
116
117
|
end
|
117
118
|
end
|
@@ -120,7 +121,8 @@ module RuboCop
|
|
120
121
|
return if style_for_empty_braces != :space
|
121
122
|
|
122
123
|
offense(left_brace.begin_pos, right_brace.end_pos,
|
123
|
-
'Space missing inside empty braces.'
|
124
|
+
'Space missing inside empty braces.',
|
125
|
+
'EnforcedStyleForEmptyBraces')
|
124
126
|
end
|
125
127
|
|
126
128
|
def braces_with_contents_inside(node, inner)
|
@@ -164,9 +166,9 @@ module RuboCop
|
|
164
166
|
if left_brace.end_pos == args_delimiter.begin_pos &&
|
165
167
|
cop_config['SpaceBeforeBlockParameters']
|
166
168
|
offense(left_brace.begin_pos, args_delimiter.end_pos,
|
167
|
-
'Space between { and | missing.')
|
168
|
-
|
169
|
-
|
169
|
+
'Space between { and | missing.')
|
170
|
+
else
|
171
|
+
correct_style_detected
|
170
172
|
end
|
171
173
|
else
|
172
174
|
# We indicate the position after the left brace. Otherwise it's
|
@@ -179,11 +181,11 @@ module RuboCop
|
|
179
181
|
|
180
182
|
def space_inside_left_brace(left_brace, args_delimiter)
|
181
183
|
if pipe?(args_delimiter)
|
182
|
-
|
184
|
+
if cop_config['SpaceBeforeBlockParameters']
|
185
|
+
correct_style_detected
|
186
|
+
else
|
183
187
|
offense(left_brace.end_pos, args_delimiter.begin_pos,
|
184
|
-
'Space between { and | detected.')
|
185
|
-
opposite_style_detected
|
186
|
-
end
|
188
|
+
'Space between { and | detected.')
|
187
189
|
end
|
188
190
|
else
|
189
191
|
brace_with_space = range_with_surrounding_space(range: left_brace,
|
@@ -220,7 +222,7 @@ module RuboCop
|
|
220
222
|
end
|
221
223
|
end
|
222
224
|
|
223
|
-
def offense(begin_pos, end_pos, msg)
|
225
|
+
def offense(begin_pos, end_pos, msg, style_param = 'EnforcedStyle')
|
224
226
|
range = range_between(begin_pos, end_pos)
|
225
227
|
add_offense(range, message: msg) do |corrector|
|
226
228
|
case range.source
|
@@ -229,6 +231,7 @@ module RuboCop
|
|
229
231
|
when '{|' then corrector.replace(range, '{ |')
|
230
232
|
else corrector.insert_before(range, ' ')
|
231
233
|
end
|
234
|
+
opposite_style_detected if style_param == 'EnforcedStyle'
|
232
235
|
end
|
233
236
|
end
|
234
237
|
|