Class: Wool::UselessDoubleQuotesWarning

Inherits:
FileWarning show all
Defined in:
lib/wool/warnings/useless_double_quotes.rb

Overview

Warning for using semicolons outside of class declarations.

Instance Attribute Summary

Attributes inherited from Warning

#body, #file, #line_number, #name, #settings, #severity

Instance Method Summary collapse

Methods inherited from FileWarning

options

Methods inherited from Warning

all_types, all_warnings, concrete_warnings, #count_occurrences, #desc, #fixable?, #generated_warnings, #get_indent, #indent, inherited, #initialize, opt, options, setting_accessor, type

Methods included from Advice

#advice_counter, #after_advice, #argument_advice, #before_advice, #bump_advice_counter!, #with_advice

Methods included from ModuleExtensions

#attr_accessor_with_default, #cattr_accessor, #cattr_accessor_with_default, #cattr_get_and_setter, #cattr_reader, #cattr_writer

Methods included from SexpAnalysis

#find_sexps, #parse

Methods included from LexicalAnalysis

#find_keyword, #find_token, #lex, #select_token, #split_on_keyword, #split_on_token, #text_between_token_positions

Constructor Details

This class inherits a constructor from Wool::Warning

Instance Method Details

#fix(body = self.body) ⇒ Object



33
34
35
36
# File 'lib/wool/warnings/useless_double_quotes.rb', line 33

def fix(body = self.body)
  body.gsub("\"#{quoted_string}\"", "'#{quoted_string}'").
       gsub("%Q{#{quoted_string}}", "%q{#{quoted_string}}")
end

#match?(body = self.body) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wool/warnings/useless_double_quotes.rb', line 14

def match?(body = self.body)
  list = find_sexps(:string_content)
  list.map do |sym, *parts|
    next if parts.size != 1  # ignore multiparts as they're fine
    inner_sym, text, pos = parts.first
    # skip if the string has a backslash or an apostrophe in it.
    next unless inner_sym == :@tstring_content && text !~ /(\\)|(')/
    
    previous_char = body.lines.to_a[pos[0] - 1][pos[1]-1,1]
    uses_q_braces = (previous_char == '{' && body.lines.to_a[pos[0] - 1][pos[1]-3,2] == '%Q')
    if previous_char == '"' || uses_q_braces
      warning = Wool::UselessDoubleQuotesWarning.new(
          file, body, :quoted_string => text, :uses_q_braces => uses_q_braces)
      warning.line_number = pos[0]
      warning
    end
  end.compact
end