Skip to content

Commit

Permalink
Fix trailing comma in implicit tuples in destructuring
Browse files Browse the repository at this point in the history
This patch fixes the usage of trailing commas in implicit tuples when
used in destructuring assignment, e.g. `x, = z`. In this context the
trailing comma is needed to preserve the tuple node, which is different
from e.g. implicit tuples in `do`-blocks. A trailing comma is allowed
(e.g. preserved from the source) for multi-item implicit tuples so that
e.g. `x, y, = z` can be used to signal that z contains more than two
items. Closes #58.
  • Loading branch information
fredrikekre committed Sep 9, 2024
1 parent 150ced9 commit 959fccc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
if opening_leaf_idx === nothing
# Implicit tuple without (), for example arguments in a do-block
implicit_tuple = true
@assert kind(node) === K"tuple"
opening_leaf_idx = findfirst(!JuliaSyntax.is_whitespace, kids)
if opening_leaf_idx === nothing
# All whitespace... return?
Expand Down Expand Up @@ -366,7 +367,13 @@ function spaces_in_listlike(ctx::Context, node::Node)
if kind(node) in KSet"call dotcall macrocall"
require_trailing_comma = false
elseif implicit_tuple
require_trailing_comma = false
# Trailing commas in implicit tuples in the LHS of an assignment, e.g. `x, = 1, 2`,
# is required for single item tuples and allowed for multiple items (to allow e.g.
# `x, y, = z` signaling that z contains more items).
# Note that an implicit single item tuple can never(?) end up on the RHS so there is
# no need to make sure this is the LHS node.
require_trailing_comma = n_items == 1 && ctx.lineage_kinds[end] === K"="
allow_trailing_comma = ctx.lineage_kinds[end] === K"="
elseif kind(node) === K"tuple" && n_items == 1 && ctx.lineage_kinds[end] !== K"function" &&
kind(kids[first_item_idx::Int]) !== K"parameters"
# TODO: May also have to check for K"where" and K"::" in the lineage above
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@ end
# implicit tuple
@test format_string("a,\n$(sp)b") == "a,\n b"
@test format_string("a,\n$(sp)b + \nb") == "a,\n b +\n b"
# implicit tuple in destructuring (LHS of K"=")
@test format_string("a,$(sp)=$(sp)z") == "a, = z"
@test format_string("a,$(sp)b$(sp)=$(sp)z") == "a, b = z"
@test format_string("a,$(sp)b$(sp),$(sp)=$(sp)z") == "a, b, = z"
# K"cartesian_iterator"
@test format_string("for i in I,\n$(sp)j in J\n# body\nend") ==
"for i in I,\n j in J\n # body\nend"
Expand Down

0 comments on commit 959fccc

Please sign in to comment.