Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Support end-less methods in Source::Token #505

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Bug Fixes:

* Use `Mutex#owned?` to allow `RSpec::Support::ReentrantMutex` to work in
nested Fibers on Ruby 3.0 and later. (Benoit Daloze, #503, #504)
* Support `end`-less methods in `RSpec::Support::Source::Token`
so that RSpec won't hang when an `end`-less method raises an error. (Yuji Nakayama, #505)

### 3.10.2 / 2021-01-28
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.10.1...v3.10.2)
Expand Down
17 changes: 12 additions & 5 deletions lib/rspec/support/source/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ def keyword?
type == :on_kw
end

def equals_operator?
type == :on_op && string == '='
end

def opening?
opening_delimiter? || opening_keyword?
end

def closed_by?(other)
closed_by_delimiter?(other) || closed_by_keyword?(other)
delimiter_closed_by?(other) || keyword_closed_by?(other)
end

private
Expand All @@ -73,13 +77,16 @@ def opening_keyword?
CLOSING_KEYWORDS_BY_OPENING_KEYWORD.key?(string)
end

def closed_by_delimiter?(other)
def delimiter_closed_by?(other)
other.type == CLOSING_TYPES_BY_OPENING_TYPE[type]
end

def closed_by_keyword?(other)
return false unless other.keyword?
other.string == CLOSING_KEYWORDS_BY_OPENING_KEYWORD[string]
def keyword_closed_by?(other)
return false unless keyword?
return true if other.string == CLOSING_KEYWORDS_BY_OPENING_KEYWORD[string]

# Ruby 3's `end`-less method definition: `def method_name = body`
string == 'def' && other.equals_operator? && location.line == other.location.line
end
end
end
Expand Down