Skip to content

Commit

Permalink
Fix let do/end block definitions with keyword overlap
Browse files Browse the repository at this point in the history
Example:

```
let(:todo) do # "do" keyword overlap
  {
    'todo' => 'end' # "end" keyword overlap
  }
end
```
  • Loading branch information
lekemula committed Jun 12, 2024
1 parent 4241f6c commit acd0f9e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/solargraph/rspec/spec_walker/fake_let_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ module Rspec
class SpecWalker
class FakeLetMethod
MATCH_BODY = Regexp.union(
/do(.*)end/m,
/.* do(.*)end/m,
/{(.*)}/m
)

# @param block_ast [RubyVM::AbstractSyntaxTree::Node]
# @return [RubyVM::AbstractSyntaxTree::Node]
# @return [RubyVM::AbstractSyntaxTree::Node, nil]
def self.transform_block(block_ast, code, method_name = nil)
method_name ||= NodeTypes.let_method_name(block_ast)
block_body = block_ast.children[1]
matches = code.lines[block_body.first_lineno - 1..block_body.last_lineno - 1].join.match(MATCH_BODY)
let_definition_code = code.lines[block_body.first_lineno - 1..block_body.last_lineno - 1].join
matches = let_definition_code.match(MATCH_BODY)
method_body = (matches[1] || matches[2]).strip

ast = RubyVM::AbstractSyntaxTree.parse <<~RUBY
Expand Down
36 changes: 36 additions & 0 deletions spec/solargraph/rspec/convention_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@
expect(completion_at(filename, [22, 8])).to include('nested_something')
end

it 'generates let methods with do/end block' do
load_string filename, <<~RUBY
RSpec.describe SomeNamespace::Transaction, type: :model do
let(:something) do
"something"
end
let!(:other_thing) do
2
end
let(:todo) do # "do" keyword overlap
{
'todo' => 'end' # "end" keyword overlap
}
end
end
RUBY

assert_public_instance_method_inferred_type(
api_map,
'RSpec::ExampleGroups::TestSomeNamespaceTransaction#something',
'String'
)
assert_public_instance_method_inferred_type(
api_map,
'RSpec::ExampleGroups::TestSomeNamespaceTransaction#other_thing',
'Integer'
)
assert_public_instance_method_inferred_type(
api_map,
'RSpec::ExampleGroups::TestSomeNamespaceTransaction#todo',
'Hash'
)
end

it 'generates implicit subject method' do
load_string filename, <<~RUBY
RSpec.describe SomeNamespace::Transaction, type: :model do
Expand Down

0 comments on commit acd0f9e

Please sign in to comment.