Module: RuboCop::Cop::StatementModifier
Overview
Common functionality for modifier cops.
Instance Method Summary
collapse
Methods included from IfNode
#elsif?, #if_else?, #if_node_parts, #modifier_if?, #ternary?
Instance Method Details
#body_has_comment?(body) ⇒ Boolean
51
52
53
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 51
def body_has_comment?(body)
.include?(body.source_range.line)
end
|
#body_length(body) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 43
def body_length(body)
if body && body.source_range
body.source_range.size
else
0
end
end
|
59
60
61
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 59
def
@comment_lines ||= processed_source..map { |c| c.location.line }
end
|
55
56
57
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 55
def (node)
.include?(node.loc.end.line)
end
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 10
def fit_within_line_as_modifier_form?(node)
cond, body, _else = if_node_parts(node)
return false if length(node) > 3
return false if body && body.begin_type?
body_length = body_length(body)
return false if body_length.zero?
return false if cond.each_node.any?(&:lvasgn_type?)
return false if body_has_comment?(body)
return false if (node)
length_in_modifier_form(node, cond, body_length) <= max_line_length
end
|
#length(node) ⇒ Object
39
40
41
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 39
def length(node)
node.source.lines.grep(/\S/).size
end
|
26
27
28
29
30
31
32
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 26
def length_in_modifier_form(node, cond, body_length)
indentation = node.loc.keyword.column
kw_length = node.loc.keyword.size
cond_length = cond.source_range.size
space = 1
indentation + body_length + space + kw_length + space + cond_length
end
|
#max_line_length ⇒ Object
34
35
36
37
|
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 34
def max_line_length
cop_config['MaxLineLength'] ||
config.for_cop('Metrics/LineLength')['Max']
end
|