Skip to content
Browse files

+ Added .rake as a supported extension.

+ Create a new parser for every file, preventing state barkification
+ Extended DSL support to include hash args (eg task :blah => :dep).
+ Extended DSL support to report nested names (eg namespace(blah)::task#woot)

[git-p4: depot-paths = "//src/flog/dev/": change = 7822]
  • Loading branch information...
1 parent d55be69 commit 3f9bc192e22a1951c280feb71865342fa13592a1 @zenspider zenspider committed Oct 8, 2012
Showing with 72 additions and 7 deletions.
  1. +21 −7 lib/flog.rb
  2. +51 −0 test/test_flog.rb
View
28 lib/flog.rb
@@ -118,7 +118,7 @@ def self.load_plugins
# REFACTOR: from flay
def self.expand_dirs_to_files *dirs
- extensions = ['rb']
+ extensions = %w[rb rake]
dirs.flatten.map { |p|
if File.directory? p then
@@ -255,6 +255,7 @@ def flog(*files_or_dirs)
ruby = file == '-' ? $stdin.read : File.binread(file)
warn "** flogging #{file}" if option[:verbose]
+ @parser = option[:parser].new
ast = @parser.process(ruby, file)
next unless ast
mass[file] = ast.mass
@@ -317,7 +318,7 @@ def initialize option = {}
@method_stack = []
@method_locations = {}
@mass = {}
- @parser = option[:parser].new
+ @parser = nil
self.auto_shift_type = true
self.reset
end
@@ -332,7 +333,7 @@ def klass_name
if Sexp === name then
raise "you shouldn't see me"
elsif @class_stack.any?
- @class_stack.reverse.join("::")
+ @class_stack.reverse.join("::").sub(/\([^\)]+\)$/, '')
else
@@no_class
end
@@ -637,6 +638,15 @@ def process_if(exp)
s()
end
+ def dsl_name? args
+ return false unless args and not args.empty?
+
+ first_arg = args.first
+ first_arg = first_arg[1] if first_arg[0] == :hash
+
+ [:lit, :str].include? first_arg[0] and first_arg[1]
+ end
+
def process_iter(exp)
context = (self.context - [:class, :module, :scope])
context = context.uniq.sort_by { |s| s.to_s }
@@ -646,10 +656,14 @@ def process_iter(exp)
# DSL w/ names. eg task :name do ... end
# looks like s(:call, nil, :task, s(:lit, :name))
- t, r, m, a = recv
- if (t == :call and r == nil and a and [:lit, :str].include? a[0]) then
- submsg = a[1]
- in_klass m do # :task
+ # or s(:call, nil, :task, s(:str, "name"))
+ # or s(:call, nil, :task, s(:hash, s(:lit, :name) ...))
+
+ t, r, m, *a = recv
+
+ if t == :call and r == nil and submsg = dsl_name?(a) then
+ m = "#{m}(#{submsg})" if m and [String, Symbol].include?(submsg.class)
+ in_klass m do # :task/namespace
in_method submsg, exp.file, exp.line do # :name
process_until_empty exp
end
View
51 test/test_flog.rb
@@ -458,6 +458,57 @@ def test_process_iter_dsl_regexp
util_process sexp, 2.0, :something => 1.0, :task => 1.0
end
+ def test_process_iter_dsl_hash
+ # task :woot => 42 do
+ # something
+ # end
+
+ sexp = s(:iter,
+ s(:call, nil, :task, s(:hash, s(:lit, :woot), s(:lit, 42))),
+ nil,
+ s(:call, nil, :something))
+
+ @klass, @meth = "task", "#woot"
+
+ util_process sexp, 2.3, :something => 1.0, :task => 1.0, :lit_fixnum => 0.3
+ end
+
+ def test_process_iter_dsl_namespaced
+ # namespace :blah do
+ # task :woot => 42 do
+ # something
+ # end
+ # end
+
+ sexp = s(:iter,
+ s(:call, nil, :namespace, s(:lit, :blah)),
+ nil,
+ s(:iter,
+ s(:call, nil, :task, s(:hash, s(:lit, :woot), s(:lit, 42))),
+ nil,
+ s(:call, nil, :something)))
+
+ @klass, @meth = "namespace(blah)::task", "woot"
+
+ score = 3.3
+ hash = {
+ "namespace(blah)::task#woot" => {
+ :something => 1.0,
+ :lit_fixnum => 0.3,
+ :task => 1.0,
+ },
+ "namespace#blah" => {
+ :namespace => 1.0,
+ },
+ }
+
+ setup
+ @flog.process sexp
+
+ assert_equal hash, @flog.calls
+ assert_in_delta score, @flog.total
+ end
+
def test_process_lit
sexp = s(:lit, :y)
util_process sexp, 0.0

3 comments on commit 3f9bc19

@will
will commented on 3f9bc19 Apr 21, 2014

please @brynary pull in this patch to your fork of flog so that sinatra files aren't an auto F

@uhoh-itsmaciek

@brynary any chance this will be patched in? Grades for anything using metaprogramming blocks are effectively meaningless without this.

@brynary

Hello! Yes, this is something we will be merging into Code Climate. We are doing some major upgrades as we speak to make rolling out patches to our analysis much easier. So as not to bug people watching this repo with Code Climate stuff, if you're looking for more info please feel free to contact us: https://codeclimate.com/help (or on Twitter -- @codeclimate) Thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.