Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing RSpec files #94

Merged
merged 1 commit into from
Feb 26, 2024
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
34 changes: 34 additions & 0 deletions lib/annotate_rb/model_annotator/file_parser/custom_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,38 @@ def on_class(const, _superclass, _bodystmt)
super
end

# Gets the `RSpec` opening in:
# ```ruby
# RSpec.describe "Collapsed::TestModel" do
# # Deliberately left empty
# end
# ```
# receiver: "RSpec", operator: ".", method: "describe"
def on_command_call(receiver, operator, method, args)
add_event(__method__, receiver, lineno)
@block_starts << [receiver, lineno]

# We keep track of blocks using a stack
@_stack_code_block << receiver
super
end

def on_method_add_block(method, block)
# When parsing a line with no explicit receiver, the method will be presented in an Array.
# It's not immediately clear why.
#
# Example:
# ```ruby
# describe "Collapsed::TestModel" do
# # Deliberately left empty
# end
# ```
#
# => method = ["describe"]
if method.is_a?(Array) && method.size == 1
method = method.first
end

add_event(__method__, method, lineno)

if @_stack_code_block.last == method
Expand Down Expand Up @@ -125,6 +156,9 @@ def on_call(receiver, operator, message)
def on_command(message, args)
add_event(__method__, message, lineno)
@block_starts << [message, lineno]

# We keep track of blocks using a stack
@_stack_code_block << message
super
end

Expand Down
48 changes: 42 additions & 6 deletions spec/lib/annotate_rb/model_annotator/custom_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ class Foo::User < ApplicationRecord
FILE
end
let(:expected_comments) { [] }
let(:expected_starts) { [["FactoryBot", 0], ["factory", 1], ["admin", 2], ["factory", 3], ["FactoryBot", 4]] }
let(:expected_ends) { [["admin", 2], ["end", 3], ["end", 4]] }
let(:expected_starts) { [["FactoryBot", 0], ["factory", 1], ["admin", 2], ["FactoryBot", 4]] }
let(:expected_ends) { [["admin", 2], ["end", 3], ["factory", 3], ["end", 4]] }

it "parses correctly" do
check_it_parses_correctly
Expand All @@ -214,9 +214,9 @@ class Foo::User < ApplicationRecord
end
let(:expected_comments) { [] }
let(:expected_starts) {
[["factory", 0], ["first_name", 1], ["last_name", 2], ["date_of_birth", 3], ["factory", 4]]
[["factory", 0], ["first_name", 1], ["last_name", 2], ["date_of_birth", 3]]
}
let(:expected_ends) { [["first_name", 1], ["last_name", 2], ["date_of_birth", 3], ["end", 4]] }
let(:expected_ends) { [["first_name", 1], ["last_name", 2], ["date_of_birth", 3], ["end", 4], ["factory", 4]] }

it "parses correctly" do
check_it_parses_correctly
Expand All @@ -235,8 +235,44 @@ class Foo::User < ApplicationRecord
FILE
end
let(:expected_comments) { [["# typed: strong", 0]] }
let(:expected_starts) { [["factory", 1], ["first_name", 2], ["last_name", 3], ["date_of_birth", 4], ["factory", 5]] }
let(:expected_ends) { [["first_name", 2], ["last_name", 3], ["date_of_birth", 4], ["end", 5]] }
let(:expected_starts) { [["factory", 1], ["first_name", 2], ["last_name", 3], ["date_of_birth", 4]] }
let(:expected_ends) { [["first_name", 2], ["last_name", 3], ["date_of_birth", 4], ["end", 5], ["factory", 5]] }

it "parses correctly" do
check_it_parses_correctly
end
end

context "when using an RSpec file" do
let(:input) do
<<~FILE
RSpec.describe "Collapsed::TestModel" do
# Deliberately left empty
end
FILE
end
let(:expected_comments) { [["# Deliberately left empty", 1]] }
let(:expected_starts) { [["RSpec", 0]] }
let(:expected_ends) { [["end", 2], ["RSpec", 2]] }

it "parses correctly" do
check_it_parses_correctly
end
end

context "when using an RSpec file with monkeypatching" do
# Should be removed by RSpec 4+
# https://github.com/rspec/rspec-core/issues/2301
let(:input) do
<<~FILE
describe "Collapsed::TestModel" do
# Deliberately left empty
end
FILE
end
let(:expected_comments) { [["# Deliberately left empty", 1]] }
let(:expected_starts) { [["describe", 0]] }
let(:expected_ends) { [["end", 2], ["describe", 2]] }

it "parses correctly" do
check_it_parses_correctly
Expand Down
Loading