Module: RuboCop::Cop::StatementModifier
- Included in:
- RuboCop::Cop::Style::IfUnlessModifier, RuboCop::Cop::Style::IfUnlessModifierOfIfUnless, RuboCop::Cop::Style::MultilineIfModifier, RuboCop::Cop::Style::WhileUntilModifier
- Defined in:
- lib/rubocop/cop/mixin/statement_modifier.rb
Overview
Common functionality for modifier cops.
Instance Method Summary collapse
- #body_length(body) ⇒ Object
- #comment_lines ⇒ Object
- #commented?(source) ⇒ Boolean
- #empty_body?(body) ⇒ Boolean
- #length_in_modifier_form(node, cond, body_length) ⇒ Object
- #line_count(node) ⇒ Object
- #max_line_length ⇒ Object
- #modifier_fits_on_single_line?(node) ⇒ Boolean
- #non_eligible_body?(body) ⇒ Boolean
- #non_eligible_condition?(condition) ⇒ Boolean
- #non_eligible_node?(node) ⇒ Boolean
- #single_line_as_modifier?(node) ⇒ Boolean
Instance Method Details
#body_length(body) ⇒ Object
55 56 57 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 55 def body_length(body) body.source_range ? body.source_range.size : 0 end |
#comment_lines ⇒ Object
63 64 65 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 63 def comment_lines @comment_lines ||= processed_source.comments.map { |c| c.location.line } end |
#commented?(source) ⇒ Boolean
59 60 61 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 59 def commented?(source) comment_lines.include?(source.line) end |
#empty_body?(body) ⇒ Boolean
51 52 53 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 51 def empty_body?(body) !body || body_length(body).zero? end |
#length_in_modifier_form(node, cond, body_length) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 34 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 |
#line_count(node) ⇒ Object
47 48 49 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 47 def line_count(node) node.source.lines.grep(/\S/).size end |
#max_line_length ⇒ Object
42 43 44 45 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 42 def max_line_length cop_config['MaxLineLength'] || config.for_cop('Metrics/LineLength')['Max'] end |
#modifier_fits_on_single_line?(node) ⇒ Boolean
27 28 29 30 31 32 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 27 def modifier_fits_on_single_line?(node) modifier_length = length_in_modifier_form(node, node.condition, body_length(node.body)) modifier_length <= max_line_length end |
#non_eligible_body?(body) ⇒ Boolean
19 20 21 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 19 def non_eligible_body?(body) empty_body?(body) || body.begin_type? || commented?(body.source_range) end |
#non_eligible_condition?(condition) ⇒ Boolean
23 24 25 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 23 def non_eligible_condition?(condition) condition.each_node.any?(&:lvasgn_type?) end |
#non_eligible_node?(node) ⇒ Boolean
15 16 17 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 15 def non_eligible_node?(node) line_count(node) > 3 || commented?(node.loc.end) end |
#single_line_as_modifier?(node) ⇒ Boolean
7 8 9 10 11 12 13 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 7 def single_line_as_modifier?(node) return false if non_eligible_node?(node) || non_eligible_body?(node.body) || non_eligible_condition?(node.condition) modifier_fits_on_single_line?(node) end |