Class: Tailor::Rulers::AllowUnnecessaryInterpolationRuler

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

Constant Summary collapse

EVENTS =
[
  :on_embexpr_beg,
  :on_embexpr_end,
  :on_rbrace,
  :on_tstring_beg,
  :on_tstring_content,
  :on_tstring_end
]

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(config, options) ⇒ AllowUnnecessaryInterpolationRuler

Returns a new instance of AllowUnnecessaryInterpolationRuler.



16
17
18
19
20
# 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

Instance Method Details

#ignored_nl_update(lexed_line, _, _) ⇒ Object



22
23
24
# 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) ⇒ Object

Checks if variables are interpolated unnecessarily.

Parameters:

  • tokens (Array)

    The filtered tokens.



38
39
40
41
42
43
44
45
# 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, _, _) ⇒ Object



26
27
28
29
30
31
32
33
# 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