Giter VIP home page Giter VIP logo

roodi's Introduction

roodi

Build Status Coverage Status

Description

Roodi stands for Ruby Object Oriented Design Inferometer. It parses your Ruby code and warns you about design issues you have based on the checks that it has configured.

Install

Open your terminal and type this:

$ gem install roodi

Alternatively, you can put it in your Gemfile:

gem "roodi"

Synopsis

To check one or more files using the default configuration that comes with Roodi, use:

$ roodi [-config=file] [pattern ...]

Example Usage

Check all ruby files recursively under the current directory:

$ roodi or $ roodi .

Check all ruby files in a rails app:

$ roodi "rails_app/**/*.rb"

Check one controller and one model file in a rails app:

$ roodi app/controller/sample_controller.rb app/models/sample.rb

Check one controller and all model files in a rails app:

$ roodi app/controller/sample_controller.rb "app/models/*.rb"

Check all ruby files in a rails app with a custom configuration file:

$ roodi -config=my_roodi_config.yml "rails_app/**/*.rb"

If you're writing a check, it is useful to see the structure of a file the way that Roodi tokenizes it (via ruby_parser). Use:

$ roodi-describe [filename]

Running it as part of your build

Add the following to your Rakefile:

require 'roodi_task'
task :default => [:roodi]

or if you want to supply your own config file...

require 'roodi_task'
RoodiTask.new :config => 'config/roodi.yml'
task :default => [:roodi]

Editor integration

You can run Roodi in Atom using the linter-roodi package.

Custom Configuration

To change the set of checks included, or to change the default values of the checks, you can provide your own config file. The config file is a YAML file that lists the checks to be included. Each check can optionally include a hash of options that are passed to the check to configure it. For example, the default config file looks like this:

AssignmentInConditionalCheck:
CaseMissingElseCheck:
ClassLineCountCheck:
  line_count: 300
ClassNameCheck:
  pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
ClassVariableCheck:
CyclomaticComplexityBlockCheck:
  complexity: 4
CyclomaticComplexityMethodCheck:
  complexity: 8
EmptyRescueBodyCheck:
ForLoopCheck:
MethodLineCountCheck:
  line_count: 20
MethodNameCheck:
  pattern: !ruby/regexp /^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
ModuleLineCountCheck:
  line_count: 300
ModuleNameCheck:
  pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
ParameterNumberCheck:
  parameter_count: 5

Supported Checks

  • AssignmentInConditionalCheck - Check for an assignment inside a conditional. It's probably a mistaken equality comparison.
  • CaseMissingElseCheck - Check that case statements have an else statement so that all cases are covered.
  • ClassLineCountCheck - Check that the number of lines in a class is below the threshold.
  • ClassNameCheck - Check that class names match convention.
  • CyclomaticComplexityBlockCheck - Check that the cyclomatic complexity of all blocks is below the threshold.
  • CyclomaticComplexityMethodCheck - Check that the cyclomatic complexity of all methods is below the threshold.
  • EmptyRescueBodyCheck - Check that there are no empty rescue blocks.
  • ForLoopCheck - Check that for loops aren't used (Use Enumerable.each instead)
  • MethodLineCountCheck - Check that the number of lines in a method is below the threshold.
  • MethodNameCheck - Check that method names match convention.
  • ModuleLineCountCheck - Check that the number of lines in a module is below the threshold.
  • ModuleNameCheck - Check that module names match convention.
  • ParameterNumberCheck - Check that the number of parameters on a method is below the threshold.

Suggested Checks

  • BlockVariableShadowCheck - Check that a block variable does not have the same name as a method parameter or local variable. It may be mistakenly referenced within the block.

Contributing

Bug reporting

Please use the GitHub issue tracker.

Want to submit some code?

Fantastic! Please follow this procedure:

  • Fork the repository
  • Create a well-named topic branch
  • Add specs for any changes you make
  • Write meaningful commit messages explaining why this change is needed
  • Create a pull request.

How to publish a new version (maintainers only)

  1. Bump the version in version.rb
  2. $ gem build roodi.gemspec
  3. $ gem push roodi-X.X.X.gem

License

(The MIT License)

Copyright (c) 2015 Marty Andrews, Peter Evjan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

roodi's People

Contributors

anicholson avatar bf4 avatar boutil avatar davearonson avatar evjan avatar martinjandrews avatar mdub avatar mgotink avatar thejefe avatar virgilm avatar xaviershay avatar zdennis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

roodi's Issues

Incorrectly detecting method line count

I have the following private method in one of my Rails controllers:

def add_resources
  @stylesheets.push(:customer_orders)
  @javascripts.push(:finder, :effects, :controls, :lightbox, :customer_orders, :customer_alert)
end

A roodi check on this method raises the following issue:

app/controllers/customer_orders_controller.rb:123 - Method "add_resources" has 124 lines.  It should have 50 or less

`evaluate_node': wrong number of arguments (1 for 0) (ArgumentError)

I get this when running roodi on any of my ruby code (I'm running ruby 1.9.1 p376). Happens on 2 different machines:

$ roodi app/controllers/home_controller.rb 
/usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/checks/check.rb:32:in `evaluate_node': wrong number of arguments (1 for 0) (ArgumentError)
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/checks/check.rb:36:in `evaluate_node_start'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/checking_visitor.rb:18:in `block in visit'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/checking_visitor.rb:18:in `each'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/checking_visitor.rb:18:in `visit'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/visitable_sexp.rb:6:in `accept'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/runner.rb:25:in `check'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/lib/roodi/core/runner.rb:33:in `check_file'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/bin/roodi:14:in `block (2 levels) in <top (required)>'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/bin/roodi:14:in `each'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/bin/roodi:14:in `block in <top (required)>'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/bin/roodi:13:in `each'
from /usr/local/ruby-1.9.1-p376/lib/ruby/gems/1.9.1/gems/roodi-2.0.1/bin/roodi:13:in `<top (required)>'
from /usr/local/bin/ruby_bin/roodi:19:in `load'
from /usr/local/bin/ruby_bin/roodi:19:in `<main>'

Anyone ever seen this before?

Suggestion: Add IfMissingElseCheck check

I was wondering if you'd consider adding a new check similar to CaseMissingElseCheck, but for if statements? I think that the reasons for having an else condition on an if are just as valid as for case statements.

Identifies rescue block as empty when returning []

I have the following code in a project, and Roodi was returning the error "Rescue block should not be empty":

def directory_listing
  @path.dirname.children
rescue Errno::ENOENT
  []
end

If I add an explicit return, or change [] to Array.new the error goes away.

Doesn't seem to load config file

When I create a roodi.yml and put what's currently in the default roodi.yml in there and then tweak the MethodLineCountCheck to 1, roodi still doesn't find any errors in my code. I definitely have methods that are longer than 1 line.

Facets breaking standard ruby methods

The standard ruby Object#extend method accepts N arguments, however facets replaces it with a method that only accepts a single argument and a block.

This is causing issues with another gem that I'm loading into my rails app - is it possible to stop using facets, or only include the bare minimum you need?

Remove noisy Rakefile line

Currently you need to have this in your Rakefile for Roodi to run as part of your build:

require 'roodi_task'
RoodiTask.new
task :default => [:roodi]

The middle line shouldn't be necessary, so remove it.

Roodi doesn't like the default application.html.erb in Rails 4

Gave the following error in a brand spanking new Rails 4 project:

Running Roodi checks
./app/views/layouts/application.html.erb looks like it's not a valid Ruby file.

Checked 20 files
rake aborted!
Found 1 errors.
/Users/peterevjan/projekt/roodi/lib/roodi_task.rb:35:in `block in define'

Can't run roodi in Windows

When I try to roodi my project, it throws EACCES.

Trace:

$ roodi .
c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/lib/roodi/core/runner.rb:34:in `read': Permission denied - . (Errno::EACCES)
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/lib/roodi/core/runner.rb:34:in `check_file'
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/bin/roodi:14:in `block (2 levels) in <top (required)>'
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/bin/roodi:14:in `each'
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/bin/roodi:14:in `block in <top (required)>'
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/bin/roodi:13:in `each'
        from c:/Ruby193/lib/ruby/gems/1.9.1/gems/roodi-2.2.0/bin/roodi:13:in `<top (required)>'
        from c:/Ruby193/bin/roodi:23:in `load'
        from c:/Ruby193/bin/roodi:23:in `<main>'

System:

$ specs ruby os
Specs:

specs 0.4
https://github.com/mcandre/specs#readme

bundle --version
Bundler version 1.3.2

gem --version
2.0.3

ruby --version
ruby 1.9.3p374 (2013-01-15) [i386-mingw32]

systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows XP Professional
OS Version:                5.1.2600 Service Pack 3 Build 2600

RuntimeError with Ruby 2.3

Source:

https://github.com/mcandre/mcandre/tree/master/ruby

Trace:

bundle exec roodi -config=roodi.yml *.rb **/*.rb

Running Roodi checks
ccard/ccard.rb:62 - Method "main" has 52 lines.  It should have 50 or less.
tiny/tiny.rb:111 - Method "main" has 51 lines.  It should have 50 or less.

Checked 47 files
/Users/andrew.pennebaker/.gem/ruby/2.3.0/gems/roodi-4.1.1/lib/roodi/core/runner.rb:52:in `output_result': Found 2 errors. (RuntimeError)
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/gems/roodi-4.1.1/lib/roodi/core/runner.rb:41:in `start'
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/gems/roodi-4.1.1/bin/roodi:13:in `<top (required)>'
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/bin/roodi:23:in `load'
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/bin/roodi:23:in `<main>'
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/bin/ruby_executable_hooks:15:in `eval'
    from /Users/andrew.pennebaker/.gem/ruby/2.3.0/bin/ruby_executable_hooks:15:in `<main>'
rake aborted!
Command failed with status (1): [bundle exec roodi -config=roodi.yml *.rb *...]

System:

  • Ruby 2.3
  • chruby
  • rvm
  • Mac OS X El Capitan 10.11.2

Roodi doesn't work in ruby 1.9

It looks like the default roodi.yml file is bad. The new more strict parser complains and blows up.

Instead of:
ModuleLineCountCheck: { line_count: 300 }

Should be
ModuleLineCountCheck:
line_count: 300

roodi 2.0.0 ignores custom roodi.yml when called via Rake task

We instantiate a Roodi Rake task using the following code:

RoodiTask.new 'roodi', ['app//*.rb', 'lib//*.rb'], RAILS_ROOT+'/config/roodi.yml'

In roodi 1.4.0, this worked fine and allowed us to override the default configuration with our own config. After upgrading to 2.0.0, no matter what we try, only the default config is used.

Upon examining lib/roodi/core/runner.rb, I can see that the load_checks method is only called in initalize(), which is too early for the Rake task to override the DEFAULT_CONFIG value. Version 1.4.0 called load_checks in the check() method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.