Skip to content

Commit b8bebe0

Browse files
committed
Fix up call chaining with new folded nodes
1 parent e0ce4ff commit b8bebe0

File tree

3 files changed

+68
-34
lines changed

3 files changed

+68
-34
lines changed

lib/syntax_tree/node.rb

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ class Alias < Node
359359
# Formats an argument to the alias keyword. For symbol literals it uses the
360360
# value of the symbol directly to look like bare words.
361361
class AliasArgumentFormatter
362-
# [Backref | DynaSymbol | GVar | SymbolLiteral] the argument being passed to alias
362+
# [Backref | DynaSymbol | GVar | SymbolLiteral] the argument being passed
363+
# to alias
363364
attr_reader :argument
364365

365366
def initialize(argument)
@@ -2245,14 +2246,26 @@ def format(q)
22452246
when Call
22462247
case (receiver = child.receiver)
22472248
when Call
2248-
children << receiver
2249+
if receiver.receiver.nil?
2250+
break
2251+
else
2252+
children << receiver
2253+
end
22492254
when MethodAddBlock
2250-
receiver.call.is_a?(Call) ? children << receiver : break
2255+
if receiver.call.is_a?(Call) && !receiver.call.receiver.nil?
2256+
children << receiver
2257+
else
2258+
break
2259+
end
22512260
else
22522261
break
22532262
end
22542263
when MethodAddBlock
2255-
child.call.is_a?(Call) ? children << child.call : break
2264+
if child.call.is_a?(Call) && !child.call.receiver.nil?
2265+
children << child.call
2266+
else
2267+
break
2268+
end
22562269
else
22572270
break
22582271
end
@@ -2271,7 +2284,8 @@ def format(q)
22712284
# of just Statements nodes.
22722285
parent = parents[3] if parent.is_a?(Block) && parent.keywords?
22732286

2274-
if parent.is_a?(MethodAddBlock) && parent.call.is_a?(Call) && parent.call.message.value == "sig"
2287+
if parent.is_a?(MethodAddBlock) && parent.call.is_a?(Call) &&
2288+
parent.call.message.value == "sig"
22752289
threshold = 2
22762290
end
22772291
end
@@ -2300,7 +2314,7 @@ def format_chain(q, children)
23002314
# formatter so it's as if we had descending normally into them. This is
23012315
# necessary so they can check their parents as normal.
23022316
q.stack.concat(children)
2303-
q.format(children.last.receiver)
2317+
q.format(children.last.receiver) if children.last.receiver
23042318

23052319
q.group do
23062320
if attach_directly?(children.last)
@@ -2343,7 +2357,8 @@ def format_chain(q, children)
23432357
# If the parent call node has a comment on the message then we need
23442358
# to print the operator trailing in order to keep it working.
23452359
last_child = children.last
2346-
if last_child.is_a?(Call) && last_child.message.comments.any?
2360+
if last_child.is_a?(Call) && last_child.message.comments.any? &&
2361+
last_child.operator
23472362
q.format(CallOperatorFormatter.new(last_child.operator))
23482363
skip_operator = true
23492364
else
@@ -2372,9 +2387,9 @@ def self.chained?(node)
23722387

23732388
case node
23742389
when Call
2375-
true
2390+
!node.receiver.nil?
23762391
when MethodAddBlock
2377-
node.call.is_a?(Call)
2392+
node.call.is_a?(Call) && !node.call.receiver.nil?
23782393
else
23792394
false
23802395
end
@@ -2405,7 +2420,7 @@ def format_child(
24052420
case child
24062421
when Call
24072422
q.group do
2408-
unless skip_operator
2423+
if !skip_operator && child.operator
24092424
q.format(CallOperatorFormatter.new(child.operator))
24102425
end
24112426
q.format(child.message) if child.message != :call
@@ -2495,7 +2510,8 @@ def format(q)
24952510
# If we're at the top of a call chain, then we're going to do some
24962511
# specialized printing in case we can print it nicely. We _only_ do this
24972512
# at the top of the chain to avoid weird recursion issues.
2498-
if CallChainFormatter.chained?(receiver) && !CallChainFormatter.chained?(q.parent)
2513+
if CallChainFormatter.chained?(receiver) &&
2514+
!CallChainFormatter.chained?(q.parent)
24992515
q.group do
25002516
q
25012517
.if_break { CallChainFormatter.new(self).format(q) }
@@ -2507,11 +2523,13 @@ def format(q)
25072523
else
25082524
q.format(message)
25092525

2510-
if arguments.is_a?(ArgParen) && arguments.arguments.nil? && !message.is_a?(Const)
2511-
# If you're using an explicit set of parentheses on something that looks
2512-
# like a constant, then we need to match that in order to maintain valid
2513-
# Ruby. For example, you could do something like Foo(), on which we
2514-
# would need to keep the parentheses to make it look like a method call.
2526+
if arguments.is_a?(ArgParen) && arguments.arguments.nil? &&
2527+
!message.is_a?(Const)
2528+
# If you're using an explicit set of parentheses on something that
2529+
# looks like a constant, then we need to match that in order to
2530+
# maintain valid Ruby. For example, you could do something like Foo(),
2531+
# on which we would need to keep the parentheses to make it look like
2532+
# a method call.
25152533
else
25162534
q.format(arguments)
25172535
end
@@ -3726,7 +3744,13 @@ def child_nodes
37263744
alias deconstruct child_nodes
37273745

37283746
def deconstruct_keys(_keys)
3729-
{ left: left, operator: operator, right: right, location: location, comments: comments }
3747+
{
3748+
left: left,
3749+
operator: operator,
3750+
right: right,
3751+
location: location,
3752+
comments: comments
3753+
}
37303754
end
37313755

37323756
def format(q)
@@ -5133,7 +5157,11 @@ def format(q)
51335157
if ContainsAssignment.call(statement) || q.parent.is_a?(In)
51345158
q.group { format_flat(q) }
51355159
else
5136-
q.group { q.if_break { format_break(q, force: false) }.if_flat { format_flat(q) } }
5160+
q.group do
5161+
q
5162+
.if_break { format_break(q, force: false) }
5163+
.if_flat { format_flat(q) }
5164+
end
51375165
end
51385166
else
51395167
# If we can transform this node into a ternary, then we're going to
@@ -9063,7 +9091,8 @@ def format(q)
90639091
#
90649092
# foo = bar while foo
90659093
#
9066-
if node.modifier? && (statement = node.statements.body.first) && (statement.is_a?(Begin) || ContainsAssignment.call(statement))
9094+
if node.modifier? && (statement = node.statements.body.first) &&
9095+
(statement.is_a?(Begin) || ContainsAssignment.call(statement))
90679096
q.format(statement)
90689097
q.text(" #{keyword} ")
90699098
q.format(node.predicate)
@@ -9466,9 +9495,7 @@ def format(q)
94669495
# last argument to the predicate is and endless range, then you are
94679496
# forced to use the "then" keyword to make it parse properly.
94689497
last = arguments.parts.last
9469-
if last.is_a?(RangeLiteral) && !last.right
9470-
q.text(" then")
9471-
end
9498+
q.text(" then") if last.is_a?(RangeLiteral) && !last.right
94729499
end
94739500
end
94749501

lib/syntax_tree/parser.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,13 @@ def on_excessed_comma(*)
16161616
# :call-seq:
16171617
# on_fcall: ((Const | Ident) value) -> Call
16181618
def on_fcall(value)
1619-
Call.new(receiver: nil, operator: nil, message: value, arguments: nil, location: value.location)
1619+
Call.new(
1620+
receiver: nil,
1621+
operator: nil,
1622+
message: value,
1623+
arguments: nil,
1624+
location: value.location
1625+
)
16201626
end
16211627

16221628
# :call-seq:
@@ -1923,7 +1929,8 @@ def on_if_mod(predicate, statement)
19231929

19241930
If.new(
19251931
predicate: predicate,
1926-
statements: Statements.new(self, body: [statement], location: statement.location),
1932+
statements:
1933+
Statements.new(self, body: [statement], location: statement.location),
19271934
consequent: nil,
19281935
location: statement.location.to(predicate.location)
19291936
)
@@ -2209,7 +2216,8 @@ def lambda_locals(source)
22092216
on_comma: :item,
22102217
on_rparen: :final
22112218
},
2212-
final: {}
2219+
final: {
2220+
}
22132221
}
22142222

22152223
tokens[(index + 1)..].each_with_object([]) do |token, locals|
@@ -3622,7 +3630,8 @@ def on_unless_mod(predicate, statement)
36223630

36233631
Unless.new(
36243632
predicate: predicate,
3625-
statements: Statements.new(self, body: [statement], location: statement.location),
3633+
statements:
3634+
Statements.new(self, body: [statement], location: statement.location),
36263635
consequent: nil,
36273636
location: statement.location.to(predicate.location)
36283637
)
@@ -3665,7 +3674,8 @@ def on_until_mod(predicate, statement)
36653674

36663675
Until.new(
36673676
predicate: predicate,
3668-
statements: Statements.new(self, body: [statement], location: statement.location),
3677+
statements:
3678+
Statements.new(self, body: [statement], location: statement.location),
36693679
location: statement.location.to(predicate.location)
36703680
)
36713681
end
@@ -3791,7 +3801,8 @@ def on_while_mod(predicate, statement)
37913801

37923802
While.new(
37933803
predicate: predicate,
3794-
statements: Statements.new(self, body: [statement], location: statement.location),
3804+
statements:
3805+
Statements.new(self, body: [statement], location: statement.location),
37953806
location: statement.location.to(predicate.location)
37963807
)
37973808
end

lib/syntax_tree/visitor/field_visitor.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,7 @@ def visit_rest_param(node)
779779
end
780780

781781
def visit_retry(node)
782-
node(node, "retry") do
783-
comments(node)
784-
end
782+
node(node, "retry") { comments(node) }
785783
end
786784

787785
def visit_return(node)
@@ -1021,9 +1019,7 @@ def visit_yield(node)
10211019
end
10221020

10231021
def visit_zsuper(node)
1024-
node(node, "zsuper") do
1025-
comments(node)
1026-
end
1022+
node(node, "zsuper") { comments(node) }
10271023
end
10281024

10291025
def visit___end__(node)

0 commit comments

Comments
 (0)