rubocop 0.91.0 → 1.0.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 +100 -54
- data/lib/rubocop.rb +10 -6
- data/lib/rubocop/cached_data.rb +2 -1
- data/lib/rubocop/cli/command/version.rb +1 -1
- data/lib/rubocop/comment_config.rb +9 -5
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_loader.rb +19 -2
- data/lib/rubocop/config_loader_resolver.rb +7 -5
- data/lib/rubocop/config_regeneration.rb +33 -0
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/badge.rb +9 -24
- data/lib/rubocop/cop/base.rb +16 -1
- data/lib/rubocop/cop/commissioner.rb +34 -20
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -10
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/case_indentation.rb +4 -7
- data/lib/rubocop/cop/layout/class_structure.rb +8 -1
- data/lib/rubocop/cop/layout/dot_position.rb +6 -9
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +4 -13
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +13 -8
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +10 -1
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +4 -13
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -7
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +0 -4
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +4 -18
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +2 -2
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +18 -1
- data/lib/rubocop/cop/lint/boolean_symbol.rb +3 -0
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +23 -3
- data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +2 -4
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +37 -0
- data/lib/rubocop/cop/lint/identity_comparison.rb +5 -3
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +2 -5
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +22 -12
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -4
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +78 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +0 -1
- data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -6
- data/lib/rubocop/cop/lint/to_json.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -5
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +3 -9
- data/lib/rubocop/cop/lint/useless_times.rb +11 -2
- data/lib/rubocop/cop/metrics/block_length.rb +3 -1
- data/lib/rubocop/cop/metrics/class_length.rb +14 -6
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +25 -16
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +3 -3
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/rescue_node.rb +1 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -3
- data/lib/rubocop/cop/mixin/visibility_help.rb +4 -16
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/offense.rb +15 -2
- data/lib/rubocop/cop/security/open.rb +12 -10
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +6 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +3 -0
- data/lib/rubocop/cop/style/array_coercion.rb +4 -0
- data/lib/rubocop/cop/style/case_like_if.rb +20 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +64 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +13 -11
- data/lib/rubocop/cop/style/comment_annotation.rb +6 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +7 -8
- data/lib/rubocop/cop/style/date_time.rb +12 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +6 -2
- data/lib/rubocop/cop/style/for.rb +0 -4
- data/lib/rubocop/cop/style/format_string_token.rb +48 -3
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +15 -6
- data/lib/rubocop/cop/style/if_unless_modifier.rb +0 -4
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -6
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -11
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/mixin_usage.rb +7 -27
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -2
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -0
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -1
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +12 -1
- data/lib/rubocop/cop/style/raise_args.rb +0 -3
- data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -9
- data/lib/rubocop/cop/style/redundant_begin.rb +36 -8
- data/lib/rubocop/cop/style/redundant_condition.rb +5 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +4 -5
- data/lib/rubocop/cop/style/redundant_interpolation.rb +6 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +6 -3
- data/lib/rubocop/cop/style/redundant_percent_q.rb +9 -11
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +39 -24
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -15
- data/lib/rubocop/cop/style/redundant_return.rb +17 -17
- data/lib/rubocop/cop/style/redundant_self.rb +10 -9
- data/lib/rubocop/cop/style/redundant_sort.rb +13 -24
- data/lib/rubocop/cop/style/redundant_sort_by.rb +5 -9
- data/lib/rubocop/cop/style/rescue_standard_error.rb +20 -16
- data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
- data/lib/rubocop/cop/style/string_concatenation.rb +14 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +4 -3
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
- data/lib/rubocop/cop/util.rb +0 -1
- data/lib/rubocop/cop/variable_force/branch.rb +0 -4
- data/lib/rubocop/directive_comment.rb +32 -0
- data/lib/rubocop/ext/regexp_node.rb +20 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +12 -5
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/options.rb +22 -17
- data/lib/rubocop/result_cache.rb +8 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +5 -5
- data/lib/rubocop/runner.rb +9 -5
- data/lib/rubocop/target_finder.rb +27 -26
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +61 -6
- metadata +14 -17
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +0 -43
- data/lib/rubocop/cop/tokens_util.rb +0 -84
@@ -52,8 +52,8 @@ module RuboCop
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def semicolon(node)
|
55
|
-
@semicolon ||= {}
|
56
|
-
@semicolon[node
|
55
|
+
@semicolon ||= {}.compare_by_identity
|
56
|
+
@semicolon[node] ||= processed_source.tokens_within(node).find(&:semicolon?)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -89,7 +89,7 @@ module RuboCop
|
|
89
89
|
begin_line_num = previous_line_num - base_line_num + 1
|
90
90
|
end_line_num = node.first_line - base_line_num + 1
|
91
91
|
lines = source_in_lines[begin_line_num...end_line_num]
|
92
|
-
"\n#{
|
92
|
+
"\n#{lines.join("\n").split(node.source).first || ''}"
|
93
93
|
end
|
94
94
|
|
95
95
|
def fix_escaped_content(word_node, escape, delimiters)
|
@@ -58,24 +58,22 @@ module RuboCop
|
|
58
58
|
(send _ :required_ruby_version= $_)
|
59
59
|
PATTERN
|
60
60
|
|
61
|
-
def_node_matcher :
|
62
|
-
{(str _) (array (str _))
|
61
|
+
def_node_matcher :defined_ruby_version, <<~PATTERN
|
62
|
+
{$(str _) $(array (str _) (str _))
|
63
|
+
(send (const (const nil? :Gem) :Requirement) :new $(str _))}
|
63
64
|
PATTERN
|
64
65
|
|
65
66
|
# rubocop:disable Metrics/AbcSize
|
66
67
|
def investigate(processed_source)
|
67
|
-
|
68
|
+
version_def = required_ruby_version(processed_source.ast).first
|
68
69
|
|
69
|
-
if
|
70
|
-
|
71
|
-
|
72
|
-
ruby_version = extract_ruby_version(version)
|
73
|
-
|
74
|
-
return if ruby_version == target_ruby_version.to_s
|
70
|
+
if version_def
|
71
|
+
ruby_version = extract_ruby_version(defined_ruby_version(version_def))
|
72
|
+
return if !ruby_version || ruby_version == target_ruby_version.to_s
|
75
73
|
|
76
74
|
add_offense(
|
77
75
|
processed_source.ast,
|
78
|
-
location:
|
76
|
+
location: version_def.loc.expression,
|
79
77
|
message: not_equal_message(ruby_version, target_ruby_version)
|
80
78
|
)
|
81
79
|
else
|
@@ -88,6 +86,8 @@ module RuboCop
|
|
88
86
|
private
|
89
87
|
|
90
88
|
def extract_ruby_version(required_ruby_version)
|
89
|
+
return unless required_ruby_version
|
90
|
+
|
91
91
|
if required_ruby_version.array_type?
|
92
92
|
required_ruby_version = required_ruby_version.children.detect do |v|
|
93
93
|
/[>=]/.match?(v.str_content)
|
@@ -105,7 +105,10 @@ module RuboCop
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def incorrect_style(when_node)
|
108
|
-
|
108
|
+
depth = indent_one_step? ? 'one step more than' : 'as deep as'
|
109
|
+
message = format(MSG, depth: depth, base: style)
|
110
|
+
|
111
|
+
add_offense(when_node.loc.keyword, message: message) do |corrector|
|
109
112
|
detect_incorrect_style(when_node)
|
110
113
|
|
111
114
|
whitespace = whitespace_range(when_node)
|
@@ -125,12 +128,6 @@ module RuboCop
|
|
125
128
|
end
|
126
129
|
end
|
127
130
|
|
128
|
-
def find_message(*)
|
129
|
-
depth = indent_one_step? ? 'one step more than' : 'as deep as'
|
130
|
-
|
131
|
-
format(MSG, depth: depth, base: style)
|
132
|
-
end
|
133
|
-
|
134
131
|
def base_column(case_node, base)
|
135
132
|
case base
|
136
133
|
when :case then case_node.location.keyword.column
|
@@ -169,7 +169,7 @@ module RuboCop
|
|
169
169
|
# Autocorrect by swapping between two nodes autocorrecting them
|
170
170
|
def autocorrect(corrector, node)
|
171
171
|
node_classification = classify(node)
|
172
|
-
previous =
|
172
|
+
previous = node.left_siblings.find do |sibling|
|
173
173
|
classification = classify(sibling)
|
174
174
|
!ignore?(classification) && node_classification != classification
|
175
175
|
end
|
@@ -265,6 +265,9 @@ module RuboCop
|
|
265
265
|
end
|
266
266
|
|
267
267
|
def end_position_for(node)
|
268
|
+
heredoc = find_heredoc(node)
|
269
|
+
return heredoc.location.heredoc_end.end_pos + 1 if heredoc
|
270
|
+
|
268
271
|
end_line = buffer.line_for_position(node.loc.expression.end_pos)
|
269
272
|
buffer.line_range(end_line).end_pos
|
270
273
|
end
|
@@ -284,6 +287,10 @@ module RuboCop
|
|
284
287
|
buffer.line_range(node.loc.line).begin_pos - 1
|
285
288
|
end
|
286
289
|
|
290
|
+
def find_heredoc(node)
|
291
|
+
node.each_node(:str, :dstr, :xstr).find(&:heredoc?)
|
292
|
+
end
|
293
|
+
|
287
294
|
def buffer
|
288
295
|
processed_source.buffer
|
289
296
|
end
|
@@ -29,17 +29,14 @@ module RuboCop
|
|
29
29
|
def on_send(node)
|
30
30
|
return unless node.dot? || ampersand_dot?(node)
|
31
31
|
|
32
|
-
if proper_dot_position?(node)
|
33
|
-
correct_style_detected
|
34
|
-
else
|
35
|
-
return unless opposite_style_detected
|
32
|
+
return correct_style_detected if proper_dot_position?(node)
|
36
33
|
|
37
|
-
|
38
|
-
|
34
|
+
opposite_style_detected
|
35
|
+
dot = node.loc.dot
|
36
|
+
message = message(dot)
|
39
37
|
|
40
|
-
|
41
|
-
|
42
|
-
end
|
38
|
+
add_offense(dot, message: message) do |corrector|
|
39
|
+
autocorrect(corrector, dot, node)
|
43
40
|
end
|
44
41
|
end
|
45
42
|
alias on_csend on_send
|
@@ -95,7 +95,7 @@ module RuboCop
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def next_sibling_parent_empty_or_else?(node)
|
98
|
-
next_sibling = node.
|
98
|
+
next_sibling = node.right_sibling
|
99
99
|
return true if next_sibling.nil?
|
100
100
|
|
101
101
|
parent = next_sibling.parent
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def next_sibling_empty_or_guard_clause?(node)
|
107
|
-
next_sibling = node.
|
107
|
+
next_sibling = node.right_sibling
|
108
108
|
return true if next_sibling.nil?
|
109
109
|
|
110
110
|
next_sibling.if_type? && contains_guard_clause?(next_sibling)
|
@@ -53,7 +53,6 @@ module RuboCop
|
|
53
53
|
#
|
54
54
|
class EmptyLineAfterMultilineCondition < Base
|
55
55
|
include RangeHelp
|
56
|
-
include RescueNode
|
57
56
|
extend AutoCorrector
|
58
57
|
|
59
58
|
MSG = 'Use empty line after multiline condition.'
|
@@ -62,7 +61,7 @@ module RuboCop
|
|
62
61
|
return if node.ternary?
|
63
62
|
|
64
63
|
if node.modifier_form?
|
65
|
-
check_condition(node.condition) if right_sibling
|
64
|
+
check_condition(node.condition) if node.right_sibling
|
66
65
|
else
|
67
66
|
check_condition(node.condition)
|
68
67
|
end
|
@@ -74,7 +73,7 @@ module RuboCop
|
|
74
73
|
alias on_until on_while
|
75
74
|
|
76
75
|
def on_while_post(node)
|
77
|
-
return unless right_sibling
|
76
|
+
return unless node.right_sibling
|
78
77
|
|
79
78
|
check_condition(node.condition)
|
80
79
|
end
|
@@ -92,10 +91,8 @@ module RuboCop
|
|
92
91
|
end
|
93
92
|
|
94
93
|
def on_rescue(node)
|
95
|
-
|
96
|
-
|
97
|
-
resbodies.each do |resbody|
|
98
|
-
rescued_exceptions = rescued_exceptions(resbody)
|
94
|
+
node.resbody_branches.each do |resbody|
|
95
|
+
rescued_exceptions = resbody.exceptions
|
99
96
|
next if !multiline_rescue_exceptions?(rescued_exceptions) ||
|
100
97
|
next_line_empty?(rescued_exceptions.last.last_line)
|
101
98
|
|
@@ -116,12 +113,6 @@ module RuboCop
|
|
116
113
|
processed_source[line].blank?
|
117
114
|
end
|
118
115
|
|
119
|
-
def right_sibling(node)
|
120
|
-
return unless node.parent
|
121
|
-
|
122
|
-
node.parent.children[node.sibling_index + 1]
|
123
|
-
end
|
124
|
-
|
125
116
|
def multiline_when_condition?(when_node)
|
126
117
|
when_node.conditions.first.first_line != when_node.conditions.last.last_line
|
127
118
|
end
|
@@ -84,14 +84,8 @@ module RuboCop
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def on_send(node)
|
87
|
-
return unless node.bare_access_modifier?
|
88
|
-
|
89
|
-
case style
|
90
|
-
when :around
|
91
|
-
return if empty_lines_around?(node)
|
92
|
-
when :only_before
|
93
|
-
return if allowed_only_before_style?(node)
|
94
|
-
end
|
87
|
+
return unless node.bare_access_modifier? && !node.parent&.block_type?
|
88
|
+
return if expected_empty_lines?(node)
|
95
89
|
|
96
90
|
message = message(node)
|
97
91
|
add_offense(node, message: message) do |corrector|
|
@@ -105,6 +99,17 @@ module RuboCop
|
|
105
99
|
|
106
100
|
private
|
107
101
|
|
102
|
+
def expected_empty_lines?(node)
|
103
|
+
case style
|
104
|
+
when :around
|
105
|
+
return true if empty_lines_around?(node)
|
106
|
+
when :only_before
|
107
|
+
return true if allowed_only_before_style?(node)
|
108
|
+
end
|
109
|
+
|
110
|
+
false
|
111
|
+
end
|
112
|
+
|
108
113
|
def allowed_only_before_style?(node)
|
109
114
|
if node.special_modifier?
|
110
115
|
return true if processed_source[node.last_line] == 'end'
|
@@ -88,7 +88,7 @@ module RuboCop
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def require_empty_line?(node)
|
91
|
-
return false unless node
|
91
|
+
return false unless node.respond_to?(:type)
|
92
92
|
|
93
93
|
!allow_alias?(node) && !attribute_or_allowed_method?(node)
|
94
94
|
end
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
def next_line_node(node)
|
97
97
|
return if node.parent.if_type?
|
98
98
|
|
99
|
-
node.
|
99
|
+
node.right_sibling
|
100
100
|
end
|
101
101
|
|
102
102
|
def allow_alias?(node)
|
@@ -113,10 +113,9 @@ module RuboCop
|
|
113
113
|
end
|
114
114
|
|
115
115
|
def keyword_locations_in_rescue(node)
|
116
|
-
_begin_body, *resbodies, _else_body = *node
|
117
116
|
[
|
118
117
|
node.loc.else,
|
119
|
-
*
|
118
|
+
*node.resbody_branches.map { |body| body.loc.keyword }
|
120
119
|
].compact
|
121
120
|
end
|
122
121
|
|
@@ -191,7 +191,16 @@ module RuboCop
|
|
191
191
|
end
|
192
192
|
|
193
193
|
def begin_end_alignment_style
|
194
|
-
|
194
|
+
# FIXME: Workaround for pending status for `Layout/BeginEndAlignment` cop
|
195
|
+
# When RuboCop 1.0 is released, please replace it with the following condition.
|
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
|
195
204
|
end
|
196
205
|
end
|
197
206
|
end
|
@@ -36,8 +36,8 @@ module RuboCop
|
|
36
36
|
MSG = 'Surrounding space %<type>s in default value assignment.'
|
37
37
|
|
38
38
|
def on_optarg(node)
|
39
|
-
|
40
|
-
arg, equals, value =
|
39
|
+
tokens = processed_source.tokens_within(node)
|
40
|
+
arg, equals, value = tokens.take(3)
|
41
41
|
check_optarg(arg, equals, value)
|
42
42
|
end
|
43
43
|
|
@@ -51,22 +51,13 @@ module RuboCop
|
|
51
51
|
style == :no_space && no_surrounding_space
|
52
52
|
correct_style_detected
|
53
53
|
else
|
54
|
-
incorrect_style_detected(arg, value
|
55
|
-
no_surrounding_space)
|
54
|
+
incorrect_style_detected(arg, value)
|
56
55
|
end
|
57
56
|
end
|
58
57
|
|
59
|
-
def incorrect_style_detected(arg, value
|
60
|
-
no_surrounding_space)
|
58
|
+
def incorrect_style_detected(arg, value)
|
61
59
|
range = range_between(arg.end_pos, value.begin_pos)
|
62
60
|
|
63
|
-
if style == :space && no_surrounding_space ||
|
64
|
-
style == :no_space && space_on_both_sides
|
65
|
-
return unless opposite_style_detected
|
66
|
-
else
|
67
|
-
return unless unrecognized_style_detected
|
68
|
-
end
|
69
|
-
|
70
61
|
add_offense(range) do |corrector|
|
71
62
|
autocorrect(corrector, range)
|
72
63
|
end
|
@@ -5,6 +5,8 @@ module RuboCop
|
|
5
5
|
module Layout
|
6
6
|
# Checks that operators have space around them, except for ** which
|
7
7
|
# should or shouldn't have surrounding space depending on configuration.
|
8
|
+
# It allows vertical alignment consisting of one or more whitespace
|
9
|
+
# around operators.
|
8
10
|
#
|
9
11
|
# This cop has `AllowForAlignment` option. When `true`, allows most
|
10
12
|
# uses of extra spacing if the intent is to align with an operator on
|
@@ -207,7 +209,8 @@ module RuboCop
|
|
207
209
|
token = Token.new(operator, nil, operator.source)
|
208
210
|
align_preceding = aligned_with_preceding_assignment(token)
|
209
211
|
|
210
|
-
return
|
212
|
+
return false if align_preceding == :yes ||
|
213
|
+
aligned_with_subsequent_assignment(token) == :none
|
211
214
|
|
212
215
|
aligned_with_subsequent_assignment(token) != :yes
|
213
216
|
end
|
@@ -108,11 +108,11 @@ module RuboCop
|
|
108
108
|
end
|
109
109
|
|
110
110
|
def left_array_bracket(node)
|
111
|
-
|
111
|
+
processed_source.tokens_within(node).find(&:left_array_bracket?)
|
112
112
|
end
|
113
113
|
|
114
114
|
def right_array_bracket(node)
|
115
|
-
|
115
|
+
processed_source.tokens_within(node).reverse.find(&:right_bracket?)
|
116
116
|
end
|
117
117
|
|
118
118
|
def empty_config
|
@@ -120,7 +120,7 @@ module RuboCop
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def next_to_newline?(node, token)
|
123
|
-
|
123
|
+
processed_source.tokens_within(node)[index_for(node, token) + 1].line != token.line
|
124
124
|
end
|
125
125
|
|
126
126
|
def end_has_own_line?(token)
|
@@ -131,7 +131,7 @@ module RuboCop
|
|
131
131
|
end
|
132
132
|
|
133
133
|
def index_for(node, token)
|
134
|
-
|
134
|
+
processed_source.tokens_within(node).index(token)
|
135
135
|
end
|
136
136
|
|
137
137
|
def line_and_column_for(token)
|
@@ -153,7 +153,7 @@ module RuboCop
|
|
153
153
|
end
|
154
154
|
|
155
155
|
def next_to_comment?(node, token)
|
156
|
-
|
156
|
+
processed_source.tokens_within(node)[index_for(node, token) + 1].comment?
|
157
157
|
end
|
158
158
|
|
159
159
|
def compact_offenses(node, left, right, start_ok, end_ok)
|
@@ -184,9 +184,9 @@ module RuboCop
|
|
184
184
|
def multi_dimensional_array?(node, token, side: :right)
|
185
185
|
i = index_for(node, token)
|
186
186
|
if side == :right
|
187
|
-
|
187
|
+
processed_source.tokens_within(node)[i - 1].right_bracket?
|
188
188
|
else
|
189
|
-
|
189
|
+
processed_source.tokens_within(node)[i + 1].left_array_bracket?
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
@@ -206,8 +206,6 @@ module RuboCop
|
|
206
206
|
|
207
207
|
def no_space(begin_pos, end_pos, msg)
|
208
208
|
if style == :space
|
209
|
-
return unless opposite_style_detected
|
210
|
-
|
211
209
|
offense(begin_pos, end_pos, msg)
|
212
210
|
else
|
213
211
|
correct_style_detected
|
@@ -216,8 +214,6 @@ module RuboCop
|
|
216
214
|
|
217
215
|
def space(begin_pos, end_pos, msg)
|
218
216
|
if style == :no_space
|
219
|
-
return unless opposite_style_detected
|
220
|
-
|
221
217
|
offense(begin_pos, end_pos, msg)
|
222
218
|
else
|
223
219
|
correct_style_detected
|
@@ -72,29 +72,15 @@ module RuboCop
|
|
72
72
|
MSG = 'Space inside %<problem>s.'
|
73
73
|
|
74
74
|
def on_hash(node)
|
75
|
-
tokens = processed_source.
|
75
|
+
tokens = processed_source.tokens_within(node)
|
76
|
+
return unless tokens.first.left_brace? && tokens.last.right_curly_brace?
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
return if begin_index == end_index - 1
|
80
|
-
|
81
|
-
check(tokens[end_index - 1], tokens[end_index])
|
82
|
-
end
|
78
|
+
check(tokens[0], tokens[1])
|
79
|
+
check(tokens[-2], tokens[-1]) if tokens.size > 2
|
83
80
|
end
|
84
81
|
|
85
82
|
private
|
86
83
|
|
87
|
-
def hash_literal_with_braces(node)
|
88
|
-
tokens = processed_source.tokens
|
89
|
-
begin_index = index_of_first_token(node)
|
90
|
-
return unless tokens[begin_index].left_brace?
|
91
|
-
|
92
|
-
end_index = index_of_last_token(node)
|
93
|
-
return unless tokens[end_index].right_curly_brace?
|
94
|
-
|
95
|
-
yield begin_index, end_index
|
96
|
-
end
|
97
|
-
|
98
84
|
def check(token1, token2)
|
99
85
|
# No offense if line break inside.
|
100
86
|
return if token1.line < token2.line
|