Skip to content

Instantly share code, notes, and snippets.

@dncrht
Last active August 29, 2015 13:56
Show Gist options
  • Save dncrht/8836171 to your computer and use it in GitHub Desktop.
Save dncrht/8836171 to your computer and use it in GitHub Desktop.
A simple CSS linter
# inspired by twitter's RECESS
# http://twitter.github.io/recess/
# usage: git diff | ruby ~/linter.rb
class Linter
def process
@result = {}
STDIN.each_line do |line|
@line = line[2..-1]
filename = line.scan(/^\+\+\+ b(.+)$/)
unless filename.empty? # it's a filename!
@filename = filename.first.first
if @filename =~ /(css|sass|scss|less)$/
@result[@filename] = []
else # ignore non-CSS files
@filename = nil
end
next
end
next unless @filename
if line =~ /^\+ / # it's a change
known_lints.each { |lint| send(lint) }
end
end
@result.each do |filename, errors|
unless errors.empty?
puts "--- ERRORS IN #{filename}:"
errors.each do |error|
puts error
end
end
end
end
def known_lints
methods.grep /detect_/
end
def detect_no_ids
if @line =~ /#\w+[^\w]/
@result[@filename] << "Don’t style IDs: #@line"
end
end
def detect_no_js_prefix
if @line =~ /\.js-/
@result[@filename] << "Don’t style .js- prefixed classnames: #@line"
end
end
def detect_no_overqualifying
if @line =~ /(div|label|a|input|span)(#|\.)\w+[^\w]/
@result[@filename] << "Don’t overqualify your selectors: #@line"
end
end
def detect_no_underscores
if @line =~ /_/
@result[@filename] << "Don’t use underscores when naming classes: #@line"
end
end
def detect_no_camelcase
if @line =~ /[A-Z]{1}/
@result[@filename] << "Don’t use camelCase: #@line"
end
end
def detect_zero_units
if @line =~ / (0px|0em|0rem|0%)/
@result[@filename] << "Don’t add the unit if zero: #@line"
end
end
end
Linter.new.process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment