@@ -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 }
0 commit comments