mccabe 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/mccabe +23 -0
- data/bin/setup +7 -0
- data/lib/mccabe/analyzer.rb +25 -0
- data/lib/mccabe/cli.rb +72 -0
- data/lib/mccabe/parser.rb +24 -0
- data/lib/mccabe/version.rb +3 -0
- data/lib/mccabe.rb +2 -0
- data/mccabe.gemspec +24 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86c4e0a18d0b2971e6d30c63b019ca1bb74d02ef
|
4
|
+
data.tar.gz: 2d457e73f9afd1cccb07a539763171a88f4e8547
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02a5ed82955b75ae647e513fa3b615959756fe0186c485277b85e509d050170ae8b75358e2be7956b89069cfe0b8b8560d5880c700752a97b92f85ad838fde9a
|
7
|
+
data.tar.gz: 48eb68e4fc58b95c73f9f2eb745ff40bfe87bab48b2ce3ca6876c5c878517f13b8f19937d4f35a53f88a83bb1db5c62c23e293bdb0679d224eeeb066b7037ca9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andrew Mason
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# McCabe
|
2
|
+
|
3
|
+
Measure the McCabe or [cyclomatic](http://en.wikipedia.org/wiki/Cyclomatic_complexity)
|
4
|
+
complexity of Ruby code.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
```
|
9
|
+
$ gem install mccabe
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
From the command line, pass the list of files you want to analyze. You can
|
15
|
+
optionally specify your own threshold as the first argument. The default
|
16
|
+
threshold is 4. You will get error messages for any methods which have
|
17
|
+
complexity greater than the threshold.
|
18
|
+
|
19
|
+
If the first argument is not an integer, it will be interpreted as a file
|
20
|
+
pattern (any other arguments are also file patterns).
|
21
|
+
|
22
|
+
Note that the script only considers files with .rb extensions, so that it
|
23
|
+
doesn't attempt to parse other languages as Ruby. So, if you have
|
24
|
+
extensionless Ruby scripts, you won't be able to use this on them.
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
```
|
28
|
+
mccabe file1 ../file2
|
29
|
+
mccabe 3 file1 # using a different threshold
|
30
|
+
mccabe *.rb # wildcards work, too
|
31
|
+
mccabe . # can also look through entire directories
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Fork and pull.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mccabe"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/mccabe
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'mccabe/analyzer'
|
4
|
+
require 'mccabe/cli'
|
5
|
+
require 'mccabe/parser'
|
6
|
+
|
7
|
+
options = McCabe::CLI.new.parse!(ARGV)
|
8
|
+
|
9
|
+
if ARGV.empty?
|
10
|
+
puts "Please specify at least one file"
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
|
14
|
+
results = McCabe::CLI.get_all_files(ARGV).map do |file|
|
15
|
+
file_results = McCabe::Analyzer.analyze(McCabe::Parser.parse(File.read(file)))
|
16
|
+
file_results.keep_if { |method, info| info[:complexity] > options[:threshold] }
|
17
|
+
unless options[:quiet]
|
18
|
+
McCabe::CLI.display_results(file_results) unless file_results.empty?
|
19
|
+
end
|
20
|
+
file_results
|
21
|
+
end
|
22
|
+
|
23
|
+
exit 1 unless results.flatten.empty?
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mccabe/parser'
|
2
|
+
|
3
|
+
module McCabe
|
4
|
+
class Analyzer
|
5
|
+
BRANCH_TYPES = [:if, :while, :until, :for, :when, :and, :or]
|
6
|
+
|
7
|
+
def self.analyze(ast)
|
8
|
+
results = {}
|
9
|
+
McCabe::Parser.collect_methods(ast).each do |name, method|
|
10
|
+
results[name] = {line: method[:line], complexity: complexity(method[:body])}
|
11
|
+
end
|
12
|
+
results
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.complexity(ast)
|
16
|
+
nodes, complexity = [ast], 0
|
17
|
+
until nodes.empty?
|
18
|
+
node = nodes.shift
|
19
|
+
complexity += 1 if BRANCH_TYPES.include?(node.type)
|
20
|
+
nodes += node.children.select { |child| child.is_a? ::Parser::AST::Node }
|
21
|
+
end
|
22
|
+
complexity
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mccabe/cli.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
require 'mccabe/version'
|
4
|
+
|
5
|
+
module McCabe
|
6
|
+
class CLI
|
7
|
+
DEFAULT_OPTIONS = {
|
8
|
+
threshold: 4,
|
9
|
+
quiet: false
|
10
|
+
}
|
11
|
+
|
12
|
+
# Print out the results of a file to the console.
|
13
|
+
def self.display_results(file_results)
|
14
|
+
file_results.each do |method, info|
|
15
|
+
puts "\t#{method} (line #{info[:line]}) had a complexity of #{info[:complexity]}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Get all files recursively.
|
20
|
+
def self.get_all_files(patterns)
|
21
|
+
files = []
|
22
|
+
patterns.each do |pattern|
|
23
|
+
files |=
|
24
|
+
if File.directory? pattern
|
25
|
+
sub_patterns =
|
26
|
+
(Dir.entries(pattern) - ['.', '..']).map { |sub_pat| "#{pattern}/#{sub_pat}" }
|
27
|
+
get_all_files(sub_patterns)
|
28
|
+
else
|
29
|
+
Dir[pattern]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
files.select { |file| file.end_with? '.rb' }
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse!(argv)
|
36
|
+
option_parser.parse! argv
|
37
|
+
DEFAULT_OPTIONS.merge options
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def options
|
43
|
+
@options ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def option_parser
|
47
|
+
@option_parser ||= OptionParser.new do |option_parser|
|
48
|
+
option_parser.banner = "Usage: #{$0} file1... [options]"
|
49
|
+
|
50
|
+
option_parser.on '-tTHRESHOLD',
|
51
|
+
'--threshold',
|
52
|
+
"threshold of mccabe's complexity to allow through" do |t|
|
53
|
+
options[:threshold] = t.to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
option_parser.on '--quiet', "No output to stdout. Exit code only (1 for failure)." do |q|
|
57
|
+
options[:quiet] = q
|
58
|
+
end
|
59
|
+
|
60
|
+
option_parser.on_tail '-h', '--help', "Display this help message" do
|
61
|
+
puts option_parser
|
62
|
+
exit 0
|
63
|
+
end
|
64
|
+
|
65
|
+
option_parser.on_tail '--version' do
|
66
|
+
puts "#{$0} #{McCabe::VERSION}"
|
67
|
+
exit 0
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'parser/current'
|
2
|
+
|
3
|
+
module McCabe
|
4
|
+
class Parser < Parser::CurrentRuby
|
5
|
+
# Collect all the methods from the AST.
|
6
|
+
def self.collect_methods(ast)
|
7
|
+
methods = {}
|
8
|
+
nodes = [ast]
|
9
|
+
until nodes.empty?
|
10
|
+
node = nodes.shift
|
11
|
+
case node.type
|
12
|
+
when :def, :defs
|
13
|
+
name = node.children.length == 4 ? node.children[1] : node.children.first
|
14
|
+
methods[name] = {body: node.children.last, line: node.loc.line}
|
15
|
+
when :class, :module, :begin
|
16
|
+
# Have to put node.children in an array and flatten in because the parser
|
17
|
+
# returns a frozen array.
|
18
|
+
nodes += node.children.select { |child| child.is_a? ::Parser::AST::Node }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
methods
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/mccabe.rb
ADDED
data/mccabe.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mccabe/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mccabe"
|
8
|
+
spec.version = McCabe::VERSION
|
9
|
+
spec.authors = ["Andrew Mason"]
|
10
|
+
spec.email = ["mason@case.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Tool for measuring McCabe's complexity of Ruby code.}
|
13
|
+
spec.homepage = "https://github.com/ajm188/mccabe"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.executables = ['mccabe']
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'parser'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mccabe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Mason
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- mason@case.edu
|
58
|
+
executables:
|
59
|
+
- mccabe
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/mccabe
|
71
|
+
- bin/setup
|
72
|
+
- lib/mccabe.rb
|
73
|
+
- lib/mccabe/analyzer.rb
|
74
|
+
- lib/mccabe/cli.rb
|
75
|
+
- lib/mccabe/parser.rb
|
76
|
+
- lib/mccabe/version.rb
|
77
|
+
- mccabe.gemspec
|
78
|
+
homepage: https://github.com/ajm188/mccabe
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.8
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Tool for measuring McCabe's complexity of Ruby code.
|
102
|
+
test_files: []
|