Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

proposed refactor for special cases

  • Loading branch information...
commit 660dc6b17989ff1ec8a7c3692b3a28e20f8ec331 1 parent 72ad098
bglusman authored
Showing with 38 additions and 11 deletions.
  1. +7 −3 lib/outlaw.rb
  2. +11 −1 lib/outlaw/law_parser.rb
  3. +20 −7 lib/outlaw/rule.rb
View
10 lib/outlaw.rb
@@ -6,16 +6,20 @@
module Outlaw
- def self.outlaw(pattern, message)
+ extend self
+ attr_accessor :ignore_types
+
+ def outlaw(pattern, message)
rule = Rule.new(pattern, message)
Outlaw::Enforcement.add(rule)
end
- def self.enforce(dir=".")
+ def enforce(dir=".")
Outlaw::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]
+ self.ignore_types = [:on_sp, :on_nl, :on_ignored_nl, :on_rparen, :on_lparen]
+ SPECIAL_CASES = [:whitespace_sensitive]
#these come from ripper's Lexer
CORE_CLASSES_FILE = File.expand_path("../../data/core_classes.txt", __FILE__)
CORE_CLASS = File.readlines(CORE_CLASSES_FILE).map &:chomp
View
12 lib/outlaw/law_parser.rb
@@ -1,11 +1,13 @@
module Outlaw
module LawParser
extend self
- def parse(restriction)
+ def parse(restriction, rule)
tokens = restriction.split
parsed_restriction = []
tokens.each do |token|
case
+ when special_case?(token)
+ handle_special(token, rule)
when multipart?(token) #this handles multi-token literals, Const.new etc
parsed_restriction += Ripper.lex(token)
.reduce([]){|array, tkn|
@@ -23,6 +25,14 @@ def parse(restriction)
private
+ def handle_special(token, rule)
+ rule.modifications = token
+ end
+
+ def special_case?(token)
+ SPECIAL_CASES.include?(token)
+ end
+
def token_type_regex(token)
/#{token[2]}/
end
View
27 lib/outlaw/rule.rb
@@ -1,6 +1,7 @@
module Outlaw
class Rule
attr_reader :message, :pattern, :detection_block
+ attr_accessor :modifications
def initialize(pattern, message=nil, &detection_block)
@pattern = pattern
@message = message ? message : "Don't do this: #{pattern}"
@@ -8,14 +9,26 @@ def initialize(pattern, message=nil, &detection_block)
end
def violation?(code)
- if detection_block.nil?
- @detection_block = LawParser.parse(pattern)
- detection_block.call(code)
- else
- detection_block.call(code)
- end
+ @detection_block = LawParser.parse(pattern, self) if detection_block.nil?
+ detect_violation(code)
end
+ private
+
+ def detect_violation(code)
+ defaults = apply_modifications
+ result = detection_block.call(code)
+ apply_modifications(defaults)
+ result
+ end
+
+ def apply_modifications(restore=nil)
+ return nil if restore.nil?
+ end
+
+
+
+
class << self
def test(program, start_index, pattern)
pattern_index = 0
@@ -24,7 +37,7 @@ def test(program, start_index, pattern)
code = program[index]
part = pattern[pattern_index]
- next if IGNORE_TYPES.include? token_type(code)
+ next if Outlaw.ignore_types.include? token_type(code)
return false unless match_token?(code, part, params[part])
pattern_index +=1
return true if pattern_index >= pattern.length
Please sign in to comment.
Something went wrong with that request. Please try again.