Sha256: 14da0c7973b8b50c77516238bc21398323953a4961c3d037bccd9194999e9d15

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 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, message)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
checkoff-0.38.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.37.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.36.1 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.36.0 .git-hooks/pre_commit/solargraph_typecheck.rb