Skip to content

Commit 4d348c5

Browse files
committed
Handle attr with true argument for writer (#3579)
### Motivation As pointed out in #3544 (comment), `attr` actually accepts a boolean argument to determine if a writer is defined or not. ### Implementation Started handling the argument in a separate branch for `attr`. ### Automated Tests Improved our existing tests.
1 parent b911923 commit 4d348c5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,15 @@ def on_call_node_enter(node)
254254
case message
255255
when :private_constant
256256
handle_private_constant(node)
257-
when :attr_reader, :attr
257+
when :attr_reader
258258
handle_attribute(node, reader: true, writer: false)
259259
when :attr_writer
260260
handle_attribute(node, reader: false, writer: true)
261261
when :attr_accessor
262262
handle_attribute(node, reader: true, writer: true)
263+
when :attr
264+
has_writer = node.arguments&.arguments&.last&.is_a?(Prism::TrueNode) || false
265+
handle_attribute(node, reader: true, writer: has_writer)
263266
when :alias_method
264267
handle_alias_method(node)
265268
when :include, :prepend, :extend

lib/ruby_indexer/test/method_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,17 @@ def test_handling_attr
954954
index(<<~RUBY)
955955
class Foo
956956
attr :bar
957+
attr :baz, true
958+
attr :qux, false
957959
end
958960
RUBY
959961

960962
assert_entry("bar", Entry::Accessor, "/fake/path/foo.rb:1-8:1-11")
963+
assert_no_entry("bar=")
964+
assert_entry("baz", Entry::Accessor, "/fake/path/foo.rb:2-8:2-11")
965+
assert_entry("baz=", Entry::Accessor, "/fake/path/foo.rb:2-8:2-11")
966+
assert_entry("qux", Entry::Accessor, "/fake/path/foo.rb:3-8:3-11")
967+
assert_no_entry("qux=")
961968
end
962969

963970
private

0 commit comments

Comments
 (0)