mirror of
https://github.com/caddyserver/caddy.git
synced 2025-03-24 06:04:51 +01:00
caddyfile: Fix formatting for backquote wrapped braces (#6903)
This commit is contained in:
parent
55c89ccf2a
commit
b3e692ed09
2 changed files with 25 additions and 2 deletions
|
@ -61,7 +61,8 @@ func Format(input []byte) []byte {
|
||||||
heredocMarker []rune
|
heredocMarker []rune
|
||||||
heredocClosingMarker []rune
|
heredocClosingMarker []rune
|
||||||
|
|
||||||
nesting int // indentation level
|
nesting int // indentation level
|
||||||
|
withinBackquote bool
|
||||||
)
|
)
|
||||||
|
|
||||||
write := func(ch rune) {
|
write := func(ch rune) {
|
||||||
|
@ -88,6 +89,9 @@ func Format(input []byte) []byte {
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
if ch == '`' {
|
||||||
|
withinBackquote = !withinBackquote
|
||||||
|
}
|
||||||
|
|
||||||
// detect whether we have the start of a heredoc
|
// detect whether we have the start of a heredoc
|
||||||
if !quoted && !(heredoc != heredocClosed || heredocEscaped) &&
|
if !quoted && !(heredoc != heredocClosed || heredocEscaped) &&
|
||||||
|
@ -236,14 +240,23 @@ func Format(input []byte) []byte {
|
||||||
switch {
|
switch {
|
||||||
case ch == '{':
|
case ch == '{':
|
||||||
openBrace = true
|
openBrace = true
|
||||||
openBraceWritten = false
|
|
||||||
openBraceSpace = spacePrior && !beginningOfLine
|
openBraceSpace = spacePrior && !beginningOfLine
|
||||||
if openBraceSpace {
|
if openBraceSpace {
|
||||||
write(' ')
|
write(' ')
|
||||||
}
|
}
|
||||||
|
openBraceWritten = false
|
||||||
|
if withinBackquote {
|
||||||
|
write('{')
|
||||||
|
openBraceWritten = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
|
|
||||||
case ch == '}' && (spacePrior || !openBrace):
|
case ch == '}' && (spacePrior || !openBrace):
|
||||||
|
if withinBackquote {
|
||||||
|
write('}')
|
||||||
|
continue
|
||||||
|
}
|
||||||
if last != '\n' {
|
if last != '\n' {
|
||||||
nextLine()
|
nextLine()
|
||||||
}
|
}
|
||||||
|
|
|
@ -434,6 +434,16 @@ block2 {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Preserve braces wrapped by backquotes",
|
||||||
|
input: "block {respond `All braces should remain: {{now | date \"2006\"}}`}",
|
||||||
|
expect: "block {respond `All braces should remain: {{now | date \"2006\"}}`}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Preserve braces wrapped by quotes",
|
||||||
|
input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
||||||
|
expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
// the formatter should output a trailing newline,
|
// the formatter should output a trailing newline,
|
||||||
// even if the tests aren't written to expect that
|
// even if the tests aren't written to expect that
|
||||||
|
|
Loading…
Add table
Reference in a new issue