fix quoted backticks not formatted correctly

pull/7045/head
keystroke3 2025-06-02 15:54:29 +03:00
parent 5b2eb66418
commit de5b048106
No known key found for this signature in database
GPG Key ID: 956C95C135734C2F
2 changed files with 38 additions and 8 deletions

View File

@ -62,7 +62,9 @@ func Format(input []byte) []byte {
heredocClosingMarker []rune heredocClosingMarker []rune
nesting int // indentation level nesting int // indentation level
withinBackquote bool withinBacktick bool
afterQuotedBacktick bool // after "`
afterBacktickedQuote bool // after `"
) )
write := func(ch rune) { write := func(ch rune) {
@ -89,10 +91,6 @@ 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) &&
space && last == '<' && ch == '<' { space && last == '<' && ch == '<' {
@ -180,14 +178,41 @@ func Format(input []byte) []byte {
continue continue
} }
if ch == '`' {
if afterQuotedBacktick {
afterBacktickedQuote = false
withinBacktick = false
} else if withinBacktick {
withinBacktick = false
} else if quoted {
afterQuotedBacktick = true
withinBacktick = true
} else {
withinBacktick = true
}
}
if quoted { if quoted {
if ch == '"' { if ch == '"' {
if afterQuotedBacktick {
withinBacktick = false
}
afterQuotedBacktick = false
quoted = false quoted = false
} }
write(ch) write(ch)
continue continue
} }
if ch == '"' && afterQuotedBacktick {
withinBacktick = false
afterQuotedBacktick = false
}
if ch == '"' && afterBacktickedQuote {
withinBacktick = false
afterBacktickedQuote = false
}
if space && ch == '"' { if space && ch == '"' {
quoted = true quoted = true
} }
@ -245,7 +270,7 @@ func Format(input []byte) []byte {
write(' ') write(' ')
} }
openBraceWritten = false openBraceWritten = false
if withinBackquote { if withinBacktick && !afterQuotedBacktick && !afterBacktickedQuote {
write('{') write('{')
openBraceWritten = true openBraceWritten = true
continue continue
@ -253,7 +278,7 @@ func Format(input []byte) []byte {
continue continue
case ch == '}' && (spacePrior || !openBrace): case ch == '}' && (spacePrior || !openBrace):
if withinBackquote { if withinBacktick && !afterQuotedBacktick && !afterBacktickedQuote {
write('}') write('}')
continue continue
} }

View File

@ -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}",
},
} { } {
// 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