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

Commit 872bc15

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

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

lib/syntax_tree/node.rb

Lines changed: 32 additions & 17 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)
@@ -2271,7 +2272,8 @@ def format(q)
22712272
# of just Statements nodes.
22722273
parent = parents[3] if parent.is_a?(Block) && parent.keywords?
22732274

2274-
if parent.is_a?(MethodAddBlock) && parent.call.is_a?(Call) && parent.call.message.value == "sig"
2275+
if parent.is_a?(MethodAddBlock) && parent.call.is_a?(Call) &&
2276+
parent.call.message.value == "sig"
22752277
threshold = 2
22762278
end
22772279
end
@@ -2300,7 +2302,7 @@ def format_chain(q, children)
23002302
# formatter so it's as if we had descending normally into them. This is
23012303
# necessary so they can check their parents as normal.
23022304
q.stack.concat(children)
2303-
q.format(children.last.receiver)
2305+
q.format(children.last.receiver) if children.last.receiver
23042306

23052307
q.group do
23062308
if attach_directly?(children.last)
@@ -2343,7 +2345,8 @@ def format_chain(q, children)
23432345
# If the parent call node has a comment on the message then we need
23442346
# to print the operator trailing in order to keep it working.
23452347
last_child = children.last
2346-
if last_child.is_a?(Call) && last_child.message.comments.any?
2348+
if last_child.is_a?(Call) && last_child.message.comments.any? &&
2349+
last_child.operator
23472350
q.format(CallOperatorFormatter.new(last_child.operator))
23482351
skip_operator = true
23492352
else
@@ -2405,7 +2408,7 @@ def format_child(
24052408
case child
24062409
when Call
24072410
q.group do
2408-
unless skip_operator
2411+
if !skip_operator && child.operator
24092412
q.format(CallOperatorFormatter.new(child.operator))
24102413
end
24112414
q.format(child.message) if child.message != :call
@@ -2495,7 +2498,8 @@ def format(q)
24952498
# If we're at the top of a call chain, then we're going to do some
24962499
# specialized printing in case we can print it nicely. We _only_ do this
24972500
# at the top of the chain to avoid weird recursion issues.
2498-
if CallChainFormatter.chained?(receiver) && !CallChainFormatter.chained?(q.parent)
2501+
if CallChainFormatter.chained?(receiver) &&
2502+
!CallChainFormatter.chained?(q.parent)
24992503
q.group do
25002504
q
25012505
.if_break { CallChainFormatter.new(self).format(q) }
@@ -2507,11 +2511,13 @@ def format(q)
25072511
else
25082512
q.format(message)
25092513

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.
2514+
if arguments.is_a?(ArgParen) && arguments.arguments.nil? &&
2515+
!message.is_a?(Const)
2516+
# If you're using an explicit set of parentheses on something that
2517+
# looks like a constant, then we need to match that in order to
2518+
# maintain valid Ruby. For example, you could do something like Foo(),
2519+
# on which we would need to keep the parentheses to make it look like
2520+
# a method call.
25152521
else
25162522
q.format(arguments)
25172523
end
@@ -3726,7 +3732,13 @@ def child_nodes
37263732
alias deconstruct child_nodes
37273733

37283734
def deconstruct_keys(_keys)
3729-
{ left: left, operator: operator, right: right, location: location, comments: comments }
3735+
{
3736+
left: left,
3737+
operator: operator,
3738+
right: right,
3739+
location: location,
3740+
comments: comments
3741+
}
37303742
end
37313743

37323744
def format(q)
@@ -5133,7 +5145,11 @@ def format(q)
51335145
if ContainsAssignment.call(statement) || q.parent.is_a?(In)
51345146
q.group { format_flat(q) }
51355147
else
5136-
q.group { q.if_break { format_break(q, force: false) }.if_flat { format_flat(q) } }
5148+
q.group do
5149+
q
5150+
.if_break { format_break(q, force: false) }
5151+
.if_flat { format_flat(q) }
5152+
end
51375153
end
51385154
else
51395155
# If we can transform this node into a ternary, then we're going to
@@ -9063,7 +9079,8 @@ def format(q)
90639079
#
90649080
# foo = bar while foo
90659081
#
9066-
if node.modifier? && (statement = node.statements.body.first) && (statement.is_a?(Begin) || ContainsAssignment.call(statement))
9082+
if node.modifier? && (statement = node.statements.body.first) &&
9083+
(statement.is_a?(Begin) || ContainsAssignment.call(statement))
90679084
q.format(statement)
90689085
q.text(" #{keyword} ")
90699086
q.format(node.predicate)
@@ -9466,9 +9483,7 @@ def format(q)
94669483
# last argument to the predicate is and endless range, then you are
94679484
# forced to use the "then" keyword to make it parse properly.
94689485
last = arguments.parts.last
9469-
if last.is_a?(RangeLiteral) && !last.right
9470-
q.text(" then")
9471-
end
9486+
q.text(" then") if last.is_a?(RangeLiteral) && !last.right
94729487
end
94739488
end
94749489

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)