rubocop 0.24.1 → 0.25.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +12 -8
- data/.travis.yml +1 -1
- data/CHANGELOG.md +42 -0
- data/Gemfile +2 -0
- data/README.md +27 -6
- data/bin/rubocop +10 -15
- data/config/default.yml +72 -25
- data/config/enabled.yml +57 -25
- data/lib/rubocop.rb +15 -8
- data/lib/rubocop/config_loader.rb +11 -7
- data/lib/rubocop/cop/cop.rb +16 -16
- data/lib/rubocop/cop/ignored_node.rb +11 -4
- data/lib/rubocop/cop/lint/block_alignment.rb +29 -4
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +5 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +5 -5
- data/lib/rubocop/cop/lint/end_in_method.rb +2 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/{style → metrics}/block_nesting.rb +1 -1
- data/lib/rubocop/cop/{style → metrics}/class_length.rb +4 -4
- data/lib/rubocop/cop/{style → metrics}/cyclomatic_complexity.rb +5 -16
- data/lib/rubocop/cop/{style → metrics}/line_length.rb +1 -1
- data/lib/rubocop/cop/{style → metrics}/method_length.rb +8 -4
- data/lib/rubocop/cop/{style → metrics}/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +61 -0
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -2
- data/lib/rubocop/cop/mixin/code_length.rb +2 -3
- data/lib/rubocop/cop/mixin/configurable_naming.rb +6 -20
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/if_node.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +32 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
- data/lib/rubocop/cop/mixin/{check_methods.rb → on_method.rb} +3 -3
- data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +24 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +2 -1
- data/lib/rubocop/cop/mixin/space_inside.rb +33 -4
- data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -14
- data/lib/rubocop/cop/mixin/string_help.rb +4 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +1 -0
- data/lib/rubocop/cop/rails/delegate.rb +2 -2
- data/lib/rubocop/cop/rails/output.rb +4 -2
- data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
- data/lib/rubocop/cop/style/align_hash.rb +9 -1
- data/lib/rubocop/cop/style/and_or.rb +37 -3
- data/lib/rubocop/cop/style/bare_percent_literals.rb +46 -0
- data/lib/rubocop/cop/style/block_end_newline.rb +56 -0
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +31 -3
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +6 -2
- data/lib/rubocop/cop/style/end_of_line.rb +3 -14
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -6
- data/lib/rubocop/cop/style/if_with_semicolon.rb +6 -7
- data/lib/rubocop/cop/style/indentation_width.rb +3 -8
- data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -2
- data/lib/rubocop/cop/style/method_name.rb +4 -18
- data/lib/rubocop/cop/style/multiline_block_layout.rb +73 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +4 -4
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +2 -2
- data/lib/rubocop/cop/style/next.rb +12 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +6 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +22 -8
- data/lib/rubocop/cop/style/percent_q_literals.rb +53 -0
- data/lib/rubocop/cop/style/predicate_name.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +2 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +7 -3
- data/lib/rubocop/cop/style/single_line_methods.rb +2 -2
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +14 -10
- data/lib/rubocop/cop/style/space_inside_brackets.rb +5 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -8
- data/lib/rubocop/cop/style/trailing_comma.rb +15 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +2 -2
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -4
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -0
- data/lib/rubocop/cop/style/variable_interpolation.rb +14 -2
- data/lib/rubocop/cop/style/variable_name.rb +17 -17
- data/lib/rubocop/cop/style/while_until_modifier.rb +2 -5
- data/lib/rubocop/cop/util.rb +5 -0
- data/lib/rubocop/cop/variable_force.rb +7 -6
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/processed_source.rb +5 -22
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.25.0.md +91 -0
- data/rubocop.gemspec +2 -3
- data/spec/project_spec.rb +1 -1
- data/spec/rubocop/cli_spec.rb +70 -44
- data/spec/rubocop/comment_config_spec.rb +6 -6
- data/spec/rubocop/config_loader_spec.rb +19 -13
- data/spec/rubocop/config_spec.rb +3 -3
- data/spec/rubocop/cop/commissioner_spec.rb +1 -1
- data/spec/rubocop/cop/cop_spec.rb +3 -3
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +113 -6
- data/spec/rubocop/cop/lint/debugger_spec.rb +10 -6
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +12 -0
- data/spec/rubocop/cop/{style → metrics}/block_nesting_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/class_length_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/cyclomatic_complexity_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/if_unless_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/{style → metrics}/line_length_spec.rb +7 -7
- data/spec/rubocop/cop/{style → metrics}/method_length_spec.rb +1 -1
- data/spec/rubocop/cop/{style → metrics}/parameter_lists_spec.rb +1 -1
- data/spec/rubocop/cop/metrics/perceived_complexity_spec.rb +222 -0
- data/spec/rubocop/cop/{style → metrics}/while_until_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/rails/output_spec.rb +8 -2
- data/spec/rubocop/cop/style/align_hash_spec.rb +7 -0
- data/spec/rubocop/cop/style/and_or_spec.rb +245 -43
- data/spec/rubocop/cop/style/bare_percent_literals_spec.rb +132 -0
- data/spec/rubocop/cop/style/block_end_newline_spec.rb +61 -0
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +34 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +8 -0
- data/spec/rubocop/cop/style/guard_clause_spec.rb +1 -1
- data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +138 -0
- data/spec/rubocop/cop/style/next_spec.rb +32 -3
- data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +34 -0
- data/spec/rubocop/cop/style/percent_q_literals_spec.rb +122 -0
- data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +20 -0
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +26 -2
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +112 -0
- data/spec/rubocop/cop/style/unneeded_percent_q_spec.rb +86 -31
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +21 -1
- data/spec/rubocop/cop/team_spec.rb +14 -9
- data/spec/spec_helper.rb +1 -0
- metadata +47 -50
- data/lib/rubocop/cop/mixin/if_then_else.rb +0 -23
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks whether the multiline do end blocks have a newline
|
7
|
+
# after the start of the block.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# blah do |i| foo(i)
|
12
|
+
# bar(i)
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# blah do |i|
|
17
|
+
# foo(i)
|
18
|
+
# bar(i)
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # bad
|
22
|
+
# blah { |i| foo(i)
|
23
|
+
# bar(i)
|
24
|
+
# }
|
25
|
+
#
|
26
|
+
# # good
|
27
|
+
# blah { |i|
|
28
|
+
# foo(i)
|
29
|
+
# bar(i)
|
30
|
+
# }
|
31
|
+
class MultilineBlockLayout < Cop
|
32
|
+
MSG = 'Block body expression is on the same line as the block start.'
|
33
|
+
|
34
|
+
def on_block(node)
|
35
|
+
end_loc = node.loc.end
|
36
|
+
do_loc = node.loc.begin # Actually it's either do or {.
|
37
|
+
return if do_loc.line == end_loc.line # One-liner, no newline needed.
|
38
|
+
|
39
|
+
# A block node has three children: the block start,
|
40
|
+
# the arguments, and the expression. We care if the block start
|
41
|
+
# and the expression start on the same line.
|
42
|
+
last_expression = node.children.last
|
43
|
+
return unless last_expression
|
44
|
+
expression_loc = last_expression.loc
|
45
|
+
return unless do_loc.line == expression_loc.line
|
46
|
+
|
47
|
+
expression = last_expression.loc.expression
|
48
|
+
range = Parser::Source::Range.new(expression.source_buffer,
|
49
|
+
expression.begin_pos,
|
50
|
+
expression.end_pos)
|
51
|
+
|
52
|
+
add_offense(node, range)
|
53
|
+
end
|
54
|
+
|
55
|
+
def autocorrect(node)
|
56
|
+
@corrections << lambda do |corrector|
|
57
|
+
_method, _args, block_body = *node
|
58
|
+
first_node = if block_body.type == :begin
|
59
|
+
block_body.children.first
|
60
|
+
else
|
61
|
+
block_body
|
62
|
+
end
|
63
|
+
|
64
|
+
block_start_col = node.loc.expression.column
|
65
|
+
|
66
|
+
corrector.insert_before(first_node.loc.expression,
|
67
|
+
"\n #{' ' * block_start_col}")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -17,9 +17,9 @@ module RuboCop
|
|
17
17
|
# end
|
18
18
|
class MultilineIfThen < Cop
|
19
19
|
include IfNode
|
20
|
-
include
|
20
|
+
include OnNormalIfUnless
|
21
21
|
|
22
|
-
def
|
22
|
+
def on_normal_if_unless(node)
|
23
23
|
condition, body, else_clause = *node
|
24
24
|
next_thing = if body && body.loc.expression
|
25
25
|
body.loc.expression.begin
|
@@ -34,14 +34,14 @@ module RuboCop
|
|
34
34
|
next_thing.begin_pos)
|
35
35
|
return unless right_after_cond.source =~ /\A\s*then\s*(#.*)?\s*\n/
|
36
36
|
|
37
|
-
node
|
37
|
+
add_offense(node, :expression, message(node))
|
38
38
|
end
|
39
39
|
|
40
40
|
def end_position(conditional_node)
|
41
41
|
conditional_node.loc.expression.end.end_pos
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
44
|
+
def message(node)
|
45
45
|
"Never use `then` for multi-line `#{node.loc.keyword.source}`."
|
46
46
|
end
|
47
47
|
end
|
@@ -10,11 +10,11 @@ module RuboCop
|
|
10
10
|
MSG = 'Favor `%s` over `%s` for negative conditions.'
|
11
11
|
|
12
12
|
def on_while(node)
|
13
|
-
|
13
|
+
check_negative_conditional(node)
|
14
14
|
end
|
15
15
|
|
16
16
|
def on_until(node)
|
17
|
-
|
17
|
+
check_negative_conditional(node)
|
18
18
|
end
|
19
19
|
|
20
20
|
def message(node)
|
@@ -56,6 +56,17 @@ module RuboCop
|
|
56
56
|
|
57
57
|
private
|
58
58
|
|
59
|
+
def min_body_length?(node)
|
60
|
+
(node.loc.end.line - node.loc.keyword.line) > min_body_length
|
61
|
+
end
|
62
|
+
|
63
|
+
def min_body_length
|
64
|
+
length = cop_config['MinBodyLength'] || 1
|
65
|
+
return length if length.is_a?(Integer) && length > 0
|
66
|
+
|
67
|
+
fail 'MinBodyLength needs to be a positive integer!'
|
68
|
+
end
|
69
|
+
|
59
70
|
def enumerator?(method_name)
|
60
71
|
ENUMERATORS.include?(method_name) || /\Aeach_/.match(method_name)
|
61
72
|
end
|
@@ -71,6 +82,7 @@ module RuboCop
|
|
71
82
|
return false if if_else?(node)
|
72
83
|
return false unless node.type == :if
|
73
84
|
return false if style == :skip_modifier_ifs && modifier_if?(node)
|
85
|
+
return false if !modifier_if?(node) && !min_body_length?(node)
|
74
86
|
|
75
87
|
# The `if` node must have only `if` body since we excluded `if` with
|
76
88
|
# `else` above.
|
@@ -6,14 +6,14 @@ module RuboCop
|
|
6
6
|
# TODO: Make configurable.
|
7
7
|
# Checks for uses of if/then/else/end on a single line.
|
8
8
|
class OneLineConditional < Cop
|
9
|
-
include
|
9
|
+
include OnNormalIfUnless
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
11
|
+
MSG = 'Favor the ternary operator (?:) ' \
|
12
|
+
'over if/then/else/end constructs.'
|
14
13
|
|
15
|
-
def
|
16
|
-
|
14
|
+
def on_normal_if_unless(node)
|
15
|
+
return if node.loc.expression.source.include?("\n")
|
16
|
+
add_offense(node, :expression, MSG)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -42,14 +42,18 @@ module RuboCop
|
|
42
42
|
type = type(node)
|
43
43
|
|
44
44
|
opening_delimiter, closing_delimiter = preferred_delimiters(type)
|
45
|
-
opening_newline = new_line(node.loc.begin, node.children.first)
|
46
|
-
closing_newline = new_line(node.loc.end, node.children.last)
|
47
45
|
|
46
|
+
first_child, *_middle, last_child = *node
|
47
|
+
opening_newline = new_line(node.loc.begin, first_child)
|
48
|
+
expression_indentation = leading_whitespace(first_child, :expression)
|
49
|
+
closing_newline = new_line(node.loc.end, last_child)
|
50
|
+
closing_indentation = leading_whitespace(node, :end)
|
48
51
|
expression, reg_opt = *contents(node)
|
52
|
+
|
49
53
|
corrected_source =
|
50
54
|
type + opening_delimiter + opening_newline +
|
51
|
-
expression +
|
52
|
-
|
55
|
+
expression_indentation + expression + closing_newline +
|
56
|
+
closing_indentation + closing_delimiter + reg_opt
|
53
57
|
|
54
58
|
@corrections << lambda do |corrector|
|
55
59
|
corrector.replace(
|
@@ -59,11 +63,10 @@ module RuboCop
|
|
59
63
|
end
|
60
64
|
end
|
61
65
|
|
62
|
-
def on_percent_literal(node
|
66
|
+
def on_percent_literal(node)
|
63
67
|
type = type(node)
|
64
|
-
return
|
65
|
-
|
66
|
-
!contains_preferred_delimiter?(node, type)
|
68
|
+
return if uses_preferred_delimiter?(node, type) ||
|
69
|
+
contains_preferred_delimiter?(node, type)
|
67
70
|
|
68
71
|
add_offense(node, :expression)
|
69
72
|
end
|
@@ -72,6 +75,17 @@ module RuboCop
|
|
72
75
|
cop_config['PreferredDelimiters'][type].split(//)
|
73
76
|
end
|
74
77
|
|
78
|
+
def leading_whitespace(object, part)
|
79
|
+
case object
|
80
|
+
when String
|
81
|
+
''
|
82
|
+
when Parser::AST::Node
|
83
|
+
/(\s*)/.match(object.loc.send(part).source_line)[1]
|
84
|
+
else
|
85
|
+
fail "Unsupported object #{object}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
75
89
|
def contents(node)
|
76
90
|
first_child, *middle, last_child = *node
|
77
91
|
last_child ||= first_child
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for usage of the %Q() syntax when %q() would do.
|
7
|
+
class PercentQLiterals < Cop
|
8
|
+
include PercentLiteral
|
9
|
+
include ConfigurableEnforcedStyle
|
10
|
+
|
11
|
+
def on_str(node)
|
12
|
+
process(node, '%Q', '%q')
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def on_percent_literal(node)
|
18
|
+
if style == :lower_case_q
|
19
|
+
if type(node) == '%Q'
|
20
|
+
check(node,
|
21
|
+
'Do not use `%Q` unless interpolation is needed. ' \
|
22
|
+
'Use `%q`.')
|
23
|
+
end
|
24
|
+
elsif type(node) == '%q'
|
25
|
+
check(node, 'Use `%Q` instead of `%q`.')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def check(node, msg)
|
30
|
+
src = node.loc.expression.source
|
31
|
+
# Report offense only if changing case doesn't change semantics,
|
32
|
+
# i.e., if the string would become dynamic or has special characters.
|
33
|
+
return if node.children !=
|
34
|
+
ProcessedSource.new(corrected(src)).ast.children
|
35
|
+
|
36
|
+
add_offense(node, :begin, msg)
|
37
|
+
end
|
38
|
+
|
39
|
+
def autocorrect(node)
|
40
|
+
src = node.loc.expression.source
|
41
|
+
|
42
|
+
@corrections << lambda do |corrector|
|
43
|
+
corrector.replace(node.loc.expression, corrected(src))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def corrected(src)
|
48
|
+
src.sub(src[1], src[1].swapcase)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -18,11 +18,11 @@ module RuboCop
|
|
18
18
|
# # good
|
19
19
|
# def value? ...
|
20
20
|
class PredicateName < Cop
|
21
|
-
include
|
21
|
+
include OnMethod
|
22
22
|
|
23
23
|
private
|
24
24
|
|
25
|
-
def
|
25
|
+
def on_method(node, method_name, _args, _body)
|
26
26
|
prefix_blacklist.each do |prefix|
|
27
27
|
next unless method_name.to_s.start_with?(prefix)
|
28
28
|
add_offense(node, :name, message(method_name.to_s, prefix))
|
@@ -18,11 +18,11 @@ module RuboCop
|
|
18
18
|
# end
|
19
19
|
# end
|
20
20
|
class RedundantBegin < Cop
|
21
|
-
include
|
21
|
+
include OnMethod
|
22
22
|
|
23
23
|
MSG = 'Redundant `begin` block detected.'
|
24
24
|
|
25
|
-
def
|
25
|
+
def on_method(_node, _method_name, _args, body)
|
26
26
|
return unless body && body.type == :kwbegin
|
27
27
|
|
28
28
|
add_offense(body, :begin)
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
# It should be extended to handle methods whose body is if/else
|
22
22
|
# or a case expression with a default branch.
|
23
23
|
class RedundantReturn < Cop
|
24
|
-
include
|
24
|
+
include OnMethod
|
25
25
|
|
26
26
|
MSG = 'Redundant `return` detected.'
|
27
27
|
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
42
|
+
def on_method(_node, _method_name, _args, body)
|
43
43
|
return unless body
|
44
44
|
|
45
45
|
if body.type == :return
|
@@ -5,7 +5,7 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for uses of rescue in its modifier form.
|
7
7
|
class RescueModifier < Cop
|
8
|
-
include
|
8
|
+
include OnMethod
|
9
9
|
|
10
10
|
MSG = 'Avoid using `rescue` in its modifier form.'
|
11
11
|
|
@@ -17,10 +17,14 @@ module RuboCop
|
|
17
17
|
|
18
18
|
def on_kwbegin(node)
|
19
19
|
body, *_ = *node
|
20
|
-
check(
|
20
|
+
check(body)
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
23
|
+
def on_method(_node, _method_name, _args, body)
|
24
|
+
check(body)
|
25
|
+
end
|
26
|
+
|
27
|
+
def check(body)
|
24
28
|
return unless body
|
25
29
|
|
26
30
|
case body.type
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
6
6
|
# This cop checks for single-line method definitions.
|
7
7
|
# It can optionally accept single-line methods with no body.
|
8
8
|
class SingleLineMethods < Cop
|
9
|
-
include
|
9
|
+
include OnMethod
|
10
10
|
|
11
11
|
MSG = 'Avoid single-line method definitions.'
|
12
12
|
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
16
16
|
|
17
17
|
private
|
18
18
|
|
19
|
-
def
|
19
|
+
def on_method(node, _method_name, _args, body)
|
20
20
|
start_line = node.loc.keyword.line
|
21
21
|
end_line = node.loc.end.line
|
22
22
|
|
@@ -13,12 +13,12 @@ module RuboCop
|
|
13
13
|
# # good
|
14
14
|
# def func(x) ... end
|
15
15
|
class SpaceAfterMethodName < Cop
|
16
|
-
include
|
16
|
+
include OnMethod
|
17
17
|
|
18
18
|
MSG = 'Never put a space between a method name and the opening ' \
|
19
19
|
'parenthesis.'
|
20
20
|
|
21
|
-
def
|
21
|
+
def on_method(_node, _method_name, args, _body)
|
22
22
|
return unless args.loc.begin && args.loc.begin.is?('(')
|
23
23
|
expr = args.loc.expression
|
24
24
|
pos_before_left_paren = Parser::Source::Range.new(expr.source_buffer,
|
@@ -56,12 +56,12 @@ module RuboCop
|
|
56
56
|
def braces_with_contents_inside(node, inner, sb)
|
57
57
|
_method, args, _body = *node
|
58
58
|
left_brace, right_brace = node.loc.begin, node.loc.end
|
59
|
-
|
59
|
+
args_delimiter = args.loc.begin # Can be ( | or nil.
|
60
60
|
|
61
61
|
if inner =~ /^\S/
|
62
|
-
no_space_inside_left_brace(left_brace,
|
62
|
+
no_space_inside_left_brace(left_brace, args_delimiter, sb)
|
63
63
|
else
|
64
|
-
space_inside_left_brace(left_brace,
|
64
|
+
space_inside_left_brace(left_brace, args_delimiter, sb)
|
65
65
|
end
|
66
66
|
|
67
67
|
if inner =~ /\S$/
|
@@ -72,11 +72,11 @@ module RuboCop
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
-
def no_space_inside_left_brace(left_brace,
|
76
|
-
if pipe
|
77
|
-
if left_brace.end_pos ==
|
75
|
+
def no_space_inside_left_brace(left_brace, args_delimiter, sb)
|
76
|
+
if pipe?(args_delimiter)
|
77
|
+
if left_brace.end_pos == args_delimiter.begin_pos &&
|
78
78
|
cop_config['SpaceBeforeBlockParameters']
|
79
|
-
offense(sb, left_brace.begin_pos,
|
79
|
+
offense(sb, left_brace.begin_pos, args_delimiter.end_pos,
|
80
80
|
'Space between { and | missing.')
|
81
81
|
end
|
82
82
|
else
|
@@ -88,10 +88,10 @@ module RuboCop
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
def space_inside_left_brace(left_brace,
|
92
|
-
if pipe
|
91
|
+
def space_inside_left_brace(left_brace, args_delimiter, sb)
|
92
|
+
if pipe?(args_delimiter)
|
93
93
|
unless cop_config['SpaceBeforeBlockParameters']
|
94
|
-
offense(sb, left_brace.end_pos,
|
94
|
+
offense(sb, left_brace.end_pos, args_delimiter.begin_pos,
|
95
95
|
'Space between { and | detected.')
|
96
96
|
end
|
97
97
|
else
|
@@ -101,6 +101,10 @@ module RuboCop
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
+
def pipe?(args_delimiter)
|
105
|
+
args_delimiter && args_delimiter.is?('|')
|
106
|
+
end
|
107
|
+
|
104
108
|
def space_inside_right_brace(right_brace, sb)
|
105
109
|
brace_with_space = range_with_surrounding_space(right_brace, :left)
|
106
110
|
space(sb, brace_with_space.begin_pos, brace_with_space.end_pos - 1,
|