Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Add ability to run summaries #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rake eslint:print_config[true]
```

You can also retrieve the current eslint.json by visiting `/eslint/eslint.json`
in your browser. To force retrieval of the default conguration, use
in your browser. To force retrieval of the default configuration, use
`/eslint/eslint.json?force_default=true`.

## Usage
Expand All @@ -48,7 +48,9 @@ Or you can run it on a single file. This will analyze `application.js`:
rake eslint:run
```

Or, you can supply a filename to the task, using several different formats, and it will lint just that file. For example, to analyze `app/assets/javascripts/components/utilities.js`, you can run any of the following:
Or, you can supply a filename to the task, using several different formats,
and it will lint just that file. For example, to analyze
`app/assets/javascripts/components/utilities.js`, you can run any of the following:

```sh
rake eslint:run[components/utilities]
Expand All @@ -57,6 +59,40 @@ rake eslint:run[app/assets/javascripts/components/utilities]
rake eslint:run[app/assets/javascripts/components/utilities.js]
```

###### Summaries

This will analyze all of the javascript files in the project and return a
summary of warnings based on the `group_by` filter parameter:

```sh
rake eslint:summary[group_by]
```

Available `group_by` filters:

- `filename` - displays number of warnings occurred per file

- `warning` - displays number of warnings occurred per assets


Or you can get a summary of the warnings on a single file. This will show
the summary for `application.js`:

```sh
rake eslint:file_summary
```

Or, you can supply a filename to the task, using several different formats,
and it will lint just that file. For example, to analyze
`app/assets/javascripts/components/utilities.js`, you can run any of the following:

```sh
rake eslint:file_summary[components/utilities]
rake eslint:file_summary[components/utilities.js]
rake eslint:file_summary[app/assets/javascripts/components/utilities]
rake eslint:file_summary[app/assets/javascripts/components/utilities.js]
```

### Web interface

On non-production environments, visit these URLs on your application:
Expand Down
25 changes: 25 additions & 0 deletions lib/eslint-rails/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Runner
include ActionView::Helpers::JavaScriptHelper

JAVASCRIPT_EXTENSIONS = %w(.js .jsx)
SUMMARY_FILTERS = %w(filename warning)

def initialize(file)
@file = normalize_infile(file)
Expand All @@ -20,6 +21,30 @@ def run
warnings.flatten
end

def summary(group_by: 'filename')
raise "Unknown filter. Accepted values: #{SUMMARY_FILTERS.join(', ')}" unless SUMMARY_FILTERS.include?(group_by)
results = []
assets.map do |asset|
file_content = asset.read
if group_by == 'filename'
results << { item: asset.relative_path_from(Pathname.new(Dir.pwd)), count: warning_hashes(file_content).count }
else
file_warnings_by_rule = warning_hashes(file_content).group_by { |w| w['ruleId'] }.values
file_warnings_by_rule.map do |a|
# When rule ID is nil set type to +UnknownError+
warning_type = a.first['ruleId'] ? "#{a.first['nodeType']}/#{a.first['ruleId']}" : 'UnexpectedError'
warning = results.find { |r| r[:item] == warning_type }
if warning
warning[:count] += a.size
else
results << { item: warning_type, count: a.size }
end
end
end
end
results.sort_by { |r| -r[:count] }
end

private

def normalize_infile(file)
Expand Down
15 changes: 14 additions & 1 deletion lib/eslint-rails/text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@ def format
end
puts colorized_message
end

puts "#{@warnings.size} warning(s) found."
end

def format_summary
max_count_length = max_length_of_attribute(:count)
@warnings.each do |warning|
message = [
warning[:count].to_s.ljust(max_count_length + 1),
warning[:item],
].join(' ')
puts message
end
total = @warnings.inject(0) {|sum, hash| sum + hash[:count]}
puts '--'
puts "#{total} warning(s) found."
end

private

def max_length_of_attribute(attr_key)
Expand Down
24 changes: 21 additions & 3 deletions lib/tasks/eslint.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ ENV['EXECJS_RUNTIME'] = 'RubyRacer'
require 'eslint-rails'

namespace :eslint do
def run_and_print_results(file)
warnings = ESLintRails::Runner.new(file).run
def run_and_print_results(file, group_by = nil)
if group_by
warnings = ESLintRails::Runner.new(file).summary(group_by: group_by)
else
warnings = ESLintRails::Runner.new(file).run
end

if warnings.empty?
puts 'All good! :)'.green
exit 0
else
formatter = ESLintRails::TextFormatter.new(warnings)
formatter.format
if group_by
formatter.format_summary
else
formatter.format
end
exit 1
end
end
Expand All @@ -26,6 +34,16 @@ namespace :eslint do
run_and_print_results(nil) # Run all
end

desc 'Run ESLint against all project javascript files and report summary based on filter'
task :summary, [:group_by] => :environment do |_, args|
run_and_print_results(nil, args[:group_by])
end

desc %{Run ESLint against the specified JavaScript file and report a summary of warnings (default is 'application')}
task :file_summary, [:filename] => :environment do |_, args|
run_and_print_results(args[:filename] || 'application', 'warning')
end

desc 'Print the current configuration file (Uses local config/eslint.json if it exists; uses default config/eslint.json if it does not; optionally force default by passing a parameter)'
task :print_config, [:force_default] => :environment do |_, args|
puts ESLintRails::Config.read(force_default: args[:force_default])
Expand Down