Class: Rubocop::Cop::StringLiterals

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/string_literals.rb

Constant Summary collapse

ERROR_MESSAGE =
"Prefer single-quoted strings when you don't need " +
'string interpolation or special symbols.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/string_literals.rb', line 9

def inspect(file, source, tokens, sexp)
  state = :outside
  tokens.each do |t|
    state = case [state, t.type]
            when [:outside, :on_tstring_beg]
              :double_quote if t.text == '"'

            when [:double_quote, :on_tstring_content]
              :valid_double_quote if t.text =~ /'|\\[ntrx]/

            when [:double_quote, :on_embexpr_beg]
              :embedded_expression

            when [:double_quote, :on_embvar]
              :embedded_variable

            when [:double_quote, :on_tstring_end]
              add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
              :outside

            when [:embedded_expression, :on_rbrace]
              :valid_double_quote

            when [:embedded_variable, :on_ivar]
              :valid_double_quote

            when [:embedded_variable, :on_cvar]
              :valid_double_quote

            when [:embedded_variable, :on_gvar]
              :valid_double_quote

            when [:valid_double_quote, :on_tstring_end]
              :outside
      end || state
  end
end