Sha256: b957a1c80ba9b7f58801e284bf03c46de0be7884dd5b58be9913455e53546423

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'overcommit'
require 'overcommit/hook/pre_commit/base'

# @!override Overcommit::Hook::Base#execute
#   @return [Overcommit::Subprocess::Result]

module Overcommit
  module Hook
    module PreCommit
      # Runs `solargraph typecheck` against any modified Ruby files.
      class SolargraphTypecheck < Base
        def run
          errors = []

          applicable_files.each do |file|
            generate_errors_for_file(file, errors)
          end

          # output message to stderr
          errors
        end

        private

        def generate_errors_for_file(file, errors)
          result = execute(['bundle', 'exec', 'solargraph', 'typecheck', '--level', 'strict', file])
          return if result.success?

          # @type [String]
          stdout = result.stdout

          stdout.split("\n").each do |error|
            error = parse_error(file, error)
            errors << error unless error.nil?
          end
        end

        def parse_error(file, error)
          # Parse the result for the line number                # @type [MatchData]
          match = error.match(/^(.+?):(\d+)/)
          return nil unless match

          # @!override MatchData.captures
          #   @return [Array]
          # @sg-ignore
          file_path, lineno = match.captures
          message = error.sub("#{file_path}:#{lineno} - ",
                              "#{file_path}:#{lineno}: ")
          # Emit the errors in the specified format
          Overcommit::Hook::Message.new(:error, file, lineno.to_i, message)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
checkoff-0.39.4 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.39.3 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.39.2 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.39.1 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.39.0 .git-hooks/pre_commit/solargraph_typecheck.rb