mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-26 10:13:39 +08:00
caddyconfig: Don't start comments in middle of tokens (#3267)
* caddyconfig: Only parse # as start of comment if preceded by space * caddyconfig: Simplify # logic using len(val), add a test
This commit is contained in:
parent
26e559662d
commit
96d6d277a4
|
@ -131,9 +131,6 @@ func Format(input []byte) []byte {
|
||||||
//////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////
|
||||||
|
|
||||||
if ch == '#' {
|
if ch == '#' {
|
||||||
if !spacePrior && !beginningOfLine {
|
|
||||||
write(' ')
|
|
||||||
}
|
|
||||||
comment = true
|
comment = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,7 @@ c
|
||||||
}
|
}
|
||||||
|
|
||||||
d {
|
d {
|
||||||
e #f
|
e#f
|
||||||
# g
|
# g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,7 +229,7 @@ bar"
|
||||||
j {
|
j {
|
||||||
"\"k\" l m"
|
"\"k\" l m"
|
||||||
}`,
|
}`,
|
||||||
expect: `"a \"b\" " #c
|
expect: `"a \"b\" "#c
|
||||||
d
|
d
|
||||||
|
|
||||||
e {
|
e {
|
||||||
|
@ -305,6 +305,11 @@ bar "{\"key\":34}"`,
|
||||||
|
|
||||||
baz`,
|
baz`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "hash within string is not a comment",
|
||||||
|
input: `redir / /some/#/path`,
|
||||||
|
expect: `redir / /some/#/path`,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
// 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
|
||||||
|
|
|
@ -141,7 +141,7 @@ func (l *lexer) next() bool {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ch == '#' {
|
if ch == '#' && len(val) == 0 {
|
||||||
comment = true
|
comment = true
|
||||||
}
|
}
|
||||||
if comment {
|
if comment {
|
||||||
|
|
|
@ -77,6 +77,26 @@ func TestLexer(t *testing.T) {
|
||||||
{Line: 6, Text: "}"},
|
{Line: 6, Text: "}"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: `host:123 {
|
||||||
|
# hash inside string is not a comment
|
||||||
|
redir / /some/#/path
|
||||||
|
}`,
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 1, Text: "host:123"},
|
||||||
|
{Line: 1, Text: "{"},
|
||||||
|
{Line: 3, Text: "redir"},
|
||||||
|
{Line: 3, Text: "/"},
|
||||||
|
{Line: 3, Text: "/some/#/path"},
|
||||||
|
{Line: 4, Text: "}"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "# comment at beginning of file\n# comment at beginning of line\nhost:123",
|
||||||
|
expected: []Token{
|
||||||
|
{Line: 3, Text: "host:123"},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
input: `a "quoted value" b
|
input: `a "quoted value" b
|
||||||
foobar`,
|
foobar`,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user