Class: Tailor::Rulers::AllowConditionalParenthesesRuler

Inherits:
Tailor::Ruler
  • Object
show all
Defined in:
lib/tailor/rulers/allow_conditional_parentheses.rb

Instance Attribute Summary

Attributes inherited from Tailor::Ruler

#lexer_observers

Instance Method Summary collapse

Methods inherited from Tailor::Ruler

#add_child_ruler, #problem_type, #problems

Methods included from Logger::Mixin

included

Constructor Details

#initialize(style, options) ⇒ AllowConditionalParenthesesRuler

Returns a new instance of AllowConditionalParenthesesRuler.



6
7
8
9
# File 'lib/tailor/rulers/allow_conditional_parentheses.rb', line 6

def initialize(style, options)
  super(style, options)
  add_lexer_observers :nl
end

Instance Method Details

#measure(line, lineno) ⇒ Object

Checks to see if a conditional is unnecessarily wrapped in parentheses.

Parameters:

  • line (Fixnum)

    The current lexed line.

  • lineno (Fixnum)

    Line the problem was found on.



19
20
21
22
23
24
25
26
27
28
# File 'lib/tailor/rulers/allow_conditional_parentheses.rb', line 19

def measure(line, lineno)
  return if @config
  return unless line.any? { |t| conditional?(t) }
  if tokens_before_lparen?(line) and ! tokens_after_rparen?(line)
    column = lparen_column(line)
    @problems << Problem.new('conditional_parentheses', lineno, column,
      "Parentheses around conditional expression at column #{column}.",
      @options[:level])
  end
end

#nl_update(current_lexed_line, lineno, _) ⇒ Object



11
12
13
# File 'lib/tailor/rulers/allow_conditional_parentheses.rb', line 11

def nl_update(current_lexed_line, lineno, _)
  measure(current_lexed_line, lineno)
end