Skip to content

Adding Features #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,12 @@ def format(q)
part = arguments.parts.first

if part.is_a?(Paren)
q.format(arguments)
if part.contents.body.length == 1 && skip_parens?(part.contents.body.first)
q.text(" ")
q.format(part.contents.body.first)
else
q.format(arguments)
end
elsif part.is_a?(ArrayLiteral)
q.text(" ")
q.format(arguments)
Expand All @@ -2091,6 +2096,17 @@ def format_arguments(q, opening, closing)
q.breakable("")
q.if_break { q.text(closing) }
end

def skip_parens?(node)
case node
in Int | FloatLiteral
true
in VarRef[value: GVar | IVar | CVar | Kw | Const]
true
else
false
end
end
end

# Break represents using the +break+ keyword.
Expand Down Expand Up @@ -4664,6 +4680,11 @@ def format(q)
return
end

if contains_conditional?
format_break(q, force: true)
return
end

if node.consequent || node.statements.empty?
q.group { format_break(q, force: true) }
else
Expand Down Expand Up @@ -4700,6 +4721,11 @@ def format_break(q, force:)
q.breakable(force: force)
q.text("end")
end

def contains_conditional?
node.statements.body.length == 1 &&
[If, IfMod, IfOp, Unless, UnlessMod].include?(node.statements.body.first.class)
end
end

# If represents the first clause in an +if+ chain.
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
if (foo += 1)
foo
end
%
if foo
a ? b : c
end
28 changes: 28 additions & 0 deletions test/fixtures/next.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,31 @@
foo
bar
)
%
next(1)
-
next 1
%
next(1.0)
-
next 1.0
%
next($a)
-
next $a
%
next(@@a)
-
next @@a
%
next(self)
-
next self
%
next(@a)
-
next @a
%
next(A)
-
next A
4 changes: 4 additions & 0 deletions test/fixtures/unless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
unless (foo += 1)
foo
end
%
unless foo
a ? b : c
end