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
|
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
- .new ⇒ BlockTokenExtractor constructor Internal use only
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
- #after_beginning_of_args_state(token) private Internal use only
- #after_beginning_of_body_state(token) private Internal use only
- #after_method_call_state(token) private Internal use only
- #after_opener_state(token) private Internal use only
- #block_locator private Internal use only
- #correct_block?(body_tokens) ⇒ Boolean private Internal use only
- #finalize_pending_tokens! private Internal use only
- #finish! private Internal use only
- #finish_or_find_next_block_if_incorrect! private Internal use only
- #handle_closer_token(token) private Internal use only
- #handle_opener_token(token) private Internal use only
- #initial_state(token) private Internal use only
- #invoke_state_handler(token) private Internal use only
- #opener_token?(token) ⇒ Boolean private Internal use only
- #opener_token_stack private Internal use only
- #parse! private Internal use only
- #pending_tokens private Internal use only
- #pipe_token?(token) ⇒ Boolean private Internal use only
Instance Attribute Details
#beginning_line_number (rw, private)
Performs extraction of block body snippet using tokens, which cannot be done with node information.
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 77
BlockTokenExtractor = Struct.new(:method_name, :source, :beginning_line_number)
#body_tokens (readonly)
[ GitHub ]# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 78
attr_reader :state, :body_tokens
#method_name (rw, private)
Performs extraction of block body snippet using tokens, which cannot be done with node information.
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 77
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.
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 77
BlockTokenExtractor = Struct.new(:method_name, :source, :beginning_line_number)
#state (readonly)
[ GitHub ]# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 78
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 126
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 130
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 109
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 113
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 193
def block_locator @block_locator ||= BlockLocator.new(method_name, source, beginning_line_number) end
#correct_block?(body_tokens) ⇒ Boolean
(private)
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 187
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 143
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 97
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 149
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 170
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 160
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 105
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 101
def invoke_state_handler(token) __send__("#{state}_state", token) end
#opener_token?(token) ⇒ Boolean
(private)
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 166
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 179
def opener_token_stack @opener_token_stack ||= [] end
#parse! (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 87
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 139
def pending_tokens @pending_tokens ||= [] end
#pipe_token?(token) ⇒ Boolean
(private)
# File 'rspec-expectations/lib/rspec/expectations/block_snippet_extractor.rb', line 183
def pipe_token?(token) token.type == :on_op && token.string == '|' end