Module: RuboCop::Cop::Parentheses

Included in:
Style::ParenthesesAroundCondition, Style::RedundantParentheses
Defined in:
lib/rubocop/cop/mixin/parentheses.rb

Overview

Common functionality for handling parentheses.

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/mixin/parentheses.rb', line 14

def autocorrect(node)
  lambda do |corrector|
    corrector.remove(node.loc.begin)
    corrector.remove(node.loc.end)

    if ternary_condition?(node) && next_char_is_question_mark?(node)
      corrector.insert_after(node.loc.end, ' ')
    end
  end
end

#next_char_is_question_mark?(node) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubocop/cop/mixin/parentheses.rb', line 29

def next_char_is_question_mark?(node)
  node.loc.last_column == node.parent.loc.question.column
end

#parens_required?(node) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
# File 'lib/rubocop/cop/mixin/parentheses.rb', line 7

def parens_required?(node)
  range  = node.source_range
  source = range.source_buffer.source
  source[range.begin_pos - 1] =~ /[a-z]/ ||
    source[range.end_pos] =~ /[a-z]/
end

#ternary_condition?(node) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rubocop/cop/mixin/parentheses.rb', line 25

def ternary_condition?(node)
  node.parent && node.parent.if_type? && node.parent.ternary?
end