Skip to content

Commit 1b4c902

Browse files
authored
Merge pull request #57 from rshyamsu/add-comment-directives
fix: Include parsed comments between arguments only when ParseComment is true
2 parents 46b450b + 7f0c08a commit 1b4c902

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ linters:
2424
- golint
2525
- maligned
2626
- scopelint
27+
- exhaustruct
2728
# deprecated
2829
- deadcode
2930
- varcheck
3031
- structcheck
3132
- nosnakecase
33+
- exhaustivestruct
3234

3335

3436
# Run options

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)