Class: Pedant::CheckContainsNoTabs
- Defined in:
- lib/pedant/checks/contains_no_tabs.rb
Instance Attribute Summary
Attributes inherited from Check
Class Method Summary collapse
-
.chunk_while(enumerable) ⇒ Object
Enumerable#chunk_while in Ruby 2.3 would remove the need for this.
- .requires ⇒ Object
Instance Method Summary collapse
Methods inherited from Check
all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, provides, ready?, #report, run_checks_in_dependency_order, #skip, #warn
Constructor Details
This class inherits a constructor from Pedant::Check
Class Method Details
.chunk_while(enumerable) ⇒ Object
Enumerable#chunk_while in Ruby 2.3 would remove the need for this
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/pedant/checks/contains_no_tabs.rb', line 31 def self.requires super + [:codes] end |
Instance Method Details
#check(file, code) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
61 62 63 64 65 66 67 |
# 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 |