rubycritic 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1099d50ed1beb525a791af3127cfae40dc525d4e
4
- data.tar.gz: 2be5a299cd4a4839ca9c096ffd613a3ae0e38647
3
+ metadata.gz: 256bd2cbd360e7435d0ad68b21db192854b49461
4
+ data.tar.gz: 6bb7d80fb6ec8cf0b7fe1e8aa460c368ce9239fd
5
5
  SHA512:
6
- metadata.gz: 11c74550a23f8fdcf3d7bcb579d72ab8cf584552bf8ee412bc3c41be13f565d21a5ed73159c615dfc177724c9d73a20eaa9efd6f081da363f2ab7b33f3278ec3
7
- data.tar.gz: dcfa39fc7b30b9d2ba6a2a3d02d93eacb1f1616cfb3c78dad9f790841172d8b202d93c4c312db95798090f7a733152837d38ce95222df0545cf5fbeadc970657
6
+ metadata.gz: cd11682da1d3f86e750fe53e60e0efbe1104e4dc1b2bf75706e1add54d493bf7dcabf3e67961b7ba89a00211f6ad773299251235395adbb821ad82a9a5f5f976
7
+ data.tar.gz: 11e0c3069c482a01200cf6f166b843478edca25ba366795b2ae9c7ebd1d8887760520b0892b0a2ee6e1779d31f46d8426cbfac9dab2ceb0adfc3500019708e52
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
7
+ - rbx-2
8
+ - jruby
9
+
10
+ script: "bundle exec rake"
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  RubyCritic
2
2
  ===========
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/rubycritic.svg)](http://badge.fury.io/rb/rubycritic)
5
+ [![Build Status](https://travis-ci.org/whitesmith/rubycritic.svg?branch=master)](https://travis-ci.org/whitesmith/rubycritic)
6
+ [![Code Climate](https://codeclimate.com/github/whitesmith/rubycritic.png)](https://codeclimate.com/github/whitesmith/rubycritic)
7
+
4
8
  RubyCritic is a gem that wraps around static analysis gems such as [Reek][1]
5
9
  and [Flay][2] to provide a quality report of your Ruby code.
6
10
 
@@ -4,8 +4,12 @@ require "rubycritic/report_generators/view_helpers"
4
4
  module Rubycritic
5
5
 
6
6
  class BaseGenerator
7
+ def self.erb_template(template_path)
8
+ ERB.new(File.read(File.join(TEMPLATES_DIR, template_path)))
9
+ end
10
+
7
11
  TEMPLATES_DIR = File.expand_path("../templates", __FILE__)
8
- LAYOUT_TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "layouts", "application.html.erb")))
12
+ LAYOUT_TEMPLATE = erb_template(File.join("layouts", "application.html.erb"))
9
13
 
10
14
  include ViewHelpers
11
15
 
@@ -32,12 +36,6 @@ module Rubycritic
32
36
  def get_binding
33
37
  binding
34
38
  end
35
-
36
- private
37
-
38
- def root_directory
39
- ::Rubycritic.configuration.root
40
- end
41
39
  end
42
40
 
43
41
  end
@@ -4,10 +4,11 @@ require "rubycritic/report_generators/base_generator"
4
4
  module Rubycritic
5
5
 
6
6
  class CodeIndexGenerator < BaseGenerator
7
- TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "code_index.html.erb")))
7
+ TEMPLATE = erb_template("code_index.html.erb")
8
8
 
9
- def initialize(file_generators)
10
- @file_generators = file_generators.sort { |a, b| a.analysed_file_name <=> b.analysed_file_name }
9
+ def initialize(source_pathnames, smelly_pathnames)
10
+ @source_pathnames = source_pathnames.sort { |a, b| a.basename <=> b.basename }
11
+ @smelly_pathnames = smelly_pathnames
11
12
  end
12
13
 
13
14
  def file_name
@@ -18,6 +19,14 @@ module Rubycritic
18
19
  index_body = TEMPLATE.result(get_binding)
19
20
  LAYOUT_TEMPLATE.result(get_binding { index_body })
20
21
  end
22
+
23
+ def file_path(path)
24
+ File.join(root_directory, path.sub_ext(".html"))
25
+ end
26
+
27
+ def smells_count(pathname)
28
+ (@smelly_pathnames[pathname] || []).length
29
+ end
21
30
  end
22
31
 
23
32
  end
@@ -6,7 +6,7 @@ module Rubycritic
6
6
 
7
7
  class FileGenerator < BaseGenerator
8
8
  LINE_NUMBER_OFFSET = 1
9
- TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "file.html.erb")))
9
+ TEMPLATE = erb_template("file.html.erb")
10
10
 
11
11
  def initialize(pathname, smells)
12
12
  @pathname = pathname
@@ -18,11 +18,7 @@ module Rubycritic
18
18
  end
19
19
 
20
20
  def file_name
21
- "#{analysed_file_name}.html"
22
- end
23
-
24
- def analysed_file_name
25
- @pathname.basename.sub_ext("")
21
+ @pathname.basename.sub_ext(".html")
26
22
  end
27
23
 
28
24
  def render
@@ -5,8 +5,8 @@ require "cgi"
5
5
  module Rubycritic
6
6
 
7
7
  class LineGenerator < BaseGenerator
8
- NORMAL_TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "line.html.erb")))
9
- SMELLY_TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "smelly_line.html.erb")))
8
+ NORMAL_TEMPLATE = erb_template("line.html.erb")
9
+ SMELLY_TEMPLATE = erb_template("smelly_line.html.erb")
10
10
 
11
11
  def initialize(text, smells)
12
12
  @text = CGI::escapeHTML(text.chomp)
@@ -33,7 +33,7 @@ module Rubycritic
33
33
  end
34
34
 
35
35
  def code_index_generator
36
- @code_index_generator ||= CodeIndexGenerator.new(file_generators)
36
+ @code_index_generator ||= CodeIndexGenerator.new(@source_pathnames, @smelly_pathnames)
37
37
  end
38
38
 
39
39
  def smells_index_generator
@@ -4,7 +4,7 @@ require "rubycritic/report_generators/base_generator"
4
4
  module Rubycritic
5
5
 
6
6
  class SmellsIndexGenerator < BaseGenerator
7
- TEMPLATE = ERB.new(File.read(File.join(TEMPLATES_DIR, "smells_index.html.erb")))
7
+ TEMPLATE = erb_template("smells_index.html.erb")
8
8
 
9
9
  def initialize(smells)
10
10
  @smells = smells.sort { |a, b| a.type <=> b.type }
@@ -2,12 +2,14 @@
2
2
  <thead>
3
3
  <tr>
4
4
  <th>Name</th>
5
+ <th>Smells</th>
5
6
  </tr>
6
7
  </thead>
7
8
  <tbody>
8
- <% @file_generators.each do |file_generator| %>
9
+ <% @source_pathnames.each do |pathname| %>
9
10
  <tr>
10
- <td><a href="<%= file_generator.file_pathname %>"><%= file_generator.analysed_file_name %></a></td>
11
+ <td><a href="<%= file_path(pathname) %>"><%= analysed_file_name(pathname) %></a></td>
12
+ <td><%= smells_count(pathname) %></td>
11
13
  </tr>
12
14
  <% end %>
13
15
  </tbody>
@@ -1,5 +1,5 @@
1
1
  <div class="file-header">
2
- <h2 class="file-name"><%= analysed_file_name %></h2>
2
+ <h2 class="file-name"><%= analysed_file_name(@pathname) %></h2>
3
3
 
4
4
  <% if file_has_smells? %>
5
5
  <button id="js-toggle-smells" class="smells-toggle-button button">Toggle Smells</button>
@@ -26,9 +26,12 @@ module Rubycritic
26
26
  File.join(root_directory, "smells_index.html")
27
27
  end
28
28
 
29
- # Includers must override
30
29
  def root_directory
31
- raise NotImplementedError.new("The #{self.class} class must implement the #{__method__} method.")
30
+ ::Rubycritic.configuration.root
31
+ end
32
+
33
+ def analysed_file_name(pathname)
34
+ pathname.basename.sub_ext("")
32
35
  end
33
36
  end
34
37
 
@@ -17,20 +17,12 @@ module Rubycritic
17
17
  private
18
18
 
19
19
  def create_smell(structural_hash, nodes)
20
- is_identical = @flay.identical[structural_hash]
21
- similarity = is_identical ? "Identical" : "Similar"
22
-
23
- locations = smell_locations(nodes)
24
- context = "#{similarity} code"
25
- message = "found in #{nodes.size} nodes"
26
- score = @flay.masses[structural_hash]
27
-
28
20
  Smell.new(
29
- :locations => locations,
30
- :context => context,
31
- :message => message,
32
- :score => score,
33
- :type => "DuplicateCode"
21
+ :locations => smell_locations(nodes),
22
+ :context => "#{similarity(structural_hash)} code",
23
+ :message => "found in #{nodes.size} nodes",
24
+ :score => @flay.masses[structural_hash],
25
+ :type => "DuplicateCode"
34
26
  )
35
27
  end
36
28
 
@@ -39,6 +31,14 @@ module Rubycritic
39
31
  Location.new(node.file, node.line)
40
32
  end.sort
41
33
  end
34
+
35
+ def similarity(structural_hash)
36
+ if @flay.identical[structural_hash]
37
+ "Identical"
38
+ else
39
+ "Similar"
40
+ end
41
+ end
42
42
  end
43
43
 
44
44
  end
@@ -23,28 +23,27 @@ module Rubycritic
23
23
  private
24
24
 
25
25
  def create_smell(context, score)
26
- location = smell_location(context)
27
- message = "has a flog score of #{score}"
28
- type =
29
- if score >= VERY_HIGH_COMPLEXITY_SCORE_THRESHOLD
30
- "VeryHighComplexity"
31
- else
32
- "HighComplexity"
33
- end
34
-
35
26
  Smell.new(
36
- :locations => [location],
37
- :context => context,
38
- :message => message,
39
- :score => score,
40
- :type => type
27
+ :locations => smell_locations(context),
28
+ :context => context,
29
+ :message => "has a flog score of #{score}",
30
+ :score => score,
31
+ :type => type(score)
41
32
  )
42
33
  end
43
34
 
44
- def smell_location(context)
35
+ def smell_locations(context)
45
36
  line = @flog.method_locations[context]
46
37
  file_path, file_line = line.split(":")
47
- Location.new(file_path, file_line)
38
+ [Location.new(file_path, file_line)]
39
+ end
40
+
41
+ def type(score)
42
+ if score >= VERY_HIGH_COMPLEXITY_SCORE_THRESHOLD
43
+ "VeryHighComplexity"
44
+ else
45
+ "HighComplexity"
46
+ end
48
47
  end
49
48
  end
50
49
 
@@ -17,12 +17,12 @@ module Rubycritic
17
17
  private
18
18
 
19
19
  def create_smell(smell)
20
- locations = smell_locations(smell.source, smell.lines)
21
- context = smell.context
22
- message = smell.message
23
- type = smell.subclass
24
-
25
- Smell.new(:locations => locations, :context => context, :message => message, :type => type)
20
+ Smell.new(
21
+ :locations => smell_locations(smell.source, smell.lines),
22
+ :context => smell.context,
23
+ :message => smell.message,
24
+ :type => smell.subclass
25
+ )
26
26
  end
27
27
 
28
28
  def smell_locations(file_path, file_lines)
@@ -1,3 +1,3 @@
1
1
  module Rubycritic
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
data/rubycritic.gemspec CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.summary = "Ruby code smell detector"
15
15
  spec.homepage = "https://github.com/whitesmith/rubycritic"
16
16
  spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 1.9.3"
17
18
 
18
19
  spec.files = `git ls-files`.split("\n")
19
20
  spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -4,7 +4,7 @@ require "rubycritic/analysers_runner"
4
4
  describe Rubycritic::AnalysersRunner do
5
5
  describe "#run" do
6
6
  it "returns an array containing smell adapters" do
7
- smell_adapters = Rubycritic::AnalysersRunner.new("test/samples/location/file0.rb").run
7
+ smell_adapters = Rubycritic::AnalysersRunner.new("test/samples/analysers_runner/empty.rb").run
8
8
  smell_adapters.each { |adapter| adapter.must_respond_to(:smells) }
9
9
  end
10
10
  end
@@ -51,10 +51,9 @@ describe Rubycritic::SourceLocator do
51
51
 
52
52
  describe "#pathnames" do
53
53
  it "finds a single file" do
54
- path = "file0.rb"
55
- paths = [path]
56
- result = [Pathname.new(path)]
57
- Rubycritic::SourceLocator.new(paths).pathnames.must_equal result
54
+ initial_paths = ["file0.rb"]
55
+ final_pathnames = [Pathname.new("file0.rb")]
56
+ Rubycritic::SourceLocator.new(initial_paths).pathnames.must_equal final_pathnames
58
57
  end
59
58
  end
60
59
 
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubycritic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Simoes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -132,12 +132,12 @@ extensions: []
132
132
  extra_rdoc_files: []
133
133
  files:
134
134
  - ".gitignore"
135
+ - ".travis.yml"
135
136
  - CONTRIBUTING.md
136
137
  - Gemfile
137
138
  - LICENSE.txt
138
139
  - README.md
139
140
  - Rakefile
140
- - SPEC.md
141
141
  - bin/rubycritic
142
142
  - lib/rubycritic.rb
143
143
  - lib/rubycritic/active_support/methods.rb
@@ -193,6 +193,7 @@ files:
193
193
  - test/lib/rubycritic/source_control_systems/source_control_system_test.rb
194
194
  - test/lib/rubycritic/source_locator_test.rb
195
195
  - test/lib/rubycritic/version_test.rb
196
+ - test/samples/analysers_runner/empty.rb
196
197
  - test/samples/flay/smelly.rb
197
198
  - test/samples/flog/smelly.rb
198
199
  - test/samples/location/dir1/file1.rb
@@ -214,7 +215,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
215
  requirements:
215
216
  - - ">="
216
217
  - !ruby/object:Gem::Version
217
- version: '0'
218
+ version: 1.9.3
218
219
  required_rubygems_version: !ruby/object:Gem::Requirement
219
220
  requirements:
220
221
  - - ">="
@@ -240,6 +241,7 @@ test_files:
240
241
  - test/lib/rubycritic/source_control_systems/source_control_system_test.rb
241
242
  - test/lib/rubycritic/source_locator_test.rb
242
243
  - test/lib/rubycritic/version_test.rb
244
+ - test/samples/analysers_runner/empty.rb
243
245
  - test/samples/flay/smelly.rb
244
246
  - test/samples/flog/smelly.rb
245
247
  - test/samples/location/dir1/file1.rb
data/SPEC.md DELETED
@@ -1,58 +0,0 @@
1
- Ruby Critic
2
- ===========
3
-
4
- Ruby Critic is a tool that detects and reports smells in Ruby code.
5
-
6
- Inspired by [RuboCop][1], [Rails Best Practices][2] and [Code Climate][3], Ruby Critic aims to better help you refactor your code. By making use of Ruby's rich ecosystem of code metrics tools, Ruby Critic generates high-quality visualizations and insightful code quality reports.
7
-
8
- [1]: https://github.com/bbatsov/rubocop/
9
- [2]: https://github.com/railsbp/rails_best_practices
10
- [3]: https://codeclimate.com/
11
-
12
- Installation
13
- ------------
14
-
15
- Add this line to your application's Gemfile:
16
-
17
- ```ruby
18
- gem "rubycritic"
19
- ```
20
-
21
- And then execute:
22
-
23
- ```bash
24
- $ bundle
25
- ```
26
-
27
- Or just install it yourself:
28
-
29
- ```bash
30
- $ gem install rubycritic
31
- ```
32
-
33
- Usage
34
- -----
35
-
36
- Running `rubycritic` with no arguments will check all Ruby source files in the
37
- current directory:
38
-
39
- ```bash
40
- $ rubycritic
41
- ```
42
-
43
- Alternatively you can pass `rubycritic` a list of files and directories to check:
44
-
45
- ```bash
46
- $ rubycritic app lib/foo.rb
47
- ```
48
-
49
- Options
50
- -------
51
-
52
- To specify an output file for the results:
53
-
54
- ```bash
55
- $ rubycritic -o output_file
56
- ```
57
-
58
- The output format is determined by the file extension or by using the `-f` option. Current options are: `text`, `html`, `yaml` and `json`.