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