123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Expectations::BlockSnippetExtractor::BlockTokenExtractor Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Struct
Instance Chain:
self, Struct
Inherits: Struct
  • Object
Defined in: rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb

Overview

Performs extraction of block body snippet using tokens, which cannot be done with node information.

Class Method Summary

Instance Attribute Summary

  • #body_tokens readonly Internal use only
  • #state readonly Internal use only
  • #beginning_line_number rw private Internal use only

    Performs extraction of block body snippet using tokens, which cannot be done with node information.

  • #method_name rw private Internal use only

    Performs extraction of block body snippet using tokens, which cannot be done with node information.

  • #source rw private Internal use only

    Performs extraction of block body snippet using tokens, which cannot be done with node information.

Instance Method Summary

Instance Attribute Details

#beginning_line_number (rw, private)

Performs extraction of block body snippet using tokens, which cannot be done with node information.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 75

BlockTokenExtractor = Struct.new(:method_name, :source, :beginning_line_number)

#body_tokens (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 76

attr_reader :state, :body_tokens

#method_name (rw, private)

Performs extraction of block body snippet using tokens, which cannot be done with node information.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 75

BlockTokenExtractor = Struct.new(:method_name, :source, :beginning_line_number)

#source (rw, private)

Performs extraction of block body snippet using tokens, which cannot be done with node information.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 75

BlockTokenExtractor = Struct.new(:method_name, :source, :beginning_line_number)

#state (readonly)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 76

attr_reader :state, :body_tokens

Instance Method Details

#after_beginning_of_args_state(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 124

def after_beginning_of_args_state(token)
  @state = :after_beginning_of_body if pipe_token?(token)
end

#after_beginning_of_body_state(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 128

def after_beginning_of_body_state(token)
  if handle_closer_token(token)
    finish_or_find_next_block_if_incorrect!
  else
    pending_tokens << token
    handle_opener_token(token)
  end
end

#after_method_call_state(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 107

def after_method_call_state(token)
  @state = :after_opener if handle_opener_token(token)
end

#after_opener_state(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 111

def after_opener_state(token)
  if handle_closer_token(token)
    finish_or_find_next_block_if_incorrect!
  elsif pipe_token?(token)
    finalize_pending_tokens!
    @state = :after_beginning_of_args
  else
    pending_tokens << token
    handle_opener_token(token)
    @state = :after_beginning_of_body unless token.type == :on_sp
  end
end

#block_locator (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 191

def block_locator
  @block_locator ||= BlockLocator.new(method_name, source, beginning_line_number)
end

#correct_block?(body_tokens) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 185

def correct_block?(body_tokens)
  return true if block_locator.body_content_locations.empty?
  content_location = block_locator.body_content_locations.first
  content_location.between?(body_tokens.first.location, body_tokens.last.location)
end

#finalize_pending_tokens! (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 141

def finalize_pending_tokens!
  pending_tokens.freeze.tap do
    @pending_tokens = nil
  end
end

#finish! (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 95

def finish!
  throw :finish
end

#finish_or_find_next_block_if_incorrect! (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 147

def finish_or_find_next_block_if_incorrect!
  body_tokens = finalize_pending_tokens!

  if correct_block?(body_tokens)
    @body_tokens = body_tokens
    finish!
  else
    @state = :after_method_call
  end
end

#handle_closer_token(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 168

def handle_closer_token(token)
  if opener_token_stack.last.closed_by?(token)
    opener_token_stack.pop
    opener_token_stack.empty?
  else
    false
  end
end

#handle_opener_token(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 158

def handle_opener_token(token)
  opener_token?(token).tap do |boolean|
    opener_token_stack.push(token) if boolean
  end
end

#initial_state(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 103

def initial_state(token)
  @state = :after_method_call if token.location == block_locator.method_call_location
end

#invoke_state_handler(token) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 99

def invoke_state_handler(token)
  __send__("#{state}_state", token)
end

#opener_token?(token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 164

def opener_token?(token)
  token.type == :on_lbrace || (token.type == :on_kw && token.string == 'do')
end

#opener_token_stack (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 177

def opener_token_stack
  @opener_token_stack ||= []
end

#parse! (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 85

def parse!
  @state = :initial

  catch(:finish) do
    source.tokens.each do |token|
      invoke_state_handler(token)
    end
  end
end

#pending_tokens (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 137

def pending_tokens
  @pending_tokens ||= []
end

#pipe_token?(token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 181

def pipe_token?(token)
  token.type == :on_op && token.string == '|'
end