Skip to content
Closed
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
41 changes: 40 additions & 1 deletion docs/rules/terraform_comment_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

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

## Configuration

Name | Default | Value
--- | --- | ---
enabled | true | Boolean
allow_multiline | false | Boolean

```hcl
rule "terraform_comment_syntax" {
enabled = true

allow_multiline = false
}
```

## Example

```hcl
Expand All @@ -18,7 +33,25 @@ Warning: Single line comments should begin with # (terraform_comment_syntax)
on main.tf line 2:
2: // Bad

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.0/docs/rules/terraform_comment_syntax.md
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.14.0/docs/rules/terraform_comment_syntax.md
```

### allow_multiline = false

Disallows usage of multi-line comments.

```hcl
# Good
/* Bad */
```

```
Warning: Multi-line comments are not allowed. Use single-line comments starting with # (terraform_comment_syntax)

on main.tf line 1:
2: /* Bad */

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.14.0/docs/rules/terraform_comment_syntax.md
```

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

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

Terraform also supports multi-line comments using `/*` and `*/` as delimiters. However, there's rarely a use-case where
it makes sense to use multi-line comments over multiple single-line comments. Additionally, modern editors make it easy
to work with single-line comments, and therefore it makes sense to disallow multi-line comments so there's only one syntax
for comments.

## How To Fix

Replace the leading double-slash (`//`) in your comment with the number sign (`#`).
Replace multi-line comments with multiple single-line comments if `allow_multiline = false` (default).
25 changes: 21 additions & 4 deletions rules/terraform_comment_syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type TerraformCommentSyntaxRule struct {
tflint.DefaultRule
}

type terraformCommentSyntaxRuleConfig struct {
AllowMultiline bool `hclext:"allow_multiline,optional"`
}

// NewTerraformCommentSyntaxRule returns a new rule
func NewTerraformCommentSyntaxRule() *TerraformCommentSyntaxRule {
return &TerraformCommentSyntaxRule{}
Expand Down Expand Up @@ -50,20 +54,25 @@ func (r *TerraformCommentSyntaxRule) Check(runner tflint.Runner) error {
return nil
}

config := terraformCommentSyntaxRuleConfig{AllowMultiline: false}
if err := runner.DecodeRuleConfig(r.Name(), &config); err != nil {
return err
}

files, err := runner.GetFiles()
if err != nil {
return err
}
for name, file := range files {
if err := r.checkComments(runner, name, file); err != nil {
if err := r.checkComments(runner, name, file, config.AllowMultiline); err != nil {
return err
}
}

return nil
}

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

for _, token := range tokens {
if token.Type != hclsyntax.TokenComment {
if token.Type != hclsyntax.TokenComment || token.Bytes[0] == '#' {
continue
}

if strings.HasPrefix(string(token.Bytes), "//") {
if token.Bytes[1] == '/' {
if err := runner.EmitIssueWithFix(
r,
"Single line comments should begin with #",
Expand All @@ -89,6 +98,14 @@ func (r *TerraformCommentSyntaxRule) checkComments(runner tflint.Runner, filenam
); err != nil {
return err
}
} else if !allowMultiline && token.Bytes[1] == '*' {
if err := runner.EmitIssue(
r,
"Multi-line comments are not allowed. Use single-line comments starting with #",
token.Range,
); err != nil {
return err
}
}
}

Expand Down
37 changes: 35 additions & 2 deletions rules/terraform_comment_syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Test_TerraformCommentSyntaxRule(t *testing.T) {
cases := []struct {
Name string
Content string
Config string
JSON bool
Expected helper.Issues
Fixed string
Expand All @@ -21,14 +22,46 @@ func Test_TerraformCommentSyntaxRule(t *testing.T) {
Expected: helper.Issues{},
},
{
Name: "multi-line comment",
Name: "multi-line comment allowed",
Content: `
/*
This comment spans multiple lines
*/
`,
Config: `
rule "terraform_comment_syntax" {
enabled = true

allow_multiline = true
}
`,
Expected: helper.Issues{},
},
{
Name: "multi-line comment disallowed",
Content: `
/*
This comment spans multiple lines
*/
`,
Expected: helper.Issues{
{
Rule: NewTerraformCommentSyntaxRule(),
Message: "Multi-line comments are not allowed. Use single-line comments starting with #",
Range: hcl.Range{
Filename: "variables.tf",
Start: hcl.Pos{
Line: 2,
Column: 1,
},
End: hcl.Pos{
Line: 4,
Column: 3,
},
},
},
},
},
{
Name: "double-slash comment",
Content: `// foo`,
Expand Down Expand Up @@ -77,7 +110,7 @@ variable "foo" {
filename += ".json"
}

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

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