Skip to content

Commit

Permalink
[ruby/prism] Flag for attribute write on calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Dec 12, 2023
1 parent 2f1d6da commit 278ce27
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 38 deletions.
2 changes: 2 additions & 0 deletions prism/config.yml
Expand Up @@ -345,6 +345,8 @@ flags:
comment: "&. operator"
- name: VARIABLE_CALL
comment: "a call that could have been a local variable"
- name: ATTRIBUTE_WRITE
comment: "a call that is an attribute write, so the value being written should be returned"
comment: Flags for call nodes.
- name: EncodingFlags
values:
Expand Down
2 changes: 2 additions & 0 deletions prism/prism.c
Expand Up @@ -10856,6 +10856,7 @@ parse_write(pm_parser_t *parser, pm_node_t *target, pm_token_t *operator, pm_nod
call->base.location.end = arguments->base.location.end;

parse_write_name(parser, &call->name);
call->base.flags |= PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE;
return (pm_node_t *) call;
}
}
Expand All @@ -10873,6 +10874,7 @@ parse_write(pm_parser_t *parser, pm_node_t *target, pm_token_t *operator, pm_nod

// Replace the name with "[]=".
call->name = pm_parser_constant_id_constant(parser, "[]=", 3);
call->base.flags |= PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE;
return target;
}

Expand Down
60 changes: 60 additions & 0 deletions test/prism/attribute_write_test.rb
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require_relative "test_helper"

module Prism
class AttributeWriteTest < TestCase
module Target
def self.value
2
end

def self.value=(value)
2
end

def self.[]=(index, value)
2
end
end

def test_named_call_with_operator
assert_attribute_write("Target.value = 1")
end

def test_named_call_without_operator
assert_attribute_write("Target.value=(1)")
end

def test_indexed_call_with_operator
assert_attribute_write("Target[0] = 1")
end

def test_indexed_call_without_operator
refute_attribute_write("Target.[]=(0, 1)")
end

def test_comparison_operators
refute_attribute_write("Target.value == 1")
refute_attribute_write("Target.value === 1")
end

private

def parse(source)
Prism.parse(source).value.statements.body.first
end

def assert_attribute_write(source)
call = parse(source)
assert(call.attribute_write?)
assert_equal(1, eval(source))
end

def refute_attribute_write(source)
call = parse(source)
refute(call.attribute_write?)
refute_equal(1, eval(source))
end
end
end
20 changes: 10 additions & 10 deletions test/prism/snapshots/arrays.txt
Expand Up @@ -22,7 +22,7 @@
│ ├── opening_loc: (1,0)-(1,1) = "["
│ └── closing_loc: (1,3)-(1,4) = "]"
├── @ CallNode (location: (3,0)-(3,23))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (3,0)-(3,3))
│ │ ├── flags: variable_call
Expand Down Expand Up @@ -205,7 +205,7 @@
│ ├── opening_loc: (28,0)-(28,1) = "["
│ └── closing_loc: (28,11)-(28,12) = "]"
├── @ CallNode (location: (30,0)-(30,19))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (30,0)-(30,8))
│ │ ├── flags: ∅
Expand Down Expand Up @@ -375,7 +375,7 @@
│ ├── closing_loc: (37,12)-(37,13) = "]"
│ └── block: ∅
├── @ CallNode (location: (39,0)-(39,19))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (39,0)-(39,3))
│ │ ├── flags: variable_call
Expand Down Expand Up @@ -510,7 +510,7 @@
│ │ ├── flags: ∅
│ │ └── arguments: (length: 1)
│ │ └── @ CallNode (location: (43,4)-(43,18))
│ │ ├── flags:
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (43,4)-(43,7))
│ │ │ ├── flags: variable_call
Expand Down Expand Up @@ -588,7 +588,7 @@
│ ├── closing_loc: (45,7)-(45,8) = "]"
│ └── block: ∅
├── @ CallNode (location: (47,0)-(47,14))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (47,0)-(47,3))
│ │ ├── flags: variable_call
Expand Down Expand Up @@ -932,7 +932,7 @@
│ ├── opening_loc: (82,0)-(82,3) = "%w["
│ └── closing_loc: (82,6)-(82,7) = "]"
├── @ CallNode (location: (84,0)-(84,13))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (84,0)-(84,3))
│ │ ├── flags: variable_call
Expand Down Expand Up @@ -970,7 +970,7 @@
│ │ └── block: ∅
│ └── operator_loc: (84,4)-(84,5) = "&"
├── @ CallNode (location: (86,0)-(86,17))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (86,0)-(86,7))
│ │ ├── flags: ∅
Expand Down Expand Up @@ -1038,7 +1038,7 @@
│ │ @ StatementsNode (location: (89,2)-(89,12))
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (89,2)-(89,12))
│ │ ├── flags:
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (89,2)-(89,5))
│ │ │ ├── flags: variable_call
Expand Down Expand Up @@ -1930,7 +1930,7 @@
│ │ @ StatementsNode (location: (132,10)-(132,18))
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (132,10)-(132,18))
│ │ ├── flags:
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (132,10)-(132,11))
│ │ │ ├── flags: variable_call
Expand Down Expand Up @@ -1986,7 +1986,7 @@
│ │ @ StatementsNode (location: (134,10)-(134,21))
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (134,10)-(134,21))
│ │ ├── flags:
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (134,10)-(134,11))
│ │ │ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/method_calls.txt
Expand Up @@ -238,7 +238,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (21,0)-(21,11))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (21,0)-(21,3))
│ │ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/attr_asgn_colon_id.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,8))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,8))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ ConstantReadNode (location: (1,0)-(1,1))
│ └── name: :A
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/attrasgn_array_arg.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,13))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,13))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/attrasgn_array_lhs.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,42))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,42))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ ArrayNode (location: (1,0)-(1,12))
│ ├── flags: ∅
Expand Down
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,7))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,7))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(3,8))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(3,8))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/index_0.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,7))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,7))
├── flags:
├── flags: attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/safe_attrasgn.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,8))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,8))
├── flags: safe_navigation
├── flags: safe_navigation, attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/safe_attrasgn_constant.txt
Expand Up @@ -4,7 +4,7 @@
@ StatementsNode (location: (1,0)-(1,8))
└── body: (length: 1)
└── @ CallNode (location: (1,0)-(1,8))
├── flags: safe_navigation
├── flags: safe_navigation, attribute_write
├── receiver:
│ @ CallNode (location: (1,0)-(1,1))
│ ├── flags: variable_call
Expand Down
18 changes: 9 additions & 9 deletions test/prism/snapshots/unparser/corpus/literal/assignment.txt
Expand Up @@ -596,7 +596,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (29,0)-(29,19))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (29,0)-(29,3))
│ │ ├── name: :foo
Expand Down Expand Up @@ -635,7 +635,7 @@
│ ├── closing_loc: (29,10)-(29,11) = "]"
│ └── block: ∅
├── @ CallNode (location: (30,0)-(30,17))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (30,0)-(30,3))
│ │ ├── name: :foo
Expand Down Expand Up @@ -670,7 +670,7 @@
│ ├── closing_loc: (30,8)-(30,9) = "]"
│ └── block: ∅
├── @ CallNode (location: (31,0)-(31,9))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (31,0)-(31,3))
│ │ ├── name: :foo
Expand All @@ -688,7 +688,7 @@
│ ├── closing_loc: (31,4)-(31,5) = "]"
│ └── block: ∅
├── @ CallNode (location: (32,0)-(32,17))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (32,0)-(32,3))
│ │ ├── name: :foo
Expand Down Expand Up @@ -720,7 +720,7 @@
│ ├── closing_loc: (32,8)-(32,9) = "]"
│ └── block: ∅
├── @ CallNode (location: (33,0)-(33,18))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (33,0)-(33,3))
│ │ ├── name: :foo
Expand Down Expand Up @@ -768,7 +768,7 @@
│ │ └── unescaped: ""
│ └── operator_loc: (34,2)-(34,3) = "="
├── @ CallNode (location: (35,0)-(35,7))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (35,0)-(35,1))
│ │ ├── name: :x
Expand All @@ -790,7 +790,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (36,0)-(36,12))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (36,0)-(36,1))
│ │ ├── name: :x
Expand Down Expand Up @@ -891,7 +891,7 @@
│ │ └── closing_loc: (41,0)-(42,0) = "HEREDOC\n"
│ └── operator_loc: (39,2)-(39,3) = "="
├── @ CallNode (location: (42,0)-(42,14))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (42,0)-(42,1))
│ │ ├── name: :x
Expand Down Expand Up @@ -927,7 +927,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (45,0)-(45,16))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ LocalVariableReadNode (location: (45,0)-(45,1))
│ │ ├── name: :x
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/unparser/corpus/literal/case.txt
Expand Up @@ -404,7 +404,7 @@
│ │ ├── operator_loc: (36,5)-(36,6) = "*"
│ │ └── expression:
│ │ @ CallNode (location: (36,6)-(36,15))
│ │ ├── flags:
│ │ ├── flags: attribute_write
│ │ ├── receiver:
│ │ │ @ CallNode (location: (36,6)-(36,9))
│ │ │ ├── flags: variable_call
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/unparser/corpus/literal/opasgn.txt
Expand Up @@ -97,7 +97,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (9,0)-(9,17))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ ParenthesesNode (location: (9,0)-(9,10))
│ │ ├── body:
Expand Down
4 changes: 2 additions & 2 deletions test/prism/snapshots/unparser/corpus/literal/send.txt
Expand Up @@ -1529,7 +1529,7 @@
│ ├── closing_loc: (68,25)-(68,26) = ")"
│ └── block: ∅
├── @ CallNode (location: (69,0)-(69,12))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ CallNode (location: (69,0)-(69,3))
│ │ ├── flags: variable_call
Expand Down Expand Up @@ -1773,7 +1773,7 @@
│ ├── closing_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (77,0)-(77,13))
│ ├── flags:
│ ├── flags: attribute_write
│ ├── receiver:
│ │ @ SelfNode (location: (77,0)-(77,4))
│ ├── call_operator_loc: (77,4)-(77,5) = "."
Expand Down

0 comments on commit 278ce27

Please sign in to comment.