]> git.immae.eu Git - github/fretlink/pronto-hlint.git/commitdiff
refactor source code a little bit
authorMarkus Doits <markus.doits@stellenticket.de>
Thu, 11 Aug 2016 15:06:34 +0000 (17:06 +0200)
committerMarkus Doits <markus.doits@stellenticket.de>
Thu, 11 Aug 2016 15:06:34 +0000 (17:06 +0200)
lib/pronto/eslint-npm.rb

index a3219eb20eac33f400e995e38e905e09e9a4c0db..afbae59e57536baac686c4414c10e8b42868581b 100644 (file)
@@ -14,24 +14,18 @@ module Pronto
     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`)
+      offences = run_eslint(patch)
+      clean_up_eslint_output(offences)
+        .map do |offence|
+          patch
+            .added_lines
+            .select { |line| line.new_lineno == offence['line'] }
+            .map { |line| new_message(offence, line) }
         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
 
+    private
+
     def new_message(offence, line)
       path = line.patch.delta.new_file[:path]
       level = :warning
@@ -42,5 +36,24 @@ module Pronto
     def js_file?(path)
       %w(.js .es6 .js.es6).include?(File.extname(path))
     end
+
+    def run_eslint(patch)
+      Dir.chdir(@_repo_path) do
+        JSON.parse(
+          `eslint #{Shellwords.escape(patch.new_file_full_path.to_s)} -f json`
+        )
+      end
+    end
+
+    # rubocop:disable Metrics/LineLength
+    def clean_up_eslint_output(output)
+      # 1. Filter out offences without a warning or error
+      # 2. Get the messages for that file
+      # 3. Ignore errors without a line number for now
+      output
+        .select { |offence| offence['errorCount'] + offence['warningCount'] > 0 }
+        .map { |offence| offence['messages'] }
+        .flatten.select { |offence| offence['line'] }
+    end
   end
 end