Class: Fuzzers::WhitespaceChecker

Inherits:
Object
  • Object
show all
Includes:
Fuzz::Fzzr
Defined in:
lib/fuzz/fuzzers/check_whitespace.rb

Instance Attribute Summary

Attributes included from Fuzz::Fzzr

#description, #errormsg, #fuzz_id

Instance Method Summary collapse

Methods included from Fuzz::Fzzr

#is_excluded?, #options

Constructor Details

#initializeWhitespaceChecker

Returns a new instance of WhitespaceChecker.



13
14
15
16
# 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

Instance Method Details

#applies_to?(object) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/fuzz/fuzzers/check_whitespace.rb', line 24

def applies_to?(object)
  Fuzz::FileObject === object && !is_excluded?(object)
end

#run(object, apply_fix) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



18
19
20
21
22
# 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