Skip to content

Commit 6e8c61d

Browse files
committed
fix: Include parsed comments between arguments only when ParseComments is true
Comments between arguments should be added to the parsed result only if the ParseComments ParseOption is true. This check was previously missing and has been added as part of this fix. A unit test has also been added to validate this.
1 parent 46b450b commit 6e8c61d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

parse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co
252252
}
253253
}
254254
for t.IsQuoted || (t.Value != "{" && t.Value != ";" && t.Value != "}") {
255-
if strings.HasPrefix(t.Value, "#") && !t.IsQuoted {
256-
commentsInArgs = append(commentsInArgs, t.Value[1:])
257-
} else {
255+
if !strings.HasPrefix(t.Value, "#") || t.IsQuoted {
258256
stmt.Args = append(stmt.Args, t.Value)
257+
} else if p.options.ParseComments {
258+
commentsInArgs = append(commentsInArgs, t.Value[1:])
259259
}
260260
t, tokenOk = <-tokens
261261
if !tokenOk {

parse_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,29 @@ var parseFixtures = []parseFixture{
881881
},
882882
},
883883
}},
884+
{"comments-between-args-disable-parse", "", ParseOptions{ParseComments: false}, Payload{
885+
Status: "ok",
886+
Config: []Config{
887+
{
888+
File: getTestConfigPath("comments-between-args-disable-parse", "nginx.conf"),
889+
Status: "ok",
890+
Parsed: Directives{
891+
{
892+
Directive: "http",
893+
Args: []string{},
894+
Line: 1,
895+
Block: Directives{
896+
{
897+
Directive: "log_format",
898+
Args: []string{"\\#arg\\ 1", "#arg 2"},
899+
Line: 2,
900+
},
901+
},
902+
},
903+
},
904+
},
905+
},
906+
}},
884907
{"premature-eof", "", ParseOptions{}, Payload{
885908
Status: "failed",
886909
Errors: []PayloadError{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
http { #comment 1
2+
log_format #comment 2
3+
\#arg\ 1 #comment 3
4+
'#arg 2' #comment 4
5+
#comment 5
6+
;
7+
}

0 commit comments

Comments
 (0)