Permalink
Browse files

+ Extracted MethodBasedSexpProcessor and pushed up to sexp_processor …

…gem.

[git-p4: depot-paths = "//src/flog/dev/": change = 8988]
  • Loading branch information...
1 parent 3d74c49 commit 30bad3b56b837f77d1b2fdf8f3dea966d9dcf414 @zenspider zenspider committed Oct 18, 2013
Showing with 10 additions and 185 deletions.
  1. +10 −114 lib/flog.rb
  2. +0 −71 test/test_flog.rb
View
@@ -10,7 +10,7 @@ class << self
end
end
-class Flog < SexpProcessor
+class Flog < MethodBasedSexpProcessor
VERSION = "4.1.2" # :nodoc:
##
@@ -93,13 +93,10 @@ class Flog < SexpProcessor
SCORES.merge!(:inject => 2)
- @@no_class = :main
- @@no_method = :none
-
# :stopdoc:
attr_accessor :multiplier
- attr_reader :calls, :option, :class_stack, :method_stack, :mass, :sclass
- attr_reader :method_locations, :method_scores, :scores
+ attr_reader :calls, :option, :mass
+ attr_reader :method_scores, :scores
attr_reader :total_score, :totals
# :startdoc:
@@ -228,48 +225,11 @@ def flog_ruby! ruby, file="-", timeout = 10
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+.
def initialize option = {}
super()
@option = option
- @sclass = []
- @class_stack = []
- @method_stack = []
@method_locations = {}
@mass = {}
@parser = nil
@@ -278,22 +238,6 @@ def initialize option = {}
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.
def max_method
@@ -308,16 +252,6 @@ def max_score
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
# by #bonus This allows the complexity of sub-expressions to be
# influenced by the expressions in which they are found. Yields 42
@@ -330,13 +264,6 @@ def penalize_by bonus
end
##
- # Process each element of #exp in turn.
-
- def process_until_empty exp
- process exp.shift until exp.empty?
- end
-
- ##
# Reset score data
def reset
@@ -345,7 +272,7 @@ def reset
@calls = Hash.new { |h,k| h[k] = Hash.new 0 }
@method_scores = Hash.new { |h,k| h[k] = [] }
@scores = Hash.new 0
- @method_locations = {}
+ method_locations.clear
end
##
@@ -364,13 +291,6 @@ def score_method(tally)
end
##
- # Returns the method signature for the current method.
-
- def signature
- "#{klass_name}#{method_name}"
- end
-
- ##
# Final threshold that is used for report
def threshold
@@ -487,13 +407,12 @@ def process_case(exp)
end
def process_class(exp)
- in_klass exp.shift do
+ super do
penalize_by 1.0 do
process exp.shift # superclass expression
end
process_until_empty exp
end
- s()
end
def process_dasgn_curr(exp) # FIX: remove
@@ -505,22 +424,6 @@ def process_dasgn_curr(exp) # FIX: remove
alias :process_iasgn :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.
def process_else(exp)
add_to_score :branch
@@ -605,20 +508,13 @@ def process_masgn(exp)
s()
end
- def process_module(exp)
- in_klass exp.shift do
- process_until_empty exp
- end
- s()
- end
-
def process_sclass(exp)
- @sclass.push(true)
- penalize_by 0.5 do
- process exp.shift # recv
- process_until_empty exp
+ super do
+ penalize_by 0.5 do
+ process exp.shift # recv
+ process_until_empty exp
+ end
end
- @sclass.pop
add_to_score :sclass
s()
View
@@ -80,64 +80,6 @@ def test_flog_erb
$stdin = old_stdin
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
assert_equal 1, @flog.multiplier
@flog.penalize_by 2 do
@@ -525,19 +467,6 @@ def test_score_method
:branch => 4.0)
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
@flog.add_to_score "blah", 2
@flog.calculate_total_scores

0 comments on commit 30bad3b

Please sign in to comment.