Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatter): prevent indentation of #FASTLY macros #335

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ func (f *Formatter) formatComment(comments ast.Comments, sep string, level int)
if comments[i].PreviousEmptyLines > 0 {
buf.WriteString("\n")
}
buf.WriteString(f.indent(level))
// #FASTLY macros are not indented
if !strings.HasPrefix(comments[i].String(), "#FASTLY") {
buf.WriteString(f.indent(level))
}
switch f.conf.CommentStyle {
case config.CommentStyleSharp, config.CommentStyleSlash:
r := '#' // default as sharp style comment
Expand Down
74 changes: 74 additions & 0 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,77 @@ func BenchmarkFormatter(b *testing.B) {
New(c).Format(vcl)
}
}

func TestFormatComment(t *testing.T) {
tests := []struct {
name string
input string
conf *config.FormatConfig
expect string
}{
{
name: "Regular comment",
input: `sub recv {
// This is a single comment
return(pass);
}`,
expect: `sub recv {
// This is a single comment
return(pass);
}
`,
conf: &config.FormatConfig{
CommentStyle: "slash",
IndentWidth: 2,
IndentStyle: "space",
ReturnStatementParenthesis: true,
},
},
{
name: "Comment starting with #FASTLY",
input: `sub recv {
#FASTLY recv
return(pass);
}`,
expect: `sub recv {
#FASTLY recv
return(pass);
}
`,
conf: &config.FormatConfig{
CommentStyle: "sharp",
IndentWidth: 2,
IndentStyle: "space",
ReturnStatementParenthesis: true,
},
},
{
name: "Multiple comments",
input: `sub recv {
# Regular comment 1
#FASTLY recv
# Regular comment 2
return(pass);
}`,
expect: `sub recv {
# Regular comment 1
#FASTLY recv
# Regular comment 2
return(pass);
}
`,
conf: &config.FormatConfig{
CommentStyle: "sharp",
IndentWidth: 2,
IndentStyle: "space",
ReturnStatementParenthesis: true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert(t, tt.input, tt.expect, tt.conf)
})
}
}