Skip to content

Commit 5b1a406

Browse files
committed
Add config option to allow proper multiline comments
1 parent c9b930d commit 5b1a406

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

rules/no_multiline_comments.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rules
22

33
import (
4+
"bytes"
45
"strings"
56

67
hcl "github.com/hashicorp/hcl/v2"
@@ -13,6 +14,10 @@ type NoMultilineCommentsRule struct {
1314
tflint.DefaultRule
1415
}
1516

17+
type terraformNoMultilineCommentsRuleConfig struct {
18+
SingleLineOnly bool `hclext:"single_line_only,optional"`
19+
}
20+
1621
// NewNoMultilineCommentsRule returns a new rule
1722
func NewNoMultilineCommentsRule() *NoMultilineCommentsRule {
1823
return &NoMultilineCommentsRule{}
@@ -49,6 +54,11 @@ func (r *NoMultilineCommentsRule) Check(runner tflint.Runner) error {
4954
return nil
5055
}
5156

57+
config := terraformNoMultilineCommentsRuleConfig{SingleLineOnly: false}
58+
if err := runner.DecodeRuleConfig(r.Name(), &config); err != nil {
59+
return err
60+
}
61+
5262
files, err := runner.GetFiles()
5363
if err != nil {
5464
return err
@@ -67,9 +77,22 @@ func (r *NoMultilineCommentsRule) Check(runner tflint.Runner) error {
6777
for _, token := range tokens {
6878
if token.Type == hclsyntax.TokenComment {
6979
if len(token.Bytes) >= 2 && token.Bytes[0] == '/' && token.Bytes[1] == '*' {
80+
if config.SingleLineOnly {
81+
if bytes.Contains(token.Bytes, []byte{'\n'}) {
82+
continue
83+
}
84+
if err := runner.EmitIssue(
85+
r,
86+
"Using multiline comment syntax for single-line comments is not allowed. Use `#` instead.",
87+
token.Range,
88+
); err != nil {
89+
return err
90+
}
91+
continue
92+
}
7093
if err := runner.EmitIssue(
7194
r,
72-
"Multiline comments are not allowed, use single-line comment",
95+
"Multiline comments are not allowed. Replace the comment with single-line comment(s)",
7396
token.Range,
7497
); err != nil {
7598
return err

rules/no_multiline_comments_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func Test_NoMultilineComments(t *testing.T) {
1111
tests := []struct {
1212
Name string
1313
Content string
14+
Config string
1415
Expected helper.Issues
1516
}{
1617
{
@@ -23,7 +24,7 @@ This is a multiline comment
2324
Expected: helper.Issues{
2425
{
2526
Rule: NewNoMultilineCommentsRule(),
26-
Message: "Multiline comments are not allowed, use single-line comment",
27+
Message: "Multiline comments are not allowed. Replace the comment with single-line comment(s)",
2728
Range: hcl.Range{
2829
Filename: "resource.tf",
2930
Start: hcl.Pos{Line: 2, Column: 1},
@@ -32,6 +33,44 @@ This is a multiline comment
3233
},
3334
},
3435
},
36+
{
37+
Name: "ProperMultilineAllowed",
38+
Content: `
39+
/*
40+
This is a multiline comment
41+
*/
42+
`,
43+
Config: `
44+
rule "no_multiline_comments" {
45+
enabled = true
46+
47+
single_line_only = true
48+
}
49+
`,
50+
Expected: helper.Issues{},
51+
},
52+
{
53+
Name: "SingleLineMultiline",
54+
Content: `/* This isn't really a multiline comment */`,
55+
Config: `
56+
rule "no_multiline_comments" {
57+
enabled = true
58+
59+
single_line_only = true
60+
}
61+
`,
62+
Expected: helper.Issues{
63+
{
64+
Rule: NewNoMultilineCommentsRule(),
65+
Message: "Using multiline comment syntax for single-line comments is not allowed. Use `#` instead.",
66+
Range: hcl.Range{
67+
Filename: "resource.tf",
68+
Start: hcl.Pos{Line: 1, Column: 1},
69+
End: hcl.Pos{Line: 1, Column: 44},
70+
},
71+
},
72+
},
73+
},
3574
{
3675
Name: "SingleLine",
3776
Content: `//`,
@@ -48,7 +87,7 @@ This is a multiline comment
4887

4988
for _, test := range tests {
5089
t.Run(test.Name, func(t *testing.T) {
51-
runner := helper.TestRunner(t, map[string]string{"resource.tf": test.Content})
90+
runner := helper.TestRunner(t, map[string]string{"resource.tf": test.Content, ".tflint.hcl": test.Config})
5291

5392
if err := rule.Check(runner); err != nil {
5493
t.Fatalf("Unexpected error occurred: %s", err)

0 commit comments

Comments
 (0)