class Fuzzers::WhitespaceChecker

Public Class Methods

new() click to toggle source
# File lib/fuzz/fuzzers/check_whitespace.rb, line 13
def initialize
  @fuzz_id = :check_whitespace
  @description = 'checks for trailing whitespace, incorrect line endings and tabs'
end

Public Instance Methods

applies_to?(object) click to toggle source
# File lib/fuzz/fuzzers/check_whitespace.rb, line 24
def applies_to?(object)
  Fuzz::FileObject === object && !is_excluded?(object)
end
run(object, apply_fix) click to toggle source
# File lib/fuzz/fuzzers/check_whitespace.rb, line 28
def run(object, apply_fix)
  _tws = []
  _tabs = []
  object.iterate(fuzz_id) do |lnptr|
    if lnptr.text =~ /(\s\n|[\ \t\f\r\x0B])\Z/
      if apply_fix
        Fuzz.log_verbose(%Q{#{object.path}:#{lnptr.line_nr} - stripping trailing whitespace})
        lnptr.text.rstrip!
        lnptr.text << "\n" if $1.end_with?("\n")
      else
        _tws << lnptr.line_nr
      end
    end
    if lnptr.text =~ /\t/
      if apply_fix
        Fuzz.log_warning(%Q{#{object.path}:#{lnptr.line_nr} - replacing tabs})
        lnptr.text.gsub!(/\t/, ' ' * tab_spacing)
      else
        _tabs << lnptr.line_nr
      end
    end
  end
  Fuzz.log_error(%Q{#{object.path}:[#{_tws.join(',')}] trailing whitespace or incorrect line ending detected}) unless _tws.empty?
  Fuzz.log_error(%Q{#{object.path}:[#{_tabs.join(',')}] tab(s) detected}) unless _tabs.empty?
  return (_tws.empty? && _tabs.empty?)
end
setup(optparser) click to toggle source
# File lib/fuzz/fuzzers/check_whitespace.rb, line 18
def setup(optparser)
  optparser.on('--wsc:tab-spacing=NUM', Integer,
               'Fuzzers::WhitespaceChecker - defines tab spacing to use for TAB replacement when --apply-fix is enabled.',
               "Default: #{tab_spacing}") {|v| self.options[:tabspacing] = v }
end

Private Instance Methods

tab_spacing() click to toggle source
# File lib/fuzz/fuzzers/check_whitespace.rb, line 56
def tab_spacing
  self.options[:tabspacing] || 2
end