class Tailor::Rulers::AllowConditionalParenthesesRuler
Public Class Methods
new(style, options)
click to toggle source
Calls superclass method
Tailor::Ruler::new
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 6 def initialize(style, options) super(style, options) add_lexer_observers :nl end
Public Instance Methods
measure(line, lineno)
click to toggle source
Checks to see if a conditional is unnecessarily wrapped in parentheses.
@param [Fixnum] line The current lexed line. @param [Fixnum] lineno Line the problem was found on.
# 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, _)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 11 def nl_update(current_lexed_line, lineno, _) measure(current_lexed_line, lineno) end
Private Instance Methods
conditional?(token)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 32 def conditional?(token) token[1] == :on_kw and %w{case if unless while}.include?(token[2]) end
lparen?(token)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 36 def lparen?(token) token[1] == :on_lparen end
lparen_column(tokens)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 40 def lparen_column(tokens) tokens.find { |t| lparen?(t) }[0][1] + 1 end
tokens_after_rparen?(tokens)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 52 def tokens_after_rparen?(tokens) without_spaces( tokens.reverse.tap do |nl| nl.shift end.take_while { |t| t[1] != :on_rparen } ).any? end
tokens_before_lparen?(tokens)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 44 def tokens_before_lparen?(tokens) without_spaces( tokens.select do |t| true if (conditional?(t))..(lparen?(t)) end.tap { |t| t.shift; t.pop } ).empty? end
without_spaces(tokens)
click to toggle source
# File lib/tailor/rulers/allow_conditional_parentheses.rb, line 60 def without_spaces(tokens) tokens.reject { |t| t[1] == :on_sp } end