Skip to content

Commit

Permalink
feat(annotations): Allow a comment at the end of a tflint-ignore anno…
Browse files Browse the repository at this point in the history
…tation (#1892)

Co-authored-by: Ben Drucker <bvdrucker@gmail.com>
  • Loading branch information
papkos and bendrucker authored Oct 27, 2023
1 parent 8eecbda commit 1c3641c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
28 changes: 28 additions & 0 deletions docs/user-guide/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,31 @@ resource "aws_instance" "foo" {
instance_type = "t1.2xlarge"
}
```

It's a good idea to add a reason for why a rule is ignored, especially temporarily:

```hcl
resource "aws_instance" "foo" {
# This instance type is new and TFLint doesn't know about it yet
# tflint-ignore: aws_instance_invalid_type
instance_type = "t10.2xlarge"
}
```

Or, on the same line:

```hcl
resource "aws_instance" "foo" {
# tflint-ignore: aws_instance_invalid_type # too new for TFLint
instance_type = "t10.2xlarge"
}
```

The `//` comment style is also supported, but Terraform recommends `#`.

```hcl
resource "aws_instance" "foo" {
// tflint-ignore: aws_instance_invalid_type // too new for TFLint
instance_type = "t10.2xlarge"
}
```
4 changes: 2 additions & 2 deletions tflint/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
)

var annotationPattern = regexp.MustCompile(`tflint-ignore: ([^\n*/]+)`)
var annotationPattern = regexp.MustCompile(`tflint-ignore: ([^\n*/#]+)`)

// Annotation represents comments with special meaning in TFLint
type Annotation struct {
Expand Down Expand Up @@ -40,7 +40,7 @@ func NewAnnotations(path string, file *hcl.File) (Annotations, hcl.Diagnostics)
continue
}
ret = append(ret, Annotation{
Content: match[1],
Content: strings.TrimSpace(match[1]),
Token: token,
})
}
Expand Down
29 changes: 28 additions & 1 deletion tflint/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ resource "aws_instance" "foo" {
# tflint-ignore: aws_instance_invalid_type
iam_instance_profile = "foo" # This is also comment
// This is also comment
instance_type_reason = "t2.micro" // tflint-ignore: aws_instance_invalid_type // With reason
# tflint-ignore: aws_instance_invalid_type # With reason
iam_instance_profile_reason = "foo" # This is also comment
}`

file, diags := hclsyntax.ParseConfig([]byte(src), "resource.tf", hcl.Pos{Byte: 0, Line: 1, Column: 1})
Expand All @@ -30,7 +33,7 @@ resource "aws_instance" "foo" {

expected := Annotations{
{
Content: "aws_instance_invalid_type, terraform_deprecated_syntax ",
Content: "aws_instance_invalid_type, terraform_deprecated_syntax",
Token: hclsyntax.Token{
Type: hclsyntax.TokenComment,
Bytes: []byte("/* tflint-ignore: aws_instance_invalid_type, terraform_deprecated_syntax */"),
Expand Down Expand Up @@ -65,6 +68,30 @@ resource "aws_instance" "foo" {
},
},
},
{
Content: "aws_instance_invalid_type",
Token: hclsyntax.Token{
Type: hclsyntax.TokenComment,
Bytes: []byte("// tflint-ignore: aws_instance_invalid_type // With reason\n"),
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 8, Column: 37},
End: hcl.Pos{Line: 9, Column: 1},
},
},
},
{
Content: "aws_instance_invalid_type",
Token: hclsyntax.Token{
Type: hclsyntax.TokenComment,
Bytes: []byte("# tflint-ignore: aws_instance_invalid_type # With reason\n"),
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 9, Column: 3},
End: hcl.Pos{Line: 10, Column: 1},
},
},
},
}

opts := cmpopts.IgnoreFields(hcl.Pos{}, "Byte")
Expand Down

0 comments on commit 1c3641c

Please sign in to comment.