Skip to content
New issue

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

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

Already on GitHub? # to your account

toml: enforce final (oxford) comma in multiline arrays, remove from singleline arrays #625

Merged
merged 1 commit into from
Sep 14, 2023
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
11 changes: 11 additions & 0 deletions topiary/languages/toml.scm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
(comment)? @do_nothing
)

; remove trailing comma from last element of single line array
(array
("," @delete . "]")(#single_line_only!)
)

; add trailing comma if absent to last string of multiline array
(array
(((string) @append_delimiter) . ","* @do_nothing . (comment)? . "]")(#delimiter! ",")
(#multi_line_only!)
)

; Indent arrays. They will only be indented inmulti-line blocks.

(array
Expand Down
12 changes: 9 additions & 3 deletions topiary/tests/samples/expected/toml.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ hosts = [
# Multi-
# line comment
"alpha",
"omega"
"omega",
]

["zip"]
Expand All @@ -76,18 +76,24 @@ comment = "this cve is only valid with attacker-controlled flags to zip"
["unzip"]
cve = [
"CVE-2018-13410",
"CVE-2018-1340"
"CVE-2018-1340",
]
cve = [ "CVE-2018-13410", "foo", "CVE-2018-1340" ]

comment = "this cve is only valid with attacker-controlled flags to zip"

["inline.comments"]
singleline_remove_trailing_comma = [ "foo", "bar", "baz" ]
multiline_with_comments = [
"bar",
"blee",
"baz", # dont
"boo"
"boo", #do
]
multiline_comma_comment = [
"first",
"next",
"last", # best
]

[dog."tater.man"]
Expand Down
8 changes: 7 additions & 1 deletion topiary/tests/samples/input/toml.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ cve = ["CVE-2018-13410",
comment = "this cve is only valid with attacker-controlled flags to zip"

["inline.comments"]
singleline_remove_trailing_comma = [ "foo","bar","baz" , ]
multiline_with_comments = ["bar","blee",
"baz", # dont
"boo"
"boo" #do
]
multiline_comma_comment = [
"first",
"next",
"last", # best
]

[dog."tater.man"]
Expand Down