diff options
author | Kevin Newton <kddnewton@gmail.com> | 2023-10-17 11:15:56 -0400 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-10-18 14:23:26 +0000 |
commit | 2a6f7cd9255b61e21cead0c761a513bde2899728 (patch) | |
tree | 7d8f270506d907f970973cdb576513e7d3b62b93 /test/prism/location_test.rb | |
parent | ef3f9f1a685effb51543d1f08831692fa68863a2 (diff) | |
download | ruby-2a6f7cd9255b61e21cead0c761a513bde2899728.tar.gz |
[ruby/prism] Index{Operator,And,Or}WriteNode
Right now, our Call{Operator,And,Or}WriteNode nodes represent two
different concepts:
```ruby
foo.bar += 1
foo[bar] += 1
```
These two statements are different in what they can support. The
former can never have arguments (or an opening_loc or closing_loc).
The former can also never have a block. Also, the former is a
variable method name.
The latter is always going to be []/[]=, it can have any number of
arguments including blocks (`foo[&bar] ||= 1`), and will always
have an opening_loc and closing_loc.
Furthermore, these statements end of having to take different paths
through the various compilers because with the latter you have to
consider the arguments and the block, whereas the former can
perform some additional peephole optimizations since there are
fewer values on the stack.
For these reasons, I'm introducing Index{Operator,And,Or}WriteNode.
These nodes never have a read_name or write_name on them because
they are always []/[]=. They also support blocks, which the previous
write nodes didn't. As a benefit of introducing these nodes, I've
removed the opening_loc, closing_loc, and arguments from the older
write nodes because they will always be null.
For the serialized format, both of these nodes end up being
smaller, and for in-memory we're storing fewer things in general,
so we have savings all around.
I don't love that we are introducing another node that is a call
node since we generally want consumers to only have to handle a
single call, but these nodes are so specific that they would have
to be handled separately anyway since in fact call 2 methods.
https://github.com/ruby/prism/commit/70155db9cd
Diffstat (limited to 'test/prism/location_test.rb')
-rw-r--r-- | test/prism/location_test.rb | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/test/prism/location_test.rb b/test/prism/location_test.rb index 7242bdeca5..25af1f7618 100644 --- a/test/prism/location_test.rb +++ b/test/prism/location_test.rb @@ -178,17 +178,14 @@ module Prism def test_CallAndWriteNode assert_location(CallAndWriteNode, "foo.foo &&= bar") - assert_location(CallAndWriteNode, "foo[foo] &&= bar") end def test_CallOperatorWriteNode assert_location(CallOperatorWriteNode, "foo.foo += bar") - assert_location(CallOperatorWriteNode, "foo[foo] += bar") end def test_CallOrWriteNode assert_location(CallOrWriteNode, "foo.foo ||= bar") - assert_location(CallOrWriteNode, "foo[foo] ||= bar") end def test_CapturePatternNode @@ -430,6 +427,18 @@ module Prism end end + def test_IndexAndWriteNode + assert_location(IndexAndWriteNode, "foo[foo] &&= bar") + end + + def test_IndexOperatorWriteNode + assert_location(IndexOperatorWriteNode, "foo[foo] += bar") + end + + def test_IndexOrWriteNode + assert_location(IndexOrWriteNode, "foo[foo] ||= bar") + end + def test_InstanceVariableAndWriteNode assert_location(InstanceVariableAndWriteNode, "@foo &&= bar") end |