Skip to content

Commit de5b048

Browse files
committed
fix quoted backticks not formatted correctly
1 parent 5b2eb66 commit de5b048

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

caddyconfig/caddyfile/formatter.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ func Format(input []byte) []byte {
6161
heredocMarker []rune
6262
heredocClosingMarker []rune
6363

64-
nesting int // indentation level
65-
withinBackquote bool
64+
nesting int // indentation level
65+
withinBacktick bool
66+
afterQuotedBacktick bool // after "`
67+
afterBacktickedQuote bool // after `"
6668
)
6769

6870
write := func(ch rune) {
@@ -89,10 +91,6 @@ func Format(input []byte) []byte {
8991
}
9092
panic(err)
9193
}
92-
if ch == '`' {
93-
withinBackquote = !withinBackquote
94-
}
95-
9694
// detect whether we have the start of a heredoc
9795
if !quoted && !(heredoc != heredocClosed || heredocEscaped) &&
9896
space && last == '<' && ch == '<' {
@@ -180,14 +178,41 @@ func Format(input []byte) []byte {
180178
continue
181179
}
182180

181+
if ch == '`' {
182+
if afterQuotedBacktick {
183+
afterBacktickedQuote = false
184+
withinBacktick = false
185+
} else if withinBacktick {
186+
withinBacktick = false
187+
} else if quoted {
188+
afterQuotedBacktick = true
189+
withinBacktick = true
190+
} else {
191+
withinBacktick = true
192+
}
193+
}
194+
183195
if quoted {
184196
if ch == '"' {
197+
if afterQuotedBacktick {
198+
withinBacktick = false
199+
}
200+
afterQuotedBacktick = false
185201
quoted = false
186202
}
187203
write(ch)
188204
continue
189205
}
190206

207+
if ch == '"' && afterQuotedBacktick {
208+
withinBacktick = false
209+
afterQuotedBacktick = false
210+
}
211+
if ch == '"' && afterBacktickedQuote {
212+
withinBacktick = false
213+
afterBacktickedQuote = false
214+
}
215+
191216
if space && ch == '"' {
192217
quoted = true
193218
}
@@ -245,15 +270,15 @@ func Format(input []byte) []byte {
245270
write(' ')
246271
}
247272
openBraceWritten = false
248-
if withinBackquote {
273+
if withinBacktick && !afterQuotedBacktick && !afterBacktickedQuote {
249274
write('{')
250275
openBraceWritten = true
251276
continue
252277
}
253278
continue
254279

255280
case ch == '}' && (spacePrior || !openBrace):
256-
if withinBackquote {
281+
if withinBacktick && !afterQuotedBacktick && !afterBacktickedQuote {
257282
write('}')
258283
continue
259284
}

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
// the formatter should output a trailing newline,
449454
// even if the tests aren't written to expect that

0 commit comments

Comments
 (0)