Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 93bf48f

Browse files
committed
The MethodAddArg node is removed in favor of an optional arguments field on Call and FCall.
1 parent 74655bb commit 93bf48f

File tree

3 files changed

+46
-92
lines changed

3 files changed

+46
-92
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
5454
### Removed
5555

5656
- The `AccessCtrl` node in favor of just formatting correctly when you hit a `Statements` node.
57+
- The `MethodAddArg` node is removed in favor of an optional `arguments` field on `Call` and `FCall`.
5758

5859
## [0.1.0] - 2021-11-16
5960

lib/syntax_tree.rb

Lines changed: 44 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,9 +3056,7 @@ def format(q)
30563056
end
30573057
end
30583058

3059-
# Call represents a method call. This node doesn't contain the arguments being
3060-
# passed (if arguments are passed, this node will get nested under a
3061-
# MethodAddArg node).
3059+
# Call represents a method call.
30623060
#
30633061
# receiver.message
30643062
#
@@ -3072,22 +3070,26 @@ class Call
30723070
# [:call | Backtick | Const | Ident | Op] the message being sent
30733071
attr_reader :message
30743072

3073+
# [nil | ArgParen | Args] the arguments to the method call
3074+
attr_reader :arguments
3075+
30753076
# [Location] the location of this node
30763077
attr_reader :location
30773078

30783079
# [Array[ Comment | EmbDoc ]] the comments attached to this node
30793080
attr_reader :comments
30803081

3081-
def initialize(receiver:, operator:, message:, location:, comments: [])
3082+
def initialize(receiver:, operator:, message:, arguments:, location:, comments: [])
30823083
@receiver = receiver
30833084
@operator = operator
30843085
@message = message
3086+
@arguments = arguments
30853087
@location = location
30863088
@comments = comments
30873089
end
30883090

30893091
def child_nodes
3090-
[receiver, (operator if operator != :"::"), (message if message != :call)]
3092+
[receiver, (operator if operator != :"::"), (message if message != :call), arguments]
30913093
end
30923094

30933095
def format(q)
@@ -3098,6 +3100,7 @@ def format(q)
30983100
q.breakable(force: true) if receiver.comments.any?
30993101
q.format(CallOperatorFormatter.new(operator))
31003102
q.format(message) if message != :call
3103+
q.format(arguments) if arguments
31013104
end
31023105
end
31033106
end
@@ -3116,6 +3119,11 @@ def pretty_print(q)
31163119
q.breakable
31173120
q.pp(message)
31183121

3122+
if arguments
3123+
q.breakable
3124+
q.pp(arguments)
3125+
end
3126+
31193127
q.pp(Comment::List.new(comments))
31203128
end
31213129
end
@@ -3126,6 +3134,7 @@ def to_json(*opts)
31263134
receiver: receiver,
31273135
op: operator,
31283136
message: message,
3137+
args: arguments,
31293138
loc: location,
31303139
cmts: comments
31313140
}.to_json(*opts)
@@ -3146,6 +3155,7 @@ def on_call(receiver, operator, message)
31463155
receiver: receiver,
31473156
operator: operator,
31483157
message: message,
3158+
arguments: nil,
31493159
location: receiver.location.to(ending.location)
31503160
)
31513161
end
@@ -5617,24 +5627,29 @@ class FCall
56175627
# [Const | Ident] the name of the method
56185628
attr_reader :value
56195629

5630+
# [nil | ArgParen | Args] the arguments to the method call
5631+
attr_reader :arguments
5632+
56205633
# [Location] the location of this node
56215634
attr_reader :location
56225635

56235636
# [Array[ Comment | EmbDoc ]] the comments attached to this node
56245637
attr_reader :comments
56255638

5626-
def initialize(value:, location:, comments: [])
5639+
def initialize(value:, arguments:, location:, comments: [])
56275640
@value = value
5641+
@arguments = arguments
56285642
@location = location
56295643
@comments = comments
56305644
end
56315645

56325646
def child_nodes
5633-
[value]
5647+
[value, arguments]
56345648
end
56355649

56365650
def format(q)
56375651
q.format(value)
5652+
q.format(arguments)
56385653
end
56395654

56405655
def pretty_print(q)
@@ -5644,12 +5659,17 @@ def pretty_print(q)
56445659
q.breakable
56455660
q.pp(value)
56465661

5662+
if arguments
5663+
q.breakable
5664+
q.pp(arguments)
5665+
end
5666+
56475667
q.pp(Comment::List.new(comments))
56485668
end
56495669
end
56505670

56515671
def to_json(*opts)
5652-
{ type: :fcall, value: value, loc: location, cmts: comments }.to_json(
5672+
{ type: :fcall, value: value, args: arguments, loc: location, cmts: comments }.to_json(
56535673
*opts
56545674
)
56555675
end
@@ -5658,7 +5678,7 @@ def to_json(*opts)
56585678
# :call-seq:
56595679
# on_fcall: ((Const | Ident) value) -> FCall
56605680
def on_fcall(value)
5661-
FCall.new(value: value, location: value.location)
5681+
FCall.new(value: value, arguments: nil, location: value.location)
56625682
end
56635683

56645684
# Field is always the child of an assignment. It represents assigning to a
@@ -7847,94 +7867,34 @@ def on_massign(target, value)
78477867
)
78487868
end
78497869

7850-
# MethodAddArg represents a method call with arguments and parentheses.
7851-
#
7852-
# method(argument)
7853-
#
7854-
# MethodAddArg can also represent with a method on an object, as in:
7855-
#
7856-
# object.method(argument)
7857-
#
7858-
# Finally, MethodAddArg can represent calling a method with no receiver that
7859-
# ends in a ?. In this case, the parser knows it's a method call and not a
7860-
# local variable, so it uses a MethodAddArg node as opposed to a VCall node,
7861-
# as in:
7862-
#
7863-
# method?
7864-
#
7865-
class MethodAddArg
7866-
# [Call | FCall] the method call
7867-
attr_reader :call
7868-
7869-
# [ArgParen | Args] the arguments to the method call
7870-
attr_reader :arguments
7871-
7872-
# [Location] the location of this node
7873-
attr_reader :location
7874-
7875-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
7876-
attr_reader :comments
7877-
7878-
def initialize(call:, arguments:, location:, comments: [])
7879-
@call = call
7880-
@arguments = arguments
7881-
@location = location
7882-
@comments = comments
7883-
end
7884-
7885-
def child_nodes
7886-
[call, arguments]
7887-
end
7888-
7889-
def format(q)
7890-
q.format(call)
7891-
q.text(" ") if !arguments.is_a?(ArgParen) && arguments.parts.any?
7892-
q.format(arguments)
7893-
end
7894-
7895-
def pretty_print(q)
7896-
q.group(2, "(", ")") do
7897-
q.text("method_add_arg")
7898-
7899-
q.breakable
7900-
q.pp(call)
7901-
7902-
q.breakable
7903-
q.pp(arguments)
7904-
7905-
q.pp(Comment::List.new(comments))
7906-
end
7907-
end
7908-
7909-
def to_json(*opts)
7910-
{
7911-
type: :method_add_arg,
7912-
call: call,
7913-
args: arguments,
7914-
loc: location,
7915-
cmts: comments
7916-
}.to_json(*opts)
7917-
end
7918-
end
7919-
79207870
# :call-seq:
79217871
# on_method_add_arg: (
79227872
# (Call | FCall) call,
79237873
# (ArgParen | Args) arguments
7924-
# ) -> MethodAddArg
7874+
# ) -> Call | FCall
79257875
def on_method_add_arg(call, arguments)
79267876
location = call.location
7927-
location = location.to(arguments.location) unless arguments.is_a?(Args)
7877+
location = location.to(arguments.location) if arguments.is_a?(ArgParen)
79287878

7929-
MethodAddArg.new(call: call, arguments: arguments, location: location)
7879+
if call.is_a?(FCall)
7880+
FCall.new(value: call.value, arguments: arguments, location: location)
7881+
else
7882+
Call.new(
7883+
receiver: call.receiver,
7884+
operator: call.operator,
7885+
message: call.message,
7886+
arguments: arguments,
7887+
location: location
7888+
)
7889+
end
79307890
end
79317891

79327892
# MethodAddBlock represents a method call with a block argument.
79337893
#
79347894
# method {}
79357895
#
79367896
class MethodAddBlock
7937-
# [Call | Command | CommandCall | FCall | MethodAddArg] the method call
7897+
# [Call | Command | CommandCall | FCall] the method call
79387898
attr_reader :call
79397899

79407900
# [BraceBlock | DoBlock] the block being sent with the method call
@@ -7989,7 +7949,7 @@ def to_json(*opts)
79897949

79907950
# :call-seq:
79917951
# on_method_add_block: (
7992-
# (Call | Command | CommandCall | FCall | MethodAddArg) call,
7952+
# (Call | Command | CommandCall | FCall) call,
79937953
# (BraceBlock | DoBlock) block
79947954
# ) -> MethodAddBlock
79957955
def on_method_add_block(call, block)

test/syntax_tree_test.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,7 @@ def test_excessed_comma
464464
end
465465

466466
def test_fcall
467-
source = "method(argument)"
468-
469-
at = location(chars: 0..6)
470-
assert_node(FCall, "fcall", source, at: at, &:call)
467+
assert_node(FCall, "fcall", "method(argument)")
471468
end
472469

473470
def test_field
@@ -634,10 +631,6 @@ def test_massign
634631
assert_node(MAssign, "massign", "first, second, third = value")
635632
end
636633

637-
def test_method_add_arg
638-
assert_node(MethodAddArg, "method_add_arg", "method(argument)")
639-
end
640-
641634
def test_method_add_block
642635
assert_node(MethodAddBlock, "method_add_block", "method {}")
643636
end

0 commit comments

Comments
 (0)