Skip to content
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

Fix trailing comma in implicit tuples in destructuring #59

Merged
merged 1 commit into from
Sep 9, 2024
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
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
Loading