@marcel91200 / caddy Mirror · read-only / commits / deba26d2
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyfile: Minor fixes to the formatter

Matthew Holt committed Mar 29, 2020 at 13:53 UTC deba26d225c5b321a944439eb6b108117ac3d569
2 files changed +23 -6
caddyconfig/caddyfile/formatter.go
+8 -5
@@ -39,6 +39,7 @@ func Format(input []byte) []byte {
39
40 openBrace bool // whether current word/token is or started with open curly brace
41 openBraceWritten bool // if openBrace, whether that brace was written or not
42 + openBraceSpace bool // whether there was a non-newline space before open brace
43
44 newLines int // count of newlines consumed
45
@@ -145,10 +146,11 @@ func Format(input []byte) []byte {
146 openBrace = false
147 if beginningOfLine {
148 indent()
148 - } else {
149 + } else if !openBraceSpace {
150 write(' ')
151 }
152 write('{')
153 + openBraceWritten = true
154 nextLine()
155 newLines = 0
156 nesting++
@@ -158,6 +160,10 @@ func Format(input []byte) []byte {
160 case ch == '{':
161 openBrace = true
162 openBraceWritten = false
163 + openBraceSpace = spacePrior && !beginningOfLine
164 + if openBraceSpace {
165 + write(' ')
166 + }
167 continue
168
169 case ch == '}' && (spacePrior || !openBrace):
@@ -183,7 +189,7 @@ func Format(input []byte) []byte {
189 if beginningOfLine {
190 indent()
191 }
186 - if nesting == 0 && last == '}' {
192 + if nesting == 0 && last == '}' && beginningOfLine {
193 nextLine()
194 nextLine()
195 }
@@ -193,9 +199,6 @@ func Format(input []byte) []byte {
199 }
200
201 if openBrace && !openBraceWritten {
196 - if !beginningOfLine {
197 - write(' ')
198 - }
202 write('{')
203 openBraceWritten = true
204 }
caddyconfig/caddyfile/formatter_test.go
+15 -1
@@ -287,10 +287,24 @@ bar "{\"key\":34}"`,
287 expect: `foo \"literal\"`,
288 },
289 {
290 - description: "simple placeholders",
290 + description: "simple placeholders as standalone tokens",
291 input: `foo {bar}`,
292 expect: `foo {bar}`,
293 },
294 + {
295 + description: "simple placeholders within tokens",
296 + input: `foo{bar} foo{bar}baz`,
297 + expect: `foo{bar} foo{bar}baz`,
298 + },
299 + {
300 + description: "placeholders and malformed braces",
301 + input: `foo{bar} foo{ bar}baz`,
302 + expect: `foo{bar} foo {
303 + bar
304 +}
305 +
306 +baz`,
307 + },
308 } {
309 // the formatter should output a trailing newline,
310 // even if the tests aren't written to expect that