mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-17 01:30:34 +00:00
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
This commit is contained in:
@@ -52,17 +52,16 @@ func Format(input []byte) []byte {
|
|||||||
|
|
||||||
newLines int // count of newlines consumed
|
newLines int // count of newlines consumed
|
||||||
|
|
||||||
comment bool // whether we're in a comment
|
comment bool // whether we're in a comment
|
||||||
quoted bool // whether we're in a quoted segment
|
quotes string // encountered quotes ('', '`', '"', '"`', '`"')
|
||||||
escaped bool // whether current char is escaped
|
escaped bool // whether current char is escaped
|
||||||
|
|
||||||
heredoc heredocState // whether we're in a heredoc
|
heredoc heredocState // whether we're in a heredoc
|
||||||
heredocEscaped bool // whether heredoc is escaped
|
heredocEscaped bool // whether heredoc is escaped
|
||||||
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) {
|
||||||
@@ -89,12 +88,8 @@ 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 quotes == "" && (heredoc == heredocClosed && !heredocEscaped) &&
|
||||||
space && last == '<' && ch == '<' {
|
space && last == '<' && ch == '<' {
|
||||||
write(ch)
|
write(ch)
|
||||||
heredoc = heredocOpening
|
heredoc = heredocOpening
|
||||||
@@ -180,16 +175,38 @@ func Format(input []byte) []byte {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if quoted {
|
if ch == '`' {
|
||||||
|
switch quotes {
|
||||||
|
case "\"`":
|
||||||
|
quotes = "\""
|
||||||
|
case "`":
|
||||||
|
quotes = ""
|
||||||
|
case "\"":
|
||||||
|
quotes = "\"`"
|
||||||
|
default:
|
||||||
|
quotes = "`"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if quotes == "\"" {
|
||||||
if ch == '"' {
|
if ch == '"' {
|
||||||
quoted = false
|
quotes = ""
|
||||||
}
|
}
|
||||||
write(ch)
|
write(ch)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if space && ch == '"' {
|
if ch == '"' {
|
||||||
quoted = true
|
switch quotes {
|
||||||
|
case "":
|
||||||
|
if space {
|
||||||
|
quotes = "\""
|
||||||
|
}
|
||||||
|
case "`\"":
|
||||||
|
quotes = "`"
|
||||||
|
case "\"`":
|
||||||
|
quotes = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if unicode.IsSpace(ch) {
|
if unicode.IsSpace(ch) {
|
||||||
@@ -245,7 +262,7 @@ func Format(input []byte) []byte {
|
|||||||
write(' ')
|
write(' ')
|
||||||
}
|
}
|
||||||
openBraceWritten = false
|
openBraceWritten = false
|
||||||
if withinBackquote {
|
if quotes == "`" {
|
||||||
write('{')
|
write('{')
|
||||||
openBraceWritten = true
|
openBraceWritten = true
|
||||||
continue
|
continue
|
||||||
@@ -253,7 +270,7 @@ func Format(input []byte) []byte {
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
case ch == '}' && (spacePrior || !openBrace):
|
case ch == '}' && (spacePrior || !openBrace):
|
||||||
if withinBackquote {
|
if quotes == "`" {
|
||||||
write('}')
|
write('}')
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -444,6 +444,11 @@ block2 {
|
|||||||
input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
||||||
expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Preserve quoted backticks and backticked quotes",
|
||||||
|
input: "block { respond \"`\" } block { respond `\"`}",
|
||||||
|
expect: "block {\n\trespond \"`\"\n}\n\nblock {\n\trespond `\"`\n}",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "No trailing space on line before env variable",
|
description: "No trailing space on line before env variable",
|
||||||
input: `{
|
input: `{
|
||||||
|
|||||||
Reference in New Issue
Block a user