Skip to content

Commit

Permalink
+ Extracted MethodBasedSexpProcessor and pushed up to sexp_processor …
Browse files Browse the repository at this point in the history
…gem.

[git-p4: depot-paths = "//src/flog/dev/": change = 8988]
  • Loading branch information
zenspider committed Oct 18, 2013
1 parent 3d74c49 commit 30bad3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 185 deletions.
124 changes: 10 additions & 114 deletions lib/flog.rb
Expand Up @@ -10,7 +10,7 @@ class << self
end end
end end


class Flog < SexpProcessor class Flog < MethodBasedSexpProcessor
VERSION = "4.1.2" # :nodoc: VERSION = "4.1.2" # :nodoc:


## ##
Expand Down Expand Up @@ -93,13 +93,10 @@ class Flog < SexpProcessor


SCORES.merge!(:inject => 2) SCORES.merge!(:inject => 2)


@@no_class = :main
@@no_method = :none

# :stopdoc: # :stopdoc:
attr_accessor :multiplier attr_accessor :multiplier
attr_reader :calls, :option, :class_stack, :method_stack, :mass, :sclass attr_reader :calls, :option, :mass
attr_reader :method_locations, :method_scores, :scores attr_reader :method_scores, :scores
attr_reader :total_score, :totals attr_reader :total_score, :totals


# :startdoc: # :startdoc:
Expand Down Expand Up @@ -227,72 +224,19 @@ def flog_ruby! ruby, file="-", timeout = 10
process ast process ast
end end


##
# Adds name to the class stack, for the duration of the block

def in_klass name
if Sexp === name then
name = case name.first
when :colon2 then
name = name.flatten
name.delete :const
name.delete :colon2
name.join("::")
when :colon3 then
name.last.to_s
else
raise "unknown type #{name.inspect}"
end
end

@class_stack.unshift name
yield
@class_stack.shift
end

##
# Adds name to the method stack, for the duration of the block

def in_method(name, file, line)
method_name = Regexp === name ? name.inspect : name.to_s
@method_stack.unshift method_name
@method_locations[signature] = "#{file}:#{line}"
yield
@method_stack.shift
end

## ##
# Creates a new Flog instance with +options+. # Creates a new Flog instance with +options+.


def initialize option = {} def initialize option = {}
super() super()
@option = option @option = option
@sclass = []
@class_stack = []
@method_stack = []
@method_locations = {} @method_locations = {}
@mass = {} @mass = {}
@parser = nil @parser = nil
self.auto_shift_type = true self.auto_shift_type = true
self.reset self.reset
end end


##
# Returns the first class in the list, or @@no_class if there are
# none.

def klass_name
name = @class_stack.first

if Sexp === name then
raise "you shouldn't see me"
elsif @class_stack.any?
@class_stack.reverse.join("::").sub(/\([^\)]+\)$/, '')
else
@@no_class
end
end

## ##
# Returns the method/score pair of the maximum score. # Returns the method/score pair of the maximum score.


Expand All @@ -307,16 +251,6 @@ def max_score
max_method.last max_method.last
end end


##
# Returns the first method in the list, or "#none" if there are
# none.

def method_name
m = @method_stack.first || @@no_method
m = "##{m}" unless m =~ /::/
m
end

## ##
# For the duration of the block the complexity factor is increased # For the duration of the block the complexity factor is increased
# by #bonus This allows the complexity of sub-expressions to be # by #bonus This allows the complexity of sub-expressions to be
Expand All @@ -329,13 +263,6 @@ def penalize_by bonus
@multiplier -= bonus @multiplier -= bonus
end end


##
# Process each element of #exp in turn.

def process_until_empty exp
process exp.shift until exp.empty?
end

## ##
# Reset score data # Reset score data


Expand All @@ -345,7 +272,7 @@ def reset
@calls = Hash.new { |h,k| h[k] = Hash.new 0 } @calls = Hash.new { |h,k| h[k] = Hash.new 0 }
@method_scores = Hash.new { |h,k| h[k] = [] } @method_scores = Hash.new { |h,k| h[k] = [] }
@scores = Hash.new 0 @scores = Hash.new 0
@method_locations = {} method_locations.clear
end end


## ##
Expand All @@ -363,13 +290,6 @@ def score_method(tally)
Math.sqrt(a*a + b*b + c*c) Math.sqrt(a*a + b*b + c*c)
end end


##
# Returns the method signature for the current method.

def signature
"#{klass_name}#{method_name}"
end

## ##
# Final threshold that is used for report # Final threshold that is used for report


Expand Down Expand Up @@ -487,13 +407,12 @@ def process_case(exp)
end end


def process_class(exp) def process_class(exp)
in_klass exp.shift do super do
penalize_by 1.0 do penalize_by 1.0 do
process exp.shift # superclass expression process exp.shift # superclass expression
end end
process_until_empty exp process_until_empty exp
end end
s()
end end


def process_dasgn_curr(exp) # FIX: remove def process_dasgn_curr(exp) # FIX: remove
Expand All @@ -505,22 +424,6 @@ def process_dasgn_curr(exp) # FIX: remove
alias :process_iasgn :process_dasgn_curr alias :process_iasgn :process_dasgn_curr
alias :process_lasgn :process_dasgn_curr alias :process_lasgn :process_dasgn_curr


def process_defn(exp)
name = @sclass.empty? ? exp.shift : "::#{exp.shift}"
in_method name, exp.file, exp.line do
process_until_empty exp
end
s()
end

def process_defs(exp)
process exp.shift # recv
in_method "::#{exp.shift}", exp.file, exp.line do
process_until_empty exp
end
s()
end

# TODO: it's not clear to me whether this can be generated at all. # TODO: it's not clear to me whether this can be generated at all.
def process_else(exp) def process_else(exp)
add_to_score :branch add_to_score :branch
Expand Down Expand Up @@ -605,20 +508,13 @@ def process_masgn(exp)
s() s()
end end


def process_module(exp)
in_klass exp.shift do
process_until_empty exp
end
s()
end

def process_sclass(exp) def process_sclass(exp)
@sclass.push(true) super do
penalize_by 0.5 do penalize_by 0.5 do
process exp.shift # recv process exp.shift # recv
process_until_empty exp process_until_empty exp
end
end end
@sclass.pop


add_to_score :sclass add_to_score :sclass
s() s()
Expand Down
71 changes: 0 additions & 71 deletions test/test_flog.rb
Expand Up @@ -80,64 +80,6 @@ def test_flog_erb
$stdin = old_stdin $stdin = old_stdin
end end


def test_in_klass
assert_empty @flog.class_stack

@flog.in_klass "xxx::yyy" do
assert_equal ["xxx::yyy"], @flog.class_stack
end

assert_empty @flog.class_stack
end

def test_in_method
assert_empty @flog.method_stack

@flog.in_method "xxx", "file.rb", 42 do
assert_equal ["xxx"], @flog.method_stack
end

assert_empty @flog.method_stack

expected = {"main#xxx" => "file.rb:42"}
assert_equal expected, @flog.method_locations
end

def test_klass_name
assert_equal :main, @flog.klass_name

@flog.class_stack << "whatevs" << "flog"
assert_equal "flog::whatevs", @flog.klass_name
end

def test_klass_name_sexp
@flog.in_klass s(:colon2, s(:const, :X), :Y) do
assert_equal "X::Y", @flog.klass_name
end

@flog.in_klass s(:colon3, :Y) do
assert_equal "Y", @flog.klass_name
end
end

def test_method_name
assert_equal "#none", @flog.method_name

@flog.method_stack << "whatevs"
assert_equal "#whatevs", @flog.method_name
end

def test_method_name_cls
assert_equal "#none", @flog.method_name

@flog.method_stack << "::whatevs"
assert_equal "::whatevs", @flog.method_name
end

# def test_process_until_empty
# flunk "no"
# end

def test_penalize_by def test_penalize_by
assert_equal 1, @flog.multiplier assert_equal 1, @flog.multiplier
@flog.penalize_by 2 do @flog.penalize_by 2 do
Expand Down Expand Up @@ -525,19 +467,6 @@ def test_score_method
:branch => 4.0) :branch => 4.0)
end end


def test_signature
assert_equal "main#none", @flog.signature

@flog.class_stack << "X"
assert_equal "X#none", @flog.signature

@flog.method_stack << "y"
assert_equal "X#y", @flog.signature

@flog.class_stack.shift
assert_equal "main#y", @flog.signature
end

def test_total_score def test_total_score
@flog.add_to_score "blah", 2 @flog.add_to_score "blah", 2
@flog.calculate_total_scores @flog.calculate_total_scores
Expand Down

0 comments on commit 30bad3b

Please sign in to comment.