Skip to content

Commit 92c8bc7

Browse files
authored
caddyfile: fix nested quotes formatted incorrectly by fmt (#7045)
* Fix incorrectly formatted quote within quotes with fmt * Fix incorrectly formatted quote within quotes with fmt
1 parent abe0aca commit 92c8bc7

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

caddyconfig/caddyfile/formatter.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,16 @@ func Format(input []byte) []byte {
5252

5353
newLines int // count of newlines consumed
5454

55-
comment bool // whether we're in a comment
56-
quoted bool // whether we're in a quoted segment
57-
escaped bool // whether current char is escaped
55+
comment bool // whether we're in a comment
56+
quotes string // encountered quotes ('', '`', '"', '"`', '`"')
57+
escaped bool // whether current char is escaped
5858

5959
heredoc heredocState // whether we're in a heredoc
6060
heredocEscaped bool // whether heredoc is escaped
6161
heredocMarker []rune
6262
heredocClosingMarker []rune
6363

64-
nesting int // indentation level
65-
withinBackquote bool
64+
nesting int // indentation level
6665
)
6766

6867
write := func(ch rune) {
@@ -89,12 +88,8 @@ func Format(input []byte) []byte {
8988
}
9089
panic(err)
9190
}
92-
if ch == '`' {
93-
withinBackquote = !withinBackquote
94-
}
95-
9691
// detect whether we have the start of a heredoc
97-
if !quoted && (heredoc == heredocClosed && !heredocEscaped) &&
92+
if quotes == "" && (heredoc == heredocClosed && !heredocEscaped) &&
9893
space && last == '<' && ch == '<' {
9994
write(ch)
10095
heredoc = heredocOpening
@@ -180,16 +175,38 @@ func Format(input []byte) []byte {
180175
continue
181176
}
182177

183-
if quoted {
178+
if ch == '`' {
179+
switch quotes {
180+
case "\"`":
181+
quotes = "\""
182+
case "`":
183+
quotes = ""
184+
case "\"":
185+
quotes = "\"`"
186+
default:
187+
quotes = "`"
188+
}
189+
}
190+
191+
if quotes == "\"" {
184192
if ch == '"' {
185-
quoted = false
193+
quotes = ""
186194
}
187195
write(ch)
188196
continue
189197
}
190198

191-
if space && ch == '"' {
192-
quoted = true
199+
if ch == '"' {
200+
switch quotes {
201+
case "":
202+
if space {
203+
quotes = "\""
204+
}
205+
case "`\"":
206+
quotes = "`"
207+
case "\"`":
208+
quotes = ""
209+
}
193210
}
194211

195212
if unicode.IsSpace(ch) {
@@ -245,15 +262,15 @@ func Format(input []byte) []byte {
245262
write(' ')
246263
}
247264
openBraceWritten = false
248-
if withinBackquote {
265+
if quotes == "`" {
249266
write('{')
250267
openBraceWritten = true
251268
continue
252269
}
253270
continue
254271

255272
case ch == '}' && (spacePrior || !openBrace):
256-
if withinBackquote {
273+
if quotes == "`" {
257274
write('}')
258275
continue
259276
}

caddyconfig/caddyfile/formatter_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ block2 {
444444
input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
445445
expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
446446
},
447+
{
448+
description: "Preserve quoted backticks and backticked quotes",
449+
input: "block { respond \"`\" } block { respond `\"`}",
450+
expect: "block {\n\trespond \"`\"\n}\n\nblock {\n\trespond `\"`\n}",
451+
},
447452
{
448453
description: "No trailing space on line before env variable",
449454
input: `{

0 commit comments

Comments
 (0)