Skip to content
This repository
Browse code

Add code statistics for Javascript and

CoffeeScript files to `rake stats` task

Orignal PR was #2270

Thanks to @nfm
  • Loading branch information...
commit a48b3f1a6834df7f99fef74a62771d927d594cf1 1 parent 08477a6
Arun Agrawal authored May 31, 2012
11  railties/lib/rails/code_statistics.rb
@@ -26,7 +26,7 @@ def calculate_statistics
26 26
       Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
27 27
     end
28 28
 
29  
-    def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
  29
+    def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
30 30
       stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
31 31
 
32 32
       Dir.foreach(directory) do |file_name|
@@ -39,6 +39,13 @@ def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
39 39
 
40 40
         comment_started = false
41 41
         
  42
+        case file_name
  43
+        when /.*\.js$/
  44
+          comment_pattern = /^\s*\/\//
  45
+        else
  46
+          comment_pattern = /^\s*#/
  47
+        end
  48
+
42 49
         File.open(directory + "/" + file_name) do |f|
43 50
           while line = f.gets
44 51
             stats["lines"]     += 1
@@ -55,7 +62,7 @@ def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
55 62
             end
56 63
             stats["classes"]   += 1 if line =~ /^\s*class\s+[_A-Z]/
57 64
             stats["methods"]   += 1 if line =~ /^\s*def\s+[_a-z]/
58  
-            stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
  65
+            stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern
59 66
           end
60 67
         end
61 68
       end
1  railties/lib/rails/tasks/statistics.rake
@@ -3,6 +3,7 @@ STATS_DIRECTORIES = [
3 3
   %w(Helpers            app/helpers),
4 4
   %w(Models             app/models),
5 5
   %w(Mailers            app/mailers),
  6
+  %w(Javascripts        app/assets/javascripts),
6 7
   %w(Libraries          lib/),
7 8
   %w(APIs               app/apis),
8 9
   %w(Integration\ tests test/integration),

0 notes on commit a48b3f1

Please sign in to comment.
Something went wrong with that request. Please try again.