Merge pull request #40597 from joelhawksley/view-stats

Add support for stylesheets and ERB views to `rails stats`.
This commit is contained in:
Kasper Timm Hansen 2020-11-23 18:40:02 +01:00 committed by GitHub
commit 4e17c7b07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 3 deletions

View File

@ -1,3 +1,7 @@
* Add support for stylesheets and ERB views to `rails stats`.
*Joel Hawksley*
* Allow appended root routes to take precedence over internal welcome controller.
*Gannon McGibbon*

View File

@ -40,7 +40,7 @@ class CodeStatistics #:nodoc:
Hash[@pairs.map { |pair| [pair.first, calculate_directory_statistics(pair.last)] }]
end
def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|coffee|rake)$/)
def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/)
stats = CodeStatisticsCalculator.new
Dir.foreach(directory) do |file_name|

View File

@ -11,6 +11,15 @@ class CodeStatisticsCalculator #:nodoc:
class: /^\s*class\s+[_A-Z]/,
method: /^\s*def\s+[_a-z]/,
},
erb: {
line_comment: %r{((^\s*<%#.*%>)|(<\!--.*-->))},
},
css: {
line_comment: %r{^\s*\/\*.*\*\/},
},
scss: {
line_comment: %r{((^\s*\/\*.*\*\/)|(^\s*\/\/))},
},
js: {
line_comment: %r{^\s*//},
begin_block_comment: %r{^\s*/\*},

View File

@ -11,17 +11,19 @@ STATS_DIRECTORIES ||= [
%w(Mailers app/mailers),
%w(Mailboxes app/mailboxes),
%w(Channels app/channels),
%w(Views app/views),
%w(JavaScripts app/assets/javascripts),
%w(Stylesheets app/assets/stylesheets),
%w(JavaScript app/javascript),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Controller\ tests test/controllers),
%w(Helper\ tests test/helpers),
%w(Job\ tests test/jobs),
%w(Model\ tests test/models),
%w(Mailer\ tests test/mailers),
%w(Mailbox\ tests test/mailboxes),
%w(Channel\ tests test/channels),
%w(Job\ tests test/jobs),
%w(Integration\ tests test/integration),
%w(System\ tests test/system),
].collect do |name, dir|

View File

@ -173,7 +173,7 @@ module ApplicationTests
end
def test_code_statistics_sanity
assert_match "Code LOC: 32 Test LOC: 3 Code to Test Ratio: 1:0.1",
assert_match "Code LOC: 74 Test LOC: 3 Code to Test Ratio: 1:0.0",
rails("stats")
end

View File

@ -218,6 +218,59 @@ class CodeStatisticsCalculatorTest < ActiveSupport::TestCase
assert_equal 0, @code_statistics_calculator.methods
end
test "skip ERB comments" do
code = <<-'CODE'
<!-- This is an HTML comment -->
<%# This is a great comment! %>
<div>
<%= hello %>
</div>
CODE
@code_statistics_calculator.add_by_io(StringIO.new(code), :erb)
assert_equal 6, @code_statistics_calculator.lines
assert_equal 3, @code_statistics_calculator.code_lines
assert_equal 0, @code_statistics_calculator.classes
assert_equal 0, @code_statistics_calculator.methods
end
test "skip CSS comments" do
code = <<-'CODE'
/* My cool CSS */
.selector {
background-color: blue;
}
CODE
@code_statistics_calculator.add_by_io(StringIO.new(code), :css)
assert_equal 5, @code_statistics_calculator.lines
assert_equal 3, @code_statistics_calculator.code_lines
assert_equal 0, @code_statistics_calculator.classes
assert_equal 0, @code_statistics_calculator.methods
end
test "skip SCSS comments" do
code = <<-'CODE'
// My cool SCSS
/* My cool SCSS */
.selector {
background-color: blue;
}
CODE
@code_statistics_calculator.add_by_io(StringIO.new(code), :scss)
assert_equal 6, @code_statistics_calculator.lines
assert_equal 3, @code_statistics_calculator.code_lines
assert_equal 0, @code_statistics_calculator.classes
assert_equal 0, @code_statistics_calculator.methods
end
test "calculate number of CoffeeScript methods" do
code = <<-'CODE'
square = (x) -> x * x