Sha256: 2ad603c9dd65d84f7b91da995997448aa7086caa46eb587b4560f9cfda8d3ad2

Contents?: true

Size: 1.82 KB

Versions: 158

Compression:

Stored size: 1.82 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
        # @return [Array<String>]
        def run
          errors = []

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

          # output message to stderr
          errors
        end

        private

        # @param file [String]
        # @param errors [Array<String>]
        # @return [void]
        def generate_errors_for_file(file, errors)
          result = execute(['bundle', 'exec', 'solargraph', 'typecheck', '--level', 'strong', 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

        # @param file [String]
        # @param error [String]
        # @return [Overcommit::Hook::Message, nil]
        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

158 entries across 158 versions & 2 rubygems

Version Path
punchlist-1.3.2 .git-hooks/pre_commit/solargraph_typecheck.rb
punchlist-1.3.1 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.201.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.200.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.199.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.198.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.197.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.196.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.195.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.194.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.193.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.192.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.191.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.190.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.189.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.188.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.187.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.186.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.185.0 .git-hooks/pre_commit/solargraph_typecheck.rb
checkoff-0.184.0 .git-hooks/pre_commit/solargraph_typecheck.rb