From 5001cb27fe21f4fdbc20f95b44ee99bbd9812730 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Thu, 11 Aug 2016 16:02:45 +0200 Subject: rebrand to Pronto::ESLintNpm --- lib/pronto/eslint-npm.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/pronto/eslint-npm.rb (limited to 'lib/pronto/eslint-npm.rb') diff --git a/lib/pronto/eslint-npm.rb b/lib/pronto/eslint-npm.rb new file mode 100644 index 0000000..a3219eb --- /dev/null +++ b/lib/pronto/eslint-npm.rb @@ -0,0 +1,46 @@ +require 'pronto' + +module Pronto + class ESLintNpm < Runner + def run + return [] unless @patches + + @patches.select { |patch| patch.additions > 0 } + .select { |patch| js_file?(patch.new_file_full_path) } + .map { |patch| inspect(patch) } + .flatten.compact + end + + def inspect(patch) + @_repo_path ||= @patches.first.repo.path + + offences = + Dir.chdir(@_repo_path) do + JSON.parse(`eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`) + end + + offences = + offences + .select { |offence| offence['errorCount'] > 0 || offence['warningCount'] > 0 } # no warning or error, no problem + .map { |offence| offence['messages'] } # get error messages for that file + .flatten + .select { |offence| offence['line'] } # for now ignore errors without a line number + + offences.map do |offence| + patch.added_lines.select { |line| line.new_lineno == offence['line'] } + .map { |line| new_message(offence, line) } + end + end + + def new_message(offence, line) + path = line.patch.delta.new_file[:path] + level = :warning + + Message.new(path, line, level, offence['message'], nil, self.class) + end + + def js_file?(path) + %w(.js .es6 .js.es6).include?(File.extname(path)) + end + end +end -- cgit v1.2.3