Sha256: fd76dabd1940a2e5bf946926ac562d25c2924b61856d40280d25ff9642589c8b

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module DidYouMean
  module Experimental
    module IvarNameCorrection
      REPLS = {
        "(irb)" => -> { Readline::HISTORY.to_a.last }
      }

      def initialize(no_method_error)
        super

        @location   = no_method_error.backtrace_locations.first
        @ivar_names = no_method_error.frame_binding.receiver.instance_variables
      end

      def corrections
        super + ivar_name_corrections
      end

      def ivar_name_corrections
        @ivar_name_corrections ||= SpellChecker.new(dictionary: @ivar_names).correct(receiver_name.to_s)
      end

      private

      def receiver_name
        return unless receiver.nil?

        abs_path = @location.absolute_path
        lineno   = @location.lineno

        /@(\w+)*\.#{method_name}/ =~ line(abs_path, lineno).to_s && $1
      end

      def line(abs_path, lineno)
        if REPLS[abs_path]
          REPLS[abs_path].call
        elsif File.exist?(abs_path)
          File.open(abs_path) do |file|
            file.detect { file.lineno == lineno }
          end
        end
      end
    end

    SPELL_CHECKERS['NoMethodError'].prepend(IvarNameCorrection)
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
did_you_mean-1.1.3 lib/did_you_mean/experimental/ivar_name_correction.rb
did_you_mean-1.0.4 lib/did_you_mean/experimental/ivar_name_correction.rb
did_you_mean-1.0.3 lib/did_you_mean/experimental/ivar_name_correction.rb
did_you_mean-1.1.2 lib/did_you_mean/experimental/ivar_name_correction.rb
ruby-compiler-0.1.1 vendor/ruby/gems/did_you_mean-1.1.0/lib/did_you_mean/experimental/ivar_name_correction.rb
did_you_mean-1.1.0 lib/did_you_mean/experimental/ivar_name_correction.rb
did_you_mean-1.0.2 lib/did_you_mean/experimental/ivar_name_correction.rb