ruby/test/prism/result/implicit_array_test.rb
Kevin Newton 0321f2c8fe [ruby/prism] Handle implicit array precedence
When an implicit array is used in a write, is causes the whole
expression to become a statement. For example:

```ruby
a = *b
a = 1, 2, 3
```

Even though these expressions are exactly equivalent to their
explicit array counterparts:

```ruby
a = [*b]
a = [1, 2, 3]
```

As such, these expressions cannot be joined with other expressions
by operators or modifiers except if, unless, while, until, or
rescue.

7cd2407272
2024-06-13 18:46:02 +00:00

59 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require_relative "../test_helper"
module Prism
class ImplicitArrayTest < TestCase
def test_call_node
assert_implicit_array("a.a = *b")
assert_implicit_array("a.a = 1, 2, 3")
assert_implicit_array("a[a] = *b")
assert_implicit_array("a[a] = 1, 2, 3")
end
def test_class_variable_write_node
assert_implicit_array("@@a = *b")
assert_implicit_array("@@a = 1, 2, 3")
end
def test_constant_path_write_node
assert_implicit_array("A::A = *b")
assert_implicit_array("A::A = 1, 2, 3")
end
def test_constant_write_node
assert_implicit_array("A = *b")
assert_implicit_array("A = 1, 2, 3")
end
def test_global_variable_write_node
assert_implicit_array("$a = *b")
assert_implicit_array("$a = 1, 2, 3")
end
def test_instance_variable_write_node
assert_implicit_array("@a = *b")
assert_implicit_array("@a = 1, 2, 3")
end
def test_local_variable_write_node
assert_implicit_array("a = *b")
assert_implicit_array("a = 1, 2, 3")
end
def test_multi_write_node
assert_implicit_array("a, b, c = *b")
assert_implicit_array("a, b, c = 1, 2, 3")
end
private
def assert_implicit_array(source)
assert Prism.parse_success?(source)
assert Prism.parse_failure?("if #{source} then end")
assert_valid_syntax(source)
refute_valid_syntax("if #{source} then end")
end
end
end