Skip to content

Commit

Permalink
Merge pull request #1176 from soutaro/ignore-attributes
Browse files Browse the repository at this point in the history
Ignore `#:` annotation on `attr_*` calls
  • Loading branch information
soutaro authored Jun 21, 2024
2 parents 31d9dce + 0346255 commit ab8cc91
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
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

0 comments on commit ab8cc91

Please sign in to comment.