class Cane::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/cane/abc_check.rb, line 14
def self.key; :abc; end
name() click to toggle source
# File lib/cane/abc_check.rb, line 15
def self.name; "ABC check"; end
options() click to toggle source
# File lib/cane/abc_check.rb, line 16
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/cane/abc_check.rb, line 36
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/cane/abc_check.rb, line 193
def exclusions
  opts.fetch(:abc_exclude, []).flatten.to_set
end
file_names() click to toggle source
# File lib/cane/abc_check.rb, line 181
def file_names
  Dir.glob(opts.fetch(:abc_glob))
end
find_violations(file_name) click to toggle source
# File lib/cane/abc_check.rb, line 46
def find_violations(file_name)
  ast = Ripper::SexpBuilder.new(Cane::File.contents(file_name)).parse
  case ast
  when nil
    InvalidAst.new(file_name)
  else
    RubyAst.new(file_name, max_allowed_complexity, ast, exclusions)
  end.violations
end
max_allowed_complexity() click to toggle source
# File lib/cane/abc_check.rb, line 189
def max_allowed_complexity
  opts.fetch(:abc_max)
end
order(result) click to toggle source
# File lib/cane/abc_check.rb, line 185
def order(result)
  result.sort_by {|x| x[:value].to_i }.reverse
end
worker() click to toggle source
# File lib/cane/abc_check.rb, line 197
def worker
  Cane.task_runner(opts)
end