class SugarCane::AbcCheck

Creates violations for methods that are too complicated using a simple algorithm run against the parse tree of a file to count assignments, branches, and conditionals. Borrows heavily from metric_abc.

Public Class Methods

key() click to toggle source
# File lib/sugarcane/abc_check.rb, line 15
def self.key; :abc; end
name() click to toggle source
# File lib/sugarcane/abc_check.rb, line 16
def self.name; "ABC check"; end
options() click to toggle source
# File lib/sugarcane/abc_check.rb, line 17
def self.options
  {
    abc_glob: ['Glob to run ABC metrics over',
                  default: '{app,lib}/**/*.rb',
                  variable: 'GLOB',
                  clobber: :no_abc],
    abc_max:  ['Ignore methods under this complexity',
                  default: 15,
                  cast:    :to_i,
                  clobber: :no_abc],
    abc_exclude: ['Exclude method from analysis (eg. Foo::Bar#method)',
                     variable: 'METHOD',
                     type: Array,
                     default: [],
                     clobber: :no_abc],
    no_abc:   ['Disable ABC checking',
                  cast: ->(x) { !x }]
  }
end

Public Instance Methods

violations() click to toggle source
# File lib/sugarcane/abc_check.rb, line 37
def violations
  return [] if opts[:no_abc]

  order worker.map(file_names) {|file_name|
    find_violations(file_name)
  }.flatten
end

Protected Instance Methods

exclusions() click to toggle source
# File lib/sugarcane/abc_check.rb, line 208
def exclusions
  opts.fetch(:abc_exclude, []).flatten.to_set
end
file_names() click to toggle source
# File lib/sugarcane/abc_check.rb, line 196
def file_names
  Dir[opts.fetch(:abc_glob)]
end
find_violations(file_name) click to toggle source
# File lib/sugarcane/abc_check.rb, line 47
def find_violations(file_name)
  ast = Ripper::SexpBuilder.new(SugarCane::File.contents(file_name)).parse
  case ast
  when nil
    ast_type = InvalidAst.new(file_name)
  else
    ast_type = RubyAst.new(file_name, max_allowed_complexity,
                           ast, exclusions)
  end
  ast_type.violations
end
max_allowed_complexity() click to toggle source
# File lib/sugarcane/abc_check.rb, line 204
def max_allowed_complexity
  opts.fetch(:abc_max)
end
order(result) click to toggle source
# File lib/sugarcane/abc_check.rb, line 200
def order(result)
  result.sort_by {|x| x[:value].to_i }.reverse
end
worker() click to toggle source
# File lib/sugarcane/abc_check.rb, line 212
def worker
  SugarCane.task_runner(opts)
end