diff --git a/lib/solargraph/rspec/spec_walker/fake_let_method.rb b/lib/solargraph/rspec/spec_walker/fake_let_method.rb index dab6445..31c3f86 100644 --- a/lib/solargraph/rspec/spec_walker/fake_let_method.rb +++ b/lib/solargraph/rspec/spec_walker/fake_let_method.rb @@ -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 diff --git a/spec/solargraph/rspec/convention_spec.rb b/spec/solargraph/rspec/convention_spec.rb index 42c0888..515dd7f 100644 --- a/spec/solargraph/rspec/convention_spec.rb +++ b/spec/solargraph/rspec/convention_spec.rb @@ -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