Class: PreCommit::Checks::NbSpace

Inherits:
Plugin
  • Object
show all
Defined in:
lib/plugins/pre_commit/checks/nb_space.rb

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize, #name

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.descriptionObject



25
26
27
# File 'lib/plugins/pre_commit/checks/nb_space.rb', line 25

def self.description
  "Detected non-breaking spaces 194, 160."
end

Instance Method Details

#call(staged_files) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plugins/pre_commit/checks/nb_space.rb', line 8

def call(staged_files)
  nb_space = " "
  raise "you messed that up" unless nb_space.bytes.to_a == [194, 160]

  staged_files.reject! { |f| f =~ /^vendor\// || !File.read(f, encoding: 'utf-8').include?(nb_space) }

  bad = staged_files.map do |file|
    content = File.read(file).lines.to_a
    line_no = content.index { |l| l.include?(nb_space) }
    char_no = content[line_no].index(nb_space)
    [file, line_no, char_no]
  end

  return if bad.empty?
  "Detected non-breaking space in #{bad.map { |f,l,c| "#{f}:#{l+1} character:#{c+1}" }.join(" and")}, remove it!"
end