Skip to content

Commit 92035f9

Browse files
committed
add option to disallow multi-line comments
1 parent c646c32 commit 92035f9

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

docs/rules/terraform_comment_syntax.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
Disallow `//` comments in favor of `#`.
44

5+
## Configuration
6+
7+
Name | Default | Value
8+
--- | --- | ---
9+
enabled | true | Boolean
10+
allow_multiline | false | Boolean
11+
12+
```hcl
13+
rule "terraform_comment_syntax" {
14+
enabled = true
15+
16+
allow_multiline = false
17+
}
18+
```
19+
520
## Example
621

722
```hcl
@@ -18,7 +33,25 @@ Warning: Single line comments should begin with # (terraform_comment_syntax)
1833
on main.tf line 2:
1934
2: // Bad
2035
21-
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_comment_syntax.md
36+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.14.0/docs/rules/terraform_comment_syntax.md
37+
```
38+
39+
### allow_multiline = false
40+
41+
Disallows usage of multi-line comments.
42+
43+
```hcl
44+
# Good
45+
/* Bad */
46+
```
47+
48+
```
49+
Warning: Multi-line comments are not allowed. Use single-line comments starting with # (terraform_comment_syntax)
50+
51+
on main.tf line 1:
52+
2: /* Bad */
53+
54+
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.14.0/docs/rules/terraform_comment_syntax.md
2255
```
2356

2457
## Why
@@ -27,6 +60,12 @@ The Terraform language supports two different syntaxes for single-line comments:
2760

2861
* [Configuration Syntax: Comments](https://developer.hashicorp.com/terraform/language/syntax/configuration#comments)
2962

63+
Terraform also supports multi-line comments using `/*` and `*/` as delimiters. However, there's rarely a use-case where
64+
it makes sense to use multi-line comments over multiple single-line comments. Additionally, modern editors make it easy
65+
to work with single-line comments, and therefore it makes sense to disallow multi-line comments so there's only one syntax
66+
for comments.
67+
3068
## How To Fix
3169

3270
Replace the leading double-slash (`//`) in your comment with the number sign (`#`).
71+
Replace multi-line comments with multiple single-line comments if `allow_multiline = false` (default).

rules/terraform_comment_syntax.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type TerraformCommentSyntaxRule struct {
1414
tflint.DefaultRule
1515
}
1616

17+
type terraformCommentSyntaxRuleConfig struct {
18+
AllowMultiline bool `hclext:"allow_multiline,optional"`
19+
}
20+
1721
// NewTerraformCommentSyntaxRule returns a new rule
1822
func NewTerraformCommentSyntaxRule() *TerraformCommentSyntaxRule {
1923
return &TerraformCommentSyntaxRule{}
@@ -50,20 +54,25 @@ func (r *TerraformCommentSyntaxRule) Check(runner tflint.Runner) error {
5054
return nil
5155
}
5256

57+
config := terraformCommentSyntaxRuleConfig{AllowMultiline: false}
58+
if err := runner.DecodeRuleConfig(r.Name(), &config); err != nil {
59+
return err
60+
}
61+
5362
files, err := runner.GetFiles()
5463
if err != nil {
5564
return err
5665
}
5766
for name, file := range files {
58-
if err := r.checkComments(runner, name, file); err != nil {
67+
if err := r.checkComments(runner, name, file, config.AllowMultiline); err != nil {
5968
return err
6069
}
6170
}
6271

6372
return nil
6473
}
6574

66-
func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filename string, file *hcl.File) error {
75+
func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filename string, file *hcl.File, allowMultiline bool) error {
6776
if strings.HasSuffix(filename, ".json") {
6877
return nil
6978
}
@@ -74,11 +83,11 @@ func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filenam
7483
}
7584

7685
for _, token := range tokens {
77-
if token.Type != hclsyntax.TokenComment {
86+
if token.Type != hclsyntax.TokenComment || token.Bytes[0] == '#' {
7887
continue
7988
}
8089

81-
if strings.HasPrefix(string(token.Bytes), "//") {
90+
if token.Bytes[1] == '/' {
8291
if err := runner.EmitIssueWithFix(
8392
r,
8493
"Single line comments should begin with #",
@@ -89,6 +98,14 @@ func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filenam
8998
); err != nil {
9099
return err
91100
}
101+
} else if !allowMultiline && token.Bytes[1] == '*' {
102+
if err := runner.EmitIssue(
103+
r,
104+
"Multi-line comments are not allowed. Use single-line comments starting with #",
105+
token.Range,
106+
); err != nil {
107+
return err
108+
}
92109
}
93110
}
94111

rules/terraform_comment_syntax_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func Test_TerraformCommentSyntaxRule(t *testing.T) {
1111
cases := []struct {
1212
Name string
1313
Content string
14+
Config string
1415
JSON bool
1516
Expected helper.Issues
1617
Fixed string
@@ -21,14 +22,46 @@ func Test_TerraformCommentSyntaxRule(t *testing.T) {
2122
Expected: helper.Issues{},
2223
},
2324
{
24-
Name: "multi-line comment",
25+
Name: "multi-line comment allowed",
2526
Content: `
2627
/*
2728
This comment spans multiple lines
2829
*/
30+
`,
31+
Config: `
32+
rule "terraform_comment_syntax" {
33+
enabled = true
34+
35+
allow_multiline = true
36+
}
2937
`,
3038
Expected: helper.Issues{},
3139
},
40+
{
41+
Name: "multi-line comment disallowed",
42+
Content: `
43+
/*
44+
This comment spans multiple lines
45+
*/
46+
`,
47+
Expected: helper.Issues{
48+
{
49+
Rule: NewTerraformCommentSyntaxRule(),
50+
Message: "Multi-line comments are not allowed. Use single-line comments starting with #",
51+
Range: hcl.Range{
52+
Filename: "variables.tf",
53+
Start: hcl.Pos{
54+
Line: 2,
55+
Column: 1,
56+
},
57+
End: hcl.Pos{
58+
Line: 4,
59+
Column: 3,
60+
},
61+
},
62+
},
63+
},
64+
},
3265
{
3366
Name: "double-slash comment",
3467
Content: `// foo`,
@@ -77,7 +110,7 @@ variable "foo" {
77110
filename += ".json"
78111
}
79112

80-
runner := helper.TestRunner(t, map[string]string{filename: tc.Content})
113+
runner := helper.TestRunner(t, map[string]string{filename: tc.Content, ".tflint.hcl": tc.Config})
81114

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

0 commit comments

Comments
 (0)