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
56 57 58 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 56 def body_length(body) body.source_range ? body.source_range.size : 0 end |
#comment_lines ⇒ Object
64 65 66 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 64 def comment_lines @comment_lines ||= processed_source.comments.map { |c| c.location.line } end |
#commented?(source) ⇒ Boolean
60 61 62 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 60 def commented?(source) comment_lines.include?(source.line) end |
#empty_body?(body) ⇒ Boolean
52 53 54 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 52 def empty_body?(body) !body || body_length(body).zero? end |
#length_in_modifier_form(node, cond, body_length) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 35 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
48 49 50 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 48 def line_count(node) node.source.lines.grep(/\S/).size end |
#max_line_length ⇒ Object
43 44 45 46 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 43 def max_line_length cop_config['MaxLineLength'] || config.for_cop('Metrics/LineLength')['Max'] end |
#modifier_fits_on_single_line?(node) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 28 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
20 21 22 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 20 def non_eligible_body?(body) empty_body?(body) || body.begin_type? || commented?(body.source_range) end |
#non_eligible_condition?(condition) ⇒ Boolean
24 25 26 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 24 def non_eligible_condition?(condition) condition.each_node.any?(&:lvasgn_type?) end |
#non_eligible_node?(node) ⇒ Boolean
15 16 17 18 |
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 15 def non_eligible_node?(node) line_count(node) > 3 || !node.modifier_form? && 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 |