class Pedant::CheckContainsNoTabs

Public Class Methods

chunk_while(enumerable) { |last.last, elem| ... } click to toggle source

Enumerable#chunk_while in Ruby 2.3 would remove the need for this

# File lib/pedant/checks/contains_no_tabs.rb, line 70
def self.chunk_while enumerable
  # If we're passed an array or something...
  enumerable = enumerable.to_enum unless enumerable.respond_to? :next

  chunks = [[enumerable.next]] rescue [[]]
  loop do
    elem = enumerable.next
    if yield chunks.last.last, elem
      chunks[-1] << elem
    else
      chunks << [elem]
    end
  end
  return chunks
end
requires() click to toggle source
Calls superclass method Pedant::Check::requires
# File lib/pedant/checks/contains_no_tabs.rb, line 31
def self.requires
  super + [:codes]
end

Public Instance Methods

check(file, code) click to toggle source
# File lib/pedant/checks/contains_no_tabs.rb, line 35
def check(file, code)
  tab_lines = Hash.new
  code.split("\n").each_with_index do |line, linenum|
    tab_lines[linenum + 1] = line if line =~ /\t/
  end

  return if tab_lines.length == 0

  # Make the consecutive sequences friendlier to read
  ranges = self.class.chunk_while(tab_lines.keys.sort) { |i, j| i + 1 == j }.map do |group|
    if group.length == 1
      group.first.to_s
    else
      "#{group.first.to_s}-#{group.last.to_s}"
    end
  end

  report(:warn, "Tabs were found in #{file}, on these lines: #{ranges.join(', ')}")
  report(:warn, "Showing up to five lines:")
  tab_lines.keys.sort.first(5).each do |linenum|
    report(:warn, "#{linenum}: #{tab_lines[linenum].gsub(/\t/, Rainbow("    ").background(:red))}")
  end

  warn
end
run() click to toggle source
# File lib/pedant/checks/contains_no_tabs.rb, line 61
def run
  # This check will pass by default.
  pass

  # Run this check on the code in every file.
  @kb[:codes].each { |file, code| check(file, code) }
end