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

Commit f537c00

Browse files
committed
Allow flow control operators to skip parens when they have single args
1 parent 36f37fe commit f537c00

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/syntax_tree/node.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,12 @@ def format(q)
20662066
part = arguments.parts.first
20672067

20682068
if part.is_a?(Paren)
2069-
q.format(arguments)
2069+
if part.contents.body.length == 1 && skip_parens?(part.contents.body.first)
2070+
q.text(" ")
2071+
q.format(part.contents.body.first)
2072+
else
2073+
q.format(arguments)
2074+
end
20702075
elsif part.is_a?(ArrayLiteral)
20712076
q.text(" ")
20722077
q.format(arguments)
@@ -2091,6 +2096,17 @@ def format_arguments(q, opening, closing)
20912096
q.breakable("")
20922097
q.if_break { q.text(closing) }
20932098
end
2099+
2100+
def skip_parens?(node)
2101+
case node
2102+
in Int | FloatLiteral
2103+
true
2104+
in VarRef[value: GVar | IVar | CVar | Kw | Const]
2105+
true
2106+
else
2107+
false
2108+
end
2109+
end
20942110
end
20952111

20962112
# Break represents using the +break+ keyword.

test/fixtures/next.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,31 @@
2525
foo
2626
bar
2727
)
28+
%
29+
next(1)
30+
-
31+
next 1
32+
%
33+
next(1.0)
34+
-
35+
next 1.0
36+
%
37+
next($a)
38+
-
39+
next $a
40+
%
41+
next(@@a)
42+
-
43+
next @@a
44+
%
45+
next(self)
46+
-
47+
next self
48+
%
49+
next(@a)
50+
-
51+
next @a
52+
%
53+
next(A)
54+
-
55+
next A

0 commit comments

Comments
 (0)