diff --git a/docs/user-guide/annotations.md b/docs/user-guide/annotations.md index 391535194..5f99fa4d2 100644 --- a/docs/user-guide/annotations.md +++ b/docs/user-guide/annotations.md @@ -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" +} +``` diff --git a/tflint/annotation.go b/tflint/annotation.go index 51082fa58..963d31283 100644 --- a/tflint/annotation.go +++ b/tflint/annotation.go @@ -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 { @@ -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, }) } diff --git a/tflint/annotation_test.go b/tflint/annotation_test.go index 9b0352707..1d6522184 100644 --- a/tflint/annotation_test.go +++ b/tflint/annotation_test.go @@ -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}) @@ -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 */"), @@ -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")