Module: Rake::DevEiate::Checks

Extended by:
Rake::DSL
Defined in:
lib/rake/deveiate/checks.rb

Overview

Sanity and quality check tasks

Constant Summary collapse

SADFACE =

Emoji for style advisories

"\u{1f622}"
TAB_ARROW =

Tab-arrow character

"\u{21e5}"
TRAILING_WHITESPACE_RE =

Regexp to match trailing whitespace

/\p{Blank}+$/m
MIXED_INDENT_RE =

Regexp to match lines with mixed indentation

/(?<!#)([ ]\t)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#quality_check_whitelistObject (readonly)

The Rake::FileList containing files which should not be quality-checked.



36
37
38
# File 'lib/rake/deveiate/checks.rb', line 36

def quality_check_whitelist
  @quality_check_whitelist
end

Instance Method Details

#define_quality_checker_tasksObject

Set up tasks that check for poor whitespace discipline



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rake/deveiate/checks.rb', line 58

def define_quality_checker_tasks

  namespace :quality_checks do

    task :all => [ :whitespace ]

    desc "Check source code for inconsistent whitespace"
    task :whitespace => [
      :for_trailing_whitespace,
      :for_mixed_indentation,
    ]

    desc "Check source code for trailing whitespace"
    task :for_trailing_whitespace do
      lines = find_matching_source_lines do |line, _|
        line =~ TRAILING_WHITESPACE_RE
      end

      unless lines.empty?
        desc = "Found some trailing whitespace"
        describe_lines_that_need_fixing( desc, lines, TRAILING_WHITESPACE_RE )
        fail
      end
    end

    desc "Check source code for mixed indentation"
    task :for_mixed_indentation do
      lines = find_matching_source_lines do |line, _|
        line =~ MIXED_INDENT_RE
      end

      unless lines.empty?
        desc = "Found mixed indentation"
        describe_lines_that_need_fixing( desc, lines, /[ ]\t/ )
        fail
      end
    end

  end

end

#define_sanity_checker_tasksObject

Set up some sanity-checks as dependencies of higher-level tasks



102
103
104
105
106
107
108
109
110
111
# File 'lib/rake/deveiate/checks.rb', line 102

def define_sanity_checker_tasks

  namespace :sanity_checks do

    desc "Check source code for common problems"
    task :all

  end

end

#define_tasksObject

Define check tasks



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rake/deveiate/checks.rb', line 40

def define_tasks
  super if defined?( super )

  self.define_quality_checker_tasks
  self.define_sanity_checker_tasks

  desc "Run several quality-checks on the code"
  task :quality_checks => [ 'quality_checks:all' ]

  desc "Run several sanity-checks on the code"
  task :sanity_checks => [ 'sanity_checks:all' ]

  task :check => [ :quality_checks, :sanity_checks ]

end

#describe_lines_that_need_fixing(description, lines, re) ⇒ Object

Output a listing of the specified lines with the given description, highlighting the characters matched by the specified re.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rake/deveiate/checks.rb', line 141

def describe_lines_that_need_fixing( description, lines, re )
  self.prompt.say "\n"
  self.prompt.say SADFACE + "  "
  self.prompt.error( "Uh-oh! " + description )

  grouped_lines = group_line_matches( lines )

  grouped_lines.each do |filename, linegroups|
    linegroups.each do |group, lines|
      if group.min == group.max
        self.prompt.say( "%s:%d" % [ filename, group.min ], color: :bold )
      else
        self.prompt.say( "%s:%d-%d" % [ filename, group.min, group.max ], color: :bold )
      end

      lines.each_with_index do |line, i|
        self.prompt.say "%s: %s" % [
          self.pastel.dark.white( group.to_a[i].to_s ),
          highlight_problems( line, re )
        ]
      end
      self.prompt.say "\n"
    end
  end
end

#find_matching_source_linesObject

Return tuples of the form:

[ <filename>, <line number>, <line> ]

for every line in the Gemspec’s source files for which the block returns true.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rake/deveiate/checks.rb', line 120

def find_matching_source_lines
  matches = []

  source_files = self.project_files.grep( /\.(h|c|rb)$/ )
  source_files -= self.quality_check_whitelist

  source_files.each do |filename|
    previous_line = nil

    IO.foreach( filename ).with_index do |line, i|
      matches << [filename, i + 1, line] if yield( line, previous_line )
      previous_line = line
    end
  end

  return matches
end

#group_line_matches(tuples) ⇒ Object

Return a Hash, keyed by filename, whose values are tuples of Ranges and lines extracted from the given [filename, linenumber, line] tuples.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rake/deveiate/checks.rb', line 170

def group_line_matches( tuples )
  by_file = tuples.group_by {|tuple| tuple.first }

  return by_file.each_with_object({}) do |(filename, lines), hash|
    last_linenum = 0
    linegroups = lines.slice_before do |filename, linenum|
      gap = linenum > last_linenum + 1
      last_linenum = linenum
      gap
    end

    hash[ filename ] = linegroups.map do |group|
      rng = group.first[1] .. group.last[1]
      grouplines = group.transpose.last
      [ rng, grouplines ]
    end
  end
end

#highlight_problems(line, re) ⇒ Object

Transform invisibles in the specified line into visible analogues.



191
192
193
194
195
# File 'lib/rake/deveiate/checks.rb', line 191

def highlight_problems( line, re )
  line.
    gsub( re )    { self.pastel.on_red( $& ) }.
    gsub( /\t+/ ) { self.pastel.dark.white( "#{TAB_ARROW}   " * $&.length ) }
end

#setup(_name, **options) ⇒ Object

Set up task defaults



27
28
29
30
31
# File 'lib/rake/deveiate/checks.rb', line 27

def setup( _name, **options )
  super if defined?( super )

  @quality_check_whitelist = Rake::FileList.new
end