Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Linters - eslint, jscs, jshint & scss #221

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
/repos

/Gemfile.lock

/node_modules
9 changes: 5 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ gem 'travis', '~>1.7.6'

gem 'awesome_spawn', '>= 1.4.1'
gem 'default_value_for'
gem 'haml_lint', '= 0.16.1', :require => false
gem 'more_core_extensions', '~> 2.0.0', :require => 'more_core_extensions/all'
gem 'rubocop', '= 0.37.2', :require => false
gem 'rugged', :require => false
gem 'haml_lint', '= 0.16.1', :require => false
gem 'more_core_extensions', '~> 2.0.0', :require => 'more_core_extensions/all'
gem 'rubocop', '= 0.37.2', :require => false
gem 'rugged', :require => false
gem 'scss_lint', '~> 0.49.0', :require => false

gem 'octokit', '~> 3.8.0'
gem 'minigit', '~> 0.0.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def process_branch
unmerged_results = []
unmerged_results << Linter::Rubocop.new(branch).run
unmerged_results << Linter::Haml.new(branch).run
unmerged_results << Linter::ESLint.new(branch).run
unmerged_results << Linter::SCSS.new(branch).run
unmerged_results.compact!

if unmerged_results.empty?
@results = {"files" => []}
else
Expand Down
23 changes: 16 additions & 7 deletions lib/linter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ def run
end
end

begin
offenses = JSON.parse(result.output.chomp)
rescue JSON::ParserError => error
logger.error("#{log_header} #{error.message}")
logger.error("#{log_header} Failed to parse JSON result #{result.output.inspect}")
return failed_linter_offenses("error parsing JSON result")
end
offenses = parse_output(result.output)
logger.info("#{log_header} Completed run with offenses #{offenses.inspect}")
offenses
end
Expand Down Expand Up @@ -69,6 +63,21 @@ def files_to_lint
@files_to_lint ||= filtered_files(diff_service.new_files)
end

def parse_output(str)
begin
convert_parsed(JSON.parse(str.chomp))
rescue JSON::ParserError => error
logger.error("#{log_header} #{error.message}")
logger.error("#{log_header} Failed to parse JSON result #{result.output.inspect}")
return failed_linter_offenses("error parsing JSON result")
end
end

# overriden in linters with different JSON format
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment typo overriden -> overridden

def convert_parsed(hash_or_array)
hash_or_array
end

def run_linter(dir)
logger.info("#{log_header} Executing linter...")
require 'awesome_spawn'
Expand Down
45 changes: 45 additions & 0 deletions lib/linter/eslint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Linter
class ESLint < Base
private

def config_files
[".eslintrc.js", ".eslintrc.json"]
end

def linter_executable
'eslint .'
end

def options
{:format => 'json'}
end

def filtered_files(files)
files.select do |file|
file.end_with? '.js'
end
end

def convert_parsed(array)
files = array.map { |f| convert_file(f) }

{:files => files,
:summary => {:offense_count => files.reduce(0) { |memo, f| memo + f.offenses.count },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe files.reduce(0) { |memo, f| memo + f.offenses.count } can be simplified to files.map { |f| f.offenses.count }.reduce(:+)

:target_file_count => files.count}}
end

def convert_file(file)
{:path => file[:filePath],
:offenses => file[:messages].map { |m| convert_offense(m) }}
end

SEVERITY = ['off', 'warning', 'error']
def convert_offense(offense)
{:severity => SEVERITY[offense[:severity]],
:message => offense[:message],
:location => {:line => offense[:line],
:column => offense[:column]},
:cop_name => offense[:ruleId]}
end
end
end
43 changes: 43 additions & 0 deletions lib/linter/scss.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Linter
class SCSS < Base
private

def config_files
[".scss-lint.yml"]
end

def linter_executable
'scss-lint'
end

def options
{:format => 'JSON'}
end

def filtered_files(files)
files.select { |file| file.end_with?(".scss") }
end

def convert_parsed(hash)
files = hash.map { |path, offenses| convert_file(path, offenses) }

{:files => files,
:summary => {:offense_count => files.reduce(0) { |memo, f| memo + f.offenses.count },
:target_file_count => files.count}}
end

def convert_file(path, offenses)
{:path => path,
:offenses => offenses.map { |o| convert_offense(o) }}
end

def convert_offense(offense)
{:severity => offense[:severity],
:message => offense[:reason],
:location => {:line => offense[:line],
:column => offense[:column]},
:cop_name => offense[:linter]}
end
end
end
end
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"eslint": "^3.13.1",
"eslint-cli": "^1.1.0"
}
}