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

Ignore #: annotation on attr_* calls #1176

Merged
merged 1 commit into from
Jun 21, 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
9 changes: 9 additions & 0 deletions lib/steep/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ def self.insert_type_node(node, comments)
return assertion_node(node, last_comment)
end
else
if (receiver, name, * = deconstruct_send_node(node))
if receiver.nil?
if name == :attr_reader || name == :attr_writer || name == :attr_accessor
child_assertions = comments.except(last_line)
node = map_child_node(node) {|child| insert_type_node(child, child_assertions) }
return adjust_location(node)
end
end
end
child_assertions = comments.except(last_line)
node = map_child_node(node) {|child| insert_type_node(child, child_assertions) }
node = adjust_location(node)
Expand Down
70 changes: 55 additions & 15 deletions test/source_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,25 +574,25 @@ def foo
end

def test_assertion_class_module_names
with_factory do |factory|
source = Steep::Source.parse(<<~RUBY, path: Pathname("foo.rb"), factory: factory)
class Foo # : Integer
end
module Bar # : nil
end
RUBY

source.node.children[0].tap do |node|
assert_equal :class, node.type
assert_equal :const, node.children[0].type
with_factory do |factory|
source = Steep::Source.parse(<<~RUBY, path: Pathname("foo.rb"), factory: factory)
class Foo # : Integer
end

source.node.children[1].tap do |node|
assert_equal :module, node.type
assert_equal :const, node.children[0].type
module Bar # : nil
end
RUBY

source.node.children[0].tap do |node|
assert_equal :class, node.type
assert_equal :const, node.children[0].type
end

source.node.children[1].tap do |node|
assert_equal :module, node.type
assert_equal :const, node.children[0].type
end
end
end

def test_assertion_assignment
with_factory do |factory|
Expand Down Expand Up @@ -672,6 +672,46 @@ def test_assertion_voids
end
end

def test_assertion__skip
with_factory do |factory|
source = Steep::Source.parse(<<~RUBY, path: Pathname("foo.rb"), factory: factory)
attr_reader :foo #: String
attr_accessor :bar #: String
attr_writer :baz #: String
RUBY

source.node.children[0].tap do |node|
assert_equal :send, node.type
end
source.node.children[1].tap do |node|
assert_equal :send, node.type
end
source.node.children[2].tap do |node|
assert_equal :send, node.type
end
end
end

def test_assertion__no_skip
with_factory do |factory|
source = Steep::Source.parse(<<~RUBY, path: Pathname("foo.rb"), factory: factory)
(attr_reader :foo) #: String
(attr_accessor :bar) #: String
(attr_writer :baz) #: String
RUBY

source.node.children[0].tap do |node|
assert_equal :assertion, node.type
end
source.node.children[1].tap do |node|
assert_equal :assertion, node.type
end
source.node.children[2].tap do |node|
assert_equal :assertion, node.type
end
end
end

def test_tapp_send
with_factory({ Pathname("foo.rbs") => <<-RBS }) do |factory|
class Tapp
Expand Down