Module: RuboCop::Cop::StatementModifier
Overview
Common functionality for modifier cops.
Instance Method Summary
collapse
Methods included from IfNode
#elsif?, #if_else?, #modifier_if?, #ternary_op?
Instance Method Details
#body_has_comment?(body) ⇒ Boolean
53
54
55
56
57
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 53
def body_has_comment?(body)
= processed_source..map(&:location).map(&:line)
body_line = body.loc.expression.line
.include?(body_line)
end
|
#body_length(body) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 45
def body_length(body)
if body && body.loc.expression
body.loc.expression.size
else
0
end
end
|
TODO: Extremely ugly solution that needs lots of polish.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 10
def fit_within_line_as_modifier_form?(node)
case node.loc.keyword.source
when 'if' then cond, body, _else = *node
when 'unless' then cond, _else, body = *node
else cond, body = *node
end
return false if length(node) > 3
body_length = body_length(body)
return false if body_length == 0
on_node(:lvasgn, cond) do
return false
end
indentation = node.loc.keyword.column
kw_length = node.loc.keyword.size
cond_length = cond.loc.expression.size
space = 1
total = indentation + body_length + space + kw_length + space +
cond_length
total <= max_line_length && !body_has_comment?(body)
end
|
#length(node) ⇒ Object
41
42
43
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 41
def length(node)
node.loc.expression.source.lines.to_a.size
end
|
#max_line_length ⇒ Object
36
37
38
39
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 36
def max_line_length
cop_config && cop_config['MaxLineLength'] ||
config.for_cop('Metrics/LineLength')['Max']
end
|