Permalink
Browse files

basic working enforcement, need to document and add raketask maybe?

  • Loading branch information...
bglusman
bglusman committed Jan 23, 2012
1 parent 343ec12 commit a94fea3af49b15ef76ee6067a32ef017807b294a
Showing with 56 additions and 13 deletions.
  1. +4 −0 Rakefile
  2. +6 −2 lib/outlaw.rb
  3. +30 −2 lib/outlaw/enforcement.rb
  4. +3 −3 lib/outlaw/law_dsl.rb
  5. +7 −2 lib/outlaw/rule.rb
  6. +6 −4 outlawed.rb
View
@@ -0,0 +1,4 @@
task :default => 'test'
task :test do
sh "ruby test/outlaw_test.rb"
end
View
@@ -4,8 +4,12 @@
require_relative 'outlaw/rule'
module Outlaw
def self.outlaw(restriction, message)
law = LawDSL.parse restriction
Enforcement.add(law, message)
law = LawDSL.parse(restriction, message)
Enforcement.add(law)
end
def self.enforce(dir=".")
Enforcement.process_directory(dir)
end
PARAM_TYPES = [:on_const, :on_ident, :on_ivar, :on_cvar]
IGNORE_TYPES = [:on_sp, :on_nl, :on_ignored_nl, :on_rparen, :on_lparen]
View
@@ -1,7 +1,35 @@
module Outlaw
class Enforcement
def self.add(law, message)
#TODO - enforce laws :-)
class << self
attr_reader :laws
def add(law)
@laws ||= []
@laws << law
end
def process_directory(path)
Dir.foreach(path) do |entry|
next if entry == '.' or entry == '..'
if File.directory?(entry)
# Dir.open(path + entry)
process_directory("#{path}/#{entry}")
else
handle("#{path}/#{entry}")
end
end
end
def handle(file)
if file.include?(".rb")
text = File.open(file) {|f| f.read}
laws.each do |law|
if law.call(text)
puts "Outlaw Violation in file: #{file}\nRestriction:" +
"#{law.restriction}\n\n#{law.message}"
end
end
end
end
end
end
end
View
@@ -1,14 +1,14 @@
module Outlaw
module LawDSL
class << self
def parse(restriction)
def parse(restriction, message="")
tokens = restriction.split
parsed_restriction = []
tokens.each do |token|
case
when special_case?(token)
next #TODO
when multipart?(token) #this jewel is for Const.new and similar stuff
when multipart?(token) #this jewel is for Const.new and similar stuff
parsed_restriction += Ripper.lex(token).reduce([]){|a, t| a << /#{t[2]}/}
when defined_collection?(token)
parsed_restriction << Outlaw.const_get(string_to_sym(token.upcase))
@@ -18,7 +18,7 @@ def parse(restriction)
parsed_restriction += build_regex(token)
end
end
Rule.new(&build_block(parsed_restriction))
Rule.new(message, restriction, &build_block(parsed_restriction))
end
private
View
@@ -1,9 +1,14 @@
module Outlaw
class Rule
NoDetectionBlockProvided = Class.new(StandardError)
def initialize(type=:ruby, scope=:file, &detection_block)
attr_reader :message, :restriction
def initialize(message, restriction, type=:ruby, scope=:file, &detection_block)
raise NoDetectionBlockProvided unless detection_block
@type, @scope, @detection_block = type, scope, detection_block
@message = message
@restriction = restriction
@type = type
@scope = scope
@detection_block = detection_block
end
def call(code)
View
@@ -5,10 +5,12 @@ module Outlaw
outlaw "module :token end", "nest modules to avoid empty module declarations"
outlaw "eval", "never eval, rarely class_eval or instance_eval, but never eval"
#ideas - http://rubyrogues.com/032-rr-ruby-antipatterns/
outlaw ":startline unless :tokens-not-end else",
"this just reads wrong"
outlaw "? true : false", "unnecessary, just use the test itself"
outlaw "!!", "bad style?"
outlaw "class :symbol < :core_class",
"core classes implemented in c, can cause bad mojo"
# outlaw ":startline unless :tokens-not-end else",
# "this just reads wrong"
# outlaw "? true : false", "unnecessary, just use the test itself"
# outlaw "!!", "bad style?"
end

0 comments on commit a94fea3

Please sign in to comment.