class Tailor::Rulers::AllowUnnecessaryInterpolationRuler

Constants

EVENTS

Public Class Methods

new(config, options) click to toggle source
Calls superclass method Tailor::Ruler::new
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 16
def initialize(config, options)
  super(config, options)
  reset_tokens
  add_lexer_observers :ignored_nl, :nl
end

Public Instance Methods

ignored_nl_update(lexed_line, _, _) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 22
def ignored_nl_update(lexed_line, _, _)
  add_string_tokens(lexed_line)
end
measure(lineno, tokens) click to toggle source

Checks if variables are interpolated unnecessarily.

@param [Array] tokens The filtered tokens.

# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 38
def measure(lineno, tokens)
  return if @config
  if no_content?(tokens) and one_expression?(tokens)
    @problems << Problem.new('unnecessary_string_interpolation', lineno,
      column(tokens.first), 'Variable interpolated unnecessarily',
      @options[:level])
  end
end
nl_update(lexed_line, _, _) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 26
def nl_update(lexed_line, _, _)
  add_string_tokens(lexed_line)
  each_string(@tokens).each do |string|
    measure(line_number(@tokens.first), string)
  end

  reset_tokens
end

Private Instance Methods

add_string_tokens(lexed_line) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 49
def add_string_tokens(lexed_line)
  @tokens += string_tokens(lexed_line)
end
column(token) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 53
def column(token)
  token.first.last + 1
end
each_string(tokens) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 57
def each_string(tokens)
  tokens.select do |t|
    true if (t[1] == :on_tstring_beg)..(t[1] == :on_tstring_end)
  end.slice_before { |t| t[1] == :on_tstring_beg }
end
line_number(token) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 63
def line_number(token)
  token.first.first
end
no_content?(tokens) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 67
def no_content?(tokens)
  ! tokens.map { |t| t[1] }.include?(:on_tstring_content)
end
one_expression?(tokens) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 71
def one_expression?(tokens)
  tokens.select { |t| t[1] == :on_embexpr_beg }.size == 1 and
    tokens.select do |t|
      t[1] == :on_embexpr_end or t[1] == :on_rbrace
    end.any?
end
reset_tokens() click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 78
def reset_tokens
  @tokens = []
end
string_tokens(lexed_line) click to toggle source
# File lib/tailor/rulers/allow_unnecessary_interpolation_ruler.rb, line 82
def string_tokens(lexed_line)
  lexed_line.select { |t| EVENTS.include?(t[1]) }
end